Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * platform.c - platform 'pseudo' bus for legacy devices |
| 3 | * |
| 4 | * Copyright (c) 2002-3 Patrick Mochel |
| 5 | * Copyright (c) 2002-3 Open Source Development Labs |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | * Please see Documentation/driver-model/platform.txt for more |
| 10 | * information. |
| 11 | */ |
| 12 | |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 21 | #include "base.h" |
| 22 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 23 | #define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver)) |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | struct device platform_bus = { |
| 26 | .bus_id = "platform", |
| 27 | }; |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 28 | EXPORT_SYMBOL_GPL(platform_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * platform_get_resource - get a resource for a device |
| 32 | * @dev: platform device |
| 33 | * @type: resource type |
| 34 | * @num: resource index |
| 35 | */ |
| 36 | struct resource * |
| 37 | platform_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 Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 53 | EXPORT_SYMBOL_GPL(platform_get_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * platform_get_irq - get an IRQ for a device |
| 57 | * @dev: platform device |
| 58 | * @num: IRQ number index |
| 59 | */ |
| 60 | int platform_get_irq(struct platform_device *dev, unsigned int num) |
| 61 | { |
| 62 | struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); |
| 63 | |
David Vrabel | 305b322 | 2006-01-19 17:52:27 +0000 | [diff] [blame] | 64 | return r ? r->start : -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 66 | EXPORT_SYMBOL_GPL(platform_get_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 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 | */ |
| 74 | struct resource * |
| 75 | platform_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 Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 90 | EXPORT_SYMBOL_GPL(platform_get_resource_byname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * platform_get_irq - get an IRQ for a device |
| 94 | * @dev: platform device |
| 95 | * @name: IRQ name |
| 96 | */ |
| 97 | int 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 Vrabel | 305b322 | 2006-01-19 17:52:27 +0000 | [diff] [blame] | 101 | return r ? r->start : -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 103 | EXPORT_SYMBOL_GPL(platform_get_irq_byname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 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 | */ |
| 110 | int 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 Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 125 | EXPORT_SYMBOL_GPL(platform_add_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 127 | struct platform_object { |
| 128 | struct platform_device pdev; |
| 129 | char name[1]; |
| 130 | }; |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | /** |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 133 | * 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 | */ |
| 140 | void platform_device_put(struct platform_device *pdev) |
| 141 | { |
| 142 | if (pdev) |
| 143 | put_device(&pdev->dev); |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(platform_device_put); |
| 146 | |
| 147 | static 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. |
David Brownell | 49a4ec1 | 2007-05-08 00:29:39 -0700 | [diff] [blame] | 163 | * |
| 164 | * This device will be marked as not supporting hotpluggable drivers; no |
| 165 | * device add/remove uevents will be generated. In the unusual case that |
| 166 | * the device isn't being dynamically allocated as a legacy "probe the |
| 167 | * hardware" driver, infrastructure code should reverse this marking. |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 168 | */ |
| 169 | struct platform_device *platform_device_alloc(const char *name, unsigned int id) |
| 170 | { |
| 171 | struct platform_object *pa; |
| 172 | |
| 173 | pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL); |
| 174 | if (pa) { |
| 175 | strcpy(pa->name, name); |
| 176 | pa->pdev.name = pa->name; |
| 177 | pa->pdev.id = id; |
| 178 | device_initialize(&pa->pdev.dev); |
| 179 | pa->pdev.dev.release = platform_device_release; |
David Brownell | 49a4ec1 | 2007-05-08 00:29:39 -0700 | [diff] [blame] | 180 | |
| 181 | /* prevent hotplug "modprobe $(MODALIAS)" from causing trouble in |
| 182 | * legacy probe-the-hardware drivers, which don't properly split |
| 183 | * out device enumeration logic from drivers. |
| 184 | */ |
| 185 | pa->pdev.dev.uevent_suppress = 1; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 188 | return pa ? &pa->pdev : NULL; |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 189 | } |
| 190 | EXPORT_SYMBOL_GPL(platform_device_alloc); |
| 191 | |
| 192 | /** |
| 193 | * platform_device_add_resources |
| 194 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
| 195 | * @res: set of resources that needs to be allocated for the device |
| 196 | * @num: number of resources |
| 197 | * |
| 198 | * Add a copy of the resources to the platform device. The memory |
| 199 | * associated with the resources will be freed when the platform |
| 200 | * device is released. |
| 201 | */ |
| 202 | int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num) |
| 203 | { |
| 204 | struct resource *r; |
| 205 | |
| 206 | r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL); |
| 207 | if (r) { |
| 208 | memcpy(r, res, sizeof(struct resource) * num); |
| 209 | pdev->resource = r; |
| 210 | pdev->num_resources = num; |
| 211 | } |
| 212 | return r ? 0 : -ENOMEM; |
| 213 | } |
| 214 | EXPORT_SYMBOL_GPL(platform_device_add_resources); |
| 215 | |
| 216 | /** |
| 217 | * platform_device_add_data |
| 218 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
| 219 | * @data: platform specific data for this platform device |
| 220 | * @size: size of platform specific data |
| 221 | * |
| 222 | * Add a copy of platform specific data to the platform device's platform_data |
| 223 | * pointer. The memory associated with the platform data will be freed |
| 224 | * when the platform device is released. |
| 225 | */ |
Scott Wood | 6eefd34 | 2006-12-04 14:57:19 -0800 | [diff] [blame] | 226 | int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size) |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 227 | { |
| 228 | void *d; |
| 229 | |
| 230 | d = kmalloc(size, GFP_KERNEL); |
| 231 | if (d) { |
| 232 | memcpy(d, data, size); |
| 233 | pdev->dev.platform_data = d; |
| 234 | } |
| 235 | return d ? 0 : -ENOMEM; |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(platform_device_add_data); |
| 238 | |
| 239 | /** |
| 240 | * platform_device_add - add a platform device to device hierarchy |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 241 | * @pdev: platform device we're adding |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | * |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 243 | * This is part 2 of platform_device_register(), though may be called |
| 244 | * separately _iff_ pdev was allocated by platform_device_alloc(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | */ |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 246 | int platform_device_add(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | int i, ret = 0; |
| 249 | |
| 250 | if (!pdev) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | if (!pdev->dev.parent) |
| 254 | pdev->dev.parent = &platform_bus; |
| 255 | |
| 256 | pdev->dev.bus = &platform_bus_type; |
| 257 | |
| 258 | if (pdev->id != -1) |
| 259 | snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id); |
| 260 | else |
| 261 | strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE); |
| 262 | |
| 263 | for (i = 0; i < pdev->num_resources; i++) { |
| 264 | struct resource *p, *r = &pdev->resource[i]; |
| 265 | |
| 266 | if (r->name == NULL) |
| 267 | r->name = pdev->dev.bus_id; |
| 268 | |
| 269 | p = r->parent; |
| 270 | if (!p) { |
| 271 | if (r->flags & IORESOURCE_MEM) |
| 272 | p = &iomem_resource; |
| 273 | else if (r->flags & IORESOURCE_IO) |
| 274 | p = &ioport_resource; |
| 275 | } |
| 276 | |
Kumar Gala | d960bb4 | 2005-11-28 10:15:39 -0600 | [diff] [blame] | 277 | if (p && insert_resource(p, r)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | printk(KERN_ERR |
| 279 | "%s: failed to claim resource %d\n", |
| 280 | pdev->dev.bus_id, i); |
| 281 | ret = -EBUSY; |
| 282 | goto failed; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | pr_debug("Registering platform device '%s'. Parent at %s\n", |
| 287 | pdev->dev.bus_id, pdev->dev.parent->bus_id); |
| 288 | |
Russell King | e391553 | 2006-05-06 08:15:26 +0100 | [diff] [blame] | 289 | ret = device_add(&pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | if (ret == 0) |
| 291 | return ret; |
| 292 | |
| 293 | failed: |
| 294 | while (--i >= 0) |
| 295 | if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO)) |
| 296 | release_resource(&pdev->resource[i]); |
| 297 | return ret; |
| 298 | } |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 299 | EXPORT_SYMBOL_GPL(platform_device_add); |
| 300 | |
| 301 | /** |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 302 | * platform_device_del - remove a platform-level device |
| 303 | * @pdev: platform device we're removing |
| 304 | * |
| 305 | * Note that this function will also release all memory- and port-based |
Jean Delvare | dc4c15d | 2007-05-02 20:55:54 +0200 | [diff] [blame] | 306 | * resources owned by the device (@dev->resource). This function |
| 307 | * must _only_ be externally called in error cases. All other usage |
| 308 | * is a bug. |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 309 | */ |
| 310 | void platform_device_del(struct platform_device *pdev) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | if (pdev) { |
Jean Delvare | dc4c15d | 2007-05-02 20:55:54 +0200 | [diff] [blame] | 315 | device_del(&pdev->dev); |
| 316 | |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 317 | for (i = 0; i < pdev->num_resources; i++) { |
| 318 | struct resource *r = &pdev->resource[i]; |
| 319 | if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO)) |
| 320 | release_resource(r); |
| 321 | } |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(platform_device_del); |
| 325 | |
| 326 | /** |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 327 | * platform_device_register - add a platform-level device |
| 328 | * @pdev: platform device we're adding |
| 329 | * |
| 330 | */ |
| 331 | int platform_device_register(struct platform_device * pdev) |
| 332 | { |
| 333 | device_initialize(&pdev->dev); |
| 334 | return platform_device_add(pdev); |
| 335 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 336 | EXPORT_SYMBOL_GPL(platform_device_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
| 338 | /** |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 339 | * platform_device_unregister - unregister a platform-level device |
| 340 | * @pdev: platform device we're unregistering |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | * |
Uwe Zeisberger | 80682fa | 2006-03-22 00:21:33 +0100 | [diff] [blame] | 342 | * Unregistration is done in 2 steps. First we release all resources |
Jean Delvare | 2d7b5a7 | 2005-12-27 19:45:58 +0100 | [diff] [blame] | 343 | * and remove it from the subsystem, then we drop reference count by |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 344 | * calling platform_device_put(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | */ |
| 346 | void platform_device_unregister(struct platform_device * pdev) |
| 347 | { |
Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 348 | platform_device_del(pdev); |
| 349 | platform_device_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 351 | EXPORT_SYMBOL_GPL(platform_device_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | /** |
| 354 | * platform_device_register_simple |
| 355 | * @name: base name of the device we're adding |
| 356 | * @id: instance id |
| 357 | * @res: set of resources that needs to be allocated for the device |
| 358 | * @num: number of resources |
| 359 | * |
| 360 | * This function creates a simple platform device that requires minimal |
| 361 | * resource and memory management. Canned release function freeing |
| 362 | * memory allocated for the device allows drivers using such devices |
Márton Németh | 01afd80 | 2007-05-09 07:48:05 +0200 | [diff] [blame] | 363 | * to be unloaded without waiting for the last reference to the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | * to be dropped. |
David Brownell | 49a4ec1 | 2007-05-08 00:29:39 -0700 | [diff] [blame] | 365 | * |
| 366 | * This interface is primarily intended for use with legacy drivers |
| 367 | * which probe hardware directly. Because such drivers create sysfs |
| 368 | * device nodes themselves, rather than letting system infrastructure |
| 369 | * handle such device enumeration tasks, they don't fully conform to |
| 370 | * the Linux driver model. In particular, when such drivers are built |
| 371 | * as modules, they can't be "hotplugged". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | */ |
| 373 | struct platform_device *platform_device_register_simple(char *name, unsigned int id, |
| 374 | struct resource *res, unsigned int num) |
| 375 | { |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 376 | struct platform_device *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | int retval; |
| 378 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 379 | pdev = platform_device_alloc(name, id); |
| 380 | if (!pdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | retval = -ENOMEM; |
| 382 | goto error; |
| 383 | } |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (num) { |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 386 | retval = platform_device_add_resources(pdev, res, num); |
| 387 | if (retval) |
| 388 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 391 | retval = platform_device_add(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (retval) |
| 393 | goto error; |
| 394 | |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 395 | return pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | error: |
Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 398 | platform_device_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return ERR_PTR(retval); |
| 400 | } |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 401 | EXPORT_SYMBOL_GPL(platform_device_register_simple); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 403 | static int platform_drv_probe(struct device *_dev) |
| 404 | { |
| 405 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 406 | struct platform_device *dev = to_platform_device(_dev); |
| 407 | |
| 408 | return drv->probe(dev); |
| 409 | } |
| 410 | |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 411 | static int platform_drv_probe_fail(struct device *_dev) |
| 412 | { |
| 413 | return -ENXIO; |
| 414 | } |
| 415 | |
Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 416 | static int platform_drv_remove(struct device *_dev) |
| 417 | { |
| 418 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 419 | struct platform_device *dev = to_platform_device(_dev); |
| 420 | |
| 421 | return drv->remove(dev); |
| 422 | } |
| 423 | |
| 424 | static void platform_drv_shutdown(struct device *_dev) |
| 425 | { |
| 426 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 427 | struct platform_device *dev = to_platform_device(_dev); |
| 428 | |
| 429 | drv->shutdown(dev); |
| 430 | } |
| 431 | |
| 432 | static int platform_drv_suspend(struct device *_dev, pm_message_t state) |
| 433 | { |
| 434 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 435 | struct platform_device *dev = to_platform_device(_dev); |
| 436 | |
| 437 | return drv->suspend(dev, state); |
| 438 | } |
| 439 | |
| 440 | static int platform_drv_resume(struct device *_dev) |
| 441 | { |
| 442 | struct platform_driver *drv = to_platform_driver(_dev->driver); |
| 443 | struct platform_device *dev = to_platform_device(_dev); |
| 444 | |
| 445 | return drv->resume(dev); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * platform_driver_register |
| 450 | * @drv: platform driver structure |
| 451 | */ |
| 452 | int platform_driver_register(struct platform_driver *drv) |
| 453 | { |
| 454 | drv->driver.bus = &platform_bus_type; |
| 455 | if (drv->probe) |
| 456 | drv->driver.probe = platform_drv_probe; |
| 457 | if (drv->remove) |
| 458 | drv->driver.remove = platform_drv_remove; |
| 459 | if (drv->shutdown) |
| 460 | drv->driver.shutdown = platform_drv_shutdown; |
| 461 | if (drv->suspend) |
| 462 | drv->driver.suspend = platform_drv_suspend; |
| 463 | if (drv->resume) |
| 464 | drv->driver.resume = platform_drv_resume; |
| 465 | return driver_register(&drv->driver); |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(platform_driver_register); |
| 468 | |
| 469 | /** |
| 470 | * platform_driver_unregister |
| 471 | * @drv: platform driver structure |
| 472 | */ |
| 473 | void platform_driver_unregister(struct platform_driver *drv) |
| 474 | { |
| 475 | driver_unregister(&drv->driver); |
| 476 | } |
| 477 | EXPORT_SYMBOL_GPL(platform_driver_unregister); |
| 478 | |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 479 | /** |
| 480 | * platform_driver_probe - register driver for non-hotpluggable device |
| 481 | * @drv: platform driver structure |
| 482 | * @probe: the driver probe routine, probably from an __init section |
| 483 | * |
| 484 | * Use this instead of platform_driver_register() when you know the device |
| 485 | * is not hotpluggable and has already been registered, and you want to |
| 486 | * remove its run-once probe() infrastructure from memory after the driver |
| 487 | * has bound to the device. |
| 488 | * |
| 489 | * One typical use for this would be with drivers for controllers integrated |
| 490 | * into system-on-chip processors, where the controller devices have been |
| 491 | * configured as part of board setup. |
| 492 | * |
| 493 | * Returns zero if the driver registered and bound to a device, else returns |
| 494 | * a negative error code and with the driver not registered. |
| 495 | */ |
Andrew Morton | c63e078 | 2006-12-04 14:56:36 -0800 | [diff] [blame] | 496 | int __init_or_module platform_driver_probe(struct platform_driver *drv, |
David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 497 | int (*probe)(struct platform_device *)) |
| 498 | { |
| 499 | int retval, code; |
| 500 | |
| 501 | /* temporary section violation during probe() */ |
| 502 | drv->probe = probe; |
| 503 | retval = code = platform_driver_register(drv); |
| 504 | |
| 505 | /* Fixup that section violation, being paranoid about code scanning |
| 506 | * the list of drivers in order to probe new devices. Check to see |
| 507 | * if the probe was successful, and make sure any forced probes of |
| 508 | * new devices fail. |
| 509 | */ |
| 510 | spin_lock(&platform_bus_type.klist_drivers.k_lock); |
| 511 | drv->probe = NULL; |
| 512 | if (code == 0 && list_empty(&drv->driver.klist_devices.k_list)) |
| 513 | retval = -ENODEV; |
| 514 | drv->driver.probe = platform_drv_probe_fail; |
| 515 | spin_unlock(&platform_bus_type.klist_drivers.k_lock); |
| 516 | |
| 517 | if (code != retval) |
| 518 | platform_driver_unregister(drv); |
| 519 | return retval; |
| 520 | } |
| 521 | EXPORT_SYMBOL_GPL(platform_driver_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 523 | /* modalias support enables more hands-off userspace setup: |
| 524 | * (a) environment variable lets new-style hotplug events work once system is |
| 525 | * fully running: "modprobe $MODALIAS" |
| 526 | * (b) sysfs attribute lets new-style coldplug recover from hotplug events |
| 527 | * mishandled before system is fully running: "modprobe $(cat modalias)" |
| 528 | */ |
| 529 | static ssize_t |
| 530 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) |
| 531 | { |
| 532 | struct platform_device *pdev = to_platform_device(dev); |
| 533 | int len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->name); |
| 534 | |
| 535 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 536 | } |
| 537 | |
| 538 | static struct device_attribute platform_dev_attrs[] = { |
| 539 | __ATTR_RO(modalias), |
| 540 | __ATTR_NULL, |
| 541 | }; |
| 542 | |
| 543 | static int platform_uevent(struct device *dev, char **envp, int num_envp, |
| 544 | char *buffer, int buffer_size) |
| 545 | { |
| 546 | struct platform_device *pdev = to_platform_device(dev); |
| 547 | |
| 548 | envp[0] = buffer; |
| 549 | snprintf(buffer, buffer_size, "MODALIAS=%s", pdev->name); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | /** |
| 555 | * platform_match - bind platform device to platform driver. |
| 556 | * @dev: device. |
| 557 | * @drv: driver. |
| 558 | * |
| 559 | * Platform device IDs are assumed to be encoded like this: |
| 560 | * "<name><instance>", where <name> is a short description of the |
| 561 | * type of device, like "pci" or "floppy", and <instance> is the |
| 562 | * enumerated instance of the device, like '0' or '42'. |
| 563 | * Driver IDs are simply "<name>". |
| 564 | * So, extract the <name> from the platform_device structure, |
| 565 | * and compare it against the name of the driver. Return whether |
| 566 | * they match or not. |
| 567 | */ |
| 568 | |
| 569 | static int platform_match(struct device * dev, struct device_driver * drv) |
| 570 | { |
| 571 | struct platform_device *pdev = container_of(dev, struct platform_device, dev); |
| 572 | |
| 573 | return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); |
| 574 | } |
| 575 | |
David Brownell | 386415d | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 576 | static int platform_suspend(struct device *dev, pm_message_t mesg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { |
| 578 | int ret = 0; |
| 579 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 580 | if (dev->driver && dev->driver->suspend) |
David Brownell | 386415d | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 581 | ret = dev->driver->suspend(dev, mesg); |
| 582 | |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static int platform_suspend_late(struct device *dev, pm_message_t mesg) |
| 587 | { |
| 588 | struct platform_driver *drv = to_platform_driver(dev->driver); |
| 589 | struct platform_device *pdev = container_of(dev, struct platform_device, dev); |
| 590 | int ret = 0; |
| 591 | |
| 592 | if (dev->driver && drv->suspend_late) |
| 593 | ret = drv->suspend_late(pdev, mesg); |
| 594 | |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | static int platform_resume_early(struct device *dev) |
| 599 | { |
| 600 | struct platform_driver *drv = to_platform_driver(dev->driver); |
| 601 | struct platform_device *pdev = container_of(dev, struct platform_device, dev); |
| 602 | int ret = 0; |
| 603 | |
| 604 | if (dev->driver && drv->resume_early) |
| 605 | ret = drv->resume_early(pdev); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | static int platform_resume(struct device * dev) |
| 611 | { |
| 612 | int ret = 0; |
| 613 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 614 | if (dev->driver && dev->driver->resume) |
| 615 | ret = dev->driver->resume(dev); |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | struct bus_type platform_bus_type = { |
| 621 | .name = "platform", |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 622 | .dev_attrs = platform_dev_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | .match = platform_match, |
David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 624 | .uevent = platform_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | .suspend = platform_suspend, |
David Brownell | 386415d | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 626 | .suspend_late = platform_suspend_late, |
| 627 | .resume_early = platform_resume_early, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | .resume = platform_resume, |
| 629 | }; |
Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 630 | EXPORT_SYMBOL_GPL(platform_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | int __init platform_bus_init(void) |
| 633 | { |
Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 634 | int error; |
| 635 | |
| 636 | error = device_register(&platform_bus); |
| 637 | if (error) |
| 638 | return error; |
| 639 | error = bus_register(&platform_bus_type); |
| 640 | if (error) |
| 641 | device_unregister(&platform_bus); |
| 642 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK |
| 646 | u64 dma_get_required_mask(struct device *dev) |
| 647 | { |
| 648 | u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); |
| 649 | u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); |
| 650 | u64 mask; |
| 651 | |
| 652 | if (!high_totalram) { |
| 653 | /* convert to mask just covering totalram */ |
| 654 | low_totalram = (1 << (fls(low_totalram) - 1)); |
| 655 | low_totalram += low_totalram - 1; |
| 656 | mask = low_totalram; |
| 657 | } else { |
| 658 | high_totalram = (1 << (fls(high_totalram) - 1)); |
| 659 | high_totalram += high_totalram - 1; |
| 660 | mask = (((u64)high_totalram) << 32) + 0xffffffff; |
| 661 | } |
| 662 | return mask & *dev->dma_mask; |
| 663 | } |
| 664 | EXPORT_SYMBOL_GPL(dma_get_required_mask); |
| 665 | #endif |