of/device: Protect against binding of_platform_drivers to non-OF devices
[linux-2.6.git] / drivers / of / platform.c
blobf3f1ec81ef487a08990535f670b7027c9bc43b06
1 /*
2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 * and Arnd Bergmann, IBM Corp.
5 * Merged from powerpc/kernel/of_platform.c and
6 * sparc{,64}/kernel/of_device.c by Stephen Rothwell
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/slab.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
25 static int platform_driver_probe_shim(struct platform_device *pdev)
27 struct platform_driver *pdrv;
28 struct of_platform_driver *ofpdrv;
29 const struct of_device_id *match;
31 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
32 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
34 /* There is an unlikely chance that an of_platform driver might match
35 * on a non-OF platform device. If so, then of_match_device() will
36 * come up empty. Return -EINVAL in this case so other drivers get
37 * the chance to bind. */
38 match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
39 return match ? ofpdrv->probe(pdev, match) : -EINVAL;
42 static void platform_driver_shutdown_shim(struct platform_device *pdev)
44 struct platform_driver *pdrv;
45 struct of_platform_driver *ofpdrv;
47 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
48 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
49 ofpdrv->shutdown(pdev);
52 /**
53 * of_register_platform_driver
55 int of_register_platform_driver(struct of_platform_driver *drv)
57 /* setup of_platform_driver to platform_driver adaptors */
58 drv->platform_driver.driver = drv->driver;
59 if (drv->probe)
60 drv->platform_driver.probe = platform_driver_probe_shim;
61 drv->platform_driver.remove = drv->remove;
62 if (drv->shutdown)
63 drv->platform_driver.shutdown = platform_driver_shutdown_shim;
64 drv->platform_driver.suspend = drv->suspend;
65 drv->platform_driver.resume = drv->resume;
67 return platform_driver_register(&drv->platform_driver);
69 EXPORT_SYMBOL(of_register_platform_driver);
71 void of_unregister_platform_driver(struct of_platform_driver *drv)
73 platform_driver_unregister(&drv->platform_driver);
75 EXPORT_SYMBOL(of_unregister_platform_driver);
77 #if defined(CONFIG_PPC_DCR)
78 #include <asm/dcr.h>
79 #endif
81 extern struct device_attribute of_platform_device_attrs[];
83 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
85 const struct of_device_id *matches = drv->of_match_table;
87 if (!matches)
88 return 0;
90 return of_match_device(matches, dev) != NULL;
93 static int of_platform_device_probe(struct device *dev)
95 int error = -ENODEV;
96 struct of_platform_driver *drv;
97 struct of_device *of_dev;
98 const struct of_device_id *match;
100 drv = to_of_platform_driver(dev->driver);
101 of_dev = to_of_device(dev);
103 if (!drv->probe)
104 return error;
106 of_dev_get(of_dev);
108 match = of_match_device(drv->driver.of_match_table, dev);
109 if (match)
110 error = drv->probe(of_dev, match);
111 if (error)
112 of_dev_put(of_dev);
114 return error;
117 static int of_platform_device_remove(struct device *dev)
119 struct of_device *of_dev = to_of_device(dev);
120 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
122 if (dev->driver && drv->remove)
123 drv->remove(of_dev);
124 return 0;
127 static void of_platform_device_shutdown(struct device *dev)
129 struct of_device *of_dev = to_of_device(dev);
130 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
132 if (dev->driver && drv->shutdown)
133 drv->shutdown(of_dev);
136 #ifdef CONFIG_PM_SLEEP
138 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
140 struct of_device *of_dev = to_of_device(dev);
141 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
142 int ret = 0;
144 if (dev->driver && drv->suspend)
145 ret = drv->suspend(of_dev, mesg);
146 return ret;
149 static int of_platform_legacy_resume(struct device *dev)
151 struct of_device *of_dev = to_of_device(dev);
152 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
153 int ret = 0;
155 if (dev->driver && drv->resume)
156 ret = drv->resume(of_dev);
157 return ret;
160 static int of_platform_pm_prepare(struct device *dev)
162 struct device_driver *drv = dev->driver;
163 int ret = 0;
165 if (drv && drv->pm && drv->pm->prepare)
166 ret = drv->pm->prepare(dev);
168 return ret;
171 static void of_platform_pm_complete(struct device *dev)
173 struct device_driver *drv = dev->driver;
175 if (drv && drv->pm && drv->pm->complete)
176 drv->pm->complete(dev);
179 #ifdef CONFIG_SUSPEND
181 static int of_platform_pm_suspend(struct device *dev)
183 struct device_driver *drv = dev->driver;
184 int ret = 0;
186 if (!drv)
187 return 0;
189 if (drv->pm) {
190 if (drv->pm->suspend)
191 ret = drv->pm->suspend(dev);
192 } else {
193 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
196 return ret;
199 static int of_platform_pm_suspend_noirq(struct device *dev)
201 struct device_driver *drv = dev->driver;
202 int ret = 0;
204 if (!drv)
205 return 0;
207 if (drv->pm) {
208 if (drv->pm->suspend_noirq)
209 ret = drv->pm->suspend_noirq(dev);
212 return ret;
215 static int of_platform_pm_resume(struct device *dev)
217 struct device_driver *drv = dev->driver;
218 int ret = 0;
220 if (!drv)
221 return 0;
223 if (drv->pm) {
224 if (drv->pm->resume)
225 ret = drv->pm->resume(dev);
226 } else {
227 ret = of_platform_legacy_resume(dev);
230 return ret;
233 static int of_platform_pm_resume_noirq(struct device *dev)
235 struct device_driver *drv = dev->driver;
236 int ret = 0;
238 if (!drv)
239 return 0;
241 if (drv->pm) {
242 if (drv->pm->resume_noirq)
243 ret = drv->pm->resume_noirq(dev);
246 return ret;
249 #else /* !CONFIG_SUSPEND */
251 #define of_platform_pm_suspend NULL
252 #define of_platform_pm_resume NULL
253 #define of_platform_pm_suspend_noirq NULL
254 #define of_platform_pm_resume_noirq NULL
256 #endif /* !CONFIG_SUSPEND */
258 #ifdef CONFIG_HIBERNATION
260 static int of_platform_pm_freeze(struct device *dev)
262 struct device_driver *drv = dev->driver;
263 int ret = 0;
265 if (!drv)
266 return 0;
268 if (drv->pm) {
269 if (drv->pm->freeze)
270 ret = drv->pm->freeze(dev);
271 } else {
272 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
275 return ret;
278 static int of_platform_pm_freeze_noirq(struct device *dev)
280 struct device_driver *drv = dev->driver;
281 int ret = 0;
283 if (!drv)
284 return 0;
286 if (drv->pm) {
287 if (drv->pm->freeze_noirq)
288 ret = drv->pm->freeze_noirq(dev);
291 return ret;
294 static int of_platform_pm_thaw(struct device *dev)
296 struct device_driver *drv = dev->driver;
297 int ret = 0;
299 if (!drv)
300 return 0;
302 if (drv->pm) {
303 if (drv->pm->thaw)
304 ret = drv->pm->thaw(dev);
305 } else {
306 ret = of_platform_legacy_resume(dev);
309 return ret;
312 static int of_platform_pm_thaw_noirq(struct device *dev)
314 struct device_driver *drv = dev->driver;
315 int ret = 0;
317 if (!drv)
318 return 0;
320 if (drv->pm) {
321 if (drv->pm->thaw_noirq)
322 ret = drv->pm->thaw_noirq(dev);
325 return ret;
328 static int of_platform_pm_poweroff(struct device *dev)
330 struct device_driver *drv = dev->driver;
331 int ret = 0;
333 if (!drv)
334 return 0;
336 if (drv->pm) {
337 if (drv->pm->poweroff)
338 ret = drv->pm->poweroff(dev);
339 } else {
340 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
343 return ret;
346 static int of_platform_pm_poweroff_noirq(struct device *dev)
348 struct device_driver *drv = dev->driver;
349 int ret = 0;
351 if (!drv)
352 return 0;
354 if (drv->pm) {
355 if (drv->pm->poweroff_noirq)
356 ret = drv->pm->poweroff_noirq(dev);
359 return ret;
362 static int of_platform_pm_restore(struct device *dev)
364 struct device_driver *drv = dev->driver;
365 int ret = 0;
367 if (!drv)
368 return 0;
370 if (drv->pm) {
371 if (drv->pm->restore)
372 ret = drv->pm->restore(dev);
373 } else {
374 ret = of_platform_legacy_resume(dev);
377 return ret;
380 static int of_platform_pm_restore_noirq(struct device *dev)
382 struct device_driver *drv = dev->driver;
383 int ret = 0;
385 if (!drv)
386 return 0;
388 if (drv->pm) {
389 if (drv->pm->restore_noirq)
390 ret = drv->pm->restore_noirq(dev);
393 return ret;
396 #else /* !CONFIG_HIBERNATION */
398 #define of_platform_pm_freeze NULL
399 #define of_platform_pm_thaw NULL
400 #define of_platform_pm_poweroff NULL
401 #define of_platform_pm_restore NULL
402 #define of_platform_pm_freeze_noirq NULL
403 #define of_platform_pm_thaw_noirq NULL
404 #define of_platform_pm_poweroff_noirq NULL
405 #define of_platform_pm_restore_noirq NULL
407 #endif /* !CONFIG_HIBERNATION */
409 static struct dev_pm_ops of_platform_dev_pm_ops = {
410 .prepare = of_platform_pm_prepare,
411 .complete = of_platform_pm_complete,
412 .suspend = of_platform_pm_suspend,
413 .resume = of_platform_pm_resume,
414 .freeze = of_platform_pm_freeze,
415 .thaw = of_platform_pm_thaw,
416 .poweroff = of_platform_pm_poweroff,
417 .restore = of_platform_pm_restore,
418 .suspend_noirq = of_platform_pm_suspend_noirq,
419 .resume_noirq = of_platform_pm_resume_noirq,
420 .freeze_noirq = of_platform_pm_freeze_noirq,
421 .thaw_noirq = of_platform_pm_thaw_noirq,
422 .poweroff_noirq = of_platform_pm_poweroff_noirq,
423 .restore_noirq = of_platform_pm_restore_noirq,
426 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
428 #else /* !CONFIG_PM_SLEEP */
430 #define OF_PLATFORM_PM_OPS_PTR NULL
432 #endif /* !CONFIG_PM_SLEEP */
434 int of_bus_type_init(struct bus_type *bus, const char *name)
436 bus->name = name;
437 bus->match = of_platform_bus_match;
438 bus->probe = of_platform_device_probe;
439 bus->remove = of_platform_device_remove;
440 bus->shutdown = of_platform_device_shutdown;
441 bus->dev_attrs = of_platform_device_attrs;
442 bus->pm = OF_PLATFORM_PM_OPS_PTR;
443 return bus_register(bus);
446 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
449 * Temporary: of_platform_bus used to be distinct from the platform
450 * bus. It isn't anymore, and so drivers on the platform bus need
451 * to be registered in a special way.
453 * After all of_platform_bus_type drivers are converted to
454 * platform_drivers, this exception can be removed.
456 if (bus == &platform_bus_type)
457 return of_register_platform_driver(drv);
459 /* register with core */
460 drv->driver.bus = bus;
461 return driver_register(&drv->driver);
463 EXPORT_SYMBOL(of_register_driver);
465 void of_unregister_driver(struct of_platform_driver *drv)
467 if (drv->driver.bus == &platform_bus_type)
468 of_unregister_platform_driver(drv);
469 else
470 driver_unregister(&drv->driver);
472 EXPORT_SYMBOL(of_unregister_driver);
474 #if !defined(CONFIG_SPARC)
476 * The following routines scan a subtree and registers a device for
477 * each applicable node.
479 * Note: sparc doesn't use these routines because it has a different
480 * mechanism for creating devices from device tree nodes.
484 * of_device_make_bus_id - Use the device node data to assign a unique name
485 * @dev: pointer to device structure that is linked to a device tree node
487 * This routine will first try using either the dcr-reg or the reg property
488 * value to derive a unique name. As a last resort it will use the node
489 * name followed by a unique number.
491 static void of_device_make_bus_id(struct device *dev)
493 static atomic_t bus_no_reg_magic;
494 struct device_node *node = dev->of_node;
495 const u32 *reg;
496 u64 addr;
497 int magic;
499 #ifdef CONFIG_PPC_DCR
501 * If it's a DCR based device, use 'd' for native DCRs
502 * and 'D' for MMIO DCRs.
504 reg = of_get_property(node, "dcr-reg", NULL);
505 if (reg) {
506 #ifdef CONFIG_PPC_DCR_NATIVE
507 dev_set_name(dev, "d%x.%s", *reg, node->name);
508 #else /* CONFIG_PPC_DCR_NATIVE */
509 u64 addr = of_translate_dcr_address(node, *reg, NULL);
510 if (addr != OF_BAD_ADDR) {
511 dev_set_name(dev, "D%llx.%s",
512 (unsigned long long)addr, node->name);
513 return;
515 #endif /* !CONFIG_PPC_DCR_NATIVE */
517 #endif /* CONFIG_PPC_DCR */
520 * For MMIO, get the physical address
522 reg = of_get_property(node, "reg", NULL);
523 if (reg) {
524 addr = of_translate_address(node, reg);
525 if (addr != OF_BAD_ADDR) {
526 dev_set_name(dev, "%llx.%s",
527 (unsigned long long)addr, node->name);
528 return;
533 * No BusID, use the node name and add a globally incremented
534 * counter (and pray...)
536 magic = atomic_add_return(1, &bus_no_reg_magic);
537 dev_set_name(dev, "%s.%d", node->name, magic - 1);
541 * of_device_alloc - Allocate and initialize an of_device
542 * @np: device node to assign to device
543 * @bus_id: Name to assign to the device. May be null to use default name.
544 * @parent: Parent device.
546 struct of_device *of_device_alloc(struct device_node *np,
547 const char *bus_id,
548 struct device *parent)
550 struct of_device *dev;
551 int rc, i, num_reg = 0, num_irq = 0;
552 struct resource *res, temp_res;
554 /* First count how many resources are needed */
555 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
556 num_reg++;
557 while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
558 num_irq++;
560 /* Allocate memory for both the struct device and the resource table */
561 dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
562 GFP_KERNEL);
563 if (!dev)
564 return NULL;
565 res = (struct resource *) &dev[1];
567 /* Populate the resource table */
568 if (num_irq || num_reg) {
569 dev->num_resources = num_reg + num_irq;
570 dev->resource = res;
571 for (i = 0; i < num_reg; i++, res++) {
572 rc = of_address_to_resource(np, i, res);
573 WARN_ON(rc);
575 for (i = 0; i < num_irq; i++, res++) {
576 rc = of_irq_to_resource(np, i, res);
577 WARN_ON(rc == NO_IRQ);
581 dev->dev.of_node = of_node_get(np);
582 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
583 dev->dev.dma_mask = &dev->archdata.dma_mask;
584 #endif
585 dev->dev.parent = parent;
586 dev->dev.release = of_release_dev;
588 if (bus_id)
589 dev_set_name(&dev->dev, "%s", bus_id);
590 else
591 of_device_make_bus_id(&dev->dev);
593 return dev;
595 EXPORT_SYMBOL(of_device_alloc);
598 * of_platform_device_create - Alloc, initialize and register an of_device
599 * @np: pointer to node to create device for
600 * @bus_id: name to assign device
601 * @parent: Linux device model parent device.
603 struct of_device *of_platform_device_create(struct device_node *np,
604 const char *bus_id,
605 struct device *parent)
607 struct of_device *dev;
609 dev = of_device_alloc(np, bus_id, parent);
610 if (!dev)
611 return NULL;
613 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
614 dev->archdata.dma_mask = 0xffffffffUL;
615 #endif
616 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
617 dev->dev.bus = &platform_bus_type;
619 /* We do not fill the DMA ops for platform devices by default.
620 * This is currently the responsibility of the platform code
621 * to do such, possibly using a device notifier
624 if (of_device_register(dev) != 0) {
625 of_device_free(dev);
626 return NULL;
629 return dev;
631 EXPORT_SYMBOL(of_platform_device_create);
634 * of_platform_bus_create - Create an OF device for a bus node and all its
635 * children. Optionally recursively instantiate matching busses.
636 * @bus: device node of the bus to instantiate
637 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
638 * disallow recursive creation of child busses
640 static int of_platform_bus_create(const struct device_node *bus,
641 const struct of_device_id *matches,
642 struct device *parent)
644 struct device_node *child;
645 struct of_device *dev;
646 int rc = 0;
648 for_each_child_of_node(bus, child) {
649 pr_debug(" create child: %s\n", child->full_name);
650 dev = of_platform_device_create(child, NULL, parent);
651 if (dev == NULL)
652 rc = -ENOMEM;
653 else if (!of_match_node(matches, child))
654 continue;
655 if (rc == 0) {
656 pr_debug(" and sub busses\n");
657 rc = of_platform_bus_create(child, matches, &dev->dev);
659 if (rc) {
660 of_node_put(child);
661 break;
664 return rc;
668 * of_platform_bus_probe - Probe the device-tree for platform busses
669 * @root: parent of the first level to probe or NULL for the root of the tree
670 * @matches: match table, NULL to use the default
671 * @parent: parent to hook devices from, NULL for toplevel
673 * Note that children of the provided root are not instantiated as devices
674 * unless the specified root itself matches the bus list and is not NULL.
676 int of_platform_bus_probe(struct device_node *root,
677 const struct of_device_id *matches,
678 struct device *parent)
680 struct device_node *child;
681 struct of_device *dev;
682 int rc = 0;
684 if (matches == NULL)
685 matches = of_default_bus_ids;
686 if (matches == OF_NO_DEEP_PROBE)
687 return -EINVAL;
688 if (root == NULL)
689 root = of_find_node_by_path("/");
690 else
691 of_node_get(root);
692 if (root == NULL)
693 return -EINVAL;
695 pr_debug("of_platform_bus_probe()\n");
696 pr_debug(" starting at: %s\n", root->full_name);
698 /* Do a self check of bus type, if there's a match, create
699 * children
701 if (of_match_node(matches, root)) {
702 pr_debug(" root match, create all sub devices\n");
703 dev = of_platform_device_create(root, NULL, parent);
704 if (dev == NULL) {
705 rc = -ENOMEM;
706 goto bail;
708 pr_debug(" create all sub busses\n");
709 rc = of_platform_bus_create(root, matches, &dev->dev);
710 goto bail;
712 for_each_child_of_node(root, child) {
713 if (!of_match_node(matches, child))
714 continue;
716 pr_debug(" match: %s\n", child->full_name);
717 dev = of_platform_device_create(child, NULL, parent);
718 if (dev == NULL)
719 rc = -ENOMEM;
720 else
721 rc = of_platform_bus_create(child, matches, &dev->dev);
722 if (rc) {
723 of_node_put(child);
724 break;
727 bail:
728 of_node_put(root);
729 return rc;
731 EXPORT_SYMBOL(of_platform_bus_probe);
732 #endif /* !CONFIG_SPARC */