netfilter: add struct net * to target parameters
[safe/jmp/linux-2.6] / drivers / base / platform.c
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/string.h>
14 #include <linux/platform_device.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/bootmem.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22
23 #include "base.h"
24
25 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
26                                  driver))
27
28 struct device platform_bus = {
29         .init_name      = "platform",
30 };
31 EXPORT_SYMBOL_GPL(platform_bus);
32
33 /**
34  * platform_get_resource - get a resource for a device
35  * @dev: platform device
36  * @type: resource type
37  * @num: resource index
38  */
39 struct resource *platform_get_resource(struct platform_device *dev,
40                                        unsigned int type, unsigned int num)
41 {
42         int i;
43
44         for (i = 0; i < dev->num_resources; i++) {
45                 struct resource *r = &dev->resource[i];
46
47                 if (type == resource_type(r) && 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,
75                                               const char *name)
76 {
77         int i;
78
79         for (i = 0; i < dev->num_resources; i++) {
80                 struct resource *r = &dev->resource[i];
81
82                 if (type == resource_type(r) && !strcmp(r->name, name))
83                         return r;
84         }
85         return NULL;
86 }
87 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
88
89 /**
90  * platform_get_irq - get an IRQ for a device
91  * @dev: platform device
92  * @name: IRQ name
93  */
94 int platform_get_irq_byname(struct platform_device *dev, const char *name)
95 {
96         struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
97                                                           name);
98
99         return r ? r->start : -ENXIO;
100 }
101 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
102
103 /**
104  * platform_add_devices - add a numbers of platform devices
105  * @devs: array of platform devices to add
106  * @num: number of platform devices in array
107  */
108 int platform_add_devices(struct platform_device **devs, int num)
109 {
110         int i, ret = 0;
111
112         for (i = 0; i < num; i++) {
113                 ret = platform_device_register(devs[i]);
114                 if (ret) {
115                         while (--i >= 0)
116                                 platform_device_unregister(devs[i]);
117                         break;
118                 }
119         }
120
121         return ret;
122 }
123 EXPORT_SYMBOL_GPL(platform_add_devices);
124
125 struct platform_object {
126         struct platform_device pdev;
127         char name[1];
128 };
129
130 /**
131  * platform_device_put
132  * @pdev: platform device to free
133  *
134  * Free all memory associated with a platform device.  This function must
135  * _only_ be externally called in error cases.  All other usage is a bug.
136  */
137 void platform_device_put(struct platform_device *pdev)
138 {
139         if (pdev)
140                 put_device(&pdev->dev);
141 }
142 EXPORT_SYMBOL_GPL(platform_device_put);
143
144 static void platform_device_release(struct device *dev)
145 {
146         struct platform_object *pa = container_of(dev, struct platform_object,
147                                                   pdev.dev);
148
149         kfree(pa->pdev.dev.platform_data);
150         kfree(pa->pdev.resource);
151         kfree(pa);
152 }
153
154 /**
155  * platform_device_alloc
156  * @name: base name of the device we're adding
157  * @id: instance id
158  *
159  * Create a platform device object which can have other objects attached
160  * to it, and which will have attached objects freed when it is released.
161  */
162 struct platform_device *platform_device_alloc(const char *name, int id)
163 {
164         struct platform_object *pa;
165
166         pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
167         if (pa) {
168                 strcpy(pa->name, name);
169                 pa->pdev.name = pa->name;
170                 pa->pdev.id = id;
171                 device_initialize(&pa->pdev.dev);
172                 pa->pdev.dev.release = platform_device_release;
173         }
174
175         return pa ? &pa->pdev : NULL;
176 }
177 EXPORT_SYMBOL_GPL(platform_device_alloc);
178
179 /**
180  * platform_device_add_resources
181  * @pdev: platform device allocated by platform_device_alloc to add resources to
182  * @res: set of resources that needs to be allocated for the device
183  * @num: number of resources
184  *
185  * Add a copy of the resources to the platform device.  The memory
186  * associated with the resources will be freed when the platform device is
187  * released.
188  */
189 int platform_device_add_resources(struct platform_device *pdev,
190                                   struct resource *res, unsigned int num)
191 {
192         struct resource *r;
193
194         r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
195         if (r) {
196                 memcpy(r, res, sizeof(struct resource) * num);
197                 pdev->resource = r;
198                 pdev->num_resources = num;
199         }
200         return r ? 0 : -ENOMEM;
201 }
202 EXPORT_SYMBOL_GPL(platform_device_add_resources);
203
204 /**
205  * platform_device_add_data
206  * @pdev: platform device allocated by platform_device_alloc to add resources to
207  * @data: platform specific data for this platform device
208  * @size: size of platform specific data
209  *
210  * Add a copy of platform specific data to the platform device's
211  * platform_data pointer.  The memory associated with the platform data
212  * will be freed when the platform device is released.
213  */
214 int platform_device_add_data(struct platform_device *pdev, const void *data,
215                              size_t size)
216 {
217         void *d = kmemdup(data, size, GFP_KERNEL);
218
219         if (d) {
220                 pdev->dev.platform_data = d;
221                 return 0;
222         }
223         return -ENOMEM;
224 }
225 EXPORT_SYMBOL_GPL(platform_device_add_data);
226
227 /**
228  * platform_device_add - add a platform device to device hierarchy
229  * @pdev: platform device we're adding
230  *
231  * This is part 2 of platform_device_register(), though may be called
232  * separately _iff_ pdev was allocated by platform_device_alloc().
233  */
234 int platform_device_add(struct platform_device *pdev)
235 {
236         int i, ret = 0;
237
238         if (!pdev)
239                 return -EINVAL;
240
241         if (!pdev->dev.parent)
242                 pdev->dev.parent = &platform_bus;
243
244         pdev->dev.bus = &platform_bus_type;
245
246         if (pdev->id != -1)
247                 dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);
248         else
249                 dev_set_name(&pdev->dev, "%s", pdev->name);
250
251         for (i = 0; i < pdev->num_resources; i++) {
252                 struct resource *p, *r = &pdev->resource[i];
253
254                 if (r->name == NULL)
255                         r->name = dev_name(&pdev->dev);
256
257                 p = r->parent;
258                 if (!p) {
259                         if (resource_type(r) == IORESOURCE_MEM)
260                                 p = &iomem_resource;
261                         else if (resource_type(r) == IORESOURCE_IO)
262                                 p = &ioport_resource;
263                 }
264
265                 if (p && insert_resource(p, r)) {
266                         printk(KERN_ERR
267                                "%s: failed to claim resource %d\n",
268                                dev_name(&pdev->dev), i);
269                         ret = -EBUSY;
270                         goto failed;
271                 }
272         }
273
274         pr_debug("Registering platform device '%s'. Parent at %s\n",
275                  dev_name(&pdev->dev), dev_name(pdev->dev.parent));
276
277         ret = device_add(&pdev->dev);
278         if (ret == 0)
279                 return ret;
280
281  failed:
282         while (--i >= 0) {
283                 struct resource *r = &pdev->resource[i];
284                 unsigned long type = resource_type(r);
285
286                 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
287                         release_resource(r);
288         }
289
290         return ret;
291 }
292 EXPORT_SYMBOL_GPL(platform_device_add);
293
294 /**
295  * platform_device_del - remove a platform-level device
296  * @pdev: platform device we're removing
297  *
298  * Note that this function will also release all memory- and port-based
299  * resources owned by the device (@dev->resource).  This function must
300  * _only_ be externally called in error cases.  All other usage is a bug.
301  */
302 void platform_device_del(struct platform_device *pdev)
303 {
304         int i;
305
306         if (pdev) {
307                 device_del(&pdev->dev);
308
309                 for (i = 0; i < pdev->num_resources; i++) {
310                         struct resource *r = &pdev->resource[i];
311                         unsigned long type = resource_type(r);
312
313                         if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
314                                 release_resource(r);
315                 }
316         }
317 }
318 EXPORT_SYMBOL_GPL(platform_device_del);
319
320 /**
321  * platform_device_register - add a platform-level device
322  * @pdev: platform device we're adding
323  */
324 int platform_device_register(struct platform_device *pdev)
325 {
326         device_initialize(&pdev->dev);
327         return platform_device_add(pdev);
328 }
329 EXPORT_SYMBOL_GPL(platform_device_register);
330
331 /**
332  * platform_device_unregister - unregister a platform-level device
333  * @pdev: platform device we're unregistering
334  *
335  * Unregistration is done in 2 steps. First we release all resources
336  * and remove it from the subsystem, then we drop reference count by
337  * calling platform_device_put().
338  */
339 void platform_device_unregister(struct platform_device *pdev)
340 {
341         platform_device_del(pdev);
342         platform_device_put(pdev);
343 }
344 EXPORT_SYMBOL_GPL(platform_device_unregister);
345
346 /**
347  * platform_device_register_simple
348  * @name: base name of the device we're adding
349  * @id: instance id
350  * @res: set of resources that needs to be allocated for the device
351  * @num: number of resources
352  *
353  * This function creates a simple platform device that requires minimal
354  * resource and memory management. Canned release function freeing memory
355  * allocated for the device allows drivers using such devices to be
356  * unloaded without waiting for the last reference to the device to be
357  * dropped.
358  *
359  * This interface is primarily intended for use with legacy drivers which
360  * probe hardware directly.  Because such drivers create sysfs device nodes
361  * themselves, rather than letting system infrastructure handle such device
362  * enumeration tasks, they don't fully conform to the Linux driver model.
363  * In particular, when such drivers are built as modules, they can't be
364  * "hotplugged".
365  */
366 struct platform_device *platform_device_register_simple(const char *name,
367                                                         int id,
368                                                         struct resource *res,
369                                                         unsigned int num)
370 {
371         struct platform_device *pdev;
372         int retval;
373
374         pdev = platform_device_alloc(name, id);
375         if (!pdev) {
376                 retval = -ENOMEM;
377                 goto error;
378         }
379
380         if (num) {
381                 retval = platform_device_add_resources(pdev, res, num);
382                 if (retval)
383                         goto error;
384         }
385
386         retval = platform_device_add(pdev);
387         if (retval)
388                 goto error;
389
390         return pdev;
391
392 error:
393         platform_device_put(pdev);
394         return ERR_PTR(retval);
395 }
396 EXPORT_SYMBOL_GPL(platform_device_register_simple);
397
398 /**
399  * platform_device_register_data
400  * @parent: parent device for the device we're adding
401  * @name: base name of the device we're adding
402  * @id: instance id
403  * @data: platform specific data for this platform device
404  * @size: size of platform specific data
405  *
406  * This function creates a simple platform device that requires minimal
407  * resource and memory management. Canned release function freeing memory
408  * allocated for the device allows drivers using such devices to be
409  * unloaded without waiting for the last reference to the device to be
410  * dropped.
411  */
412 struct platform_device *platform_device_register_data(
413                 struct device *parent,
414                 const char *name, int id,
415                 const void *data, size_t size)
416 {
417         struct platform_device *pdev;
418         int retval;
419
420         pdev = platform_device_alloc(name, id);
421         if (!pdev) {
422                 retval = -ENOMEM;
423                 goto error;
424         }
425
426         pdev->dev.parent = parent;
427
428         if (size) {
429                 retval = platform_device_add_data(pdev, data, size);
430                 if (retval)
431                         goto error;
432         }
433
434         retval = platform_device_add(pdev);
435         if (retval)
436                 goto error;
437
438         return pdev;
439
440 error:
441         platform_device_put(pdev);
442         return ERR_PTR(retval);
443 }
444 EXPORT_SYMBOL_GPL(platform_device_register_data);
445
446 static int platform_drv_probe(struct device *_dev)
447 {
448         struct platform_driver *drv = to_platform_driver(_dev->driver);
449         struct platform_device *dev = to_platform_device(_dev);
450
451         return drv->probe(dev);
452 }
453
454 static int platform_drv_probe_fail(struct device *_dev)
455 {
456         return -ENXIO;
457 }
458
459 static int platform_drv_remove(struct device *_dev)
460 {
461         struct platform_driver *drv = to_platform_driver(_dev->driver);
462         struct platform_device *dev = to_platform_device(_dev);
463
464         return drv->remove(dev);
465 }
466
467 static void platform_drv_shutdown(struct device *_dev)
468 {
469         struct platform_driver *drv = to_platform_driver(_dev->driver);
470         struct platform_device *dev = to_platform_device(_dev);
471
472         drv->shutdown(dev);
473 }
474
475 /**
476  * platform_driver_register
477  * @drv: platform driver structure
478  */
479 int platform_driver_register(struct platform_driver *drv)
480 {
481         drv->driver.bus = &platform_bus_type;
482         if (drv->probe)
483                 drv->driver.probe = platform_drv_probe;
484         if (drv->remove)
485                 drv->driver.remove = platform_drv_remove;
486         if (drv->shutdown)
487                 drv->driver.shutdown = platform_drv_shutdown;
488
489         return driver_register(&drv->driver);
490 }
491 EXPORT_SYMBOL_GPL(platform_driver_register);
492
493 /**
494  * platform_driver_unregister
495  * @drv: platform driver structure
496  */
497 void platform_driver_unregister(struct platform_driver *drv)
498 {
499         driver_unregister(&drv->driver);
500 }
501 EXPORT_SYMBOL_GPL(platform_driver_unregister);
502
503 /**
504  * platform_driver_probe - register driver for non-hotpluggable device
505  * @drv: platform driver structure
506  * @probe: the driver probe routine, probably from an __init section
507  *
508  * Use this instead of platform_driver_register() when you know the device
509  * is not hotpluggable and has already been registered, and you want to
510  * remove its run-once probe() infrastructure from memory after the driver
511  * has bound to the device.
512  *
513  * One typical use for this would be with drivers for controllers integrated
514  * into system-on-chip processors, where the controller devices have been
515  * configured as part of board setup.
516  *
517  * Returns zero if the driver registered and bound to a device, else returns
518  * a negative error code and with the driver not registered.
519  */
520 int __init_or_module platform_driver_probe(struct platform_driver *drv,
521                 int (*probe)(struct platform_device *))
522 {
523         int retval, code;
524
525         /* make sure driver won't have bind/unbind attributes */
526         drv->driver.suppress_bind_attrs = true;
527
528         /* temporary section violation during probe() */
529         drv->probe = probe;
530         retval = code = platform_driver_register(drv);
531
532         /*
533          * Fixup that section violation, being paranoid about code scanning
534          * the list of drivers in order to probe new devices.  Check to see
535          * if the probe was successful, and make sure any forced probes of
536          * new devices fail.
537          */
538         spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
539         drv->probe = NULL;
540         if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
541                 retval = -ENODEV;
542         drv->driver.probe = platform_drv_probe_fail;
543         spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
544
545         if (code != retval)
546                 platform_driver_unregister(drv);
547         return retval;
548 }
549 EXPORT_SYMBOL_GPL(platform_driver_probe);
550
551 /* modalias support enables more hands-off userspace setup:
552  * (a) environment variable lets new-style hotplug events work once system is
553  *     fully running:  "modprobe $MODALIAS"
554  * (b) sysfs attribute lets new-style coldplug recover from hotplug events
555  *     mishandled before system is fully running:  "modprobe $(cat modalias)"
556  */
557 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
558                              char *buf)
559 {
560         struct platform_device  *pdev = to_platform_device(dev);
561         int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
562
563         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
564 }
565
566 static struct device_attribute platform_dev_attrs[] = {
567         __ATTR_RO(modalias),
568         __ATTR_NULL,
569 };
570
571 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
572 {
573         struct platform_device  *pdev = to_platform_device(dev);
574
575         add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
576                 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
577         return 0;
578 }
579
580 static const struct platform_device_id *platform_match_id(
581                         struct platform_device_id *id,
582                         struct platform_device *pdev)
583 {
584         while (id->name[0]) {
585                 if (strcmp(pdev->name, id->name) == 0) {
586                         pdev->id_entry = id;
587                         return id;
588                 }
589                 id++;
590         }
591         return NULL;
592 }
593
594 /**
595  * platform_match - bind platform device to platform driver.
596  * @dev: device.
597  * @drv: driver.
598  *
599  * Platform device IDs are assumed to be encoded like this:
600  * "<name><instance>", where <name> is a short description of the type of
601  * device, like "pci" or "floppy", and <instance> is the enumerated
602  * instance of the device, like '0' or '42'.  Driver IDs are simply
603  * "<name>".  So, extract the <name> from the platform_device structure,
604  * and compare it against the name of the driver. Return whether they match
605  * or not.
606  */
607 static int platform_match(struct device *dev, struct device_driver *drv)
608 {
609         struct platform_device *pdev = to_platform_device(dev);
610         struct platform_driver *pdrv = to_platform_driver(drv);
611
612         /* match against the id table first */
613         if (pdrv->id_table)
614                 return platform_match_id(pdrv->id_table, pdev) != NULL;
615
616         /* fall-back to driver name match */
617         return (strcmp(pdev->name, drv->name) == 0);
618 }
619
620 #ifdef CONFIG_PM_SLEEP
621
622 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
623 {
624         struct platform_driver *pdrv = to_platform_driver(dev->driver);
625         struct platform_device *pdev = to_platform_device(dev);
626         int ret = 0;
627
628         if (dev->driver && pdrv->suspend)
629                 ret = pdrv->suspend(pdev, mesg);
630
631         return ret;
632 }
633
634 static int platform_legacy_resume(struct device *dev)
635 {
636         struct platform_driver *pdrv = to_platform_driver(dev->driver);
637         struct platform_device *pdev = to_platform_device(dev);
638         int ret = 0;
639
640         if (dev->driver && pdrv->resume)
641                 ret = pdrv->resume(pdev);
642
643         return ret;
644 }
645
646 static int platform_pm_prepare(struct device *dev)
647 {
648         struct device_driver *drv = dev->driver;
649         int ret = 0;
650
651         if (drv && drv->pm && drv->pm->prepare)
652                 ret = drv->pm->prepare(dev);
653
654         return ret;
655 }
656
657 static void platform_pm_complete(struct device *dev)
658 {
659         struct device_driver *drv = dev->driver;
660
661         if (drv && drv->pm && drv->pm->complete)
662                 drv->pm->complete(dev);
663 }
664
665 #else /* !CONFIG_PM_SLEEP */
666
667 #define platform_pm_prepare             NULL
668 #define platform_pm_complete            NULL
669
670 #endif /* !CONFIG_PM_SLEEP */
671
672 #ifdef CONFIG_SUSPEND
673
674 static int platform_pm_suspend(struct device *dev)
675 {
676         struct device_driver *drv = dev->driver;
677         int ret = 0;
678
679         if (!drv)
680                 return 0;
681
682         if (drv->pm) {
683                 if (drv->pm->suspend)
684                         ret = drv->pm->suspend(dev);
685         } else {
686                 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
687         }
688
689         return ret;
690 }
691
692 static int platform_pm_suspend_noirq(struct device *dev)
693 {
694         struct device_driver *drv = dev->driver;
695         int ret = 0;
696
697         if (!drv)
698                 return 0;
699
700         if (drv->pm) {
701                 if (drv->pm->suspend_noirq)
702                         ret = drv->pm->suspend_noirq(dev);
703         }
704
705         return ret;
706 }
707
708 static int platform_pm_resume(struct device *dev)
709 {
710         struct device_driver *drv = dev->driver;
711         int ret = 0;
712
713         if (!drv)
714                 return 0;
715
716         if (drv->pm) {
717                 if (drv->pm->resume)
718                         ret = drv->pm->resume(dev);
719         } else {
720                 ret = platform_legacy_resume(dev);
721         }
722
723         return ret;
724 }
725
726 static int platform_pm_resume_noirq(struct device *dev)
727 {
728         struct device_driver *drv = dev->driver;
729         int ret = 0;
730
731         if (!drv)
732                 return 0;
733
734         if (drv->pm) {
735                 if (drv->pm->resume_noirq)
736                         ret = drv->pm->resume_noirq(dev);
737         }
738
739         return ret;
740 }
741
742 #else /* !CONFIG_SUSPEND */
743
744 #define platform_pm_suspend             NULL
745 #define platform_pm_resume              NULL
746 #define platform_pm_suspend_noirq       NULL
747 #define platform_pm_resume_noirq        NULL
748
749 #endif /* !CONFIG_SUSPEND */
750
751 #ifdef CONFIG_HIBERNATION
752
753 static int platform_pm_freeze(struct device *dev)
754 {
755         struct device_driver *drv = dev->driver;
756         int ret = 0;
757
758         if (!drv)
759                 return 0;
760
761         if (drv->pm) {
762                 if (drv->pm->freeze)
763                         ret = drv->pm->freeze(dev);
764         } else {
765                 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
766         }
767
768         return ret;
769 }
770
771 static int platform_pm_freeze_noirq(struct device *dev)
772 {
773         struct device_driver *drv = dev->driver;
774         int ret = 0;
775
776         if (!drv)
777                 return 0;
778
779         if (drv->pm) {
780                 if (drv->pm->freeze_noirq)
781                         ret = drv->pm->freeze_noirq(dev);
782         }
783
784         return ret;
785 }
786
787 static int platform_pm_thaw(struct device *dev)
788 {
789         struct device_driver *drv = dev->driver;
790         int ret = 0;
791
792         if (!drv)
793                 return 0;
794
795         if (drv->pm) {
796                 if (drv->pm->thaw)
797                         ret = drv->pm->thaw(dev);
798         } else {
799                 ret = platform_legacy_resume(dev);
800         }
801
802         return ret;
803 }
804
805 static int platform_pm_thaw_noirq(struct device *dev)
806 {
807         struct device_driver *drv = dev->driver;
808         int ret = 0;
809
810         if (!drv)
811                 return 0;
812
813         if (drv->pm) {
814                 if (drv->pm->thaw_noirq)
815                         ret = drv->pm->thaw_noirq(dev);
816         }
817
818         return ret;
819 }
820
821 static int platform_pm_poweroff(struct device *dev)
822 {
823         struct device_driver *drv = dev->driver;
824         int ret = 0;
825
826         if (!drv)
827                 return 0;
828
829         if (drv->pm) {
830                 if (drv->pm->poweroff)
831                         ret = drv->pm->poweroff(dev);
832         } else {
833                 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
834         }
835
836         return ret;
837 }
838
839 static int platform_pm_poweroff_noirq(struct device *dev)
840 {
841         struct device_driver *drv = dev->driver;
842         int ret = 0;
843
844         if (!drv)
845                 return 0;
846
847         if (drv->pm) {
848                 if (drv->pm->poweroff_noirq)
849                         ret = drv->pm->poweroff_noirq(dev);
850         }
851
852         return ret;
853 }
854
855 static int platform_pm_restore(struct device *dev)
856 {
857         struct device_driver *drv = dev->driver;
858         int ret = 0;
859
860         if (!drv)
861                 return 0;
862
863         if (drv->pm) {
864                 if (drv->pm->restore)
865                         ret = drv->pm->restore(dev);
866         } else {
867                 ret = platform_legacy_resume(dev);
868         }
869
870         return ret;
871 }
872
873 static int platform_pm_restore_noirq(struct device *dev)
874 {
875         struct device_driver *drv = dev->driver;
876         int ret = 0;
877
878         if (!drv)
879                 return 0;
880
881         if (drv->pm) {
882                 if (drv->pm->restore_noirq)
883                         ret = drv->pm->restore_noirq(dev);
884         }
885
886         return ret;
887 }
888
889 #else /* !CONFIG_HIBERNATION */
890
891 #define platform_pm_freeze              NULL
892 #define platform_pm_thaw                NULL
893 #define platform_pm_poweroff            NULL
894 #define platform_pm_restore             NULL
895 #define platform_pm_freeze_noirq        NULL
896 #define platform_pm_thaw_noirq          NULL
897 #define platform_pm_poweroff_noirq      NULL
898 #define platform_pm_restore_noirq       NULL
899
900 #endif /* !CONFIG_HIBERNATION */
901
902 #ifdef CONFIG_PM_RUNTIME
903
904 int __weak platform_pm_runtime_suspend(struct device *dev)
905 {
906         return -ENOSYS;
907 };
908
909 int __weak platform_pm_runtime_resume(struct device *dev)
910 {
911         return -ENOSYS;
912 };
913
914 int __weak platform_pm_runtime_idle(struct device *dev)
915 {
916         return -ENOSYS;
917 };
918
919 #else /* !CONFIG_PM_RUNTIME */
920
921 #define platform_pm_runtime_suspend NULL
922 #define platform_pm_runtime_resume NULL
923 #define platform_pm_runtime_idle NULL
924
925 #endif /* !CONFIG_PM_RUNTIME */
926
927 static const struct dev_pm_ops platform_dev_pm_ops = {
928         .prepare = platform_pm_prepare,
929         .complete = platform_pm_complete,
930         .suspend = platform_pm_suspend,
931         .resume = platform_pm_resume,
932         .freeze = platform_pm_freeze,
933         .thaw = platform_pm_thaw,
934         .poweroff = platform_pm_poweroff,
935         .restore = platform_pm_restore,
936         .suspend_noirq = platform_pm_suspend_noirq,
937         .resume_noirq = platform_pm_resume_noirq,
938         .freeze_noirq = platform_pm_freeze_noirq,
939         .thaw_noirq = platform_pm_thaw_noirq,
940         .poweroff_noirq = platform_pm_poweroff_noirq,
941         .restore_noirq = platform_pm_restore_noirq,
942         .runtime_suspend = platform_pm_runtime_suspend,
943         .runtime_resume = platform_pm_runtime_resume,
944         .runtime_idle = platform_pm_runtime_idle,
945 };
946
947 struct bus_type platform_bus_type = {
948         .name           = "platform",
949         .dev_attrs      = platform_dev_attrs,
950         .match          = platform_match,
951         .uevent         = platform_uevent,
952         .pm             = &platform_dev_pm_ops,
953 };
954 EXPORT_SYMBOL_GPL(platform_bus_type);
955
956 int __init platform_bus_init(void)
957 {
958         int error;
959
960         early_platform_cleanup();
961
962         error = device_register(&platform_bus);
963         if (error)
964                 return error;
965         error =  bus_register(&platform_bus_type);
966         if (error)
967                 device_unregister(&platform_bus);
968         return error;
969 }
970
971 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
972 u64 dma_get_required_mask(struct device *dev)
973 {
974         u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
975         u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
976         u64 mask;
977
978         if (!high_totalram) {
979                 /* convert to mask just covering totalram */
980                 low_totalram = (1 << (fls(low_totalram) - 1));
981                 low_totalram += low_totalram - 1;
982                 mask = low_totalram;
983         } else {
984                 high_totalram = (1 << (fls(high_totalram) - 1));
985                 high_totalram += high_totalram - 1;
986                 mask = (((u64)high_totalram) << 32) + 0xffffffff;
987         }
988         return mask;
989 }
990 EXPORT_SYMBOL_GPL(dma_get_required_mask);
991 #endif
992
993 static __initdata LIST_HEAD(early_platform_driver_list);
994 static __initdata LIST_HEAD(early_platform_device_list);
995
996 /**
997  * early_platform_driver_register
998  * @epdrv: early_platform driver structure
999  * @buf: string passed from early_param()
1000  */
1001 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1002                                           char *buf)
1003 {
1004         char *tmp;
1005         int n;
1006
1007         /* Simply add the driver to the end of the global list.
1008          * Drivers will by default be put on the list in compiled-in order.
1009          */
1010         if (!epdrv->list.next) {
1011                 INIT_LIST_HEAD(&epdrv->list);
1012                 list_add_tail(&epdrv->list, &early_platform_driver_list);
1013         }
1014
1015         /* If the user has specified device then make sure the driver
1016          * gets prioritized. The driver of the last device specified on
1017          * command line will be put first on the list.
1018          */
1019         n = strlen(epdrv->pdrv->driver.name);
1020         if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1021                 list_move(&epdrv->list, &early_platform_driver_list);
1022
1023                 /* Allow passing parameters after device name */
1024                 if (buf[n] == '\0' || buf[n] == ',')
1025                         epdrv->requested_id = -1;
1026                 else {
1027                         epdrv->requested_id = simple_strtoul(&buf[n + 1],
1028                                                              &tmp, 10);
1029
1030                         if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1031                                 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1032                                 n = 0;
1033                         } else
1034                                 n += strcspn(&buf[n + 1], ",") + 1;
1035                 }
1036
1037                 if (buf[n] == ',')
1038                         n++;
1039
1040                 if (epdrv->bufsize) {
1041                         memcpy(epdrv->buffer, &buf[n],
1042                                min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1043                         epdrv->buffer[epdrv->bufsize - 1] = '\0';
1044                 }
1045         }
1046
1047         return 0;
1048 }
1049
1050 /**
1051  * early_platform_add_devices - add a numbers of early platform devices
1052  * @devs: array of early platform devices to add
1053  * @num: number of early platform devices in array
1054  */
1055 void __init early_platform_add_devices(struct platform_device **devs, int num)
1056 {
1057         struct device *dev;
1058         int i;
1059
1060         /* simply add the devices to list */
1061         for (i = 0; i < num; i++) {
1062                 dev = &devs[i]->dev;
1063
1064                 if (!dev->devres_head.next) {
1065                         INIT_LIST_HEAD(&dev->devres_head);
1066                         list_add_tail(&dev->devres_head,
1067                                       &early_platform_device_list);
1068                 }
1069         }
1070 }
1071
1072 /**
1073  * early_platform_driver_register_all
1074  * @class_str: string to identify early platform driver class
1075  */
1076 void __init early_platform_driver_register_all(char *class_str)
1077 {
1078         /* The "class_str" parameter may or may not be present on the kernel
1079          * command line. If it is present then there may be more than one
1080          * matching parameter.
1081          *
1082          * Since we register our early platform drivers using early_param()
1083          * we need to make sure that they also get registered in the case
1084          * when the parameter is missing from the kernel command line.
1085          *
1086          * We use parse_early_options() to make sure the early_param() gets
1087          * called at least once. The early_param() may be called more than
1088          * once since the name of the preferred device may be specified on
1089          * the kernel command line. early_platform_driver_register() handles
1090          * this case for us.
1091          */
1092         parse_early_options(class_str);
1093 }
1094
1095 /**
1096  * early_platform_match
1097  * @epdrv: early platform driver structure
1098  * @id: id to match against
1099  */
1100 static  __init struct platform_device *
1101 early_platform_match(struct early_platform_driver *epdrv, int id)
1102 {
1103         struct platform_device *pd;
1104
1105         list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1106                 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1107                         if (pd->id == id)
1108                                 return pd;
1109
1110         return NULL;
1111 }
1112
1113 /**
1114  * early_platform_left
1115  * @epdrv: early platform driver structure
1116  * @id: return true if id or above exists
1117  */
1118 static  __init int early_platform_left(struct early_platform_driver *epdrv,
1119                                        int id)
1120 {
1121         struct platform_device *pd;
1122
1123         list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1124                 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1125                         if (pd->id >= id)
1126                                 return 1;
1127
1128         return 0;
1129 }
1130
1131 /**
1132  * early_platform_driver_probe_id
1133  * @class_str: string to identify early platform driver class
1134  * @id: id to match against
1135  * @nr_probe: number of platform devices to successfully probe before exiting
1136  */
1137 static int __init early_platform_driver_probe_id(char *class_str,
1138                                                  int id,
1139                                                  int nr_probe)
1140 {
1141         struct early_platform_driver *epdrv;
1142         struct platform_device *match;
1143         int match_id;
1144         int n = 0;
1145         int left = 0;
1146
1147         list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1148                 /* only use drivers matching our class_str */
1149                 if (strcmp(class_str, epdrv->class_str))
1150                         continue;
1151
1152                 if (id == -2) {
1153                         match_id = epdrv->requested_id;
1154                         left = 1;
1155
1156                 } else {
1157                         match_id = id;
1158                         left += early_platform_left(epdrv, id);
1159
1160                         /* skip requested id */
1161                         switch (epdrv->requested_id) {
1162                         case EARLY_PLATFORM_ID_ERROR:
1163                         case EARLY_PLATFORM_ID_UNSET:
1164                                 break;
1165                         default:
1166                                 if (epdrv->requested_id == id)
1167                                         match_id = EARLY_PLATFORM_ID_UNSET;
1168                         }
1169                 }
1170
1171                 switch (match_id) {
1172                 case EARLY_PLATFORM_ID_ERROR:
1173                         pr_warning("%s: unable to parse %s parameter\n",
1174                                    class_str, epdrv->pdrv->driver.name);
1175                         /* fall-through */
1176                 case EARLY_PLATFORM_ID_UNSET:
1177                         match = NULL;
1178                         break;
1179                 default:
1180                         match = early_platform_match(epdrv, match_id);
1181                 }
1182
1183                 if (match) {
1184                         if (epdrv->pdrv->probe(match))
1185                                 pr_warning("%s: unable to probe %s early.\n",
1186                                            class_str, match->name);
1187                         else
1188                                 n++;
1189                 }
1190
1191                 if (n >= nr_probe)
1192                         break;
1193         }
1194
1195         if (left)
1196                 return n;
1197         else
1198                 return -ENODEV;
1199 }
1200
1201 /**
1202  * early_platform_driver_probe
1203  * @class_str: string to identify early platform driver class
1204  * @nr_probe: number of platform devices to successfully probe before exiting
1205  * @user_only: only probe user specified early platform devices
1206  */
1207 int __init early_platform_driver_probe(char *class_str,
1208                                        int nr_probe,
1209                                        int user_only)
1210 {
1211         int k, n, i;
1212
1213         n = 0;
1214         for (i = -2; n < nr_probe; i++) {
1215                 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1216
1217                 if (k < 0)
1218                         break;
1219
1220                 n += k;
1221
1222                 if (user_only)
1223                         break;
1224         }
1225
1226         return n;
1227 }
1228
1229 /**
1230  * early_platform_cleanup - clean up early platform code
1231  */
1232 void __init early_platform_cleanup(void)
1233 {
1234         struct platform_device *pd, *pd2;
1235
1236         /* clean up the devres list used to chain devices */
1237         list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1238                                  dev.devres_head) {
1239                 list_del(&pd->dev.devres_head);
1240                 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1241         }
1242 }
1243