This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / ppc / ocp / ocp-driver.c
1 /*
2  * FILE NAME: ocp-driver.c
3  *
4  * BRIEF MODULE DESCRIPTION:
5  * driver callback, id matching and registration
6  * Based on drivers/pci/pci-driver, Copyright (c) 1997--1999 Martin Mares
7  *
8  * Maintained by: Armin <akuster@mvista.com>
9  *
10  *
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.
15  *
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.
26  *
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.
30  */
31
32 #include <asm/ocp.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35
36 /*
37  *  Registration of OCP drivers and handling of hot-pluggable devices.
38  */
39
40 static int
41 ocp_device_probe(struct device *dev)
42 {
43         int error = 0;
44         struct ocp_driver *drv;
45         struct ocp_device *ocp_dev;
46
47         drv = to_ocp_driver(dev->driver);
48         ocp_dev = to_ocp_dev(dev);
49
50         if (drv->probe) {
51                 error = drv->probe(ocp_dev);
52                 DBG("probe return code %d\n", error);
53                 if (error >= 0) {
54                         ocp_dev->driver = drv;
55                         error = 0;
56                 }
57         }
58         return error;
59 }
60
61 static int
62 ocp_device_remove(struct device *dev)
63 {
64         struct ocp_device *ocp_dev = to_ocp_dev(dev);
65
66         if (ocp_dev->driver) {
67                 if (ocp_dev->driver->remove)
68                         ocp_dev->driver->remove(ocp_dev);
69                 ocp_dev->driver = NULL;
70         }
71         return 0;
72 }
73
74 static int
75 ocp_device_suspend(struct device *dev, u32 state, u32 level)
76 {
77         struct ocp_device *ocp_dev = to_ocp_dev(dev);
78
79         int error = 0;
80
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);
87         }
88         return error;
89 }
90
91 static int
92 ocp_device_resume(struct device *dev, u32 level)
93 {
94         struct ocp_device *ocp_dev = to_ocp_dev(dev);
95
96         if (ocp_dev->driver) {
97                 if (level == RESUME_POWER_ON && ocp_dev->driver->resume)
98                         ocp_dev->driver->resume(ocp_dev);
99         }
100         return 0;
101 }
102
103 /**
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
108  *
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.
112  */
113 static int
114 ocp_bus_match(struct device *dev, struct device_driver *drv)
115 {
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;
119
120         if (!ids)
121                 return 0;
122
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,
129                             ids->device);
130                         return 1;
131                 }
132                 ids++;
133         }
134         return 0;
135 }
136
137 struct bus_type ocp_bus_type = {
138         .name = "ocp",
139         .match = ocp_bus_match,
140 };
141
142 static int __init
143 ocp_driver_init(void)
144 {
145         return bus_register(&ocp_bus_type);
146 }
147
148 postcore_initcall(ocp_driver_init);
149
150 /**
151  * ocp_register_driver - register a new ocp driver
152  * @drv: the driver structure to register
153  *
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.
158  */
159 int
160 ocp_register_driver(struct ocp_driver *drv)
161 {
162         int count = 0;
163
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;
171
172         /* register with core */
173         count = driver_register(&drv->driver);
174         return count ? count : 1;
175 }
176
177 /**
178  * ocp_unregister_driver - unregister a ocp driver
179  * @drv: the driver structure to unregister
180  *
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
184  * driverless.
185  */
186
187 void
188 ocp_unregister_driver(struct ocp_driver *drv)
189 {
190         driver_unregister(&drv->driver);
191 }
192
193 EXPORT_SYMBOL(ocp_register_driver);
194 EXPORT_SYMBOL(ocp_unregister_driver);
195 EXPORT_SYMBOL(ocp_bus_type);