Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * RapidIO driver support
0004  *
0005  * Copyright 2005 MontaVista Software, Inc.
0006  * Matt Porter <mporter@kernel.crashing.org>
0007  */
0008 
0009 #include <linux/init.h>
0010 #include <linux/module.h>
0011 #include <linux/rio.h>
0012 #include <linux/rio_ids.h>
0013 #include <linux/rio_drv.h>
0014 
0015 #include "rio.h"
0016 
0017 /**
0018  *  rio_match_device - Tell if a RIO device has a matching RIO device id structure
0019  *  @id: the RIO device id structure to match against
0020  *  @rdev: the RIO device structure to match against
0021  *
0022  *  Used from driver probe and bus matching to check whether a RIO device
0023  *  matches a device id structure provided by a RIO driver. Returns the
0024  *  matching &struct rio_device_id or %NULL if there is no match.
0025  */
0026 static const struct rio_device_id *rio_match_device(const struct rio_device_id
0027                             *id,
0028                             const struct rio_dev *rdev)
0029 {
0030     while (id->vid || id->asm_vid) {
0031         if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
0032             ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
0033             ((id->asm_vid == RIO_ANY_ID)
0034              || (id->asm_vid == rdev->asm_vid))
0035             && ((id->asm_did == RIO_ANY_ID)
0036             || (id->asm_did == rdev->asm_did)))
0037             return id;
0038         id++;
0039     }
0040     return NULL;
0041 }
0042 
0043 /**
0044  * rio_dev_get - Increments the reference count of the RIO device structure
0045  *
0046  * @rdev: RIO device being referenced
0047  *
0048  * Each live reference to a device should be refcounted.
0049  *
0050  * Drivers for RIO devices should normally record such references in
0051  * their probe() methods, when they bind to a device, and release
0052  * them by calling rio_dev_put(), in their disconnect() methods.
0053  */
0054 struct rio_dev *rio_dev_get(struct rio_dev *rdev)
0055 {
0056     if (rdev)
0057         get_device(&rdev->dev);
0058 
0059     return rdev;
0060 }
0061 
0062 /**
0063  * rio_dev_put - Release a use of the RIO device structure
0064  *
0065  * @rdev: RIO device being disconnected
0066  *
0067  * Must be called when a user of a device is finished with it.
0068  * When the last user of the device calls this function, the
0069  * memory of the device is freed.
0070  */
0071 void rio_dev_put(struct rio_dev *rdev)
0072 {
0073     if (rdev)
0074         put_device(&rdev->dev);
0075 }
0076 
0077 /**
0078  *  rio_device_probe - Tell if a RIO device structure has a matching RIO device id structure
0079  *  @dev: the RIO device structure to match against
0080  *
0081  * return 0 and set rio_dev->driver when drv claims rio_dev, else error
0082  */
0083 static int rio_device_probe(struct device *dev)
0084 {
0085     struct rio_driver *rdrv = to_rio_driver(dev->driver);
0086     struct rio_dev *rdev = to_rio_dev(dev);
0087     int error = -ENODEV;
0088     const struct rio_device_id *id;
0089 
0090     if (!rdev->driver && rdrv->probe) {
0091         if (!rdrv->id_table)
0092             return error;
0093         id = rio_match_device(rdrv->id_table, rdev);
0094         rio_dev_get(rdev);
0095         if (id)
0096             error = rdrv->probe(rdev, id);
0097         if (error >= 0) {
0098             rdev->driver = rdrv;
0099             error = 0;
0100         } else
0101             rio_dev_put(rdev);
0102     }
0103     return error;
0104 }
0105 
0106 /**
0107  *  rio_device_remove - Remove a RIO device from the system
0108  *
0109  *  @dev: the RIO device structure to match against
0110  *
0111  * Remove a RIO device from the system. If it has an associated
0112  * driver, then run the driver remove() method.  Then update
0113  * the reference count.
0114  */
0115 static void rio_device_remove(struct device *dev)
0116 {
0117     struct rio_dev *rdev = to_rio_dev(dev);
0118     struct rio_driver *rdrv = rdev->driver;
0119 
0120     if (rdrv) {
0121         if (rdrv->remove)
0122             rdrv->remove(rdev);
0123         rdev->driver = NULL;
0124     }
0125 
0126     rio_dev_put(rdev);
0127 }
0128 
0129 static void rio_device_shutdown(struct device *dev)
0130 {
0131     struct rio_dev *rdev = to_rio_dev(dev);
0132     struct rio_driver *rdrv = rdev->driver;
0133 
0134     dev_dbg(dev, "RIO: %s\n", __func__);
0135 
0136     if (rdrv && rdrv->shutdown)
0137         rdrv->shutdown(rdev);
0138 }
0139 
0140 /**
0141  *  rio_register_driver - register a new RIO driver
0142  *  @rdrv: the RIO driver structure to register
0143  *
0144  *  Adds a &struct rio_driver to the list of registered drivers.
0145  *  Returns a negative value on error, otherwise 0. If no error
0146  *  occurred, the driver remains registered even if no device
0147  *  was claimed during registration.
0148  */
0149 int rio_register_driver(struct rio_driver *rdrv)
0150 {
0151     /* initialize common driver fields */
0152     rdrv->driver.name = rdrv->name;
0153     rdrv->driver.bus = &rio_bus_type;
0154 
0155     /* register with core */
0156     return driver_register(&rdrv->driver);
0157 }
0158 
0159 /**
0160  *  rio_unregister_driver - unregister a RIO driver
0161  *  @rdrv: the RIO driver structure to unregister
0162  *
0163  *  Deletes the &struct rio_driver from the list of registered RIO
0164  *  drivers, gives it a chance to clean up by calling its remove()
0165  *  function for each device it was responsible for, and marks those
0166  *  devices as driverless.
0167  */
0168 void rio_unregister_driver(struct rio_driver *rdrv)
0169 {
0170     driver_unregister(&rdrv->driver);
0171 }
0172 
0173 void rio_attach_device(struct rio_dev *rdev)
0174 {
0175     rdev->dev.bus = &rio_bus_type;
0176 }
0177 EXPORT_SYMBOL_GPL(rio_attach_device);
0178 
0179 /**
0180  *  rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
0181  *  @dev: the standard device structure to match against
0182  *  @drv: the standard driver structure containing the ids to match against
0183  *
0184  *  Used by a driver to check whether a RIO device present in the
0185  *  system is in its list of supported devices. Returns 1 if
0186  *  there is a matching &struct rio_device_id or 0 if there is
0187  *  no match.
0188  */
0189 static int rio_match_bus(struct device *dev, struct device_driver *drv)
0190 {
0191     struct rio_dev *rdev = to_rio_dev(dev);
0192     struct rio_driver *rdrv = to_rio_driver(drv);
0193     const struct rio_device_id *id = rdrv->id_table;
0194     const struct rio_device_id *found_id;
0195 
0196     if (!id)
0197         goto out;
0198 
0199     found_id = rio_match_device(id, rdev);
0200 
0201     if (found_id)
0202         return 1;
0203 
0204       out:return 0;
0205 }
0206 
0207 static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
0208 {
0209     struct rio_dev *rdev;
0210 
0211     if (!dev)
0212         return -ENODEV;
0213 
0214     rdev = to_rio_dev(dev);
0215     if (!rdev)
0216         return -ENODEV;
0217 
0218     if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
0219                rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
0220         return -ENOMEM;
0221     return 0;
0222 }
0223 
0224 struct class rio_mport_class = {
0225     .name       = "rapidio_port",
0226     .owner      = THIS_MODULE,
0227     .dev_groups = rio_mport_groups,
0228 };
0229 EXPORT_SYMBOL_GPL(rio_mport_class);
0230 
0231 struct bus_type rio_bus_type = {
0232     .name = "rapidio",
0233     .match = rio_match_bus,
0234     .dev_groups = rio_dev_groups,
0235     .bus_groups = rio_bus_groups,
0236     .probe = rio_device_probe,
0237     .remove = rio_device_remove,
0238     .shutdown = rio_device_shutdown,
0239     .uevent = rio_uevent,
0240 };
0241 
0242 /**
0243  *  rio_bus_init - Register the RapidIO bus with the device model
0244  *
0245  *  Registers the RIO mport device class and RIO bus type with the Linux
0246  *  device model.
0247  */
0248 static int __init rio_bus_init(void)
0249 {
0250     int ret;
0251 
0252     ret = class_register(&rio_mport_class);
0253     if (!ret) {
0254         ret = bus_register(&rio_bus_type);
0255         if (ret)
0256             class_unregister(&rio_mport_class);
0257     }
0258     return ret;
0259 }
0260 
0261 postcore_initcall(rio_bus_init);
0262 
0263 EXPORT_SYMBOL_GPL(rio_register_driver);
0264 EXPORT_SYMBOL_GPL(rio_unregister_driver);
0265 EXPORT_SYMBOL_GPL(rio_bus_type);
0266 EXPORT_SYMBOL_GPL(rio_dev_get);
0267 EXPORT_SYMBOL_GPL(rio_dev_put);