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