0001
0002
0003
0004
0005
0006
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
0019
0020
0021
0022
0023
0024
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
0045
0046
0047
0048
0049
0050
0051
0052
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
0064
0065
0066
0067
0068
0069
0070
0071 void rio_dev_put(struct rio_dev *rdev)
0072 {
0073 if (rdev)
0074 put_device(&rdev->dev);
0075 }
0076
0077
0078
0079
0080
0081
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
0108
0109
0110
0111
0112
0113
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
0142
0143
0144
0145
0146
0147
0148
0149 int rio_register_driver(struct rio_driver *rdrv)
0150 {
0151
0152 rdrv->driver.name = rdrv->name;
0153 rdrv->driver.bus = &rio_bus_type;
0154
0155
0156 return driver_register(&rdrv->driver);
0157 }
0158
0159
0160
0161
0162
0163
0164
0165
0166
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
0181
0182
0183
0184
0185
0186
0187
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
0244
0245
0246
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);