2 * FILE NAME: ocp-driver.c
4 * BRIEF MODULE DESCRIPTION:
5 * driver callback, id matching and registration
6 * Based on drivers/pci/pci-driver, Copyright (c) 1997--1999 Martin Mares
8 * Maintained by: Armin <akuster@mvista.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <linux/module.h>
34 #include <linux/init.h>
37 * Registration of OCP drivers and handling of hot-pluggable devices.
41 ocp_device_probe(struct device *dev)
44 struct ocp_driver *drv;
45 struct ocp_device *ocp_dev;
47 drv = to_ocp_driver(dev->driver);
48 ocp_dev = to_ocp_dev(dev);
51 error = drv->probe(ocp_dev);
52 DBG("probe return code %d\n", error);
54 ocp_dev->driver = drv;
62 ocp_device_remove(struct device *dev)
64 struct ocp_device *ocp_dev = to_ocp_dev(dev);
66 if (ocp_dev->driver) {
67 if (ocp_dev->driver->remove)
68 ocp_dev->driver->remove(ocp_dev);
69 ocp_dev->driver = NULL;
75 ocp_device_suspend(struct device *dev, u32 state, u32 level)
77 struct ocp_device *ocp_dev = to_ocp_dev(dev);
81 if (ocp_dev->driver) {
82 if (level == SUSPEND_SAVE_STATE && ocp_dev->driver->save_state)
83 error = ocp_dev->driver->save_state(ocp_dev, state);
84 else if (level == SUSPEND_POWER_DOWN
85 && ocp_dev->driver->suspend)
86 error = ocp_dev->driver->suspend(ocp_dev, state);
92 ocp_device_resume(struct device *dev, u32 level)
94 struct ocp_device *ocp_dev = to_ocp_dev(dev);
96 if (ocp_dev->driver) {
97 if (level == RESUME_POWER_ON && ocp_dev->driver->resume)
98 ocp_dev->driver->resume(ocp_dev);
104 * ocp_bus_match - Works out whether an OCP device matches any
105 * of the IDs listed for a given OCP driver.
106 * @dev: the generic device struct for the OCP device
107 * @drv: the generic driver struct for the OCP driver
109 * Used by a driver to check whether a OCP device present in the
110 * system is in its list of supported devices. Returns 1 for a
111 * match, or 0 if there is no match.
114 ocp_bus_match(struct device *dev, struct device_driver *drv)
116 struct ocp_device *ocp_dev = to_ocp_dev(dev);
117 struct ocp_driver *ocp_drv = to_ocp_driver(drv);
118 const struct ocp_device_id *ids = ocp_drv->id_table;
123 while (ids->vendor || ids->device) {
124 if ((ids->vendor == OCP_ANY_ID
125 || ids->vendor == ocp_dev->vendor)
126 && (ids->device == OCP_ANY_ID
127 || ids->device == ocp_dev->device)) {
128 DBG("Bus match -vendor:%x device:%x\n", ids->vendor,
137 struct bus_type ocp_bus_type = {
139 .match = ocp_bus_match,
143 ocp_driver_init(void)
145 return bus_register(&ocp_bus_type);
148 postcore_initcall(ocp_driver_init);
151 * ocp_register_driver - register a new ocp driver
152 * @drv: the driver structure to register
154 * Adds the driver structure to the list of registered drivers
155 * Returns the number of ocp devices which were claimed by the driver
156 * during registration. The driver remains registered even if the
157 * return value is zero.
160 ocp_register_driver(struct ocp_driver *drv)
164 /* initialize common driver fields */
165 drv->driver.name = drv->name;
166 drv->driver.bus = &ocp_bus_type;
167 drv->driver.probe = ocp_device_probe;
168 drv->driver.resume = ocp_device_resume;
169 drv->driver.suspend = ocp_device_suspend;
170 drv->driver.remove = ocp_device_remove;
172 /* register with core */
173 count = driver_register(&drv->driver);
174 return count ? count : 1;
178 * ocp_unregister_driver - unregister a ocp driver
179 * @drv: the driver structure to unregister
181 * Deletes the driver structure from the list of registered OCP drivers,
182 * gives it a chance to clean up by calling its remove() function for
183 * each device it was responsible for, and marks those devices as
188 ocp_unregister_driver(struct ocp_driver *drv)
190 driver_unregister(&drv->driver);
193 EXPORT_SYMBOL(ocp_register_driver);
194 EXPORT_SYMBOL(ocp_unregister_driver);
195 EXPORT_SYMBOL(ocp_bus_type);