Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * MEN Chameleon Bus. |
| 4 | * |
| 5 | * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de) |
| 6 | * Author: Johannes Thumshirn <johannes.thumshirn@men.de> |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/idr.h> |
| 13 | #include <linux/mcb.h> |
| 14 | |
| 15 | static DEFINE_IDA(mcb_ida); |
| 16 | |
| 17 | static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids, |
| 18 | struct mcb_device *dev) |
| 19 | { |
| 20 | if (ids) { |
| 21 | while (ids->device) { |
| 22 | if (ids->device == dev->id) |
| 23 | return ids; |
| 24 | ids++; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | static int mcb_match(struct device *dev, struct device_driver *drv) |
| 32 | { |
| 33 | struct mcb_driver *mdrv = to_mcb_driver(drv); |
| 34 | struct mcb_device *mdev = to_mcb_device(dev); |
| 35 | const struct mcb_device_id *found_id; |
| 36 | |
| 37 | found_id = mcb_match_id(mdrv->id_table, mdev); |
| 38 | if (found_id) |
| 39 | return 1; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 45 | { |
| 46 | struct mcb_device *mdev = to_mcb_device(dev); |
| 47 | int ret; |
| 48 | |
| 49 | ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id); |
| 50 | if (ret) |
| 51 | return -ENOMEM; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int mcb_probe(struct device *dev) |
| 57 | { |
| 58 | struct mcb_driver *mdrv = to_mcb_driver(dev->driver); |
| 59 | struct mcb_device *mdev = to_mcb_device(dev); |
| 60 | const struct mcb_device_id *found_id; |
Johannes Thumshirn | 4d2ec85 | 2016-05-10 12:39:45 +0200 | [diff] [blame] | 61 | struct module *carrier_mod; |
| 62 | int ret; |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 63 | |
| 64 | found_id = mcb_match_id(mdrv->id_table, mdev); |
| 65 | if (!found_id) |
| 66 | return -ENODEV; |
| 67 | |
Johannes Thumshirn | 4d2ec85 | 2016-05-10 12:39:45 +0200 | [diff] [blame] | 68 | carrier_mod = mdev->dev.parent->driver->owner; |
| 69 | if (!try_module_get(carrier_mod)) |
| 70 | return -EINVAL; |
| 71 | |
Johannes Thumshirn | 7bc3640 | 2016-05-10 12:39:44 +0200 | [diff] [blame] | 72 | get_device(dev); |
Johannes Thumshirn | 4d2ec85 | 2016-05-10 12:39:45 +0200 | [diff] [blame] | 73 | ret = mdrv->probe(mdev, found_id); |
| 74 | if (ret) |
| 75 | module_put(carrier_mod); |
| 76 | |
| 77 | return ret; |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Uwe Kleine-König | fc7a620 | 2021-07-13 21:35:22 +0200 | [diff] [blame] | 80 | static void mcb_remove(struct device *dev) |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 81 | { |
| 82 | struct mcb_driver *mdrv = to_mcb_driver(dev->driver); |
| 83 | struct mcb_device *mdev = to_mcb_device(dev); |
Johannes Thumshirn | 4d2ec85 | 2016-05-10 12:39:45 +0200 | [diff] [blame] | 84 | struct module *carrier_mod; |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 85 | |
| 86 | mdrv->remove(mdev); |
| 87 | |
Johannes Thumshirn | 4d2ec85 | 2016-05-10 12:39:45 +0200 | [diff] [blame] | 88 | carrier_mod = mdev->dev.parent->driver->owner; |
| 89 | module_put(carrier_mod); |
| 90 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 91 | put_device(&mdev->dev); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void mcb_shutdown(struct device *dev) |
| 95 | { |
Johannes Thumshirn | 5d9e2ab | 2016-05-03 12:42:03 +0200 | [diff] [blame] | 96 | struct mcb_driver *mdrv = to_mcb_driver(dev->driver); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 97 | struct mcb_device *mdev = to_mcb_device(dev); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 98 | |
| 99 | if (mdrv && mdrv->shutdown) |
| 100 | mdrv->shutdown(mdev); |
| 101 | } |
| 102 | |
Johannes Thumshirn | 803f1ca | 2016-05-03 09:46:23 +0200 | [diff] [blame] | 103 | static ssize_t revision_show(struct device *dev, struct device_attribute *attr, |
| 104 | char *buf) |
| 105 | { |
| 106 | struct mcb_bus *bus = to_mcb_bus(dev); |
| 107 | |
| 108 | return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision); |
| 109 | } |
| 110 | static DEVICE_ATTR_RO(revision); |
| 111 | |
| 112 | static ssize_t model_show(struct device *dev, struct device_attribute *attr, |
| 113 | char *buf) |
| 114 | { |
| 115 | struct mcb_bus *bus = to_mcb_bus(dev); |
| 116 | |
| 117 | return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model); |
| 118 | } |
| 119 | static DEVICE_ATTR_RO(model); |
| 120 | |
| 121 | static ssize_t minor_show(struct device *dev, struct device_attribute *attr, |
| 122 | char *buf) |
| 123 | { |
| 124 | struct mcb_bus *bus = to_mcb_bus(dev); |
| 125 | |
| 126 | return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor); |
| 127 | } |
| 128 | static DEVICE_ATTR_RO(minor); |
| 129 | |
| 130 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 131 | char *buf) |
| 132 | { |
| 133 | struct mcb_bus *bus = to_mcb_bus(dev); |
| 134 | |
| 135 | return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name); |
| 136 | } |
| 137 | static DEVICE_ATTR_RO(name); |
| 138 | |
| 139 | static struct attribute *mcb_bus_attrs[] = { |
| 140 | &dev_attr_revision.attr, |
| 141 | &dev_attr_model.attr, |
| 142 | &dev_attr_minor.attr, |
| 143 | &dev_attr_name.attr, |
| 144 | NULL, |
| 145 | }; |
| 146 | |
| 147 | static const struct attribute_group mcb_carrier_group = { |
| 148 | .attrs = mcb_bus_attrs, |
| 149 | }; |
| 150 | |
| 151 | static const struct attribute_group *mcb_carrier_groups[] = { |
| 152 | &mcb_carrier_group, |
| 153 | NULL, |
| 154 | }; |
| 155 | |
| 156 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 157 | static struct bus_type mcb_bus_type = { |
| 158 | .name = "mcb", |
| 159 | .match = mcb_match, |
| 160 | .uevent = mcb_uevent, |
| 161 | .probe = mcb_probe, |
| 162 | .remove = mcb_remove, |
| 163 | .shutdown = mcb_shutdown, |
| 164 | }; |
| 165 | |
Johannes Thumshirn | 803f1ca | 2016-05-03 09:46:23 +0200 | [diff] [blame] | 166 | static struct device_type mcb_carrier_device_type = { |
| 167 | .name = "mcb-carrier", |
| 168 | .groups = mcb_carrier_groups, |
| 169 | }; |
| 170 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 171 | /** |
| 172 | * __mcb_register_driver() - Register a @mcb_driver at the system |
| 173 | * @drv: The @mcb_driver |
| 174 | * @owner: The @mcb_driver's module |
| 175 | * @mod_name: The name of the @mcb_driver's module |
| 176 | * |
| 177 | * Register a @mcb_driver at the system. Perform some sanity checks, if |
| 178 | * the .probe and .remove methods are provided by the driver. |
| 179 | */ |
| 180 | int __mcb_register_driver(struct mcb_driver *drv, struct module *owner, |
| 181 | const char *mod_name) |
| 182 | { |
| 183 | if (!drv->probe || !drv->remove) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | drv->driver.owner = owner; |
| 187 | drv->driver.bus = &mcb_bus_type; |
| 188 | drv->driver.mod_name = mod_name; |
| 189 | |
| 190 | return driver_register(&drv->driver); |
| 191 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 192 | EXPORT_SYMBOL_NS_GPL(__mcb_register_driver, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * mcb_unregister_driver() - Unregister a @mcb_driver from the system |
| 196 | * @drv: The @mcb_driver |
| 197 | * |
| 198 | * Unregister a @mcb_driver from the system. |
| 199 | */ |
| 200 | void mcb_unregister_driver(struct mcb_driver *drv) |
| 201 | { |
| 202 | driver_unregister(&drv->driver); |
| 203 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 204 | EXPORT_SYMBOL_NS_GPL(mcb_unregister_driver, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 205 | |
| 206 | static void mcb_release_dev(struct device *dev) |
| 207 | { |
| 208 | struct mcb_device *mdev = to_mcb_device(dev); |
| 209 | |
| 210 | mcb_bus_put(mdev->bus); |
| 211 | kfree(mdev); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * mcb_device_register() - Register a mcb_device |
| 216 | * @bus: The @mcb_bus of the device |
| 217 | * @dev: The @mcb_device |
| 218 | * |
| 219 | * Register a specific @mcb_device at a @mcb_bus and the system itself. |
| 220 | */ |
| 221 | int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev) |
| 222 | { |
| 223 | int ret; |
| 224 | int device_id; |
| 225 | |
| 226 | device_initialize(&dev->dev); |
Johannes Thumshirn | 5d9e2ab | 2016-05-03 12:42:03 +0200 | [diff] [blame] | 227 | mcb_bus_get(bus); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 228 | dev->dev.bus = &mcb_bus_type; |
| 229 | dev->dev.parent = bus->dev.parent; |
| 230 | dev->dev.release = mcb_release_dev; |
Michael Moese | 2d8784d | 2016-09-14 12:05:24 +0200 | [diff] [blame] | 231 | dev->dma_dev = bus->carrier; |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 232 | |
| 233 | device_id = dev->id; |
| 234 | dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d", |
| 235 | bus->bus_nr, device_id, dev->inst, dev->group, dev->var); |
| 236 | |
| 237 | ret = device_add(&dev->dev); |
| 238 | if (ret < 0) { |
| 239 | pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n", |
| 240 | device_id, bus->bus_nr, ret); |
| 241 | goto out; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | |
| 246 | out: |
| 247 | |
| 248 | return ret; |
| 249 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 250 | EXPORT_SYMBOL_NS_GPL(mcb_device_register, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 251 | |
Johannes Thumshirn | 5d9e2ab | 2016-05-03 12:42:03 +0200 | [diff] [blame] | 252 | static void mcb_free_bus(struct device *dev) |
| 253 | { |
| 254 | struct mcb_bus *bus = to_mcb_bus(dev); |
| 255 | |
| 256 | put_device(bus->carrier); |
| 257 | ida_simple_remove(&mcb_ida, bus->bus_nr); |
| 258 | kfree(bus); |
| 259 | } |
| 260 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 261 | /** |
| 262 | * mcb_alloc_bus() - Allocate a new @mcb_bus |
| 263 | * |
| 264 | * Allocate a new @mcb_bus. |
| 265 | */ |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 266 | struct mcb_bus *mcb_alloc_bus(struct device *carrier) |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 267 | { |
| 268 | struct mcb_bus *bus; |
| 269 | int bus_nr; |
Johannes Thumshirn | 18d2881 | 2016-05-03 09:46:22 +0200 | [diff] [blame] | 270 | int rc; |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 271 | |
| 272 | bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL); |
| 273 | if (!bus) |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 274 | return ERR_PTR(-ENOMEM); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 275 | |
| 276 | bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL); |
| 277 | if (bus_nr < 0) { |
Dan Carpenter | 25a1433 | 2021-09-06 21:35:48 +0900 | [diff] [blame] | 278 | kfree(bus); |
| 279 | return ERR_PTR(bus_nr); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 282 | bus->bus_nr = bus_nr; |
Johannes Thumshirn | 5d9e2ab | 2016-05-03 12:42:03 +0200 | [diff] [blame] | 283 | bus->carrier = get_device(carrier); |
Johannes Thumshirn | 18d2881 | 2016-05-03 09:46:22 +0200 | [diff] [blame] | 284 | |
| 285 | device_initialize(&bus->dev); |
| 286 | bus->dev.parent = carrier; |
| 287 | bus->dev.bus = &mcb_bus_type; |
Johannes Thumshirn | 803f1ca | 2016-05-03 09:46:23 +0200 | [diff] [blame] | 288 | bus->dev.type = &mcb_carrier_device_type; |
Johannes Thumshirn | 5d9e2ab | 2016-05-03 12:42:03 +0200 | [diff] [blame] | 289 | bus->dev.release = &mcb_free_bus; |
Johannes Thumshirn | 18d2881 | 2016-05-03 09:46:22 +0200 | [diff] [blame] | 290 | |
| 291 | dev_set_name(&bus->dev, "mcb:%d", bus_nr); |
| 292 | rc = device_add(&bus->dev); |
| 293 | if (rc) |
Dan Carpenter | 25a1433 | 2021-09-06 21:35:48 +0900 | [diff] [blame] | 294 | goto err_put; |
Johannes Thumshirn | 18d2881 | 2016-05-03 09:46:22 +0200 | [diff] [blame] | 295 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 296 | return bus; |
Dan Carpenter | 25a1433 | 2021-09-06 21:35:48 +0900 | [diff] [blame] | 297 | |
| 298 | err_put: |
| 299 | put_device(&bus->dev); |
Johannes Thumshirn | 18d2881 | 2016-05-03 09:46:22 +0200 | [diff] [blame] | 300 | return ERR_PTR(rc); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 301 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 302 | EXPORT_SYMBOL_NS_GPL(mcb_alloc_bus, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 303 | |
| 304 | static int __mcb_devices_unregister(struct device *dev, void *data) |
| 305 | { |
| 306 | device_unregister(dev); |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static void mcb_devices_unregister(struct mcb_bus *bus) |
| 311 | { |
| 312 | bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister); |
| 313 | } |
| 314 | /** |
| 315 | * mcb_release_bus() - Free a @mcb_bus |
| 316 | * @bus: The @mcb_bus to release |
| 317 | * |
| 318 | * Release an allocated @mcb_bus from the system. |
| 319 | */ |
| 320 | void mcb_release_bus(struct mcb_bus *bus) |
| 321 | { |
| 322 | mcb_devices_unregister(bus); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 323 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 324 | EXPORT_SYMBOL_NS_GPL(mcb_release_bus, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * mcb_bus_put() - Increment refcnt |
| 328 | * @bus: The @mcb_bus |
| 329 | * |
| 330 | * Get a @mcb_bus' ref |
| 331 | */ |
| 332 | struct mcb_bus *mcb_bus_get(struct mcb_bus *bus) |
| 333 | { |
| 334 | if (bus) |
| 335 | get_device(&bus->dev); |
| 336 | |
| 337 | return bus; |
| 338 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 339 | EXPORT_SYMBOL_NS_GPL(mcb_bus_get, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 340 | |
| 341 | /** |
| 342 | * mcb_bus_put() - Decrement refcnt |
| 343 | * @bus: The @mcb_bus |
| 344 | * |
| 345 | * Release a @mcb_bus' ref |
| 346 | */ |
| 347 | void mcb_bus_put(struct mcb_bus *bus) |
| 348 | { |
| 349 | if (bus) |
| 350 | put_device(&bus->dev); |
| 351 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 352 | EXPORT_SYMBOL_NS_GPL(mcb_bus_put, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * mcb_alloc_dev() - Allocate a device |
| 356 | * @bus: The @mcb_bus the device is part of |
| 357 | * |
| 358 | * Allocate a @mcb_device and add bus. |
| 359 | */ |
| 360 | struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus) |
| 361 | { |
| 362 | struct mcb_device *dev; |
| 363 | |
| 364 | dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL); |
| 365 | if (!dev) |
| 366 | return NULL; |
| 367 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 368 | dev->bus = bus; |
| 369 | |
| 370 | return dev; |
| 371 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 372 | EXPORT_SYMBOL_NS_GPL(mcb_alloc_dev, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 373 | |
| 374 | /** |
| 375 | * mcb_free_dev() - Free @mcb_device |
| 376 | * @dev: The device to free |
| 377 | * |
| 378 | * Free a @mcb_device |
| 379 | */ |
| 380 | void mcb_free_dev(struct mcb_device *dev) |
| 381 | { |
| 382 | kfree(dev); |
| 383 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 384 | EXPORT_SYMBOL_NS_GPL(mcb_free_dev, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 385 | |
| 386 | static int __mcb_bus_add_devices(struct device *dev, void *data) |
| 387 | { |
| 388 | struct mcb_device *mdev = to_mcb_device(dev); |
| 389 | int retval; |
| 390 | |
| 391 | if (mdev->is_added) |
| 392 | return 0; |
| 393 | |
| 394 | retval = device_attach(dev); |
| 395 | if (retval < 0) |
| 396 | dev_err(dev, "Error adding device (%d)\n", retval); |
| 397 | |
| 398 | mdev->is_added = true; |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 403 | /** |
| 404 | * mcb_bus_add_devices() - Add devices in the bus' internal device list |
| 405 | * @bus: The @mcb_bus we add the devices |
| 406 | * |
| 407 | * Add devices in the bus' internal device list to the system. |
| 408 | */ |
| 409 | void mcb_bus_add_devices(const struct mcb_bus *bus) |
| 410 | { |
| 411 | bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 412 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 413 | EXPORT_SYMBOL_NS_GPL(mcb_bus_add_devices, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 414 | |
| 415 | /** |
Johannes Thumshirn | 2ce8008 | 2017-08-02 09:58:52 +0200 | [diff] [blame] | 416 | * mcb_get_resource() - get a resource for a mcb device |
| 417 | * @dev: the mcb device |
| 418 | * @type: the type of resource |
| 419 | */ |
| 420 | struct resource *mcb_get_resource(struct mcb_device *dev, unsigned int type) |
| 421 | { |
| 422 | if (type == IORESOURCE_MEM) |
| 423 | return &dev->mem; |
| 424 | else if (type == IORESOURCE_IRQ) |
| 425 | return &dev->irq; |
| 426 | else |
| 427 | return NULL; |
| 428 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 429 | EXPORT_SYMBOL_NS_GPL(mcb_get_resource, MCB); |
Johannes Thumshirn | 2ce8008 | 2017-08-02 09:58:52 +0200 | [diff] [blame] | 430 | |
| 431 | /** |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 432 | * mcb_request_mem() - Request memory |
| 433 | * @dev: The @mcb_device the memory is for |
| 434 | * @name: The name for the memory reference. |
| 435 | * |
| 436 | * Request memory for a @mcb_device. If @name is NULL the driver name will |
| 437 | * be used. |
| 438 | */ |
| 439 | struct resource *mcb_request_mem(struct mcb_device *dev, const char *name) |
| 440 | { |
| 441 | struct resource *mem; |
| 442 | u32 size; |
| 443 | |
| 444 | if (!name) |
| 445 | name = dev->dev.driver->name; |
| 446 | |
| 447 | size = resource_size(&dev->mem); |
| 448 | |
| 449 | mem = request_mem_region(dev->mem.start, size, name); |
| 450 | if (!mem) |
| 451 | return ERR_PTR(-EBUSY); |
| 452 | |
| 453 | return mem; |
| 454 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 455 | EXPORT_SYMBOL_NS_GPL(mcb_request_mem, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * mcb_release_mem() - Release memory requested by device |
| 459 | * @dev: The @mcb_device that requested the memory |
| 460 | * |
| 461 | * Release memory that was prior requested via @mcb_request_mem(). |
| 462 | */ |
| 463 | void mcb_release_mem(struct resource *mem) |
| 464 | { |
| 465 | u32 size; |
| 466 | |
| 467 | size = resource_size(mem); |
| 468 | release_mem_region(mem->start, size); |
| 469 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 470 | EXPORT_SYMBOL_NS_GPL(mcb_release_mem, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 471 | |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 472 | static int __mcb_get_irq(struct mcb_device *dev) |
| 473 | { |
Johannes Thumshirn | 2ce8008 | 2017-08-02 09:58:52 +0200 | [diff] [blame] | 474 | struct resource *irq; |
| 475 | |
| 476 | irq = mcb_get_resource(dev, IORESOURCE_IRQ); |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 477 | |
| 478 | return irq->start; |
| 479 | } |
| 480 | |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 481 | /** |
| 482 | * mcb_get_irq() - Get device's IRQ number |
| 483 | * @dev: The @mcb_device the IRQ is for |
| 484 | * |
| 485 | * Get the IRQ number of a given @mcb_device. |
| 486 | */ |
| 487 | int mcb_get_irq(struct mcb_device *dev) |
| 488 | { |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 489 | struct mcb_bus *bus = dev->bus; |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 490 | |
Johannes Thumshirn | 4ec65b7 | 2014-04-24 14:35:25 +0200 | [diff] [blame] | 491 | if (bus->get_irq) |
| 492 | return bus->get_irq(dev); |
| 493 | |
| 494 | return __mcb_get_irq(dev); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 495 | } |
Johannes Thumshirn | 891e603 | 2019-10-16 12:01:58 +0200 | [diff] [blame] | 496 | EXPORT_SYMBOL_NS_GPL(mcb_get_irq, MCB); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 497 | |
| 498 | static int mcb_init(void) |
| 499 | { |
| 500 | return bus_register(&mcb_bus_type); |
| 501 | } |
| 502 | |
| 503 | static void mcb_exit(void) |
| 504 | { |
Johannes Thumshirn | 169883a | 2015-10-28 08:22:16 +0100 | [diff] [blame] | 505 | ida_destroy(&mcb_ida); |
Johannes Thumshirn | 3764e82 | 2014-02-26 17:29:05 +0100 | [diff] [blame] | 506 | bus_unregister(&mcb_bus_type); |
| 507 | } |
| 508 | |
| 509 | /* mcb must be initialized after PCI but before the chameleon drivers. |
| 510 | * That means we must use some initcall between subsys_initcall and |
| 511 | * device_initcall. |
| 512 | */ |
| 513 | fs_initcall(mcb_init); |
| 514 | module_exit(mcb_exit); |
| 515 | |
| 516 | MODULE_DESCRIPTION("MEN Chameleon Bus Driver"); |
| 517 | MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>"); |
| 518 | MODULE_LICENSE("GPL v2"); |