Loading...
1/*
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/**
81 * rio_device_probe - Tell if a RIO device structure has a matching RIO device id structure
82 * @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;
103 } else
104 rio_dev_put(rdev);
105 }
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
134static void rio_device_shutdown(struct device *dev)
135{
136 struct rio_dev *rdev = to_rio_dev(dev);
137 struct rio_driver *rdrv = rdev->driver;
138
139 dev_dbg(dev, "RIO: %s\n", __func__);
140
141 if (rdrv && rdrv->shutdown)
142 rdrv->shutdown(rdev);
143}
144
145/**
146 * rio_register_driver - register a new RIO driver
147 * @rdrv: the RIO driver structure to register
148 *
149 * Adds a &struct rio_driver to the list of registered drivers.
150 * Returns a negative value on error, otherwise 0. If no error
151 * occurred, the driver remains registered even if no device
152 * was claimed during registration.
153 */
154int rio_register_driver(struct rio_driver *rdrv)
155{
156 /* initialize common driver fields */
157 rdrv->driver.name = rdrv->name;
158 rdrv->driver.bus = &rio_bus_type;
159
160 /* register with core */
161 return driver_register(&rdrv->driver);
162}
163
164/**
165 * rio_unregister_driver - unregister a RIO driver
166 * @rdrv: the RIO driver structure to unregister
167 *
168 * Deletes the &struct rio_driver from the list of registered RIO
169 * drivers, gives it a chance to clean up by calling its remove()
170 * function for each device it was responsible for, and marks those
171 * devices as driverless.
172 */
173void rio_unregister_driver(struct rio_driver *rdrv)
174{
175 driver_unregister(&rdrv->driver);
176}
177
178void rio_attach_device(struct rio_dev *rdev)
179{
180 rdev->dev.bus = &rio_bus_type;
181}
182EXPORT_SYMBOL_GPL(rio_attach_device);
183
184/**
185 * rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
186 * @dev: the standard device structure to match against
187 * @drv: the standard driver structure containing the ids to match against
188 *
189 * Used by a driver to check whether a RIO device present in the
190 * system is in its list of supported devices. Returns 1 if
191 * there is a matching &struct rio_device_id or 0 if there is
192 * no match.
193 */
194static int rio_match_bus(struct device *dev, struct device_driver *drv)
195{
196 struct rio_dev *rdev = to_rio_dev(dev);
197 struct rio_driver *rdrv = to_rio_driver(drv);
198 const struct rio_device_id *id = rdrv->id_table;
199 const struct rio_device_id *found_id;
200
201 if (!id)
202 goto out;
203
204 found_id = rio_match_device(id, rdev);
205
206 if (found_id)
207 return 1;
208
209 out:return 0;
210}
211
212static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
213{
214 struct rio_dev *rdev;
215
216 if (!dev)
217 return -ENODEV;
218
219 rdev = to_rio_dev(dev);
220 if (!rdev)
221 return -ENODEV;
222
223 if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
224 rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
225 return -ENOMEM;
226 return 0;
227}
228
229struct class rio_mport_class = {
230 .name = "rapidio_port",
231 .owner = THIS_MODULE,
232 .dev_groups = rio_mport_groups,
233};
234EXPORT_SYMBOL_GPL(rio_mport_class);
235
236struct bus_type rio_bus_type = {
237 .name = "rapidio",
238 .match = rio_match_bus,
239 .dev_groups = rio_dev_groups,
240 .bus_groups = rio_bus_groups,
241 .probe = rio_device_probe,
242 .remove = rio_device_remove,
243 .shutdown = rio_device_shutdown,
244 .uevent = rio_uevent,
245};
246
247/**
248 * rio_bus_init - Register the RapidIO bus with the device model
249 *
250 * Registers the RIO mport device class and RIO bus type with the Linux
251 * device model.
252 */
253static int __init rio_bus_init(void)
254{
255 int ret;
256
257 ret = class_register(&rio_mport_class);
258 if (!ret) {
259 ret = bus_register(&rio_bus_type);
260 if (ret)
261 class_unregister(&rio_mport_class);
262 }
263 return ret;
264}
265
266postcore_initcall(rio_bus_init);
267
268EXPORT_SYMBOL_GPL(rio_register_driver);
269EXPORT_SYMBOL_GPL(rio_unregister_driver);
270EXPORT_SYMBOL_GPL(rio_bus_type);
271EXPORT_SYMBOL_GPL(rio_dev_get);
272EXPORT_SYMBOL_GPL(rio_dev_put);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * RapidIO driver support
4 *
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
7 */
8
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/rio.h>
12#include <linux/rio_ids.h>
13
14#include "rio.h"
15
16/**
17 * rio_match_device - Tell if a RIO device has a matching RIO device id structure
18 * @id: the RIO device id structure to match against
19 * @rdev: the RIO device structure to match against
20 *
21 * Used from driver probe and bus matching to check whether a RIO device
22 * matches a device id structure provided by a RIO driver. Returns the
23 * matching &struct rio_device_id or %NULL if there is no match.
24 */
25static const struct rio_device_id *rio_match_device(const struct rio_device_id
26 *id,
27 const struct rio_dev *rdev)
28{
29 while (id->vid || id->asm_vid) {
30 if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
31 ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
32 ((id->asm_vid == RIO_ANY_ID)
33 || (id->asm_vid == rdev->asm_vid))
34 && ((id->asm_did == RIO_ANY_ID)
35 || (id->asm_did == rdev->asm_did)))
36 return id;
37 id++;
38 }
39 return NULL;
40}
41
42/**
43 * rio_dev_get - Increments the reference count of the RIO device structure
44 *
45 * @rdev: RIO device being referenced
46 *
47 * Each live reference to a device should be refcounted.
48 *
49 * Drivers for RIO devices should normally record such references in
50 * their probe() methods, when they bind to a device, and release
51 * them by calling rio_dev_put(), in their disconnect() methods.
52 */
53struct rio_dev *rio_dev_get(struct rio_dev *rdev)
54{
55 if (rdev)
56 get_device(&rdev->dev);
57
58 return rdev;
59}
60
61/**
62 * rio_dev_put - Release a use of the RIO device structure
63 *
64 * @rdev: RIO device being disconnected
65 *
66 * Must be called when a user of a device is finished with it.
67 * When the last user of the device calls this function, the
68 * memory of the device is freed.
69 */
70void rio_dev_put(struct rio_dev *rdev)
71{
72 if (rdev)
73 put_device(&rdev->dev);
74}
75
76/**
77 * rio_device_probe - Tell if a RIO device structure has a matching RIO device id structure
78 * @dev: the RIO device structure to match against
79 *
80 * return 0 and set rio_dev->driver when drv claims rio_dev, else error
81 */
82static int rio_device_probe(struct device *dev)
83{
84 struct rio_driver *rdrv = to_rio_driver(dev->driver);
85 struct rio_dev *rdev = to_rio_dev(dev);
86 int error = -ENODEV;
87 const struct rio_device_id *id;
88
89 if (!rdev->driver && rdrv->probe) {
90 if (!rdrv->id_table)
91 return error;
92 id = rio_match_device(rdrv->id_table, rdev);
93 rio_dev_get(rdev);
94 if (id)
95 error = rdrv->probe(rdev, id);
96 if (error >= 0) {
97 rdev->driver = rdrv;
98 error = 0;
99 } else
100 rio_dev_put(rdev);
101 }
102 return error;
103}
104
105/**
106 * rio_device_remove - Remove a RIO device from the system
107 *
108 * @dev: the RIO device structure to match against
109 *
110 * Remove a RIO device from the system. If it has an associated
111 * driver, then run the driver remove() method. Then update
112 * the reference count.
113 */
114static int rio_device_remove(struct device *dev)
115{
116 struct rio_dev *rdev = to_rio_dev(dev);
117 struct rio_driver *rdrv = rdev->driver;
118
119 if (rdrv) {
120 if (rdrv->remove)
121 rdrv->remove(rdev);
122 rdev->driver = NULL;
123 }
124
125 rio_dev_put(rdev);
126
127 return 0;
128}
129
130static void rio_device_shutdown(struct device *dev)
131{
132 struct rio_dev *rdev = to_rio_dev(dev);
133 struct rio_driver *rdrv = rdev->driver;
134
135 dev_dbg(dev, "RIO: %s\n", __func__);
136
137 if (rdrv && rdrv->shutdown)
138 rdrv->shutdown(rdev);
139}
140
141/**
142 * rio_register_driver - register a new RIO driver
143 * @rdrv: the RIO driver structure to register
144 *
145 * Adds a &struct rio_driver to the list of registered drivers.
146 * Returns a negative value on error, otherwise 0. If no error
147 * occurred, the driver remains registered even if no device
148 * was claimed during registration.
149 */
150int rio_register_driver(struct rio_driver *rdrv)
151{
152 /* initialize common driver fields */
153 rdrv->driver.name = rdrv->name;
154 rdrv->driver.bus = &rio_bus_type;
155
156 /* register with core */
157 return driver_register(&rdrv->driver);
158}
159
160/**
161 * rio_unregister_driver - unregister a RIO driver
162 * @rdrv: the RIO driver structure to unregister
163 *
164 * Deletes the &struct rio_driver from the list of registered RIO
165 * drivers, gives it a chance to clean up by calling its remove()
166 * function for each device it was responsible for, and marks those
167 * devices as driverless.
168 */
169void rio_unregister_driver(struct rio_driver *rdrv)
170{
171 driver_unregister(&rdrv->driver);
172}
173
174void rio_attach_device(struct rio_dev *rdev)
175{
176 rdev->dev.bus = &rio_bus_type;
177}
178EXPORT_SYMBOL_GPL(rio_attach_device);
179
180/**
181 * rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
182 * @dev: the standard device structure to match against
183 * @drv: the standard driver structure containing the ids to match against
184 *
185 * Used by a driver to check whether a RIO device present in the
186 * system is in its list of supported devices. Returns 1 if
187 * there is a matching &struct rio_device_id or 0 if there is
188 * no match.
189 */
190static int rio_match_bus(struct device *dev, struct device_driver *drv)
191{
192 struct rio_dev *rdev = to_rio_dev(dev);
193 struct rio_driver *rdrv = to_rio_driver(drv);
194 const struct rio_device_id *id = rdrv->id_table;
195 const struct rio_device_id *found_id;
196
197 if (!id)
198 goto out;
199
200 found_id = rio_match_device(id, rdev);
201
202 if (found_id)
203 return 1;
204
205 out:return 0;
206}
207
208static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
209{
210 struct rio_dev *rdev;
211
212 if (!dev)
213 return -ENODEV;
214
215 rdev = to_rio_dev(dev);
216 if (!rdev)
217 return -ENODEV;
218
219 if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
220 rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
221 return -ENOMEM;
222 return 0;
223}
224
225struct class rio_mport_class = {
226 .name = "rapidio_port",
227 .owner = THIS_MODULE,
228 .dev_groups = rio_mport_groups,
229};
230EXPORT_SYMBOL_GPL(rio_mport_class);
231
232struct bus_type rio_bus_type = {
233 .name = "rapidio",
234 .match = rio_match_bus,
235 .dev_groups = rio_dev_groups,
236 .bus_groups = rio_bus_groups,
237 .probe = rio_device_probe,
238 .remove = rio_device_remove,
239 .shutdown = rio_device_shutdown,
240 .uevent = rio_uevent,
241};
242
243/**
244 * rio_bus_init - Register the RapidIO bus with the device model
245 *
246 * Registers the RIO mport device class and RIO bus type with the Linux
247 * device model.
248 */
249static int __init rio_bus_init(void)
250{
251 int ret;
252
253 ret = class_register(&rio_mport_class);
254 if (!ret) {
255 ret = bus_register(&rio_bus_type);
256 if (ret)
257 class_unregister(&rio_mport_class);
258 }
259 return ret;
260}
261
262postcore_initcall(rio_bus_init);
263
264EXPORT_SYMBOL_GPL(rio_register_driver);
265EXPORT_SYMBOL_GPL(rio_unregister_driver);
266EXPORT_SYMBOL_GPL(rio_bus_type);
267EXPORT_SYMBOL_GPL(rio_dev_get);
268EXPORT_SYMBOL_GPL(rio_dev_put);