]> Git Repo - linux.git/blob - drivers/base/platform.c
Merge 5.3-rc4 into driver-core-next
[linux.git] / drivers / base / platform.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * platform.c - platform 'pseudo' bus for legacy devices
4  *
5  * Copyright (c) 2002-3 Patrick Mochel
6  * Copyright (c) 2002-3 Open Source Development Labs
7  *
8  * Please see Documentation/driver-api/driver-model/platform.rst for more
9  * information.
10  */
11
12 #include <linux/string.h>
13 #include <linux/platform_device.h>
14 #include <linux/of_device.h>
15 #include <linux/of_irq.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/memblock.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/pm_domain.h>
24 #include <linux/idr.h>
25 #include <linux/acpi.h>
26 #include <linux/clk/clk-conf.h>
27 #include <linux/limits.h>
28 #include <linux/property.h>
29 #include <linux/kmemleak.h>
30
31 #include "base.h"
32 #include "power/power.h"
33
34 /* For automatically allocated device IDs */
35 static DEFINE_IDA(platform_devid_ida);
36
37 struct device platform_bus = {
38         .init_name      = "platform",
39 };
40 EXPORT_SYMBOL_GPL(platform_bus);
41
42 /**
43  * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
44  * @pdev: platform device
45  *
46  * This is called before platform_device_add() such that any pdev_archdata may
47  * be setup before the platform_notifier is called.  So if a user needs to
48  * manipulate any relevant information in the pdev_archdata they can do:
49  *
50  *      platform_device_alloc()
51  *      ... manipulate ...
52  *      platform_device_add()
53  *
54  * And if they don't care they can just call platform_device_register() and
55  * everything will just work out.
56  */
57 void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
58 {
59 }
60
61 /**
62  * platform_get_resource - get a resource for a device
63  * @dev: platform device
64  * @type: resource type
65  * @num: resource index
66  */
67 struct resource *platform_get_resource(struct platform_device *dev,
68                                        unsigned int type, unsigned int num)
69 {
70         int i;
71
72         for (i = 0; i < dev->num_resources; i++) {
73                 struct resource *r = &dev->resource[i];
74
75                 if (type == resource_type(r) && num-- == 0)
76                         return r;
77         }
78         return NULL;
79 }
80 EXPORT_SYMBOL_GPL(platform_get_resource);
81
82 /**
83  * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
84  *                                  device
85  *
86  * @pdev: platform device to use both for memory resource lookup as well as
87  *        resource management
88  * @index: resource index
89  */
90 #ifdef CONFIG_HAS_IOMEM
91 void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
92                                              unsigned int index)
93 {
94         struct resource *res;
95
96         res = platform_get_resource(pdev, IORESOURCE_MEM, index);
97         return devm_ioremap_resource(&pdev->dev, res);
98 }
99 EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
100 #endif /* CONFIG_HAS_IOMEM */
101
102 static int __platform_get_irq(struct platform_device *dev, unsigned int num)
103 {
104 #ifdef CONFIG_SPARC
105         /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
106         if (!dev || num >= dev->archdata.num_irqs)
107                 return -ENXIO;
108         return dev->archdata.irqs[num];
109 #else
110         struct resource *r;
111         if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
112                 int ret;
113
114                 ret = of_irq_get(dev->dev.of_node, num);
115                 if (ret > 0 || ret == -EPROBE_DEFER)
116                         return ret;
117         }
118
119         r = platform_get_resource(dev, IORESOURCE_IRQ, num);
120         if (has_acpi_companion(&dev->dev)) {
121                 if (r && r->flags & IORESOURCE_DISABLED) {
122                         int ret;
123
124                         ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
125                         if (ret)
126                                 return ret;
127                 }
128         }
129
130         /*
131          * The resources may pass trigger flags to the irqs that need
132          * to be set up. It so happens that the trigger flags for
133          * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
134          * settings.
135          */
136         if (r && r->flags & IORESOURCE_BITS) {
137                 struct irq_data *irqd;
138
139                 irqd = irq_get_irq_data(r->start);
140                 if (!irqd)
141                         return -ENXIO;
142                 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
143         }
144
145         if (r)
146                 return r->start;
147
148         /*
149          * For the index 0 interrupt, allow falling back to GpioInt
150          * resources. While a device could have both Interrupt and GpioInt
151          * resources, making this fallback ambiguous, in many common cases
152          * the device will only expose one IRQ, and this fallback
153          * allows a common code path across either kind of resource.
154          */
155         if (num == 0 && has_acpi_companion(&dev->dev)) {
156                 int ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
157
158                 /* Our callers expect -ENXIO for missing IRQs. */
159                 if (ret >= 0 || ret == -EPROBE_DEFER)
160                         return ret;
161         }
162
163         return -ENXIO;
164 #endif
165 }
166
167 /**
168  * platform_get_irq - get an IRQ for a device
169  * @dev: platform device
170  * @num: IRQ number index
171  *
172  * Gets an IRQ for a platform device and prints an error message if finding the
173  * IRQ fails. Device drivers should check the return value for errors so as to
174  * not pass a negative integer value to the request_irq() APIs.
175  *
176  * Example:
177  *              int irq = platform_get_irq(pdev, 0);
178  *              if (irq < 0)
179  *                      return irq;
180  *
181  * Return: IRQ number on success, negative error number on failure.
182  */
183 int platform_get_irq(struct platform_device *dev, unsigned int num)
184 {
185         int ret;
186
187         ret = __platform_get_irq(dev, num);
188         if (ret < 0 && ret != -EPROBE_DEFER)
189                 dev_err(&dev->dev, "IRQ index %u not found\n", num);
190
191         return ret;
192 }
193 EXPORT_SYMBOL_GPL(platform_get_irq);
194
195 /**
196  * platform_irq_count - Count the number of IRQs a platform device uses
197  * @dev: platform device
198  *
199  * Return: Number of IRQs a platform device uses or EPROBE_DEFER
200  */
201 int platform_irq_count(struct platform_device *dev)
202 {
203         int ret, nr = 0;
204
205         while ((ret = __platform_get_irq(dev, nr)) >= 0)
206                 nr++;
207
208         if (ret == -EPROBE_DEFER)
209                 return ret;
210
211         return nr;
212 }
213 EXPORT_SYMBOL_GPL(platform_irq_count);
214
215 /**
216  * platform_get_resource_byname - get a resource for a device by name
217  * @dev: platform device
218  * @type: resource type
219  * @name: resource name
220  */
221 struct resource *platform_get_resource_byname(struct platform_device *dev,
222                                               unsigned int type,
223                                               const char *name)
224 {
225         int i;
226
227         for (i = 0; i < dev->num_resources; i++) {
228                 struct resource *r = &dev->resource[i];
229
230                 if (unlikely(!r->name))
231                         continue;
232
233                 if (type == resource_type(r) && !strcmp(r->name, name))
234                         return r;
235         }
236         return NULL;
237 }
238 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
239
240 /**
241  * platform_get_irq_byname - get an IRQ for a device by name
242  * @dev: platform device
243  * @name: IRQ name
244  */
245 int platform_get_irq_byname(struct platform_device *dev, const char *name)
246 {
247         struct resource *r;
248
249         if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
250                 int ret;
251
252                 ret = of_irq_get_byname(dev->dev.of_node, name);
253                 if (ret > 0 || ret == -EPROBE_DEFER)
254                         return ret;
255         }
256
257         r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
258         if (r)
259                 return r->start;
260
261         dev_err(&dev->dev, "IRQ %s not found\n", name);
262         return -ENXIO;
263 }
264 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
265
266 /**
267  * platform_add_devices - add a numbers of platform devices
268  * @devs: array of platform devices to add
269  * @num: number of platform devices in array
270  */
271 int platform_add_devices(struct platform_device **devs, int num)
272 {
273         int i, ret = 0;
274
275         for (i = 0; i < num; i++) {
276                 ret = platform_device_register(devs[i]);
277                 if (ret) {
278                         while (--i >= 0)
279                                 platform_device_unregister(devs[i]);
280                         break;
281                 }
282         }
283
284         return ret;
285 }
286 EXPORT_SYMBOL_GPL(platform_add_devices);
287
288 struct platform_object {
289         struct platform_device pdev;
290         char name[];
291 };
292
293 /**
294  * platform_device_put - destroy a platform device
295  * @pdev: platform device to free
296  *
297  * Free all memory associated with a platform device.  This function must
298  * _only_ be externally called in error cases.  All other usage is a bug.
299  */
300 void platform_device_put(struct platform_device *pdev)
301 {
302         if (!IS_ERR_OR_NULL(pdev))
303                 put_device(&pdev->dev);
304 }
305 EXPORT_SYMBOL_GPL(platform_device_put);
306
307 static void platform_device_release(struct device *dev)
308 {
309         struct platform_object *pa = container_of(dev, struct platform_object,
310                                                   pdev.dev);
311
312         of_device_node_put(&pa->pdev.dev);
313         kfree(pa->pdev.dev.platform_data);
314         kfree(pa->pdev.mfd_cell);
315         kfree(pa->pdev.resource);
316         kfree(pa->pdev.driver_override);
317         kfree(pa);
318 }
319
320 /**
321  * platform_device_alloc - create a platform device
322  * @name: base name of the device we're adding
323  * @id: instance id
324  *
325  * Create a platform device object which can have other objects attached
326  * to it, and which will have attached objects freed when it is released.
327  */
328 struct platform_device *platform_device_alloc(const char *name, int id)
329 {
330         struct platform_object *pa;
331
332         pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
333         if (pa) {
334                 strcpy(pa->name, name);
335                 pa->pdev.name = pa->name;
336                 pa->pdev.id = id;
337                 device_initialize(&pa->pdev.dev);
338                 pa->pdev.dev.release = platform_device_release;
339                 arch_setup_pdev_archdata(&pa->pdev);
340         }
341
342         return pa ? &pa->pdev : NULL;
343 }
344 EXPORT_SYMBOL_GPL(platform_device_alloc);
345
346 /**
347  * platform_device_add_resources - add resources to a platform device
348  * @pdev: platform device allocated by platform_device_alloc to add resources to
349  * @res: set of resources that needs to be allocated for the device
350  * @num: number of resources
351  *
352  * Add a copy of the resources to the platform device.  The memory
353  * associated with the resources will be freed when the platform device is
354  * released.
355  */
356 int platform_device_add_resources(struct platform_device *pdev,
357                                   const struct resource *res, unsigned int num)
358 {
359         struct resource *r = NULL;
360
361         if (res) {
362                 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
363                 if (!r)
364                         return -ENOMEM;
365         }
366
367         kfree(pdev->resource);
368         pdev->resource = r;
369         pdev->num_resources = num;
370         return 0;
371 }
372 EXPORT_SYMBOL_GPL(platform_device_add_resources);
373
374 /**
375  * platform_device_add_data - add platform-specific data to a platform device
376  * @pdev: platform device allocated by platform_device_alloc to add resources to
377  * @data: platform specific data for this platform device
378  * @size: size of platform specific data
379  *
380  * Add a copy of platform specific data to the platform device's
381  * platform_data pointer.  The memory associated with the platform data
382  * will be freed when the platform device is released.
383  */
384 int platform_device_add_data(struct platform_device *pdev, const void *data,
385                              size_t size)
386 {
387         void *d = NULL;
388
389         if (data) {
390                 d = kmemdup(data, size, GFP_KERNEL);
391                 if (!d)
392                         return -ENOMEM;
393         }
394
395         kfree(pdev->dev.platform_data);
396         pdev->dev.platform_data = d;
397         return 0;
398 }
399 EXPORT_SYMBOL_GPL(platform_device_add_data);
400
401 /**
402  * platform_device_add_properties - add built-in properties to a platform device
403  * @pdev: platform device to add properties to
404  * @properties: null terminated array of properties to add
405  *
406  * The function will take deep copy of @properties and attach the copy to the
407  * platform device. The memory associated with properties will be freed when the
408  * platform device is released.
409  */
410 int platform_device_add_properties(struct platform_device *pdev,
411                                    const struct property_entry *properties)
412 {
413         return device_add_properties(&pdev->dev, properties);
414 }
415 EXPORT_SYMBOL_GPL(platform_device_add_properties);
416
417 /**
418  * platform_device_add - add a platform device to device hierarchy
419  * @pdev: platform device we're adding
420  *
421  * This is part 2 of platform_device_register(), though may be called
422  * separately _iff_ pdev was allocated by platform_device_alloc().
423  */
424 int platform_device_add(struct platform_device *pdev)
425 {
426         int i, ret;
427
428         if (!pdev)
429                 return -EINVAL;
430
431         if (!pdev->dev.parent)
432                 pdev->dev.parent = &platform_bus;
433
434         pdev->dev.bus = &platform_bus_type;
435
436         switch (pdev->id) {
437         default:
438                 dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);
439                 break;
440         case PLATFORM_DEVID_NONE:
441                 dev_set_name(&pdev->dev, "%s", pdev->name);
442                 break;
443         case PLATFORM_DEVID_AUTO:
444                 /*
445                  * Automatically allocated device ID. We mark it as such so
446                  * that we remember it must be freed, and we append a suffix
447                  * to avoid namespace collision with explicit IDs.
448                  */
449                 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
450                 if (ret < 0)
451                         goto err_out;
452                 pdev->id = ret;
453                 pdev->id_auto = true;
454                 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
455                 break;
456         }
457
458         for (i = 0; i < pdev->num_resources; i++) {
459                 struct resource *p, *r = &pdev->resource[i];
460
461                 if (r->name == NULL)
462                         r->name = dev_name(&pdev->dev);
463
464                 p = r->parent;
465                 if (!p) {
466                         if (resource_type(r) == IORESOURCE_MEM)
467                                 p = &iomem_resource;
468                         else if (resource_type(r) == IORESOURCE_IO)
469                                 p = &ioport_resource;
470                 }
471
472                 if (p) {
473                         ret = insert_resource(p, r);
474                         if (ret) {
475                                 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
476                                 goto failed;
477                         }
478                 }
479         }
480
481         pr_debug("Registering platform device '%s'. Parent at %s\n",
482                  dev_name(&pdev->dev), dev_name(pdev->dev.parent));
483
484         ret = device_add(&pdev->dev);
485         if (ret == 0)
486                 return ret;
487
488  failed:
489         if (pdev->id_auto) {
490                 ida_simple_remove(&platform_devid_ida, pdev->id);
491                 pdev->id = PLATFORM_DEVID_AUTO;
492         }
493
494         while (--i >= 0) {
495                 struct resource *r = &pdev->resource[i];
496                 if (r->parent)
497                         release_resource(r);
498         }
499
500  err_out:
501         return ret;
502 }
503 EXPORT_SYMBOL_GPL(platform_device_add);
504
505 /**
506  * platform_device_del - remove a platform-level device
507  * @pdev: platform device we're removing
508  *
509  * Note that this function will also release all memory- and port-based
510  * resources owned by the device (@dev->resource).  This function must
511  * _only_ be externally called in error cases.  All other usage is a bug.
512  */
513 void platform_device_del(struct platform_device *pdev)
514 {
515         int i;
516
517         if (!IS_ERR_OR_NULL(pdev)) {
518                 device_del(&pdev->dev);
519
520                 if (pdev->id_auto) {
521                         ida_simple_remove(&platform_devid_ida, pdev->id);
522                         pdev->id = PLATFORM_DEVID_AUTO;
523                 }
524
525                 for (i = 0; i < pdev->num_resources; i++) {
526                         struct resource *r = &pdev->resource[i];
527                         if (r->parent)
528                                 release_resource(r);
529                 }
530         }
531 }
532 EXPORT_SYMBOL_GPL(platform_device_del);
533
534 /**
535  * platform_device_register - add a platform-level device
536  * @pdev: platform device we're adding
537  */
538 int platform_device_register(struct platform_device *pdev)
539 {
540         device_initialize(&pdev->dev);
541         arch_setup_pdev_archdata(pdev);
542         return platform_device_add(pdev);
543 }
544 EXPORT_SYMBOL_GPL(platform_device_register);
545
546 /**
547  * platform_device_unregister - unregister a platform-level device
548  * @pdev: platform device we're unregistering
549  *
550  * Unregistration is done in 2 steps. First we release all resources
551  * and remove it from the subsystem, then we drop reference count by
552  * calling platform_device_put().
553  */
554 void platform_device_unregister(struct platform_device *pdev)
555 {
556         platform_device_del(pdev);
557         platform_device_put(pdev);
558 }
559 EXPORT_SYMBOL_GPL(platform_device_unregister);
560
561 /**
562  * platform_device_register_full - add a platform-level device with
563  * resources and platform-specific data
564  *
565  * @pdevinfo: data used to create device
566  *
567  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
568  */
569 struct platform_device *platform_device_register_full(
570                 const struct platform_device_info *pdevinfo)
571 {
572         int ret = -ENOMEM;
573         struct platform_device *pdev;
574
575         pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
576         if (!pdev)
577                 return ERR_PTR(-ENOMEM);
578
579         pdev->dev.parent = pdevinfo->parent;
580         pdev->dev.fwnode = pdevinfo->fwnode;
581         pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
582         pdev->dev.of_node_reused = pdevinfo->of_node_reused;
583
584         if (pdevinfo->dma_mask) {
585                 /*
586                  * This memory isn't freed when the device is put,
587                  * I don't have a nice idea for that though.  Conceptually
588                  * dma_mask in struct device should not be a pointer.
589                  * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
590                  */
591                 pdev->dev.dma_mask =
592                         kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
593                 if (!pdev->dev.dma_mask)
594                         goto err;
595
596                 kmemleak_ignore(pdev->dev.dma_mask);
597
598                 *pdev->dev.dma_mask = pdevinfo->dma_mask;
599                 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
600         }
601
602         ret = platform_device_add_resources(pdev,
603                         pdevinfo->res, pdevinfo->num_res);
604         if (ret)
605                 goto err;
606
607         ret = platform_device_add_data(pdev,
608                         pdevinfo->data, pdevinfo->size_data);
609         if (ret)
610                 goto err;
611
612         if (pdevinfo->properties) {
613                 ret = platform_device_add_properties(pdev,
614                                                      pdevinfo->properties);
615                 if (ret)
616                         goto err;
617         }
618
619         ret = platform_device_add(pdev);
620         if (ret) {
621 err:
622                 ACPI_COMPANION_SET(&pdev->dev, NULL);
623                 kfree(pdev->dev.dma_mask);
624                 platform_device_put(pdev);
625                 return ERR_PTR(ret);
626         }
627
628         return pdev;
629 }
630 EXPORT_SYMBOL_GPL(platform_device_register_full);
631
632 static int platform_drv_probe(struct device *_dev)
633 {
634         struct platform_driver *drv = to_platform_driver(_dev->driver);
635         struct platform_device *dev = to_platform_device(_dev);
636         int ret;
637
638         ret = of_clk_set_defaults(_dev->of_node, false);
639         if (ret < 0)
640                 return ret;
641
642         ret = dev_pm_domain_attach(_dev, true);
643         if (ret)
644                 goto out;
645
646         if (drv->probe) {
647                 ret = drv->probe(dev);
648                 if (ret)
649                         dev_pm_domain_detach(_dev, true);
650         }
651
652 out:
653         if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
654                 dev_warn(_dev, "probe deferral not supported\n");
655                 ret = -ENXIO;
656         }
657
658         return ret;
659 }
660
661 static int platform_drv_probe_fail(struct device *_dev)
662 {
663         return -ENXIO;
664 }
665
666 static int platform_drv_remove(struct device *_dev)
667 {
668         struct platform_driver *drv = to_platform_driver(_dev->driver);
669         struct platform_device *dev = to_platform_device(_dev);
670         int ret = 0;
671
672         if (drv->remove)
673                 ret = drv->remove(dev);
674         dev_pm_domain_detach(_dev, true);
675
676         return ret;
677 }
678
679 static void platform_drv_shutdown(struct device *_dev)
680 {
681         struct platform_driver *drv = to_platform_driver(_dev->driver);
682         struct platform_device *dev = to_platform_device(_dev);
683
684         if (drv->shutdown)
685                 drv->shutdown(dev);
686 }
687
688 /**
689  * __platform_driver_register - register a driver for platform-level devices
690  * @drv: platform driver structure
691  * @owner: owning module/driver
692  */
693 int __platform_driver_register(struct platform_driver *drv,
694                                 struct module *owner)
695 {
696         drv->driver.owner = owner;
697         drv->driver.bus = &platform_bus_type;
698         drv->driver.probe = platform_drv_probe;
699         drv->driver.remove = platform_drv_remove;
700         drv->driver.shutdown = platform_drv_shutdown;
701
702         return driver_register(&drv->driver);
703 }
704 EXPORT_SYMBOL_GPL(__platform_driver_register);
705
706 /**
707  * platform_driver_unregister - unregister a driver for platform-level devices
708  * @drv: platform driver structure
709  */
710 void platform_driver_unregister(struct platform_driver *drv)
711 {
712         driver_unregister(&drv->driver);
713 }
714 EXPORT_SYMBOL_GPL(platform_driver_unregister);
715
716 /**
717  * __platform_driver_probe - register driver for non-hotpluggable device
718  * @drv: platform driver structure
719  * @probe: the driver probe routine, probably from an __init section
720  * @module: module which will be the owner of the driver
721  *
722  * Use this instead of platform_driver_register() when you know the device
723  * is not hotpluggable and has already been registered, and you want to
724  * remove its run-once probe() infrastructure from memory after the driver
725  * has bound to the device.
726  *
727  * One typical use for this would be with drivers for controllers integrated
728  * into system-on-chip processors, where the controller devices have been
729  * configured as part of board setup.
730  *
731  * Note that this is incompatible with deferred probing.
732  *
733  * Returns zero if the driver registered and bound to a device, else returns
734  * a negative error code and with the driver not registered.
735  */
736 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
737                 int (*probe)(struct platform_device *), struct module *module)
738 {
739         int retval, code;
740
741         if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
742                 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
743                          drv->driver.name, __func__);
744                 return -EINVAL;
745         }
746
747         /*
748          * We have to run our probes synchronously because we check if
749          * we find any devices to bind to and exit with error if there
750          * are any.
751          */
752         drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
753
754         /*
755          * Prevent driver from requesting probe deferral to avoid further
756          * futile probe attempts.
757          */
758         drv->prevent_deferred_probe = true;
759
760         /* make sure driver won't have bind/unbind attributes */
761         drv->driver.suppress_bind_attrs = true;
762
763         /* temporary section violation during probe() */
764         drv->probe = probe;
765         retval = code = __platform_driver_register(drv, module);
766
767         /*
768          * Fixup that section violation, being paranoid about code scanning
769          * the list of drivers in order to probe new devices.  Check to see
770          * if the probe was successful, and make sure any forced probes of
771          * new devices fail.
772          */
773         spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
774         drv->probe = NULL;
775         if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
776                 retval = -ENODEV;
777         drv->driver.probe = platform_drv_probe_fail;
778         spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
779
780         if (code != retval)
781                 platform_driver_unregister(drv);
782         return retval;
783 }
784 EXPORT_SYMBOL_GPL(__platform_driver_probe);
785
786 /**
787  * __platform_create_bundle - register driver and create corresponding device
788  * @driver: platform driver structure
789  * @probe: the driver probe routine, probably from an __init section
790  * @res: set of resources that needs to be allocated for the device
791  * @n_res: number of resources
792  * @data: platform specific data for this platform device
793  * @size: size of platform specific data
794  * @module: module which will be the owner of the driver
795  *
796  * Use this in legacy-style modules that probe hardware directly and
797  * register a single platform device and corresponding platform driver.
798  *
799  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
800  */
801 struct platform_device * __init_or_module __platform_create_bundle(
802                         struct platform_driver *driver,
803                         int (*probe)(struct platform_device *),
804                         struct resource *res, unsigned int n_res,
805                         const void *data, size_t size, struct module *module)
806 {
807         struct platform_device *pdev;
808         int error;
809
810         pdev = platform_device_alloc(driver->driver.name, -1);
811         if (!pdev) {
812                 error = -ENOMEM;
813                 goto err_out;
814         }
815
816         error = platform_device_add_resources(pdev, res, n_res);
817         if (error)
818                 goto err_pdev_put;
819
820         error = platform_device_add_data(pdev, data, size);
821         if (error)
822                 goto err_pdev_put;
823
824         error = platform_device_add(pdev);
825         if (error)
826                 goto err_pdev_put;
827
828         error = __platform_driver_probe(driver, probe, module);
829         if (error)
830                 goto err_pdev_del;
831
832         return pdev;
833
834 err_pdev_del:
835         platform_device_del(pdev);
836 err_pdev_put:
837         platform_device_put(pdev);
838 err_out:
839         return ERR_PTR(error);
840 }
841 EXPORT_SYMBOL_GPL(__platform_create_bundle);
842
843 /**
844  * __platform_register_drivers - register an array of platform drivers
845  * @drivers: an array of drivers to register
846  * @count: the number of drivers to register
847  * @owner: module owning the drivers
848  *
849  * Registers platform drivers specified by an array. On failure to register a
850  * driver, all previously registered drivers will be unregistered. Callers of
851  * this API should use platform_unregister_drivers() to unregister drivers in
852  * the reverse order.
853  *
854  * Returns: 0 on success or a negative error code on failure.
855  */
856 int __platform_register_drivers(struct platform_driver * const *drivers,
857                                 unsigned int count, struct module *owner)
858 {
859         unsigned int i;
860         int err;
861
862         for (i = 0; i < count; i++) {
863                 pr_debug("registering platform driver %ps\n", drivers[i]);
864
865                 err = __platform_driver_register(drivers[i], owner);
866                 if (err < 0) {
867                         pr_err("failed to register platform driver %ps: %d\n",
868                                drivers[i], err);
869                         goto error;
870                 }
871         }
872
873         return 0;
874
875 error:
876         while (i--) {
877                 pr_debug("unregistering platform driver %ps\n", drivers[i]);
878                 platform_driver_unregister(drivers[i]);
879         }
880
881         return err;
882 }
883 EXPORT_SYMBOL_GPL(__platform_register_drivers);
884
885 /**
886  * platform_unregister_drivers - unregister an array of platform drivers
887  * @drivers: an array of drivers to unregister
888  * @count: the number of drivers to unregister
889  *
890  * Unegisters platform drivers specified by an array. This is typically used
891  * to complement an earlier call to platform_register_drivers(). Drivers are
892  * unregistered in the reverse order in which they were registered.
893  */
894 void platform_unregister_drivers(struct platform_driver * const *drivers,
895                                  unsigned int count)
896 {
897         while (count--) {
898                 pr_debug("unregistering platform driver %ps\n", drivers[count]);
899                 platform_driver_unregister(drivers[count]);
900         }
901 }
902 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
903
904 /* modalias support enables more hands-off userspace setup:
905  * (a) environment variable lets new-style hotplug events work once system is
906  *     fully running:  "modprobe $MODALIAS"
907  * (b) sysfs attribute lets new-style coldplug recover from hotplug events
908  *     mishandled before system is fully running:  "modprobe $(cat modalias)"
909  */
910 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
911                              char *buf)
912 {
913         struct platform_device  *pdev = to_platform_device(dev);
914         int len;
915
916         len = of_device_modalias(dev, buf, PAGE_SIZE);
917         if (len != -ENODEV)
918                 return len;
919
920         len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
921         if (len != -ENODEV)
922                 return len;
923
924         len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
925
926         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
927 }
928 static DEVICE_ATTR_RO(modalias);
929
930 static ssize_t driver_override_store(struct device *dev,
931                                      struct device_attribute *attr,
932                                      const char *buf, size_t count)
933 {
934         struct platform_device *pdev = to_platform_device(dev);
935         char *driver_override, *old, *cp;
936
937         /* We need to keep extra room for a newline */
938         if (count >= (PAGE_SIZE - 1))
939                 return -EINVAL;
940
941         driver_override = kstrndup(buf, count, GFP_KERNEL);
942         if (!driver_override)
943                 return -ENOMEM;
944
945         cp = strchr(driver_override, '\n');
946         if (cp)
947                 *cp = '\0';
948
949         device_lock(dev);
950         old = pdev->driver_override;
951         if (strlen(driver_override)) {
952                 pdev->driver_override = driver_override;
953         } else {
954                 kfree(driver_override);
955                 pdev->driver_override = NULL;
956         }
957         device_unlock(dev);
958
959         kfree(old);
960
961         return count;
962 }
963
964 static ssize_t driver_override_show(struct device *dev,
965                                     struct device_attribute *attr, char *buf)
966 {
967         struct platform_device *pdev = to_platform_device(dev);
968         ssize_t len;
969
970         device_lock(dev);
971         len = sprintf(buf, "%s\n", pdev->driver_override);
972         device_unlock(dev);
973         return len;
974 }
975 static DEVICE_ATTR_RW(driver_override);
976
977
978 static struct attribute *platform_dev_attrs[] = {
979         &dev_attr_modalias.attr,
980         &dev_attr_driver_override.attr,
981         NULL,
982 };
983 ATTRIBUTE_GROUPS(platform_dev);
984
985 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
986 {
987         struct platform_device  *pdev = to_platform_device(dev);
988         int rc;
989
990         /* Some devices have extra OF data and an OF-style MODALIAS */
991         rc = of_device_uevent_modalias(dev, env);
992         if (rc != -ENODEV)
993                 return rc;
994
995         rc = acpi_device_uevent_modalias(dev, env);
996         if (rc != -ENODEV)
997                 return rc;
998
999         add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
1000                         pdev->name);
1001         return 0;
1002 }
1003
1004 static const struct platform_device_id *platform_match_id(
1005                         const struct platform_device_id *id,
1006                         struct platform_device *pdev)
1007 {
1008         while (id->name[0]) {
1009                 if (strcmp(pdev->name, id->name) == 0) {
1010                         pdev->id_entry = id;
1011                         return id;
1012                 }
1013                 id++;
1014         }
1015         return NULL;
1016 }
1017
1018 /**
1019  * platform_match - bind platform device to platform driver.
1020  * @dev: device.
1021  * @drv: driver.
1022  *
1023  * Platform device IDs are assumed to be encoded like this:
1024  * "<name><instance>", where <name> is a short description of the type of
1025  * device, like "pci" or "floppy", and <instance> is the enumerated
1026  * instance of the device, like '0' or '42'.  Driver IDs are simply
1027  * "<name>".  So, extract the <name> from the platform_device structure,
1028  * and compare it against the name of the driver. Return whether they match
1029  * or not.
1030  */
1031 static int platform_match(struct device *dev, struct device_driver *drv)
1032 {
1033         struct platform_device *pdev = to_platform_device(dev);
1034         struct platform_driver *pdrv = to_platform_driver(drv);
1035
1036         /* When driver_override is set, only bind to the matching driver */
1037         if (pdev->driver_override)
1038                 return !strcmp(pdev->driver_override, drv->name);
1039
1040         /* Attempt an OF style match first */
1041         if (of_driver_match_device(dev, drv))
1042                 return 1;
1043
1044         /* Then try ACPI style match */
1045         if (acpi_driver_match_device(dev, drv))
1046                 return 1;
1047
1048         /* Then try to match against the id table */
1049         if (pdrv->id_table)
1050                 return platform_match_id(pdrv->id_table, pdev) != NULL;
1051
1052         /* fall-back to driver name match */
1053         return (strcmp(pdev->name, drv->name) == 0);
1054 }
1055
1056 #ifdef CONFIG_PM_SLEEP
1057
1058 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
1059 {
1060         struct platform_driver *pdrv = to_platform_driver(dev->driver);
1061         struct platform_device *pdev = to_platform_device(dev);
1062         int ret = 0;
1063
1064         if (dev->driver && pdrv->suspend)
1065                 ret = pdrv->suspend(pdev, mesg);
1066
1067         return ret;
1068 }
1069
1070 static int platform_legacy_resume(struct device *dev)
1071 {
1072         struct platform_driver *pdrv = to_platform_driver(dev->driver);
1073         struct platform_device *pdev = to_platform_device(dev);
1074         int ret = 0;
1075
1076         if (dev->driver && pdrv->resume)
1077                 ret = pdrv->resume(pdev);
1078
1079         return ret;
1080 }
1081
1082 #endif /* CONFIG_PM_SLEEP */
1083
1084 #ifdef CONFIG_SUSPEND
1085
1086 int platform_pm_suspend(struct device *dev)
1087 {
1088         struct device_driver *drv = dev->driver;
1089         int ret = 0;
1090
1091         if (!drv)
1092                 return 0;
1093
1094         if (drv->pm) {
1095                 if (drv->pm->suspend)
1096                         ret = drv->pm->suspend(dev);
1097         } else {
1098                 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1099         }
1100
1101         return ret;
1102 }
1103
1104 int platform_pm_resume(struct device *dev)
1105 {
1106         struct device_driver *drv = dev->driver;
1107         int ret = 0;
1108
1109         if (!drv)
1110                 return 0;
1111
1112         if (drv->pm) {
1113                 if (drv->pm->resume)
1114                         ret = drv->pm->resume(dev);
1115         } else {
1116                 ret = platform_legacy_resume(dev);
1117         }
1118
1119         return ret;
1120 }
1121
1122 #endif /* CONFIG_SUSPEND */
1123
1124 #ifdef CONFIG_HIBERNATE_CALLBACKS
1125
1126 int platform_pm_freeze(struct device *dev)
1127 {
1128         struct device_driver *drv = dev->driver;
1129         int ret = 0;
1130
1131         if (!drv)
1132                 return 0;
1133
1134         if (drv->pm) {
1135                 if (drv->pm->freeze)
1136                         ret = drv->pm->freeze(dev);
1137         } else {
1138                 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1139         }
1140
1141         return ret;
1142 }
1143
1144 int platform_pm_thaw(struct device *dev)
1145 {
1146         struct device_driver *drv = dev->driver;
1147         int ret = 0;
1148
1149         if (!drv)
1150                 return 0;
1151
1152         if (drv->pm) {
1153                 if (drv->pm->thaw)
1154                         ret = drv->pm->thaw(dev);
1155         } else {
1156                 ret = platform_legacy_resume(dev);
1157         }
1158
1159         return ret;
1160 }
1161
1162 int platform_pm_poweroff(struct device *dev)
1163 {
1164         struct device_driver *drv = dev->driver;
1165         int ret = 0;
1166
1167         if (!drv)
1168                 return 0;
1169
1170         if (drv->pm) {
1171                 if (drv->pm->poweroff)
1172                         ret = drv->pm->poweroff(dev);
1173         } else {
1174                 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1175         }
1176
1177         return ret;
1178 }
1179
1180 int platform_pm_restore(struct device *dev)
1181 {
1182         struct device_driver *drv = dev->driver;
1183         int ret = 0;
1184
1185         if (!drv)
1186                 return 0;
1187
1188         if (drv->pm) {
1189                 if (drv->pm->restore)
1190                         ret = drv->pm->restore(dev);
1191         } else {
1192                 ret = platform_legacy_resume(dev);
1193         }
1194
1195         return ret;
1196 }
1197
1198 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1199
1200 int platform_dma_configure(struct device *dev)
1201 {
1202         enum dev_dma_attr attr;
1203         int ret = 0;
1204
1205         if (dev->of_node) {
1206                 ret = of_dma_configure(dev, dev->of_node, true);
1207         } else if (has_acpi_companion(dev)) {
1208                 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1209                 ret = acpi_dma_configure(dev, attr);
1210         }
1211
1212         return ret;
1213 }
1214
1215 static const struct dev_pm_ops platform_dev_pm_ops = {
1216         .runtime_suspend = pm_generic_runtime_suspend,
1217         .runtime_resume = pm_generic_runtime_resume,
1218         USE_PLATFORM_PM_SLEEP_OPS
1219 };
1220
1221 struct bus_type platform_bus_type = {
1222         .name           = "platform",
1223         .dev_groups     = platform_dev_groups,
1224         .match          = platform_match,
1225         .uevent         = platform_uevent,
1226         .dma_configure  = platform_dma_configure,
1227         .pm             = &platform_dev_pm_ops,
1228 };
1229 EXPORT_SYMBOL_GPL(platform_bus_type);
1230
1231 /**
1232  * platform_find_device_by_driver - Find a platform device with a given
1233  * driver.
1234  * @start: The device to start the search from.
1235  * @drv: The device driver to look for.
1236  */
1237 struct device *platform_find_device_by_driver(struct device *start,
1238                                               const struct device_driver *drv)
1239 {
1240         return bus_find_device(&platform_bus_type, start, drv,
1241                                (void *)platform_match);
1242 }
1243 EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1244
1245 int __init platform_bus_init(void)
1246 {
1247         int error;
1248
1249         early_platform_cleanup();
1250
1251         error = device_register(&platform_bus);
1252         if (error) {
1253                 put_device(&platform_bus);
1254                 return error;
1255         }
1256         error =  bus_register(&platform_bus_type);
1257         if (error)
1258                 device_unregister(&platform_bus);
1259         of_platform_register_reconfig_notifier();
1260         return error;
1261 }
1262
1263 static __initdata LIST_HEAD(early_platform_driver_list);
1264 static __initdata LIST_HEAD(early_platform_device_list);
1265
1266 /**
1267  * early_platform_driver_register - register early platform driver
1268  * @epdrv: early_platform driver structure
1269  * @buf: string passed from early_param()
1270  *
1271  * Helper function for early_platform_init() / early_platform_init_buffer()
1272  */
1273 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1274                                           char *buf)
1275 {
1276         char *tmp;
1277         int n;
1278
1279         /* Simply add the driver to the end of the global list.
1280          * Drivers will by default be put on the list in compiled-in order.
1281          */
1282         if (!epdrv->list.next) {
1283                 INIT_LIST_HEAD(&epdrv->list);
1284                 list_add_tail(&epdrv->list, &early_platform_driver_list);
1285         }
1286
1287         /* If the user has specified device then make sure the driver
1288          * gets prioritized. The driver of the last device specified on
1289          * command line will be put first on the list.
1290          */
1291         n = strlen(epdrv->pdrv->driver.name);
1292         if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1293                 list_move(&epdrv->list, &early_platform_driver_list);
1294
1295                 /* Allow passing parameters after device name */
1296                 if (buf[n] == '\0' || buf[n] == ',')
1297                         epdrv->requested_id = -1;
1298                 else {
1299                         epdrv->requested_id = simple_strtoul(&buf[n + 1],
1300                                                              &tmp, 10);
1301
1302                         if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1303                                 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1304                                 n = 0;
1305                         } else
1306                                 n += strcspn(&buf[n + 1], ",") + 1;
1307                 }
1308
1309                 if (buf[n] == ',')
1310                         n++;
1311
1312                 if (epdrv->bufsize) {
1313                         memcpy(epdrv->buffer, &buf[n],
1314                                min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1315                         epdrv->buffer[epdrv->bufsize - 1] = '\0';
1316                 }
1317         }
1318
1319         return 0;
1320 }
1321
1322 /**
1323  * early_platform_add_devices - adds a number of early platform devices
1324  * @devs: array of early platform devices to add
1325  * @num: number of early platform devices in array
1326  *
1327  * Used by early architecture code to register early platform devices and
1328  * their platform data.
1329  */
1330 void __init early_platform_add_devices(struct platform_device **devs, int num)
1331 {
1332         struct device *dev;
1333         int i;
1334
1335         /* simply add the devices to list */
1336         for (i = 0; i < num; i++) {
1337                 dev = &devs[i]->dev;
1338
1339                 if (!dev->devres_head.next) {
1340                         pm_runtime_early_init(dev);
1341                         INIT_LIST_HEAD(&dev->devres_head);
1342                         list_add_tail(&dev->devres_head,
1343                                       &early_platform_device_list);
1344                 }
1345         }
1346 }
1347
1348 /**
1349  * early_platform_driver_register_all - register early platform drivers
1350  * @class_str: string to identify early platform driver class
1351  *
1352  * Used by architecture code to register all early platform drivers
1353  * for a certain class. If omitted then only early platform drivers
1354  * with matching kernel command line class parameters will be registered.
1355  */
1356 void __init early_platform_driver_register_all(char *class_str)
1357 {
1358         /* The "class_str" parameter may or may not be present on the kernel
1359          * command line. If it is present then there may be more than one
1360          * matching parameter.
1361          *
1362          * Since we register our early platform drivers using early_param()
1363          * we need to make sure that they also get registered in the case
1364          * when the parameter is missing from the kernel command line.
1365          *
1366          * We use parse_early_options() to make sure the early_param() gets
1367          * called at least once. The early_param() may be called more than
1368          * once since the name of the preferred device may be specified on
1369          * the kernel command line. early_platform_driver_register() handles
1370          * this case for us.
1371          */
1372         parse_early_options(class_str);
1373 }
1374
1375 /**
1376  * early_platform_match - find early platform device matching driver
1377  * @epdrv: early platform driver structure
1378  * @id: id to match against
1379  */
1380 static struct platform_device * __init
1381 early_platform_match(struct early_platform_driver *epdrv, int id)
1382 {
1383         struct platform_device *pd;
1384
1385         list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1386                 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1387                         if (pd->id == id)
1388                                 return pd;
1389
1390         return NULL;
1391 }
1392
1393 /**
1394  * early_platform_left - check if early platform driver has matching devices
1395  * @epdrv: early platform driver structure
1396  * @id: return true if id or above exists
1397  */
1398 static int __init early_platform_left(struct early_platform_driver *epdrv,
1399                                        int id)
1400 {
1401         struct platform_device *pd;
1402
1403         list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1404                 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1405                         if (pd->id >= id)
1406                                 return 1;
1407
1408         return 0;
1409 }
1410
1411 /**
1412  * early_platform_driver_probe_id - probe drivers matching class_str and id
1413  * @class_str: string to identify early platform driver class
1414  * @id: id to match against
1415  * @nr_probe: number of platform devices to successfully probe before exiting
1416  */
1417 static int __init early_platform_driver_probe_id(char *class_str,
1418                                                  int id,
1419                                                  int nr_probe)
1420 {
1421         struct early_platform_driver *epdrv;
1422         struct platform_device *match;
1423         int match_id;
1424         int n = 0;
1425         int left = 0;
1426
1427         list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1428                 /* only use drivers matching our class_str */
1429                 if (strcmp(class_str, epdrv->class_str))
1430                         continue;
1431
1432                 if (id == -2) {
1433                         match_id = epdrv->requested_id;
1434                         left = 1;
1435
1436                 } else {
1437                         match_id = id;
1438                         left += early_platform_left(epdrv, id);
1439
1440                         /* skip requested id */
1441                         switch (epdrv->requested_id) {
1442                         case EARLY_PLATFORM_ID_ERROR:
1443                         case EARLY_PLATFORM_ID_UNSET:
1444                                 break;
1445                         default:
1446                                 if (epdrv->requested_id == id)
1447                                         match_id = EARLY_PLATFORM_ID_UNSET;
1448                         }
1449                 }
1450
1451                 switch (match_id) {
1452                 case EARLY_PLATFORM_ID_ERROR:
1453                         pr_warn("%s: unable to parse %s parameter\n",
1454                                 class_str, epdrv->pdrv->driver.name);
1455                         /* fall-through */
1456                 case EARLY_PLATFORM_ID_UNSET:
1457                         match = NULL;
1458                         break;
1459                 default:
1460                         match = early_platform_match(epdrv, match_id);
1461                 }
1462
1463                 if (match) {
1464                         /*
1465                          * Set up a sensible init_name to enable
1466                          * dev_name() and others to be used before the
1467                          * rest of the driver core is initialized.
1468                          */
1469                         if (!match->dev.init_name && slab_is_available()) {
1470                                 if (match->id != -1)
1471                                         match->dev.init_name =
1472                                                 kasprintf(GFP_KERNEL, "%s.%d",
1473                                                           match->name,
1474                                                           match->id);
1475                                 else
1476                                         match->dev.init_name =
1477                                                 kasprintf(GFP_KERNEL, "%s",
1478                                                           match->name);
1479
1480                                 if (!match->dev.init_name)
1481                                         return -ENOMEM;
1482                         }
1483
1484                         if (epdrv->pdrv->probe(match))
1485                                 pr_warn("%s: unable to probe %s early.\n",
1486                                         class_str, match->name);
1487                         else
1488                                 n++;
1489                 }
1490
1491                 if (n >= nr_probe)
1492                         break;
1493         }
1494
1495         if (left)
1496                 return n;
1497         else
1498                 return -ENODEV;
1499 }
1500
1501 /**
1502  * early_platform_driver_probe - probe a class of registered drivers
1503  * @class_str: string to identify early platform driver class
1504  * @nr_probe: number of platform devices to successfully probe before exiting
1505  * @user_only: only probe user specified early platform devices
1506  *
1507  * Used by architecture code to probe registered early platform drivers
1508  * within a certain class. For probe to happen a registered early platform
1509  * device matching a registered early platform driver is needed.
1510  */
1511 int __init early_platform_driver_probe(char *class_str,
1512                                        int nr_probe,
1513                                        int user_only)
1514 {
1515         int k, n, i;
1516
1517         n = 0;
1518         for (i = -2; n < nr_probe; i++) {
1519                 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1520
1521                 if (k < 0)
1522                         break;
1523
1524                 n += k;
1525
1526                 if (user_only)
1527                         break;
1528         }
1529
1530         return n;
1531 }
1532
1533 /**
1534  * early_platform_cleanup - clean up early platform code
1535  */
1536 void __init early_platform_cleanup(void)
1537 {
1538         struct platform_device *pd, *pd2;
1539
1540         /* clean up the devres list used to chain devices */
1541         list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1542                                  dev.devres_head) {
1543                 list_del(&pd->dev.devres_head);
1544                 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1545         }
1546 }
1547
This page took 0.118286 seconds and 4 git commands to generate.