1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RapidIO driver support
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/rio.h>
12 #include <linux/rio_ids.h>
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
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.
25 static const struct rio_device_id *rio_match_device(const struct rio_device_id
27 const struct rio_dev *rdev)
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)))
43 * rio_dev_get - Increments the reference count of the RIO device structure
45 * @rdev: RIO device being referenced
47 * Each live reference to a device should be refcounted.
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.
53 struct rio_dev *rio_dev_get(struct rio_dev *rdev)
56 get_device(&rdev->dev);
62 * rio_dev_put - Release a use of the RIO device structure
64 * @rdev: RIO device being disconnected
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.
70 void rio_dev_put(struct rio_dev *rdev)
73 put_device(&rdev->dev);
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
80 * return 0 and set rio_dev->driver when drv claims rio_dev, else error
82 static int rio_device_probe(struct device *dev)
84 struct rio_driver *rdrv = to_rio_driver(dev->driver);
85 struct rio_dev *rdev = to_rio_dev(dev);
87 const struct rio_device_id *id;
89 if (!rdev->driver && rdrv->probe) {
92 id = rio_match_device(rdrv->id_table, rdev);
95 error = rdrv->probe(rdev, id);
106 * rio_device_remove - Remove a RIO device from the system
108 * @dev: the RIO device structure to match against
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.
114 static int rio_device_remove(struct device *dev)
116 struct rio_dev *rdev = to_rio_dev(dev);
117 struct rio_driver *rdrv = rdev->driver;
130 static void rio_device_shutdown(struct device *dev)
132 struct rio_dev *rdev = to_rio_dev(dev);
133 struct rio_driver *rdrv = rdev->driver;
135 dev_dbg(dev, "RIO: %s\n", __func__);
137 if (rdrv && rdrv->shutdown)
138 rdrv->shutdown(rdev);
142 * rio_register_driver - register a new RIO driver
143 * @rdrv: the RIO driver structure to register
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.
150 int rio_register_driver(struct rio_driver *rdrv)
152 /* initialize common driver fields */
153 rdrv->driver.name = rdrv->name;
154 rdrv->driver.bus = &rio_bus_type;
156 /* register with core */
157 return driver_register(&rdrv->driver);
161 * rio_unregister_driver - unregister a RIO driver
162 * @rdrv: the RIO driver structure to unregister
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.
169 void rio_unregister_driver(struct rio_driver *rdrv)
171 driver_unregister(&rdrv->driver);
174 void rio_attach_device(struct rio_dev *rdev)
176 rdev->dev.bus = &rio_bus_type;
178 EXPORT_SYMBOL_GPL(rio_attach_device);
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
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
190 static int rio_match_bus(struct device *dev, struct device_driver *drv)
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;
200 found_id = rio_match_device(id, rdev);
208 static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
210 struct rio_dev *rdev;
215 rdev = to_rio_dev(dev);
219 if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
220 rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
225 struct class rio_mport_class = {
226 .name = "rapidio_port",
227 .owner = THIS_MODULE,
228 .dev_groups = rio_mport_groups,
230 EXPORT_SYMBOL_GPL(rio_mport_class);
232 struct bus_type rio_bus_type = {
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,
244 * rio_bus_init - Register the RapidIO bus with the device model
246 * Registers the RIO mport device class and RIO bus type with the Linux
249 static int __init rio_bus_init(void)
253 ret = class_register(&rio_mport_class);
255 ret = bus_register(&rio_bus_type);
257 class_unregister(&rio_mport_class);
262 postcore_initcall(rio_bus_init);
264 EXPORT_SYMBOL_GPL(rio_register_driver);
265 EXPORT_SYMBOL_GPL(rio_unregister_driver);
266 EXPORT_SYMBOL_GPL(rio_bus_type);
267 EXPORT_SYMBOL_GPL(rio_dev_get);
268 EXPORT_SYMBOL_GPL(rio_dev_put);