platform: prefix MODALIAS with "platform:"
[linux-2.6/zen-sources.git] / drivers / base / platform.c
blob9bfc434d132734c759fcfabac43cc11b3835f994
1 /*
2 * platform.c - platform 'pseudo' bus for legacy devices
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
9 * Please see Documentation/driver-model/platform.txt for more
10 * information.
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/bootmem.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
21 #include "base.h"
23 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
25 struct device platform_bus = {
26 .bus_id = "platform",
28 EXPORT_SYMBOL_GPL(platform_bus);
30 /**
31 * platform_get_resource - get a resource for a device
32 * @dev: platform device
33 * @type: resource type
34 * @num: resource index
36 struct resource *
37 platform_get_resource(struct platform_device *dev, unsigned int type,
38 unsigned int num)
40 int i;
42 for (i = 0; i < dev->num_resources; i++) {
43 struct resource *r = &dev->resource[i];
45 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
46 IORESOURCE_IRQ|IORESOURCE_DMA))
47 == type)
48 if (num-- == 0)
49 return r;
51 return NULL;
53 EXPORT_SYMBOL_GPL(platform_get_resource);
55 /**
56 * platform_get_irq - get an IRQ for a device
57 * @dev: platform device
58 * @num: IRQ number index
60 int platform_get_irq(struct platform_device *dev, unsigned int num)
62 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
64 return r ? r->start : -ENXIO;
66 EXPORT_SYMBOL_GPL(platform_get_irq);
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
74 struct resource *
75 platform_get_resource_byname(struct platform_device *dev, unsigned int type,
76 char *name)
78 int i;
80 for (i = 0; i < dev->num_resources; i++) {
81 struct resource *r = &dev->resource[i];
83 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
84 IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
85 if (!strcmp(r->name, name))
86 return r;
88 return NULL;
90 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
92 /**
93 * platform_get_irq - get an IRQ for a device
94 * @dev: platform device
95 * @name: IRQ name
97 int platform_get_irq_byname(struct platform_device *dev, char *name)
99 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
101 return r ? r->start : -ENXIO;
103 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
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
110 int platform_add_devices(struct platform_device **devs, int num)
112 int i, ret = 0;
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;
123 return ret;
125 EXPORT_SYMBOL_GPL(platform_add_devices);
127 struct platform_object {
128 struct platform_device pdev;
129 char name[1];
133 * platform_device_put
134 * @pdev: platform device to free
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.
140 void platform_device_put(struct platform_device *pdev)
142 if (pdev)
143 put_device(&pdev->dev);
145 EXPORT_SYMBOL_GPL(platform_device_put);
147 static void platform_device_release(struct device *dev)
149 struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
151 kfree(pa->pdev.dev.platform_data);
152 kfree(pa->pdev.resource);
153 kfree(pa);
157 * platform_device_alloc
158 * @name: base name of the device we're adding
159 * @id: instance id
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.
164 struct platform_device *platform_device_alloc(const char *name, unsigned int id)
166 struct platform_object *pa;
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;
177 return pa ? &pa->pdev : NULL;
179 EXPORT_SYMBOL_GPL(platform_device_alloc);
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
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.
191 int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
193 struct resource *r;
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;
201 return r ? 0 : -ENOMEM;
203 EXPORT_SYMBOL_GPL(platform_device_add_resources);
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
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.
215 int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size)
217 void *d;
219 d = kmalloc(size, GFP_KERNEL);
220 if (d) {
221 memcpy(d, data, size);
222 pdev->dev.platform_data = d;
224 return d ? 0 : -ENOMEM;
226 EXPORT_SYMBOL_GPL(platform_device_add_data);
229 * platform_device_add - add a platform device to device hierarchy
230 * @pdev: platform device we're adding
232 * This is part 2 of platform_device_register(), though may be called
233 * separately _iff_ pdev was allocated by platform_device_alloc().
235 int platform_device_add(struct platform_device *pdev)
237 int i, ret = 0;
239 if (!pdev)
240 return -EINVAL;
242 if (!pdev->dev.parent)
243 pdev->dev.parent = &platform_bus;
245 pdev->dev.bus = &platform_bus_type;
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);
252 for (i = 0; i < pdev->num_resources; i++) {
253 struct resource *p, *r = &pdev->resource[i];
255 if (r->name == NULL)
256 r->name = pdev->dev.bus_id;
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;
266 if (p && insert_resource(p, r)) {
267 printk(KERN_ERR
268 "%s: failed to claim resource %d\n",
269 pdev->dev.bus_id, i);
270 ret = -EBUSY;
271 goto failed;
275 pr_debug("Registering platform device '%s'. Parent at %s\n",
276 pdev->dev.bus_id, pdev->dev.parent->bus_id);
278 ret = device_add(&pdev->dev);
279 if (ret == 0)
280 return ret;
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;
288 EXPORT_SYMBOL_GPL(platform_device_add);
291 * platform_device_del - remove a platform-level device
292 * @pdev: platform device we're removing
294 * Note that this function will also release all memory- and port-based
295 * 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.
299 void platform_device_del(struct platform_device *pdev)
301 int i;
303 if (pdev) {
304 device_del(&pdev->dev);
306 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);
313 EXPORT_SYMBOL_GPL(platform_device_del);
316 * platform_device_register - add a platform-level device
317 * @pdev: platform device we're adding
320 int platform_device_register(struct platform_device * pdev)
322 device_initialize(&pdev->dev);
323 return platform_device_add(pdev);
325 EXPORT_SYMBOL_GPL(platform_device_register);
328 * platform_device_unregister - unregister a platform-level device
329 * @pdev: platform device we're unregistering
331 * Unregistration is done in 2 steps. First we release all resources
332 * and remove it from the subsystem, then we drop reference count by
333 * calling platform_device_put().
335 void platform_device_unregister(struct platform_device * pdev)
337 platform_device_del(pdev);
338 platform_device_put(pdev);
340 EXPORT_SYMBOL_GPL(platform_device_unregister);
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
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
352 * to be unloaded without waiting for the last reference to the device
353 * to be dropped.
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".
362 struct platform_device *platform_device_register_simple(char *name, unsigned int id,
363 struct resource *res, unsigned int num)
365 struct platform_device *pdev;
366 int retval;
368 pdev = platform_device_alloc(name, id);
369 if (!pdev) {
370 retval = -ENOMEM;
371 goto error;
374 if (num) {
375 retval = platform_device_add_resources(pdev, res, num);
376 if (retval)
377 goto error;
380 retval = platform_device_add(pdev);
381 if (retval)
382 goto error;
384 return pdev;
386 error:
387 platform_device_put(pdev);
388 return ERR_PTR(retval);
390 EXPORT_SYMBOL_GPL(platform_device_register_simple);
392 static int platform_drv_probe(struct device *_dev)
394 struct platform_driver *drv = to_platform_driver(_dev->driver);
395 struct platform_device *dev = to_platform_device(_dev);
397 return drv->probe(dev);
400 static int platform_drv_probe_fail(struct device *_dev)
402 return -ENXIO;
405 static int platform_drv_remove(struct device *_dev)
407 struct platform_driver *drv = to_platform_driver(_dev->driver);
408 struct platform_device *dev = to_platform_device(_dev);
410 return drv->remove(dev);
413 static void platform_drv_shutdown(struct device *_dev)
415 struct platform_driver *drv = to_platform_driver(_dev->driver);
416 struct platform_device *dev = to_platform_device(_dev);
418 drv->shutdown(dev);
421 static int platform_drv_suspend(struct device *_dev, pm_message_t state)
423 struct platform_driver *drv = to_platform_driver(_dev->driver);
424 struct platform_device *dev = to_platform_device(_dev);
426 return drv->suspend(dev, state);
429 static int platform_drv_resume(struct device *_dev)
431 struct platform_driver *drv = to_platform_driver(_dev->driver);
432 struct platform_device *dev = to_platform_device(_dev);
434 return drv->resume(dev);
438 * platform_driver_register
439 * @drv: platform driver structure
441 int platform_driver_register(struct platform_driver *drv)
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);
456 EXPORT_SYMBOL_GPL(platform_driver_register);
459 * platform_driver_unregister
460 * @drv: platform driver structure
462 void platform_driver_unregister(struct platform_driver *drv)
464 driver_unregister(&drv->driver);
466 EXPORT_SYMBOL_GPL(platform_driver_unregister);
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
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.
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.
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.
485 int __init_or_module platform_driver_probe(struct platform_driver *drv,
486 int (*probe)(struct platform_device *))
488 int retval, code;
490 /* temporary section violation during probe() */
491 drv->probe = probe;
492 retval = code = platform_driver_register(drv);
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.
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);
506 if (code != retval)
507 platform_driver_unregister(drv);
508 return retval;
510 EXPORT_SYMBOL_GPL(platform_driver_probe);
512 /* 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)"
518 static ssize_t
519 modalias_show(struct device *dev, struct device_attribute *a, char *buf)
521 struct platform_device *pdev = to_platform_device(dev);
522 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
524 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
527 static struct device_attribute platform_dev_attrs[] = {
528 __ATTR_RO(modalias),
529 __ATTR_NULL,
532 static int platform_uevent(struct device *dev, char **envp, int num_envp,
533 char *buffer, int buffer_size)
535 struct platform_device *pdev = to_platform_device(dev);
537 envp[0] = buffer;
538 snprintf(buffer, buffer_size, "MODALIAS=platform:%s", pdev->name);
539 return 0;
544 * platform_match - bind platform device to platform driver.
545 * @dev: device.
546 * @drv: driver.
548 * Platform device IDs are assumed to be encoded like this:
549 * "<name><instance>", where <name> is a short description of the
550 * type of device, like "pci" or "floppy", and <instance> is the
551 * enumerated instance of the device, like '0' or '42'.
552 * Driver IDs are simply "<name>".
553 * So, extract the <name> from the platform_device structure,
554 * and compare it against the name of the driver. Return whether
555 * they match or not.
558 static int platform_match(struct device * dev, struct device_driver * drv)
560 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
562 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
565 static int platform_suspend(struct device *dev, pm_message_t mesg)
567 int ret = 0;
569 if (dev->driver && dev->driver->suspend)
570 ret = dev->driver->suspend(dev, mesg);
572 return ret;
575 static int platform_suspend_late(struct device *dev, pm_message_t mesg)
577 struct platform_driver *drv = to_platform_driver(dev->driver);
578 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
579 int ret = 0;
581 if (dev->driver && drv->suspend_late)
582 ret = drv->suspend_late(pdev, mesg);
584 return ret;
587 static int platform_resume_early(struct device *dev)
589 struct platform_driver *drv = to_platform_driver(dev->driver);
590 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
591 int ret = 0;
593 if (dev->driver && drv->resume_early)
594 ret = drv->resume_early(pdev);
596 return ret;
599 static int platform_resume(struct device * dev)
601 int ret = 0;
603 if (dev->driver && dev->driver->resume)
604 ret = dev->driver->resume(dev);
606 return ret;
609 struct bus_type platform_bus_type = {
610 .name = "platform",
611 .dev_attrs = platform_dev_attrs,
612 .match = platform_match,
613 .uevent = platform_uevent,
614 .suspend = platform_suspend,
615 .suspend_late = platform_suspend_late,
616 .resume_early = platform_resume_early,
617 .resume = platform_resume,
619 EXPORT_SYMBOL_GPL(platform_bus_type);
621 int __init platform_bus_init(void)
623 int error;
625 error = device_register(&platform_bus);
626 if (error)
627 return error;
628 error = bus_register(&platform_bus_type);
629 if (error)
630 device_unregister(&platform_bus);
631 return error;
634 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
635 u64 dma_get_required_mask(struct device *dev)
637 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
638 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
639 u64 mask;
641 if (!high_totalram) {
642 /* convert to mask just covering totalram */
643 low_totalram = (1 << (fls(low_totalram) - 1));
644 low_totalram += low_totalram - 1;
645 mask = low_totalram;
646 } else {
647 high_totalram = (1 << (fls(high_totalram) - 1));
648 high_totalram += high_totalram - 1;
649 mask = (((u64)high_totalram) << 32) + 0xffffffff;
651 return mask & *dev->dma_mask;
653 EXPORT_SYMBOL_GPL(dma_get_required_mask);
654 #endif