| 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. | 
 | 163 |  */ | 
 | 164 | struct 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 Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 177 | 	return pa ? &pa->pdev : NULL; | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 178 | } | 
 | 179 | EXPORT_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 |  */ | 
 | 191 | int 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 | } | 
 | 203 | EXPORT_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 Wood | 6eefd34 | 2006-12-04 14:57:19 -0800 | [diff] [blame] | 215 | 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] | 216 | { | 
 | 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 | } | 
 | 226 | EXPORT_SYMBOL_GPL(platform_device_add_data); | 
 | 227 |  | 
 | 228 | /** | 
 | 229 |  *	platform_device_add - add a platform device to device hierarchy | 
| Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 230 |  *	@pdev:	platform device we're adding | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 |  * | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 232 |  *	This is part 2 of platform_device_register(), though may be called | 
 | 233 |  *	separately _iff_ pdev was allocated by platform_device_alloc(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 |  */ | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 235 | int platform_device_add(struct platform_device *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { | 
 | 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 Gala | d960bb4 | 2005-11-28 10:15:39 -0600 | [diff] [blame] | 266 | 		if (p && insert_resource(p, r)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | 			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 King | e391553 | 2006-05-06 08:15:26 +0100 | [diff] [blame] | 278 | 	ret = device_add(&pdev->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | 	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 King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 288 | EXPORT_SYMBOL_GPL(platform_device_add); | 
 | 289 |  | 
 | 290 | /** | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 291 |  *	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 | 
 | 295 |  *	resources owned by the device (@dev->resource). | 
 | 296 |  */ | 
 | 297 | void platform_device_del(struct platform_device *pdev) | 
 | 298 | { | 
 | 299 | 	int i; | 
 | 300 |  | 
 | 301 | 	if (pdev) { | 
 | 302 | 		for (i = 0; i < pdev->num_resources; i++) { | 
 | 303 | 			struct resource *r = &pdev->resource[i]; | 
 | 304 | 			if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO)) | 
 | 305 | 				release_resource(r); | 
 | 306 | 		} | 
 | 307 |  | 
 | 308 | 		device_del(&pdev->dev); | 
 | 309 | 	} | 
 | 310 | } | 
 | 311 | EXPORT_SYMBOL_GPL(platform_device_del); | 
 | 312 |  | 
 | 313 | /** | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 314 |  *	platform_device_register - add a platform-level device | 
 | 315 |  *	@pdev:	platform device we're adding | 
 | 316 |  * | 
 | 317 |  */ | 
 | 318 | int platform_device_register(struct platform_device * pdev) | 
 | 319 | { | 
 | 320 | 	device_initialize(&pdev->dev); | 
 | 321 | 	return platform_device_add(pdev); | 
 | 322 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 323 | EXPORT_SYMBOL_GPL(platform_device_register); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 |  | 
 | 325 | /** | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 326 |  *	platform_device_unregister - unregister a platform-level device | 
 | 327 |  *	@pdev:	platform device we're unregistering | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 |  * | 
| Uwe Zeisberger | 80682fa | 2006-03-22 00:21:33 +0100 | [diff] [blame] | 329 |  *	Unregistration is done in 2 steps. First we release all resources | 
| Jean Delvare | 2d7b5a7 | 2005-12-27 19:45:58 +0100 | [diff] [blame] | 330 |  *	and remove it from the subsystem, then we drop reference count by | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 331 |  *	calling platform_device_put(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 |  */ | 
 | 333 | void platform_device_unregister(struct platform_device * pdev) | 
 | 334 | { | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 335 | 	platform_device_del(pdev); | 
 | 336 | 	platform_device_put(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 338 | EXPORT_SYMBOL_GPL(platform_device_unregister); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | /** | 
 | 341 |  *	platform_device_register_simple | 
 | 342 |  *	@name:  base name of the device we're adding | 
 | 343 |  *	@id:    instance id | 
 | 344 |  *	@res:   set of resources that needs to be allocated for the device | 
 | 345 |  *	@num:	number of resources | 
 | 346 |  * | 
 | 347 |  *	This function creates a simple platform device that requires minimal | 
 | 348 |  *	resource and memory management. Canned release function freeing | 
 | 349 |  *	memory allocated for the device allows drivers using such devices | 
 | 350 |  *	to be unloaded iwithout waiting for the last reference to the device | 
 | 351 |  *	to be dropped. | 
 | 352 |  */ | 
 | 353 | struct platform_device *platform_device_register_simple(char *name, unsigned int id, | 
 | 354 | 							struct resource *res, unsigned int num) | 
 | 355 | { | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 356 | 	struct platform_device *pdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | 	int retval; | 
 | 358 |  | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 359 | 	pdev = platform_device_alloc(name, id); | 
 | 360 | 	if (!pdev) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | 		retval = -ENOMEM; | 
 | 362 | 		goto error; | 
 | 363 | 	} | 
 | 364 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 	if (num) { | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 366 | 		retval = platform_device_add_resources(pdev, res, num); | 
 | 367 | 		if (retval) | 
 | 368 | 			goto error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | 	} | 
 | 370 |  | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 371 | 	retval = platform_device_add(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | 	if (retval) | 
 | 373 | 		goto error; | 
 | 374 |  | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 375 | 	return pdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 |  | 
 | 377 | error: | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 378 | 	platform_device_put(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | 	return ERR_PTR(retval); | 
 | 380 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 381 | EXPORT_SYMBOL_GPL(platform_device_register_simple); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 |  | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 383 | static int platform_drv_probe(struct device *_dev) | 
 | 384 | { | 
 | 385 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 386 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 387 |  | 
 | 388 | 	return drv->probe(dev); | 
 | 389 | } | 
 | 390 |  | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 391 | static int platform_drv_probe_fail(struct device *_dev) | 
 | 392 | { | 
 | 393 | 	return -ENXIO; | 
 | 394 | } | 
 | 395 |  | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 396 | static int platform_drv_remove(struct device *_dev) | 
 | 397 | { | 
 | 398 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 399 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 400 |  | 
 | 401 | 	return drv->remove(dev); | 
 | 402 | } | 
 | 403 |  | 
 | 404 | static void platform_drv_shutdown(struct device *_dev) | 
 | 405 | { | 
 | 406 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 407 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 408 |  | 
 | 409 | 	drv->shutdown(dev); | 
 | 410 | } | 
 | 411 |  | 
 | 412 | static int platform_drv_suspend(struct device *_dev, pm_message_t state) | 
 | 413 | { | 
 | 414 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 415 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 416 |  | 
 | 417 | 	return drv->suspend(dev, state); | 
 | 418 | } | 
 | 419 |  | 
 | 420 | static int platform_drv_resume(struct device *_dev) | 
 | 421 | { | 
 | 422 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 423 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 424 |  | 
 | 425 | 	return drv->resume(dev); | 
 | 426 | } | 
 | 427 |  | 
 | 428 | /** | 
 | 429 |  *	platform_driver_register | 
 | 430 |  *	@drv: platform driver structure | 
 | 431 |  */ | 
 | 432 | int platform_driver_register(struct platform_driver *drv) | 
 | 433 | { | 
 | 434 | 	drv->driver.bus = &platform_bus_type; | 
 | 435 | 	if (drv->probe) | 
 | 436 | 		drv->driver.probe = platform_drv_probe; | 
 | 437 | 	if (drv->remove) | 
 | 438 | 		drv->driver.remove = platform_drv_remove; | 
 | 439 | 	if (drv->shutdown) | 
 | 440 | 		drv->driver.shutdown = platform_drv_shutdown; | 
 | 441 | 	if (drv->suspend) | 
 | 442 | 		drv->driver.suspend = platform_drv_suspend; | 
 | 443 | 	if (drv->resume) | 
 | 444 | 		drv->driver.resume = platform_drv_resume; | 
 | 445 | 	return driver_register(&drv->driver); | 
 | 446 | } | 
 | 447 | EXPORT_SYMBOL_GPL(platform_driver_register); | 
 | 448 |  | 
 | 449 | /** | 
 | 450 |  *	platform_driver_unregister | 
 | 451 |  *	@drv: platform driver structure | 
 | 452 |  */ | 
 | 453 | void platform_driver_unregister(struct platform_driver *drv) | 
 | 454 | { | 
 | 455 | 	driver_unregister(&drv->driver); | 
 | 456 | } | 
 | 457 | EXPORT_SYMBOL_GPL(platform_driver_unregister); | 
 | 458 |  | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 459 | /** | 
 | 460 |  * platform_driver_probe - register driver for non-hotpluggable device | 
 | 461 |  * @drv: platform driver structure | 
 | 462 |  * @probe: the driver probe routine, probably from an __init section | 
 | 463 |  * | 
 | 464 |  * Use this instead of platform_driver_register() when you know the device | 
 | 465 |  * is not hotpluggable and has already been registered, and you want to | 
 | 466 |  * remove its run-once probe() infrastructure from memory after the driver | 
 | 467 |  * has bound to the device. | 
 | 468 |  * | 
 | 469 |  * One typical use for this would be with drivers for controllers integrated | 
 | 470 |  * into system-on-chip processors, where the controller devices have been | 
 | 471 |  * configured as part of board setup. | 
 | 472 |  * | 
 | 473 |  * Returns zero if the driver registered and bound to a device, else returns | 
 | 474 |  * a negative error code and with the driver not registered. | 
 | 475 |  */ | 
| Andrew Morton | c63e078 | 2006-12-04 14:56:36 -0800 | [diff] [blame] | 476 | int __init_or_module platform_driver_probe(struct platform_driver *drv, | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 477 | 		int (*probe)(struct platform_device *)) | 
 | 478 | { | 
 | 479 | 	int retval, code; | 
 | 480 |  | 
 | 481 | 	/* temporary section violation during probe() */ | 
 | 482 | 	drv->probe = probe; | 
 | 483 | 	retval = code = platform_driver_register(drv); | 
 | 484 |  | 
 | 485 | 	/* Fixup that section violation, being paranoid about code scanning | 
 | 486 | 	 * the list of drivers in order to probe new devices.  Check to see | 
 | 487 | 	 * if the probe was successful, and make sure any forced probes of | 
 | 488 | 	 * new devices fail. | 
 | 489 | 	 */ | 
 | 490 | 	spin_lock(&platform_bus_type.klist_drivers.k_lock); | 
 | 491 | 	drv->probe = NULL; | 
 | 492 | 	if (code == 0 && list_empty(&drv->driver.klist_devices.k_list)) | 
 | 493 | 		retval = -ENODEV; | 
 | 494 | 	drv->driver.probe = platform_drv_probe_fail; | 
 | 495 | 	spin_unlock(&platform_bus_type.klist_drivers.k_lock); | 
 | 496 |  | 
 | 497 | 	if (code != retval) | 
 | 498 | 		platform_driver_unregister(drv); | 
 | 499 | 	return retval; | 
 | 500 | } | 
 | 501 | EXPORT_SYMBOL_GPL(platform_driver_probe); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 |  | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 503 | /* modalias support enables more hands-off userspace setup: | 
 | 504 |  * (a) environment variable lets new-style hotplug events work once system is | 
 | 505 |  *     fully running:  "modprobe $MODALIAS" | 
 | 506 |  * (b) sysfs attribute lets new-style coldplug recover from hotplug events | 
 | 507 |  *     mishandled before system is fully running:  "modprobe $(cat modalias)" | 
 | 508 |  */ | 
 | 509 | static ssize_t | 
 | 510 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) | 
 | 511 | { | 
 | 512 | 	struct platform_device	*pdev = to_platform_device(dev); | 
 | 513 | 	int len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->name); | 
 | 514 |  | 
 | 515 | 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; | 
 | 516 | } | 
 | 517 |  | 
 | 518 | static struct device_attribute platform_dev_attrs[] = { | 
 | 519 | 	__ATTR_RO(modalias), | 
 | 520 | 	__ATTR_NULL, | 
 | 521 | }; | 
 | 522 |  | 
 | 523 | static int platform_uevent(struct device *dev, char **envp, int num_envp, | 
 | 524 | 		char *buffer, int buffer_size) | 
 | 525 | { | 
 | 526 | 	struct platform_device	*pdev = to_platform_device(dev); | 
 | 527 |  | 
 | 528 | 	envp[0] = buffer; | 
 | 529 | 	snprintf(buffer, buffer_size, "MODALIAS=%s", pdev->name); | 
 | 530 | 	return 0; | 
 | 531 | } | 
 | 532 |  | 
 | 533 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | /** | 
 | 535 |  *	platform_match - bind platform device to platform driver. | 
 | 536 |  *	@dev:	device. | 
 | 537 |  *	@drv:	driver. | 
 | 538 |  * | 
 | 539 |  *	Platform device IDs are assumed to be encoded like this: | 
 | 540 |  *	"<name><instance>", where <name> is a short description of the | 
 | 541 |  *	type of device, like "pci" or "floppy", and <instance> is the | 
 | 542 |  *	enumerated instance of the device, like '0' or '42'. | 
 | 543 |  *	Driver IDs are simply "<name>". | 
 | 544 |  *	So, extract the <name> from the platform_device structure, | 
 | 545 |  *	and compare it against the name of the driver. Return whether | 
 | 546 |  *	they match or not. | 
 | 547 |  */ | 
 | 548 |  | 
 | 549 | static int platform_match(struct device * dev, struct device_driver * drv) | 
 | 550 | { | 
 | 551 | 	struct platform_device *pdev = container_of(dev, struct platform_device, dev); | 
 | 552 |  | 
 | 553 | 	return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); | 
 | 554 | } | 
 | 555 |  | 
| David Brownell | 386415d | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 556 | static int platform_suspend(struct device *dev, pm_message_t mesg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { | 
 | 558 | 	int ret = 0; | 
 | 559 |  | 
| Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 560 | 	if (dev->driver && dev->driver->suspend) | 
| David Brownell | 386415d | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 561 | 		ret = dev->driver->suspend(dev, mesg); | 
 | 562 |  | 
 | 563 | 	return ret; | 
 | 564 | } | 
 | 565 |  | 
 | 566 | static int platform_suspend_late(struct device *dev, pm_message_t mesg) | 
 | 567 | { | 
 | 568 | 	struct platform_driver *drv = to_platform_driver(dev->driver); | 
 | 569 | 	struct platform_device *pdev = container_of(dev, struct platform_device, dev); | 
 | 570 | 	int ret = 0; | 
 | 571 |  | 
 | 572 | 	if (dev->driver && drv->suspend_late) | 
 | 573 | 		ret = drv->suspend_late(pdev, mesg); | 
 | 574 |  | 
 | 575 | 	return ret; | 
 | 576 | } | 
 | 577 |  | 
 | 578 | static int platform_resume_early(struct device *dev) | 
 | 579 | { | 
 | 580 | 	struct platform_driver *drv = to_platform_driver(dev->driver); | 
 | 581 | 	struct platform_device *pdev = container_of(dev, struct platform_device, dev); | 
 | 582 | 	int ret = 0; | 
 | 583 |  | 
 | 584 | 	if (dev->driver && drv->resume_early) | 
 | 585 | 		ret = drv->resume_early(pdev); | 
| Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 586 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | 	return ret; | 
 | 588 | } | 
 | 589 |  | 
 | 590 | static int platform_resume(struct device * dev) | 
 | 591 | { | 
 | 592 | 	int ret = 0; | 
 | 593 |  | 
| Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 594 | 	if (dev->driver && dev->driver->resume) | 
 | 595 | 		ret = dev->driver->resume(dev); | 
 | 596 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | 	return ret; | 
 | 598 | } | 
 | 599 |  | 
 | 600 | struct bus_type platform_bus_type = { | 
 | 601 | 	.name		= "platform", | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 602 | 	.dev_attrs	= platform_dev_attrs, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | 	.match		= platform_match, | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 604 | 	.uevent		= platform_uevent, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | 	.suspend	= platform_suspend, | 
| David Brownell | 386415d | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 606 | 	.suspend_late	= platform_suspend_late, | 
 | 607 | 	.resume_early	= platform_resume_early, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | 	.resume		= platform_resume, | 
 | 609 | }; | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 610 | EXPORT_SYMBOL_GPL(platform_bus_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 |  | 
 | 612 | int __init platform_bus_init(void) | 
 | 613 | { | 
| Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 614 | 	int error; | 
 | 615 |  | 
 | 616 | 	error = device_register(&platform_bus); | 
 | 617 | 	if (error) | 
 | 618 | 		return error; | 
 | 619 | 	error =  bus_register(&platform_bus_type); | 
 | 620 | 	if (error) | 
 | 621 | 		device_unregister(&platform_bus); | 
 | 622 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } | 
 | 624 |  | 
 | 625 | #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK | 
 | 626 | u64 dma_get_required_mask(struct device *dev) | 
 | 627 | { | 
 | 628 | 	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); | 
 | 629 | 	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); | 
 | 630 | 	u64 mask; | 
 | 631 |  | 
 | 632 | 	if (!high_totalram) { | 
 | 633 | 		/* convert to mask just covering totalram */ | 
 | 634 | 		low_totalram = (1 << (fls(low_totalram) - 1)); | 
 | 635 | 		low_totalram += low_totalram - 1; | 
 | 636 | 		mask = low_totalram; | 
 | 637 | 	} else { | 
 | 638 | 		high_totalram = (1 << (fls(high_totalram) - 1)); | 
 | 639 | 		high_totalram += high_totalram - 1; | 
 | 640 | 		mask = (((u64)high_totalram) << 32) + 0xffffffff; | 
 | 641 | 	} | 
 | 642 | 	return mask & *dev->dma_mask; | 
 | 643 | } | 
 | 644 | EXPORT_SYMBOL_GPL(dma_get_required_mask); | 
 | 645 | #endif |