1 /* 2 * platform.c - platform 'pseudo' bus for legacy devices 3 * 4 * Copyright (c) 2002-3 Patrick Mochel 5 * Copyright (c) 2002-3 Open Source Development Labs 6 * 7 * This file is released under the GPLv2 8 * 9 * Please see Documentation/driver-model/platform.txt for more 10 * information. 11 */ 12 13 #include <linux/platform_device.h> 14 #include <linux/module.h> 15 #include <linux/init.h> 16 #include <linux/dma-mapping.h> 17 #include <linux/bootmem.h> 18 #include <linux/err.h> 19 #include <linux/slab.h> 20 21 #include "base.h" 22 23 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \ 24 driver)) 25 26 struct device platform_bus = { 27 .bus_id = "platform", 28 }; 29 EXPORT_SYMBOL_GPL(platform_bus); 30 31 /** 32 * platform_get_resource - get a resource for a device 33 * @dev: platform device 34 * @type: resource type 35 * @num: resource index 36 */ 37 struct resource *platform_get_resource(struct platform_device *dev, 38 unsigned int type, unsigned int num) 39 { 40 int i; 41 42 for (i = 0; i < dev->num_resources; i++) { 43 struct resource *r = &dev->resource[i]; 44 45 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM| 46 IORESOURCE_IRQ|IORESOURCE_DMA)) == type) 47 if (num-- == 0) 48 return r; 49 } 50 return NULL; 51 } 52 EXPORT_SYMBOL_GPL(platform_get_resource); 53 54 /** 55 * platform_get_irq - get an IRQ for a device 56 * @dev: platform device 57 * @num: IRQ number index 58 */ 59 int platform_get_irq(struct platform_device *dev, unsigned int num) 60 { 61 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); 62 63 return r ? r->start : -ENXIO; 64 } 65 EXPORT_SYMBOL_GPL(platform_get_irq); 66 67 /** 68 * platform_get_resource_byname - get a resource for a device by name 69 * @dev: platform device 70 * @type: resource type 71 * @name: resource name 72 */ 73 struct resource *platform_get_resource_byname(struct platform_device *dev, 74 unsigned int type, char *name) 75 { 76 int i; 77 78 for (i = 0; i < dev->num_resources; i++) { 79 struct resource *r = &dev->resource[i]; 80 81 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM| 82 IORESOURCE_IRQ|IORESOURCE_DMA)) == type) 83 if (!strcmp(r->name, name)) 84 return r; 85 } 86 return NULL; 87 } 88 EXPORT_SYMBOL_GPL(platform_get_resource_byname); 89 90 /** 91 * platform_get_irq - get an IRQ for a device 92 * @dev: platform device 93 * @name: IRQ name 94 */ 95 int platform_get_irq_byname(struct platform_device *dev, char *name) 96 { 97 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, 98 name); 99 100 return r ? r->start : -ENXIO; 101 } 102 EXPORT_SYMBOL_GPL(platform_get_irq_byname); 103 104 /** 105 * platform_add_devices - add a numbers of platform devices 106 * @devs: array of platform devices to add 107 * @num: number of platform devices in array 108 */ 109 int platform_add_devices(struct platform_device **devs, int num) 110 { 111 int i, ret = 0; 112 113 for (i = 0; i < num; i++) { 114 ret = platform_device_register(devs[i]); 115 if (ret) { 116 while (--i >= 0) 117 platform_device_unregister(devs[i]); 118 break; 119 } 120 } 121 122 return ret; 123 } 124 EXPORT_SYMBOL_GPL(platform_add_devices); 125 126 struct platform_object { 127 struct platform_device pdev; 128 char name[1]; 129 }; 130 131 /** 132 * platform_device_put 133 * @pdev: platform device to free 134 * 135 * Free all memory associated with a platform device. This function must 136 * _only_ be externally called in error cases. All other usage is a bug. 137 */ 138 void platform_device_put(struct platform_device *pdev) 139 { 140 if (pdev) 141 put_device(&pdev->dev); 142 } 143 EXPORT_SYMBOL_GPL(platform_device_put); 144 145 static void platform_device_release(struct device *dev) 146 { 147 struct platform_object *pa = container_of(dev, struct platform_object, 148 pdev.dev); 149 150 kfree(pa->pdev.dev.platform_data); 151 kfree(pa->pdev.resource); 152 kfree(pa); 153 } 154 155 /** 156 * platform_device_alloc 157 * @name: base name of the device we're adding 158 * @id: instance id 159 * 160 * Create a platform device object which can have other objects attached 161 * to it, and which will have attached objects freed when it is released. 162 */ 163 struct platform_device *platform_device_alloc(const char *name, int id) 164 { 165 struct platform_object *pa; 166 167 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL); 168 if (pa) { 169 strcpy(pa->name, name); 170 pa->pdev.name = pa->name; 171 pa->pdev.id = id; 172 device_initialize(&pa->pdev.dev); 173 pa->pdev.dev.release = platform_device_release; 174 } 175 176 return pa ? &pa->pdev : NULL; 177 } 178 EXPORT_SYMBOL_GPL(platform_device_alloc); 179 180 /** 181 * platform_device_add_resources 182 * @pdev: platform device allocated by platform_device_alloc to add resources to 183 * @res: set of resources that needs to be allocated for the device 184 * @num: number of resources 185 * 186 * Add a copy of the resources to the platform device. The memory 187 * associated with the resources will be freed when the platform device is 188 * released. 189 */ 190 int platform_device_add_resources(struct platform_device *pdev, 191 struct resource *res, unsigned int num) 192 { 193 struct resource *r; 194 195 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL); 196 if (r) { 197 memcpy(r, res, sizeof(struct resource) * num); 198 pdev->resource = r; 199 pdev->num_resources = num; 200 } 201 return r ? 0 : -ENOMEM; 202 } 203 EXPORT_SYMBOL_GPL(platform_device_add_resources); 204 205 /** 206 * platform_device_add_data 207 * @pdev: platform device allocated by platform_device_alloc to add resources to 208 * @data: platform specific data for this platform device 209 * @size: size of platform specific data 210 * 211 * Add a copy of platform specific data to the platform device's 212 * platform_data pointer. The memory associated with the platform data 213 * will be freed when the platform device is released. 214 */ 215 int platform_device_add_data(struct platform_device *pdev, const void *data, 216 size_t size) 217 { 218 void *d; 219 220 d = kmalloc(size, GFP_KERNEL); 221 if (d) { 222 memcpy(d, data, size); 223 pdev->dev.platform_data = d; 224 } 225 return d ? 0 : -ENOMEM; 226 } 227 EXPORT_SYMBOL_GPL(platform_device_add_data); 228 229 /** 230 * platform_device_add - add a platform device to device hierarchy 231 * @pdev: platform device we're adding 232 * 233 * This is part 2 of platform_device_register(), though may be called 234 * separately _iff_ pdev was allocated by platform_device_alloc(). 235 */ 236 int platform_device_add(struct platform_device *pdev) 237 { 238 int i, ret = 0; 239 240 if (!pdev) 241 return -EINVAL; 242 243 if (!pdev->dev.parent) 244 pdev->dev.parent = &platform_bus; 245 246 pdev->dev.bus = &platform_bus_type; 247 248 if (pdev->id != -1) 249 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%d", pdev->name, 250 pdev->id); 251 else 252 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE); 253 254 for (i = 0; i < pdev->num_resources; i++) { 255 struct resource *p, *r = &pdev->resource[i]; 256 257 if (r->name == NULL) 258 r->name = pdev->dev.bus_id; 259 260 p = r->parent; 261 if (!p) { 262 if (r->flags & IORESOURCE_MEM) 263 p = &iomem_resource; 264 else if (r->flags & IORESOURCE_IO) 265 p = &ioport_resource; 266 } 267 268 if (p && insert_resource(p, r)) { 269 printk(KERN_ERR 270 "%s: failed to claim resource %d\n", 271 pdev->dev.bus_id, i); 272 ret = -EBUSY; 273 goto failed; 274 } 275 } 276 277 pr_debug("Registering platform device '%s'. Parent at %s\n", 278 pdev->dev.bus_id, pdev->dev.parent->bus_id); 279 280 ret = device_add(&pdev->dev); 281 if (ret == 0) 282 return ret; 283 284 failed: 285 while (--i >= 0) 286 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO)) 287 release_resource(&pdev->resource[i]); 288 return ret; 289 } 290 EXPORT_SYMBOL_GPL(platform_device_add); 291 292 /** 293 * platform_device_del - remove a platform-level device 294 * @pdev: platform device we're removing 295 * 296 * Note that this function will also release all memory- and port-based 297 * resources owned by the device (@dev->resource). This function must 298 * _only_ be externally called in error cases. All other usage is a bug. 299 */ 300 void platform_device_del(struct platform_device *pdev) 301 { 302 int i; 303 304 if (pdev) { 305 device_del(&pdev->dev); 306 307 for (i = 0; i < pdev->num_resources; i++) { 308 struct resource *r = &pdev->resource[i]; 309 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO)) 310 release_resource(r); 311 } 312 } 313 } 314 EXPORT_SYMBOL_GPL(platform_device_del); 315 316 /** 317 * platform_device_register - add a platform-level device 318 * @pdev: platform device we're adding 319 */ 320 int platform_device_register(struct platform_device *pdev) 321 { 322 device_initialize(&pdev->dev); 323 return platform_device_add(pdev); 324 } 325 EXPORT_SYMBOL_GPL(platform_device_register); 326 327 /** 328 * platform_device_unregister - unregister a platform-level device 329 * @pdev: platform device we're unregistering 330 * 331 * Unregistration is done in 2 steps. First we release all resources 332 * and remove it from the subsystem, then we drop reference count by 333 * calling platform_device_put(). 334 */ 335 void platform_device_unregister(struct platform_device *pdev) 336 { 337 platform_device_del(pdev); 338 platform_device_put(pdev); 339 } 340 EXPORT_SYMBOL_GPL(platform_device_unregister); 341 342 /** 343 * platform_device_register_simple 344 * @name: base name of the device we're adding 345 * @id: instance id 346 * @res: set of resources that needs to be allocated for the device 347 * @num: number of resources 348 * 349 * This function creates a simple platform device that requires minimal 350 * resource and memory management. Canned release function freeing memory 351 * allocated for the device allows drivers using such devices to be 352 * unloaded without waiting for the last reference to the device to be 353 * dropped. 354 * 355 * This interface is primarily intended for use with legacy drivers which 356 * probe hardware directly. Because such drivers create sysfs device nodes 357 * themselves, rather than letting system infrastructure handle such device 358 * enumeration tasks, they don't fully conform to the Linux driver model. 359 * In particular, when such drivers are built as modules, they can't be 360 * "hotplugged". 361 */ 362 struct platform_device *platform_device_register_simple(const char *name, 363 int id, 364 struct resource *res, 365 unsigned int num) 366 { 367 struct platform_device *pdev; 368 int retval; 369 370 pdev = platform_device_alloc(name, id); 371 if (!pdev) { 372 retval = -ENOMEM; 373 goto error; 374 } 375 376 if (num) { 377 retval = platform_device_add_resources(pdev, res, num); 378 if (retval) 379 goto error; 380 } 381 382 retval = platform_device_add(pdev); 383 if (retval) 384 goto error; 385 386 return pdev; 387 388 error: 389 platform_device_put(pdev); 390 return ERR_PTR(retval); 391 } 392 EXPORT_SYMBOL_GPL(platform_device_register_simple); 393 394 static int platform_drv_probe(struct device *_dev) 395 { 396 struct platform_driver *drv = to_platform_driver(_dev->driver); 397 struct platform_device *dev = to_platform_device(_dev); 398 399 return drv->probe(dev); 400 } 401 402 static int platform_drv_probe_fail(struct device *_dev) 403 { 404 return -ENXIO; 405 } 406 407 static int platform_drv_remove(struct device *_dev) 408 { 409 struct platform_driver *drv = to_platform_driver(_dev->driver); 410 struct platform_device *dev = to_platform_device(_dev); 411 412 return drv->remove(dev); 413 } 414 415 static void platform_drv_shutdown(struct device *_dev) 416 { 417 struct platform_driver *drv = to_platform_driver(_dev->driver); 418 struct platform_device *dev = to_platform_device(_dev); 419 420 drv->shutdown(dev); 421 } 422 423 static int platform_drv_suspend(struct device *_dev, pm_message_t state) 424 { 425 struct platform_driver *drv = to_platform_driver(_dev->driver); 426 struct platform_device *dev = to_platform_device(_dev); 427 428 return drv->suspend(dev, state); 429 } 430 431 static int platform_drv_resume(struct device *_dev) 432 { 433 struct platform_driver *drv = to_platform_driver(_dev->driver); 434 struct platform_device *dev = to_platform_device(_dev); 435 436 return drv->resume(dev); 437 } 438 439 /** 440 * platform_driver_register 441 * @drv: platform driver structure 442 */ 443 int platform_driver_register(struct platform_driver *drv) 444 { 445 drv->driver.bus = &platform_bus_type; 446 if (drv->probe) 447 drv->driver.probe = platform_drv_probe; 448 if (drv->remove) 449 drv->driver.remove = platform_drv_remove; 450 if (drv->shutdown) 451 drv->driver.shutdown = platform_drv_shutdown; 452 if (drv->suspend) 453 drv->driver.suspend = platform_drv_suspend; 454 if (drv->resume) 455 drv->driver.resume = platform_drv_resume; 456 return driver_register(&drv->driver); 457 } 458 EXPORT_SYMBOL_GPL(platform_driver_register); 459 460 /** 461 * platform_driver_unregister 462 * @drv: platform driver structure 463 */ 464 void platform_driver_unregister(struct platform_driver *drv) 465 { 466 driver_unregister(&drv->driver); 467 } 468 EXPORT_SYMBOL_GPL(platform_driver_unregister); 469 470 /** 471 * platform_driver_probe - register driver for non-hotpluggable device 472 * @drv: platform driver structure 473 * @probe: the driver probe routine, probably from an __init section 474 * 475 * Use this instead of platform_driver_register() when you know the device 476 * is not hotpluggable and has already been registered, and you want to 477 * remove its run-once probe() infrastructure from memory after the driver 478 * has bound to the device. 479 * 480 * One typical use for this would be with drivers for controllers integrated 481 * into system-on-chip processors, where the controller devices have been 482 * configured as part of board setup. 483 * 484 * Returns zero if the driver registered and bound to a device, else returns 485 * a negative error code and with the driver not registered. 486 */ 487 int __init_or_module platform_driver_probe(struct platform_driver *drv, 488 int (*probe)(struct platform_device *)) 489 { 490 int retval, code; 491 492 /* temporary section violation during probe() */ 493 drv->probe = probe; 494 retval = code = platform_driver_register(drv); 495 496 /* Fixup that section violation, being paranoid about code scanning 497 * the list of drivers in order to probe new devices. Check to see 498 * if the probe was successful, and make sure any forced probes of 499 * new devices fail. 500 */ 501 spin_lock(&platform_bus_type.p->klist_drivers.k_lock); 502 drv->probe = NULL; 503 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) 504 retval = -ENODEV; 505 drv->driver.probe = platform_drv_probe_fail; 506 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock); 507 508 if (code != retval) 509 platform_driver_unregister(drv); 510 return retval; 511 } 512 EXPORT_SYMBOL_GPL(platform_driver_probe); 513 514 /* modalias support enables more hands-off userspace setup: 515 * (a) environment variable lets new-style hotplug events work once system is 516 * fully running: "modprobe $MODALIAS" 517 * (b) sysfs attribute lets new-style coldplug recover from hotplug events 518 * mishandled before system is fully running: "modprobe $(cat modalias)" 519 */ 520 static ssize_t modalias_show(struct device *dev, struct device_attribute *a, 521 char *buf) 522 { 523 struct platform_device *pdev = to_platform_device(dev); 524 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); 525 526 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; 527 } 528 529 static struct device_attribute platform_dev_attrs[] = { 530 __ATTR_RO(modalias), 531 __ATTR_NULL, 532 }; 533 534 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) 535 { 536 struct platform_device *pdev = to_platform_device(dev); 537 538 add_uevent_var(env, "MODALIAS=platform:%s", pdev->name); 539 return 0; 540 } 541 542 /** 543 * platform_match - bind platform device to platform driver. 544 * @dev: device. 545 * @drv: driver. 546 * 547 * Platform device IDs are assumed to be encoded like this: 548 * "<name><instance>", where <name> is a short description of the type of 549 * device, like "pci" or "floppy", and <instance> is the enumerated 550 * instance of the device, like '0' or '42'. Driver IDs are simply 551 * "<name>". So, extract the <name> from the platform_device structure, 552 * and compare it against the name of the driver. Return whether they match 553 * or not. 554 */ 555 static int platform_match(struct device *dev, struct device_driver *drv) 556 { 557 struct platform_device *pdev; 558 559 pdev = container_of(dev, struct platform_device, dev); 560 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); 561 } 562 563 static int platform_suspend(struct device *dev, pm_message_t mesg) 564 { 565 int ret = 0; 566 567 if (dev->driver && dev->driver->suspend) 568 ret = dev->driver->suspend(dev, mesg); 569 570 return ret; 571 } 572 573 static int platform_suspend_late(struct device *dev, pm_message_t mesg) 574 { 575 struct platform_driver *drv = to_platform_driver(dev->driver); 576 struct platform_device *pdev; 577 int ret = 0; 578 579 pdev = container_of(dev, struct platform_device, dev); 580 if (dev->driver && drv->suspend_late) 581 ret = drv->suspend_late(pdev, mesg); 582 583 return ret; 584 } 585 586 static int platform_resume_early(struct device *dev) 587 { 588 struct platform_driver *drv = to_platform_driver(dev->driver); 589 struct platform_device *pdev; 590 int ret = 0; 591 592 pdev = container_of(dev, struct platform_device, dev); 593 if (dev->driver && drv->resume_early) 594 ret = drv->resume_early(pdev); 595 596 return ret; 597 } 598 599 static int platform_resume(struct device *dev) 600 { 601 int ret = 0; 602 603 if (dev->driver && dev->driver->resume) 604 ret = dev->driver->resume(dev); 605 606 return ret; 607 } 608 609 struct bus_type platform_bus_type = { 610 .name = "platform", 611 .dev_attrs = platform_dev_attrs, 612 .match = platform_match, 613 .uevent = platform_uevent, 614 .suspend = platform_suspend, 615 .suspend_late = platform_suspend_late, 616 .resume_early = platform_resume_early, 617 .resume = platform_resume, 618 }; 619 EXPORT_SYMBOL_GPL(platform_bus_type); 620 621 int __init platform_bus_init(void) 622 { 623 int error; 624 625 error = device_register(&platform_bus); 626 if (error) 627 return error; 628 error = bus_register(&platform_bus_type); 629 if (error) 630 device_unregister(&platform_bus); 631 return error; 632 } 633 634 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK 635 u64 dma_get_required_mask(struct device *dev) 636 { 637 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); 638 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); 639 u64 mask; 640 641 if (!high_totalram) { 642 /* convert to mask just covering totalram */ 643 low_totalram = (1 << (fls(low_totalram) - 1)); 644 low_totalram += low_totalram - 1; 645 mask = low_totalram; 646 } else { 647 high_totalram = (1 << (fls(high_totalram) - 1)); 648 high_totalram += high_totalram - 1; 649 mask = (((u64)high_totalram) << 32) + 0xffffffff; 650 } 651 return mask & *dev->dma_mask; 652 } 653 EXPORT_SYMBOL_GPL(dma_get_required_mask); 654 #endif 655