xref: /openbmc/linux/drivers/zorro/zorro-driver.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2  *  Zorro Driver Services
3  *
4  *  Copyright (C) 2003 Geert Uytterhoeven
5  *
6  *  Loosely based on drivers/pci/pci-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/zorro.h>
16 
17 
18     /**
19      *  zorro_match_device - Tell if a Zorro device structure has a matching
20      *                       Zorro device id structure
21      *  @ids: array of Zorro device id structures to search in
22      *  @dev: the Zorro device structure to match against
23      *
24      *  Used by a driver to check whether a Zorro device present in the
25      *  system is in its list of supported devices. Returns the matching
26      *  zorro_device_id structure or %NULL if there is no match.
27      */
28 
29 const struct zorro_device_id *
30 zorro_match_device(const struct zorro_device_id *ids,
31 		   const struct zorro_dev *z)
32 {
33 	while (ids->id) {
34 		if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
35 			return ids;
36 		ids++;
37 	}
38 	return NULL;
39 }
40 
41 
42 static int zorro_device_probe(struct device *dev)
43 {
44 	int error = 0;
45 	struct zorro_driver *drv = to_zorro_driver(dev->driver);
46 	struct zorro_dev *z = to_zorro_dev(dev);
47 
48 	if (!z->driver && drv->probe) {
49 		const struct zorro_device_id *id;
50 
51 		id = zorro_match_device(drv->id_table, z);
52 		if (id)
53 			error = drv->probe(z, id);
54 		if (error >= 0) {
55 			z->driver = drv;
56 			error = 0;
57 		}
58 	}
59 	return error;
60 }
61 
62 
63     /**
64      *  zorro_register_driver - register a new Zorro driver
65      *  @drv: the driver structure to register
66      *
67      *  Adds the driver structure to the list of registered drivers
68      *  Returns the number of Zorro devices which were claimed by the driver
69      *  during registration.  The driver remains registered even if the
70      *  return value is zero.
71      */
72 
73 int zorro_register_driver(struct zorro_driver *drv)
74 {
75 	int count = 0;
76 
77 	/* initialize common driver fields */
78 	drv->driver.name = drv->name;
79 	drv->driver.bus = &zorro_bus_type;
80 	drv->driver.probe = zorro_device_probe;
81 
82 	/* register with core */
83 	count = driver_register(&drv->driver);
84 	return count ? count : 1;
85 }
86 
87 
88     /**
89      *  zorro_unregister_driver - unregister a zorro driver
90      *  @drv: the driver structure to unregister
91      *
92      *  Deletes the driver structure from the list of registered Zorro 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 
98 void zorro_unregister_driver(struct zorro_driver *drv)
99 {
100 	driver_unregister(&drv->driver);
101 }
102 
103 
104     /**
105      *  zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
106      *                    device id structure
107      *  @ids: array of Zorro device id structures to search in
108      *  @dev: the Zorro device structure to match against
109      *
110      *  Used by a driver to check whether a Zorro device present in the
111      *  system is in its list of supported devices.Returns the matching
112      *  zorro_device_id structure or %NULL if there is no match.
113      */
114 
115 static int zorro_bus_match(struct device *dev, struct device_driver *drv)
116 {
117 	struct zorro_dev *z = to_zorro_dev(dev);
118 	struct zorro_driver *zorro_drv = to_zorro_driver(drv);
119 	const struct zorro_device_id *ids = zorro_drv->id_table;
120 
121 	if (!ids)
122 		return 0;
123 
124 	while (ids->id) {
125 		if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
126 			return 1;
127 		ids++;
128 	}
129 	return 0;
130 }
131 
132 
133 struct bus_type zorro_bus_type = {
134 	.name	= "zorro",
135 	.match	= zorro_bus_match
136 };
137 
138 
139 static int __init zorro_driver_init(void)
140 {
141 	return bus_register(&zorro_bus_type);
142 }
143 
144 postcore_initcall(zorro_driver_init);
145 
146 EXPORT_SYMBOL(zorro_match_device);
147 EXPORT_SYMBOL(zorro_register_driver);
148 EXPORT_SYMBOL(zorro_unregister_driver);
149 EXPORT_SYMBOL(zorro_dev_driver);
150 EXPORT_SYMBOL(zorro_bus_type);
151