blob: a2e3910196e001c10ae637c3e08288deae812af0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/dma-mapping.h>
17#include <linux/bootmem.h>
18#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010021#include "base.h"
22
Russell King00d3dcd2005-11-09 17:23:39 +000023#define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct device platform_bus = {
26 .bus_id = "platform",
27};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050028EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/**
31 * platform_get_resource - get a resource for a device
32 * @dev: platform device
33 * @type: resource type
34 * @num: resource index
35 */
36struct resource *
37platform_get_resource(struct platform_device *dev, unsigned int type,
38 unsigned int num)
39{
40 int i;
41
42 for (i = 0; i < dev->num_resources; i++) {
43 struct resource *r = &dev->resource[i];
44
45 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
46 IORESOURCE_IRQ|IORESOURCE_DMA))
47 == type)
48 if (num-- == 0)
49 return r;
50 }
51 return NULL;
52}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050053EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/**
56 * platform_get_irq - get an IRQ for a device
57 * @dev: platform device
58 * @num: IRQ number index
59 */
60int platform_get_irq(struct platform_device *dev, unsigned int num)
61{
62 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
63
David Vrabel305b3222006-01-19 17:52:27 +000064 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050066EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/**
69 * platform_get_resource_byname - get a resource for a device by name
70 * @dev: platform device
71 * @type: resource type
72 * @name: resource name
73 */
74struct resource *
75platform_get_resource_byname(struct platform_device *dev, unsigned int type,
76 char *name)
77{
78 int i;
79
80 for (i = 0; i < dev->num_resources; i++) {
81 struct resource *r = &dev->resource[i];
82
83 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
84 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
85 if (!strcmp(r->name, name))
86 return r;
87 }
88 return NULL;
89}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050090EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/**
93 * platform_get_irq - get an IRQ for a device
94 * @dev: platform device
95 * @name: IRQ name
96 */
97int platform_get_irq_byname(struct platform_device *dev, char *name)
98{
99 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
100
David Vrabel305b3222006-01-19 17:52:27 +0000101 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500103EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/**
106 * platform_add_devices - add a numbers of platform devices
107 * @devs: array of platform devices to add
108 * @num: number of platform devices in array
109 */
110int platform_add_devices(struct platform_device **devs, int num)
111{
112 int i, ret = 0;
113
114 for (i = 0; i < num; i++) {
115 ret = platform_device_register(devs[i]);
116 if (ret) {
117 while (--i >= 0)
118 platform_device_unregister(devs[i]);
119 break;
120 }
121 }
122
123 return ret;
124}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500125EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Russell King37c12e72005-11-05 21:19:33 +0000127struct platform_object {
128 struct platform_device pdev;
129 char name[1];
130};
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
Russell King37c12e72005-11-05 21:19:33 +0000133 * platform_device_put
134 * @pdev: platform device to free
135 *
136 * Free all memory associated with a platform device. This function
137 * must _only_ be externally called in error cases. All other usage
138 * is a bug.
139 */
140void platform_device_put(struct platform_device *pdev)
141{
142 if (pdev)
143 put_device(&pdev->dev);
144}
145EXPORT_SYMBOL_GPL(platform_device_put);
146
147static void platform_device_release(struct device *dev)
148{
149 struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
150
151 kfree(pa->pdev.dev.platform_data);
152 kfree(pa->pdev.resource);
153 kfree(pa);
154}
155
156/**
157 * platform_device_alloc
158 * @name: base name of the device we're adding
159 * @id: instance id
160 *
161 * Create a platform device object which can have other objects attached
162 * to it, and which will have attached objects freed when it is released.
163 */
164struct platform_device *platform_device_alloc(const char *name, unsigned int id)
165{
166 struct platform_object *pa;
167
168 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
169 if (pa) {
170 strcpy(pa->name, name);
171 pa->pdev.name = pa->name;
172 pa->pdev.id = id;
173 device_initialize(&pa->pdev.dev);
174 pa->pdev.dev.release = platform_device_release;
175 }
176
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500177 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000178}
179EXPORT_SYMBOL_GPL(platform_device_alloc);
180
181/**
182 * platform_device_add_resources
183 * @pdev: platform device allocated by platform_device_alloc to add resources to
184 * @res: set of resources that needs to be allocated for the device
185 * @num: number of resources
186 *
187 * Add a copy of the resources to the platform device. The memory
188 * associated with the resources will be freed when the platform
189 * device is released.
190 */
191int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
192{
193 struct resource *r;
194
195 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
196 if (r) {
197 memcpy(r, res, sizeof(struct resource) * num);
198 pdev->resource = r;
199 pdev->num_resources = num;
200 }
201 return r ? 0 : -ENOMEM;
202}
203EXPORT_SYMBOL_GPL(platform_device_add_resources);
204
205/**
206 * platform_device_add_data
207 * @pdev: platform device allocated by platform_device_alloc to add resources to
208 * @data: platform specific data for this platform device
209 * @size: size of platform specific data
210 *
211 * Add a copy of platform specific data to the platform device's platform_data
212 * pointer. The memory associated with the platform data will be freed
213 * when the platform device is released.
214 */
Scott Wood6eefd342006-12-04 14:57:19 -0800215int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000216{
217 void *d;
218
219 d = kmalloc(size, GFP_KERNEL);
220 if (d) {
221 memcpy(d, data, size);
222 pdev->dev.platform_data = d;
223 }
224 return d ? 0 : -ENOMEM;
225}
226EXPORT_SYMBOL_GPL(platform_device_add_data);
227
228/**
229 * platform_device_add - add a platform device to device hierarchy
Martin Waitz67be2dd2005-05-01 08:59:26 -0700230 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *
Russell King37c12e72005-11-05 21:19:33 +0000232 * This is part 2 of platform_device_register(), though may be called
233 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Russell King37c12e72005-11-05 21:19:33 +0000235int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i, ret = 0;
238
239 if (!pdev)
240 return -EINVAL;
241
242 if (!pdev->dev.parent)
243 pdev->dev.parent = &platform_bus;
244
245 pdev->dev.bus = &platform_bus_type;
246
247 if (pdev->id != -1)
248 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
249 else
250 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
251
252 for (i = 0; i < pdev->num_resources; i++) {
253 struct resource *p, *r = &pdev->resource[i];
254
255 if (r->name == NULL)
256 r->name = pdev->dev.bus_id;
257
258 p = r->parent;
259 if (!p) {
260 if (r->flags & IORESOURCE_MEM)
261 p = &iomem_resource;
262 else if (r->flags & IORESOURCE_IO)
263 p = &ioport_resource;
264 }
265
Kumar Galad960bb42005-11-28 10:15:39 -0600266 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_ERR
268 "%s: failed to claim resource %d\n",
269 pdev->dev.bus_id, i);
270 ret = -EBUSY;
271 goto failed;
272 }
273 }
274
275 pr_debug("Registering platform device '%s'. Parent at %s\n",
276 pdev->dev.bus_id, pdev->dev.parent->bus_id);
277
Russell Kinge3915532006-05-06 08:15:26 +0100278 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (ret == 0)
280 return ret;
281
282 failed:
283 while (--i >= 0)
284 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
285 release_resource(&pdev->resource[i]);
286 return ret;
287}
Russell King37c12e72005-11-05 21:19:33 +0000288EXPORT_SYMBOL_GPL(platform_device_add);
289
290/**
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500291 * platform_device_del - remove a platform-level device
292 * @pdev: platform device we're removing
293 *
294 * Note that this function will also release all memory- and port-based
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200295 * resources owned by the device (@dev->resource). This function
296 * must _only_ be externally called in error cases. All other usage
297 * is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500298 */
299void platform_device_del(struct platform_device *pdev)
300{
301 int i;
302
303 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200304 device_del(&pdev->dev);
305
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500306 for (i = 0; i < pdev->num_resources; i++) {
307 struct resource *r = &pdev->resource[i];
308 if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
309 release_resource(r);
310 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500311 }
312}
313EXPORT_SYMBOL_GPL(platform_device_del);
314
315/**
Russell King37c12e72005-11-05 21:19:33 +0000316 * platform_device_register - add a platform-level device
317 * @pdev: platform device we're adding
318 *
319 */
320int platform_device_register(struct platform_device * pdev)
321{
322 device_initialize(&pdev->dev);
323 return platform_device_add(pdev);
324}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500325EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327/**
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500328 * platform_device_unregister - unregister a platform-level device
329 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
Uwe Zeisberger80682fa2006-03-22 00:21:33 +0100331 * Unregistration is done in 2 steps. First we release all resources
Jean Delvare2d7b5a72005-12-27 19:45:58 +0100332 * and remove it from the subsystem, then we drop reference count by
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500333 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335void platform_device_unregister(struct platform_device * pdev)
336{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500337 platform_device_del(pdev);
338 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500340EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/**
343 * platform_device_register_simple
344 * @name: base name of the device we're adding
345 * @id: instance id
346 * @res: set of resources that needs to be allocated for the device
347 * @num: number of resources
348 *
349 * This function creates a simple platform device that requires minimal
350 * resource and memory management. Canned release function freeing
351 * memory allocated for the device allows drivers using such devices
Márton Németh01afd802007-05-09 07:48:05 +0200352 * to be unloaded without waiting for the last reference to the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * to be dropped.
David Brownell49a4ec12007-05-08 00:29:39 -0700354 *
355 * This interface is primarily intended for use with legacy drivers
356 * which probe hardware directly. Because such drivers create sysfs
357 * device nodes themselves, rather than letting system infrastructure
358 * handle such device enumeration tasks, they don't fully conform to
359 * the Linux driver model. In particular, when such drivers are built
360 * as modules, they can't be "hotplugged".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
362struct platform_device *platform_device_register_simple(char *name, unsigned int id,
363 struct resource *res, unsigned int num)
364{
Russell King37c12e72005-11-05 21:19:33 +0000365 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int retval;
367
Russell King37c12e72005-11-05 21:19:33 +0000368 pdev = platform_device_alloc(name, id);
369 if (!pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 retval = -ENOMEM;
371 goto error;
372 }
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (num) {
Russell King37c12e72005-11-05 21:19:33 +0000375 retval = platform_device_add_resources(pdev, res, num);
376 if (retval)
377 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Russell King37c12e72005-11-05 21:19:33 +0000380 retval = platform_device_add(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (retval)
382 goto error;
383
Russell King37c12e72005-11-05 21:19:33 +0000384 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386error:
Russell King37c12e72005-11-05 21:19:33 +0000387 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return ERR_PTR(retval);
389}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500390EXPORT_SYMBOL_GPL(platform_device_register_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Russell King00d3dcd2005-11-09 17:23:39 +0000392static int platform_drv_probe(struct device *_dev)
393{
394 struct platform_driver *drv = to_platform_driver(_dev->driver);
395 struct platform_device *dev = to_platform_device(_dev);
396
397 return drv->probe(dev);
398}
399
David Brownellc67334f2006-11-16 23:28:47 -0800400static int platform_drv_probe_fail(struct device *_dev)
401{
402 return -ENXIO;
403}
404
Russell King00d3dcd2005-11-09 17:23:39 +0000405static int platform_drv_remove(struct device *_dev)
406{
407 struct platform_driver *drv = to_platform_driver(_dev->driver);
408 struct platform_device *dev = to_platform_device(_dev);
409
410 return drv->remove(dev);
411}
412
413static void platform_drv_shutdown(struct device *_dev)
414{
415 struct platform_driver *drv = to_platform_driver(_dev->driver);
416 struct platform_device *dev = to_platform_device(_dev);
417
418 drv->shutdown(dev);
419}
420
421static int platform_drv_suspend(struct device *_dev, pm_message_t state)
422{
423 struct platform_driver *drv = to_platform_driver(_dev->driver);
424 struct platform_device *dev = to_platform_device(_dev);
425
426 return drv->suspend(dev, state);
427}
428
429static int platform_drv_resume(struct device *_dev)
430{
431 struct platform_driver *drv = to_platform_driver(_dev->driver);
432 struct platform_device *dev = to_platform_device(_dev);
433
434 return drv->resume(dev);
435}
436
437/**
438 * platform_driver_register
439 * @drv: platform driver structure
440 */
441int platform_driver_register(struct platform_driver *drv)
442{
443 drv->driver.bus = &platform_bus_type;
444 if (drv->probe)
445 drv->driver.probe = platform_drv_probe;
446 if (drv->remove)
447 drv->driver.remove = platform_drv_remove;
448 if (drv->shutdown)
449 drv->driver.shutdown = platform_drv_shutdown;
450 if (drv->suspend)
451 drv->driver.suspend = platform_drv_suspend;
452 if (drv->resume)
453 drv->driver.resume = platform_drv_resume;
454 return driver_register(&drv->driver);
455}
456EXPORT_SYMBOL_GPL(platform_driver_register);
457
458/**
459 * platform_driver_unregister
460 * @drv: platform driver structure
461 */
462void platform_driver_unregister(struct platform_driver *drv)
463{
464 driver_unregister(&drv->driver);
465}
466EXPORT_SYMBOL_GPL(platform_driver_unregister);
467
David Brownellc67334f2006-11-16 23:28:47 -0800468/**
469 * platform_driver_probe - register driver for non-hotpluggable device
470 * @drv: platform driver structure
471 * @probe: the driver probe routine, probably from an __init section
472 *
473 * Use this instead of platform_driver_register() when you know the device
474 * is not hotpluggable and has already been registered, and you want to
475 * remove its run-once probe() infrastructure from memory after the driver
476 * has bound to the device.
477 *
478 * One typical use for this would be with drivers for controllers integrated
479 * into system-on-chip processors, where the controller devices have been
480 * configured as part of board setup.
481 *
482 * Returns zero if the driver registered and bound to a device, else returns
483 * a negative error code and with the driver not registered.
484 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800485int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800486 int (*probe)(struct platform_device *))
487{
488 int retval, code;
489
490 /* temporary section violation during probe() */
491 drv->probe = probe;
492 retval = code = platform_driver_register(drv);
493
494 /* Fixup that section violation, being paranoid about code scanning
495 * the list of drivers in order to probe new devices. Check to see
496 * if the probe was successful, and make sure any forced probes of
497 * new devices fail.
498 */
499 spin_lock(&platform_bus_type.klist_drivers.k_lock);
500 drv->probe = NULL;
501 if (code == 0 && list_empty(&drv->driver.klist_devices.k_list))
502 retval = -ENODEV;
503 drv->driver.probe = platform_drv_probe_fail;
504 spin_unlock(&platform_bus_type.klist_drivers.k_lock);
505
506 if (code != retval)
507 platform_driver_unregister(drv);
508 return retval;
509}
510EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
David Brownella0245f72006-05-29 10:37:33 -0700512/* modalias support enables more hands-off userspace setup:
513 * (a) environment variable lets new-style hotplug events work once system is
514 * fully running: "modprobe $MODALIAS"
515 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
516 * mishandled before system is fully running: "modprobe $(cat modalias)"
517 */
518static ssize_t
519modalias_show(struct device *dev, struct device_attribute *a, char *buf)
520{
521 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200522 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700523
524 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
525}
526
527static struct device_attribute platform_dev_attrs[] = {
528 __ATTR_RO(modalias),
529 __ATTR_NULL,
530};
531
Kay Sievers7eff2e72007-08-14 15:15:12 +0200532static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700533{
534 struct platform_device *pdev = to_platform_device(dev);
535
Kay Sievers7eff2e72007-08-14 15:15:12 +0200536 add_uevent_var(env, "MODALIAS=platform:%s", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700537 return 0;
538}
539
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/**
542 * platform_match - bind platform device to platform driver.
543 * @dev: device.
544 * @drv: driver.
545 *
546 * Platform device IDs are assumed to be encoded like this:
547 * "<name><instance>", where <name> is a short description of the
548 * type of device, like "pci" or "floppy", and <instance> is the
549 * enumerated instance of the device, like '0' or '42'.
550 * Driver IDs are simply "<name>".
551 * So, extract the <name> from the platform_device structure,
552 * and compare it against the name of the driver. Return whether
553 * they match or not.
554 */
555
556static int platform_match(struct device * dev, struct device_driver * drv)
557{
558 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
559
560 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
561}
562
David Brownell386415d2006-09-03 13:16:45 -0700563static int platform_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int ret = 0;
566
Russell King9480e302005-10-28 09:52:56 -0700567 if (dev->driver && dev->driver->suspend)
David Brownell386415d2006-09-03 13:16:45 -0700568 ret = dev->driver->suspend(dev, mesg);
569
570 return ret;
571}
572
573static int platform_suspend_late(struct device *dev, pm_message_t mesg)
574{
575 struct platform_driver *drv = to_platform_driver(dev->driver);
576 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
577 int ret = 0;
578
579 if (dev->driver && drv->suspend_late)
580 ret = drv->suspend_late(pdev, mesg);
581
582 return ret;
583}
584
585static int platform_resume_early(struct device *dev)
586{
587 struct platform_driver *drv = to_platform_driver(dev->driver);
588 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
589 int ret = 0;
590
591 if (dev->driver && drv->resume_early)
592 ret = drv->resume_early(pdev);
Russell King9480e302005-10-28 09:52:56 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return ret;
595}
596
597static int platform_resume(struct device * dev)
598{
599 int ret = 0;
600
Russell King9480e302005-10-28 09:52:56 -0700601 if (dev->driver && dev->driver->resume)
602 ret = dev->driver->resume(dev);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return ret;
605}
606
607struct bus_type platform_bus_type = {
608 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700609 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700611 .uevent = platform_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 .suspend = platform_suspend,
David Brownell386415d2006-09-03 13:16:45 -0700613 .suspend_late = platform_suspend_late,
614 .resume_early = platform_resume_early,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 .resume = platform_resume,
616};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500617EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619int __init platform_bus_init(void)
620{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100621 int error;
622
623 error = device_register(&platform_bus);
624 if (error)
625 return error;
626 error = bus_register(&platform_bus_type);
627 if (error)
628 device_unregister(&platform_bus);
629 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
633u64 dma_get_required_mask(struct device *dev)
634{
635 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
636 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
637 u64 mask;
638
639 if (!high_totalram) {
640 /* convert to mask just covering totalram */
641 low_totalram = (1 << (fls(low_totalram) - 1));
642 low_totalram += low_totalram - 1;
643 mask = low_totalram;
644 } else {
645 high_totalram = (1 << (fls(high_totalram) - 1));
646 high_totalram += high_totalram - 1;
647 mask = (((u64)high_totalram) << 32) + 0xffffffff;
648 }
649 return mask & *dev->dma_mask;
650}
651EXPORT_SYMBOL_GPL(dma_get_required_mask);
652#endif