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