blob: 3222fa3c808ca4c96eeb3fc436122f041a84eebc [file] [log] [blame]
Matt Porter394b7012005-11-07 01:00:15 -08001/*
2 * RapidIO driver support
3 *
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/rio.h>
16#include <linux/rio_ids.h>
17
18#include "rio.h"
19
20/**
21 * rio_match_device - Tell if a RIO device has a matching RIO device id structure
22 * @id: the RIO device id structure to match against
23 * @rdev: the RIO device structure to match against
24 *
25 * Used from driver probe and bus matching to check whether a RIO device
26 * matches a device id structure provided by a RIO driver. Returns the
27 * matching &struct rio_device_id or %NULL if there is no match.
28 */
29static const struct rio_device_id *rio_match_device(const struct rio_device_id
30 *id,
31 const struct rio_dev *rdev)
32{
33 while (id->vid || id->asm_vid) {
34 if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
35 ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
36 ((id->asm_vid == RIO_ANY_ID)
37 || (id->asm_vid == rdev->asm_vid))
38 && ((id->asm_did == RIO_ANY_ID)
39 || (id->asm_did == rdev->asm_did)))
40 return id;
41 id++;
42 }
43 return NULL;
44}
45
46/**
47 * rio_dev_get - Increments the reference count of the RIO device structure
48 *
49 * @rdev: RIO device being referenced
50 *
51 * Each live reference to a device should be refcounted.
52 *
53 * Drivers for RIO devices should normally record such references in
54 * their probe() methods, when they bind to a device, and release
55 * them by calling rio_dev_put(), in their disconnect() methods.
56 */
57struct rio_dev *rio_dev_get(struct rio_dev *rdev)
58{
59 if (rdev)
60 get_device(&rdev->dev);
61
62 return rdev;
63}
64
65/**
66 * rio_dev_put - Release a use of the RIO device structure
67 *
68 * @rdev: RIO device being disconnected
69 *
70 * Must be called when a user of a device is finished with it.
71 * When the last user of the device calls this function, the
72 * memory of the device is freed.
73 */
74void rio_dev_put(struct rio_dev *rdev)
75{
76 if (rdev)
77 put_device(&rdev->dev);
78}
79
80/**
Randy Dunlap7b089c82008-02-29 22:02:40 -080081 * rio_device_probe - Tell if a RIO device structure has a matching RIO device id structure
Matt Porter394b7012005-11-07 01:00:15 -080082 * @dev: the RIO device structure to match against
83 *
84 * return 0 and set rio_dev->driver when drv claims rio_dev, else error
85 */
86static int rio_device_probe(struct device *dev)
87{
88 struct rio_driver *rdrv = to_rio_driver(dev->driver);
89 struct rio_dev *rdev = to_rio_dev(dev);
90 int error = -ENODEV;
91 const struct rio_device_id *id;
92
93 if (!rdev->driver && rdrv->probe) {
94 if (!rdrv->id_table)
95 return error;
96 id = rio_match_device(rdrv->id_table, rdev);
97 rio_dev_get(rdev);
98 if (id)
99 error = rdrv->probe(rdev, id);
100 if (error >= 0) {
101 rdev->driver = rdrv;
102 error = 0;
Eugene Surovegina7de3902008-07-10 17:30:44 -0700103 } else
Matt Porter394b7012005-11-07 01:00:15 -0800104 rio_dev_put(rdev);
Matt Porter394b7012005-11-07 01:00:15 -0800105 }
106 return error;
107}
108
109/**
110 * rio_device_remove - Remove a RIO device from the system
111 *
112 * @dev: the RIO device structure to match against
113 *
114 * Remove a RIO device from the system. If it has an associated
115 * driver, then run the driver remove() method. Then update
116 * the reference count.
117 */
118static int rio_device_remove(struct device *dev)
119{
120 struct rio_dev *rdev = to_rio_dev(dev);
121 struct rio_driver *rdrv = rdev->driver;
122
123 if (rdrv) {
124 if (rdrv->remove)
125 rdrv->remove(rdev);
126 rdev->driver = NULL;
127 }
128
129 rio_dev_put(rdev);
130
131 return 0;
132}
133
134/**
135 * rio_register_driver - register a new RIO driver
136 * @rdrv: the RIO driver structure to register
137 *
Randy Dunlap7b089c82008-02-29 22:02:40 -0800138 * Adds a &struct rio_driver to the list of registered drivers.
Matt Porter394b7012005-11-07 01:00:15 -0800139 * Returns a negative value on error, otherwise 0. If no error
140 * occurred, the driver remains registered even if no device
141 * was claimed during registration.
142 */
143int rio_register_driver(struct rio_driver *rdrv)
144{
145 /* initialize common driver fields */
146 rdrv->driver.name = rdrv->name;
147 rdrv->driver.bus = &rio_bus_type;
Matt Porter394b7012005-11-07 01:00:15 -0800148
149 /* register with core */
150 return driver_register(&rdrv->driver);
151}
152
153/**
154 * rio_unregister_driver - unregister a RIO driver
155 * @rdrv: the RIO driver structure to unregister
156 *
157 * Deletes the &struct rio_driver from the list of registered RIO
158 * drivers, gives it a chance to clean up by calling its remove()
159 * function for each device it was responsible for, and marks those
160 * devices as driverless.
161 */
162void rio_unregister_driver(struct rio_driver *rdrv)
163{
164 driver_unregister(&rdrv->driver);
165}
166
167/**
Randy Dunlap7b089c82008-02-29 22:02:40 -0800168 * rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
Matt Porter394b7012005-11-07 01:00:15 -0800169 * @dev: the standard device structure to match against
170 * @drv: the standard driver structure containing the ids to match against
171 *
172 * Used by a driver to check whether a RIO device present in the
173 * system is in its list of supported devices. Returns 1 if
174 * there is a matching &struct rio_device_id or 0 if there is
175 * no match.
176 */
177static int rio_match_bus(struct device *dev, struct device_driver *drv)
178{
179 struct rio_dev *rdev = to_rio_dev(dev);
180 struct rio_driver *rdrv = to_rio_driver(drv);
181 const struct rio_device_id *id = rdrv->id_table;
182 const struct rio_device_id *found_id;
183
184 if (!id)
185 goto out;
186
187 found_id = rio_match_device(id, rdev);
188
189 if (found_id)
190 return 1;
191
192 out:return 0;
193}
194
195static struct device rio_bus = {
Kay Sievers37f10542009-03-24 16:38:21 -0700196 .init_name = "rapidio",
Matt Porter394b7012005-11-07 01:00:15 -0800197};
198
199struct bus_type rio_bus_type = {
200 .name = "rapidio",
201 .match = rio_match_bus,
Russell Kingfc3d3dd2006-01-05 14:44:14 +0000202 .dev_attrs = rio_dev_attrs,
203 .probe = rio_device_probe,
204 .remove = rio_device_remove,
Matt Porter394b7012005-11-07 01:00:15 -0800205};
206
207/**
208 * rio_bus_init - Register the RapidIO bus with the device model
209 *
210 * Registers the RIO bus device and RIO bus type with the Linux
211 * device model.
212 */
213static int __init rio_bus_init(void)
214{
215 if (device_register(&rio_bus) < 0)
216 printk("RIO: failed to register RIO bus device\n");
217 return bus_register(&rio_bus_type);
218}
219
220postcore_initcall(rio_bus_init);
221
222EXPORT_SYMBOL_GPL(rio_register_driver);
223EXPORT_SYMBOL_GPL(rio_unregister_driver);
224EXPORT_SYMBOL_GPL(rio_bus_type);
225EXPORT_SYMBOL_GPL(rio_dev_get);
226EXPORT_SYMBOL_GPL(rio_dev_put);