Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
daa41226 | 13 | #include <linux/string.h> |
d052d1be | 14 | #include <linux/platform_device.h> |
1da177e4 LT |
15 | #include <linux/module.h> |
16 | #include <linux/init.h> | |
17 | #include <linux/dma-mapping.h> | |
18 | #include <linux/bootmem.h> | |
19 | #include <linux/err.h> | |
4e57b681 | 20 | #include <linux/slab.h> |
9d730229 | 21 | #include <linux/pm_runtime.h> |
1da177e4 | 22 | |
a1bdc7aa BD |
23 | #include "base.h" |
24 | ||
4a3ad20c GKH |
25 | #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \ |
26 | driver)) | |
00d3dcdd | 27 | |
1da177e4 | 28 | struct device platform_bus = { |
1e0b2cf9 | 29 | .init_name = "platform", |
1da177e4 | 30 | }; |
a96b2042 | 31 | EXPORT_SYMBOL_GPL(platform_bus); |
1da177e4 LT |
32 | |
33 | /** | |
4a3ad20c GKH |
34 | * platform_get_resource - get a resource for a device |
35 | * @dev: platform device | |
36 | * @type: resource type | |
37 | * @num: resource index | |
1da177e4 | 38 | */ |
4a3ad20c GKH |
39 | struct resource *platform_get_resource(struct platform_device *dev, |
40 | unsigned int type, unsigned int num) | |
1da177e4 LT |
41 | { |
42 | int i; | |
43 | ||
44 | for (i = 0; i < dev->num_resources; i++) { | |
45 | struct resource *r = &dev->resource[i]; | |
46 | ||
c9f66169 MD |
47 | if (type == resource_type(r) && num-- == 0) |
48 | return r; | |
1da177e4 LT |
49 | } |
50 | return NULL; | |
51 | } | |
a96b2042 | 52 | EXPORT_SYMBOL_GPL(platform_get_resource); |
1da177e4 LT |
53 | |
54 | /** | |
4a3ad20c GKH |
55 | * platform_get_irq - get an IRQ for a device |
56 | * @dev: platform device | |
57 | * @num: IRQ number index | |
1da177e4 LT |
58 | */ |
59 | int platform_get_irq(struct platform_device *dev, unsigned int num) | |
60 | { | |
61 | struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); | |
62 | ||
305b3228 | 63 | return r ? r->start : -ENXIO; |
1da177e4 | 64 | } |
a96b2042 | 65 | EXPORT_SYMBOL_GPL(platform_get_irq); |
1da177e4 LT |
66 | |
67 | /** | |
4a3ad20c GKH |
68 | * platform_get_resource_byname - get a resource for a device by name |
69 | * @dev: platform device | |
70 | * @type: resource type | |
71 | * @name: resource name | |
1da177e4 | 72 | */ |
4a3ad20c | 73 | struct resource *platform_get_resource_byname(struct platform_device *dev, |
c0afe7ba LW |
74 | unsigned int type, |
75 | const char *name) | |
1da177e4 LT |
76 | { |
77 | int i; | |
78 | ||
79 | for (i = 0; i < dev->num_resources; i++) { | |
80 | struct resource *r = &dev->resource[i]; | |
81 | ||
c9f66169 MD |
82 | if (type == resource_type(r) && !strcmp(r->name, name)) |
83 | return r; | |
1da177e4 LT |
84 | } |
85 | return NULL; | |
86 | } | |
a96b2042 | 87 | EXPORT_SYMBOL_GPL(platform_get_resource_byname); |
1da177e4 LT |
88 | |
89 | /** | |
4a3ad20c GKH |
90 | * platform_get_irq - get an IRQ for a device |
91 | * @dev: platform device | |
92 | * @name: IRQ name | |
1da177e4 | 93 | */ |
c0afe7ba | 94 | int platform_get_irq_byname(struct platform_device *dev, const char *name) |
1da177e4 | 95 | { |
4a3ad20c GKH |
96 | struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, |
97 | name); | |
1da177e4 | 98 | |
305b3228 | 99 | return r ? r->start : -ENXIO; |
1da177e4 | 100 | } |
a96b2042 | 101 | EXPORT_SYMBOL_GPL(platform_get_irq_byname); |
1da177e4 LT |
102 | |
103 | /** | |
4a3ad20c GKH |
104 | * platform_add_devices - add a numbers of platform devices |
105 | * @devs: array of platform devices to add | |
106 | * @num: number of platform devices in array | |
1da177e4 LT |
107 | */ |
108 | int platform_add_devices(struct platform_device **devs, int num) | |
109 | { | |
110 | int i, ret = 0; | |
111 | ||
112 | for (i = 0; i < num; i++) { | |
113 | ret = platform_device_register(devs[i]); | |
114 | if (ret) { | |
115 | while (--i >= 0) | |
116 | platform_device_unregister(devs[i]); | |
117 | break; | |
118 | } | |
119 | } | |
120 | ||
121 | return ret; | |
122 | } | |
a96b2042 | 123 | EXPORT_SYMBOL_GPL(platform_add_devices); |
1da177e4 | 124 | |
37c12e74 RK |
125 | struct platform_object { |
126 | struct platform_device pdev; | |
127 | char name[1]; | |
128 | }; | |
129 | ||
1da177e4 | 130 | /** |
3c31f07a | 131 | * platform_device_put - destroy a platform device |
4a3ad20c | 132 | * @pdev: platform device to free |
37c12e74 | 133 | * |
4a3ad20c GKH |
134 | * Free all memory associated with a platform device. This function must |
135 | * _only_ be externally called in error cases. All other usage is a bug. | |
37c12e74 RK |
136 | */ |
137 | void platform_device_put(struct platform_device *pdev) | |
138 | { | |
139 | if (pdev) | |
140 | put_device(&pdev->dev); | |
141 | } | |
142 | EXPORT_SYMBOL_GPL(platform_device_put); | |
143 | ||
144 | static void platform_device_release(struct device *dev) | |
145 | { | |
4a3ad20c GKH |
146 | struct platform_object *pa = container_of(dev, struct platform_object, |
147 | pdev.dev); | |
37c12e74 RK |
148 | |
149 | kfree(pa->pdev.dev.platform_data); | |
150 | kfree(pa->pdev.resource); | |
151 | kfree(pa); | |
152 | } | |
153 | ||
154 | /** | |
3c31f07a | 155 | * platform_device_alloc - create a platform device |
4a3ad20c GKH |
156 | * @name: base name of the device we're adding |
157 | * @id: instance id | |
37c12e74 | 158 | * |
4a3ad20c GKH |
159 | * Create a platform device object which can have other objects attached |
160 | * to it, and which will have attached objects freed when it is released. | |
37c12e74 | 161 | */ |
1359555e | 162 | struct platform_device *platform_device_alloc(const char *name, int id) |
37c12e74 RK |
163 | { |
164 | struct platform_object *pa; | |
165 | ||
166 | pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL); | |
167 | if (pa) { | |
168 | strcpy(pa->name, name); | |
169 | pa->pdev.name = pa->name; | |
170 | pa->pdev.id = id; | |
171 | device_initialize(&pa->pdev.dev); | |
172 | pa->pdev.dev.release = platform_device_release; | |
173 | } | |
174 | ||
93ce3061 | 175 | return pa ? &pa->pdev : NULL; |
37c12e74 RK |
176 | } |
177 | EXPORT_SYMBOL_GPL(platform_device_alloc); | |
178 | ||
179 | /** | |
3c31f07a | 180 | * platform_device_add_resources - add resources to a platform device |
4a3ad20c GKH |
181 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
182 | * @res: set of resources that needs to be allocated for the device | |
183 | * @num: number of resources | |
37c12e74 | 184 | * |
4a3ad20c GKH |
185 | * Add a copy of the resources to the platform device. The memory |
186 | * associated with the resources will be freed when the platform device is | |
187 | * released. | |
37c12e74 | 188 | */ |
4a3ad20c GKH |
189 | int platform_device_add_resources(struct platform_device *pdev, |
190 | struct resource *res, unsigned int num) | |
37c12e74 RK |
191 | { |
192 | struct resource *r; | |
193 | ||
194 | r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL); | |
195 | if (r) { | |
196 | memcpy(r, res, sizeof(struct resource) * num); | |
197 | pdev->resource = r; | |
198 | pdev->num_resources = num; | |
199 | } | |
200 | return r ? 0 : -ENOMEM; | |
201 | } | |
202 | EXPORT_SYMBOL_GPL(platform_device_add_resources); | |
203 | ||
204 | /** | |
3c31f07a | 205 | * platform_device_add_data - add platform-specific data to a platform device |
4a3ad20c GKH |
206 | * @pdev: platform device allocated by platform_device_alloc to add resources to |
207 | * @data: platform specific data for this platform device | |
208 | * @size: size of platform specific data | |
37c12e74 | 209 | * |
4a3ad20c GKH |
210 | * Add a copy of platform specific data to the platform device's |
211 | * platform_data pointer. The memory associated with the platform data | |
212 | * will be freed when the platform device is released. | |
37c12e74 | 213 | */ |
4a3ad20c GKH |
214 | int platform_device_add_data(struct platform_device *pdev, const void *data, |
215 | size_t size) | |
37c12e74 | 216 | { |
daa41226 | 217 | void *d = kmemdup(data, size, GFP_KERNEL); |
37c12e74 | 218 | |
37c12e74 | 219 | if (d) { |
37c12e74 | 220 | pdev->dev.platform_data = d; |
daa41226 | 221 | return 0; |
37c12e74 | 222 | } |
daa41226 | 223 | return -ENOMEM; |
37c12e74 RK |
224 | } |
225 | EXPORT_SYMBOL_GPL(platform_device_add_data); | |
226 | ||
227 | /** | |
4a3ad20c GKH |
228 | * platform_device_add - add a platform device to device hierarchy |
229 | * @pdev: platform device we're adding | |
1da177e4 | 230 | * |
4a3ad20c GKH |
231 | * This is part 2 of platform_device_register(), though may be called |
232 | * separately _iff_ pdev was allocated by platform_device_alloc(). | |
1da177e4 | 233 | */ |
37c12e74 | 234 | int platform_device_add(struct platform_device *pdev) |
1da177e4 LT |
235 | { |
236 | int i, ret = 0; | |
237 | ||
238 | if (!pdev) | |
239 | return -EINVAL; | |
240 | ||
241 | if (!pdev->dev.parent) | |
242 | pdev->dev.parent = &platform_bus; | |
243 | ||
244 | pdev->dev.bus = &platform_bus_type; | |
245 | ||
246 | if (pdev->id != -1) | |
1e0b2cf9 | 247 | dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); |
1da177e4 | 248 | else |
acc0e90f | 249 | dev_set_name(&pdev->dev, "%s", pdev->name); |
1da177e4 LT |
250 | |
251 | for (i = 0; i < pdev->num_resources; i++) { | |
252 | struct resource *p, *r = &pdev->resource[i]; | |
253 | ||
254 | if (r->name == NULL) | |
1e0b2cf9 | 255 | r->name = dev_name(&pdev->dev); |
1da177e4 LT |
256 | |
257 | p = r->parent; | |
258 | if (!p) { | |
c9f66169 | 259 | if (resource_type(r) == IORESOURCE_MEM) |
1da177e4 | 260 | p = &iomem_resource; |
c9f66169 | 261 | else if (resource_type(r) == IORESOURCE_IO) |
1da177e4 LT |
262 | p = &ioport_resource; |
263 | } | |
264 | ||
d960bb4d | 265 | if (p && insert_resource(p, r)) { |
1da177e4 LT |
266 | printk(KERN_ERR |
267 | "%s: failed to claim resource %d\n", | |
1e0b2cf9 | 268 | dev_name(&pdev->dev), i); |
1da177e4 LT |
269 | ret = -EBUSY; |
270 | goto failed; | |
271 | } | |
272 | } | |
273 | ||
274 | pr_debug("Registering platform device '%s'. Parent at %s\n", | |
1e0b2cf9 | 275 | dev_name(&pdev->dev), dev_name(pdev->dev.parent)); |
1da177e4 | 276 | |
e3915532 | 277 | ret = device_add(&pdev->dev); |
1da177e4 LT |
278 | if (ret == 0) |
279 | return ret; | |
280 | ||
281 | failed: | |
c9f66169 MD |
282 | while (--i >= 0) { |
283 | struct resource *r = &pdev->resource[i]; | |
284 | unsigned long type = resource_type(r); | |
285 | ||
286 | if (type == IORESOURCE_MEM || type == IORESOURCE_IO) | |
287 | release_resource(r); | |
288 | } | |
289 | ||
1da177e4 LT |
290 | return ret; |
291 | } | |
37c12e74 RK |
292 | EXPORT_SYMBOL_GPL(platform_device_add); |
293 | ||
294 | /** | |
4a3ad20c GKH |
295 | * platform_device_del - remove a platform-level device |
296 | * @pdev: platform device we're removing | |
1da177e4 | 297 | * |
4a3ad20c GKH |
298 | * Note that this function will also release all memory- and port-based |
299 | * resources owned by the device (@dev->resource). This function must | |
300 | * _only_ be externally called in error cases. All other usage is a bug. | |
1da177e4 | 301 | */ |
93ce3061 | 302 | void platform_device_del(struct platform_device *pdev) |
1da177e4 LT |
303 | { |
304 | int i; | |
305 | ||
306 | if (pdev) { | |
dc4c15d4 JD |
307 | device_del(&pdev->dev); |
308 | ||
1da177e4 LT |
309 | for (i = 0; i < pdev->num_resources; i++) { |
310 | struct resource *r = &pdev->resource[i]; | |
c9f66169 MD |
311 | unsigned long type = resource_type(r); |
312 | ||
313 | if (type == IORESOURCE_MEM || type == IORESOURCE_IO) | |
1da177e4 LT |
314 | release_resource(r); |
315 | } | |
1da177e4 LT |
316 | } |
317 | } | |
93ce3061 DT |
318 | EXPORT_SYMBOL_GPL(platform_device_del); |
319 | ||
320 | /** | |
4a3ad20c GKH |
321 | * platform_device_register - add a platform-level device |
322 | * @pdev: platform device we're adding | |
93ce3061 | 323 | */ |
4a3ad20c | 324 | int platform_device_register(struct platform_device *pdev) |
93ce3061 DT |
325 | { |
326 | device_initialize(&pdev->dev); | |
327 | return platform_device_add(pdev); | |
328 | } | |
a96b2042 | 329 | EXPORT_SYMBOL_GPL(platform_device_register); |
93ce3061 DT |
330 | |
331 | /** | |
4a3ad20c GKH |
332 | * platform_device_unregister - unregister a platform-level device |
333 | * @pdev: platform device we're unregistering | |
93ce3061 | 334 | * |
4a3ad20c GKH |
335 | * Unregistration is done in 2 steps. First we release all resources |
336 | * and remove it from the subsystem, then we drop reference count by | |
337 | * calling platform_device_put(). | |
93ce3061 | 338 | */ |
4a3ad20c | 339 | void platform_device_unregister(struct platform_device *pdev) |
93ce3061 DT |
340 | { |
341 | platform_device_del(pdev); | |
342 | platform_device_put(pdev); | |
343 | } | |
a96b2042 | 344 | EXPORT_SYMBOL_GPL(platform_device_unregister); |
1da177e4 | 345 | |
1da177e4 | 346 | /** |
3c31f07a | 347 | * platform_device_register_simple - add a platform-level device and its resources |
4a3ad20c GKH |
348 | * @name: base name of the device we're adding |
349 | * @id: instance id | |
350 | * @res: set of resources that needs to be allocated for the device | |
351 | * @num: number of resources | |
1da177e4 | 352 | * |
4a3ad20c GKH |
353 | * This function creates a simple platform device that requires minimal |
354 | * resource and memory management. Canned release function freeing memory | |
355 | * allocated for the device allows drivers using such devices to be | |
356 | * unloaded without waiting for the last reference to the device to be | |
357 | * dropped. | |
49a4ec18 | 358 | * |
4a3ad20c GKH |
359 | * This interface is primarily intended for use with legacy drivers which |
360 | * probe hardware directly. Because such drivers create sysfs device nodes | |
361 | * themselves, rather than letting system infrastructure handle such device | |
362 | * enumeration tasks, they don't fully conform to the Linux driver model. | |
363 | * In particular, when such drivers are built as modules, they can't be | |
364 | * "hotplugged". | |
1da177e4 | 365 | */ |
4a3ad20c GKH |
366 | struct platform_device *platform_device_register_simple(const char *name, |
367 | int id, | |
368 | struct resource *res, | |
369 | unsigned int num) | |
1da177e4 | 370 | { |
37c12e74 | 371 | struct platform_device *pdev; |
1da177e4 LT |
372 | int retval; |
373 | ||
37c12e74 RK |
374 | pdev = platform_device_alloc(name, id); |
375 | if (!pdev) { | |
1da177e4 LT |
376 | retval = -ENOMEM; |
377 | goto error; | |
378 | } | |
379 | ||
1da177e4 | 380 | if (num) { |
37c12e74 RK |
381 | retval = platform_device_add_resources(pdev, res, num); |
382 | if (retval) | |
383 | goto error; | |
1da177e4 LT |
384 | } |
385 | ||
37c12e74 | 386 | retval = platform_device_add(pdev); |
1da177e4 LT |
387 | if (retval) |
388 | goto error; | |
389 | ||
37c12e74 | 390 | return pdev; |
1da177e4 LT |
391 | |
392 | error: | |
37c12e74 | 393 | platform_device_put(pdev); |
1da177e4 LT |
394 | return ERR_PTR(retval); |
395 | } | |
a96b2042 | 396 | EXPORT_SYMBOL_GPL(platform_device_register_simple); |
1da177e4 | 397 | |
d8bf2540 | 398 | /** |
3c31f07a | 399 | * platform_device_register_data - add a platform-level device with platform-specific data |
d8bf2540 DB |
400 | * @parent: parent device for the device we're adding |
401 | * @name: base name of the device we're adding | |
402 | * @id: instance id | |
403 | * @data: platform specific data for this platform device | |
404 | * @size: size of platform specific data | |
405 | * | |
406 | * This function creates a simple platform device that requires minimal | |
407 | * resource and memory management. Canned release function freeing memory | |
408 | * allocated for the device allows drivers using such devices to be | |
409 | * unloaded without waiting for the last reference to the device to be | |
410 | * dropped. | |
411 | */ | |
412 | struct platform_device *platform_device_register_data( | |
413 | struct device *parent, | |
414 | const char *name, int id, | |
415 | const void *data, size_t size) | |
416 | { | |
417 | struct platform_device *pdev; | |
418 | int retval; | |
419 | ||
420 | pdev = platform_device_alloc(name, id); | |
421 | if (!pdev) { | |
422 | retval = -ENOMEM; | |
423 | goto error; | |
424 | } | |
425 | ||
426 | pdev->dev.parent = parent; | |
427 | ||
428 | if (size) { | |
429 | retval = platform_device_add_data(pdev, data, size); | |
430 | if (retval) | |
431 | goto error; | |
432 | } | |
433 | ||
434 | retval = platform_device_add(pdev); | |
435 | if (retval) | |
436 | goto error; | |
437 | ||
438 | return pdev; | |
439 | ||
440 | error: | |
441 | platform_device_put(pdev); | |
442 | return ERR_PTR(retval); | |
443 | } | |
0787fdf7 | 444 | EXPORT_SYMBOL_GPL(platform_device_register_data); |
d8bf2540 | 445 | |
00d3dcdd RK |
446 | static int platform_drv_probe(struct device *_dev) |
447 | { | |
448 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
449 | struct platform_device *dev = to_platform_device(_dev); | |
450 | ||
451 | return drv->probe(dev); | |
452 | } | |
453 | ||
c67334fb DB |
454 | static int platform_drv_probe_fail(struct device *_dev) |
455 | { | |
456 | return -ENXIO; | |
457 | } | |
458 | ||
00d3dcdd RK |
459 | static int platform_drv_remove(struct device *_dev) |
460 | { | |
461 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
462 | struct platform_device *dev = to_platform_device(_dev); | |
463 | ||
464 | return drv->remove(dev); | |
465 | } | |
466 | ||
467 | static void platform_drv_shutdown(struct device *_dev) | |
468 | { | |
469 | struct platform_driver *drv = to_platform_driver(_dev->driver); | |
470 | struct platform_device *dev = to_platform_device(_dev); | |
471 | ||
472 | drv->shutdown(dev); | |
473 | } | |
474 | ||
00d3dcdd | 475 | /** |
3c31f07a | 476 | * platform_driver_register - register a driver for platform-level devices |
4a3ad20c | 477 | * @drv: platform driver structure |
00d3dcdd RK |
478 | */ |
479 | int platform_driver_register(struct platform_driver *drv) | |
480 | { | |
481 | drv->driver.bus = &platform_bus_type; | |
482 | if (drv->probe) | |
483 | drv->driver.probe = platform_drv_probe; | |
484 | if (drv->remove) | |
485 | drv->driver.remove = platform_drv_remove; | |
486 | if (drv->shutdown) | |
487 | drv->driver.shutdown = platform_drv_shutdown; | |
783ea7d4 | 488 | |
00d3dcdd RK |
489 | return driver_register(&drv->driver); |
490 | } | |
491 | EXPORT_SYMBOL_GPL(platform_driver_register); | |
492 | ||
493 | /** | |
3c31f07a | 494 | * platform_driver_unregister - unregister a driver for platform-level devices |
4a3ad20c | 495 | * @drv: platform driver structure |
00d3dcdd RK |
496 | */ |
497 | void platform_driver_unregister(struct platform_driver *drv) | |
498 | { | |
499 | driver_unregister(&drv->driver); | |
500 | } | |
501 | EXPORT_SYMBOL_GPL(platform_driver_unregister); | |
502 | ||
c67334fb DB |
503 | /** |
504 | * platform_driver_probe - register driver for non-hotpluggable device | |
505 | * @drv: platform driver structure | |
506 | * @probe: the driver probe routine, probably from an __init section | |
507 | * | |
508 | * Use this instead of platform_driver_register() when you know the device | |
509 | * is not hotpluggable and has already been registered, and you want to | |
510 | * remove its run-once probe() infrastructure from memory after the driver | |
511 | * has bound to the device. | |
512 | * | |
513 | * One typical use for this would be with drivers for controllers integrated | |
514 | * into system-on-chip processors, where the controller devices have been | |
515 | * configured as part of board setup. | |
516 | * | |
517 | * Returns zero if the driver registered and bound to a device, else returns | |
518 | * a negative error code and with the driver not registered. | |
519 | */ | |
c63e0783 | 520 | int __init_or_module platform_driver_probe(struct platform_driver *drv, |
c67334fb DB |
521 | int (*probe)(struct platform_device *)) |
522 | { | |
523 | int retval, code; | |
524 | ||
1a6f2a75 DT |
525 | /* make sure driver won't have bind/unbind attributes */ |
526 | drv->driver.suppress_bind_attrs = true; | |
527 | ||
c67334fb DB |
528 | /* temporary section violation during probe() */ |
529 | drv->probe = probe; | |
530 | retval = code = platform_driver_register(drv); | |
531 | ||
1a6f2a75 DT |
532 | /* |
533 | * Fixup that section violation, being paranoid about code scanning | |
c67334fb DB |
534 | * the list of drivers in order to probe new devices. Check to see |
535 | * if the probe was successful, and make sure any forced probes of | |
536 | * new devices fail. | |
537 | */ | |
c6f7e72a | 538 | spin_lock(&platform_bus_type.p->klist_drivers.k_lock); |
c67334fb | 539 | drv->probe = NULL; |
e5dd1278 | 540 | if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) |
c67334fb DB |
541 | retval = -ENODEV; |
542 | drv->driver.probe = platform_drv_probe_fail; | |
c6f7e72a | 543 | spin_unlock(&platform_bus_type.p->klist_drivers.k_lock); |
c67334fb DB |
544 | |
545 | if (code != retval) | |
546 | platform_driver_unregister(drv); | |
547 | return retval; | |
548 | } | |
549 | EXPORT_SYMBOL_GPL(platform_driver_probe); | |
1da177e4 | 550 | |
ecdf6ceb DT |
551 | /** |
552 | * platform_create_bundle - register driver and create corresponding device | |
553 | * @driver: platform driver structure | |
554 | * @probe: the driver probe routine, probably from an __init section | |
555 | * @res: set of resources that needs to be allocated for the device | |
556 | * @n_res: number of resources | |
557 | * @data: platform specific data for this platform device | |
558 | * @size: size of platform specific data | |
559 | * | |
560 | * Use this in legacy-style modules that probe hardware directly and | |
561 | * register a single platform device and corresponding platform driver. | |
562 | */ | |
563 | struct platform_device * __init_or_module platform_create_bundle( | |
564 | struct platform_driver *driver, | |
565 | int (*probe)(struct platform_device *), | |
566 | struct resource *res, unsigned int n_res, | |
567 | const void *data, size_t size) | |
568 | { | |
569 | struct platform_device *pdev; | |
570 | int error; | |
571 | ||
572 | pdev = platform_device_alloc(driver->driver.name, -1); | |
573 | if (!pdev) { | |
574 | error = -ENOMEM; | |
575 | goto err_out; | |
576 | } | |
577 | ||
578 | if (res) { | |
579 | error = platform_device_add_resources(pdev, res, n_res); | |
580 | if (error) | |
581 | goto err_pdev_put; | |
582 | } | |
583 | ||
584 | if (data) { | |
585 | error = platform_device_add_data(pdev, data, size); | |
586 | if (error) | |
587 | goto err_pdev_put; | |
588 | } | |
589 | ||
590 | error = platform_device_add(pdev); | |
591 | if (error) | |
592 | goto err_pdev_put; | |
593 | ||
594 | error = platform_driver_probe(driver, probe); | |
595 | if (error) | |
596 | goto err_pdev_del; | |
597 | ||
598 | return pdev; | |
599 | ||
600 | err_pdev_del: | |
601 | platform_device_del(pdev); | |
602 | err_pdev_put: | |
603 | platform_device_put(pdev); | |
604 | err_out: | |
605 | return ERR_PTR(error); | |
606 | } | |
607 | EXPORT_SYMBOL_GPL(platform_create_bundle); | |
608 | ||
a0245f7a DB |
609 | /* modalias support enables more hands-off userspace setup: |
610 | * (a) environment variable lets new-style hotplug events work once system is | |
611 | * fully running: "modprobe $MODALIAS" | |
612 | * (b) sysfs attribute lets new-style coldplug recover from hotplug events | |
613 | * mishandled before system is fully running: "modprobe $(cat modalias)" | |
614 | */ | |
4a3ad20c GKH |
615 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
616 | char *buf) | |
a0245f7a DB |
617 | { |
618 | struct platform_device *pdev = to_platform_device(dev); | |
43cc71ee | 619 | int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); |
a0245f7a DB |
620 | |
621 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; | |
622 | } | |
623 | ||
624 | static struct device_attribute platform_dev_attrs[] = { | |
625 | __ATTR_RO(modalias), | |
626 | __ATTR_NULL, | |
627 | }; | |
628 | ||
7eff2e7a | 629 | static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) |
a0245f7a DB |
630 | { |
631 | struct platform_device *pdev = to_platform_device(dev); | |
632 | ||
57fee4a5 EM |
633 | add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, |
634 | (pdev->id_entry) ? pdev->id_entry->name : pdev->name); | |
a0245f7a DB |
635 | return 0; |
636 | } | |
637 | ||
57fee4a5 | 638 | static const struct platform_device_id *platform_match_id( |
831fad2f | 639 | const struct platform_device_id *id, |
57fee4a5 EM |
640 | struct platform_device *pdev) |
641 | { | |
642 | while (id->name[0]) { | |
643 | if (strcmp(pdev->name, id->name) == 0) { | |
644 | pdev->id_entry = id; | |
645 | return id; | |
646 | } | |
647 | id++; | |
648 | } | |
649 | return NULL; | |
650 | } | |
651 | ||
1da177e4 | 652 | /** |
4a3ad20c GKH |
653 | * platform_match - bind platform device to platform driver. |
654 | * @dev: device. | |
655 | * @drv: driver. | |
1da177e4 | 656 | * |
4a3ad20c GKH |
657 | * Platform device IDs are assumed to be encoded like this: |
658 | * "<name><instance>", where <name> is a short description of the type of | |
659 | * device, like "pci" or "floppy", and <instance> is the enumerated | |
660 | * instance of the device, like '0' or '42'. Driver IDs are simply | |
661 | * "<name>". So, extract the <name> from the platform_device structure, | |
662 | * and compare it against the name of the driver. Return whether they match | |
663 | * or not. | |
1da177e4 | 664 | */ |
4a3ad20c | 665 | static int platform_match(struct device *dev, struct device_driver *drv) |
1da177e4 | 666 | { |
71b3e0c1 | 667 | struct platform_device *pdev = to_platform_device(dev); |
57fee4a5 EM |
668 | struct platform_driver *pdrv = to_platform_driver(drv); |
669 | ||
670 | /* match against the id table first */ | |
671 | if (pdrv->id_table) | |
672 | return platform_match_id(pdrv->id_table, pdev) != NULL; | |
1da177e4 | 673 | |
57fee4a5 | 674 | /* fall-back to driver name match */ |
1e0b2cf9 | 675 | return (strcmp(pdev->name, drv->name) == 0); |
1da177e4 LT |
676 | } |
677 | ||
25e18499 RW |
678 | #ifdef CONFIG_PM_SLEEP |
679 | ||
680 | static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) | |
1da177e4 | 681 | { |
783ea7d4 MD |
682 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
683 | struct platform_device *pdev = to_platform_device(dev); | |
1da177e4 LT |
684 | int ret = 0; |
685 | ||
783ea7d4 MD |
686 | if (dev->driver && pdrv->suspend) |
687 | ret = pdrv->suspend(pdev, mesg); | |
386415d8 DB |
688 | |
689 | return ret; | |
690 | } | |
691 | ||
25e18499 | 692 | static int platform_legacy_resume(struct device *dev) |
1da177e4 | 693 | { |
783ea7d4 MD |
694 | struct platform_driver *pdrv = to_platform_driver(dev->driver); |
695 | struct platform_device *pdev = to_platform_device(dev); | |
1da177e4 LT |
696 | int ret = 0; |
697 | ||
783ea7d4 MD |
698 | if (dev->driver && pdrv->resume) |
699 | ret = pdrv->resume(pdev); | |
9480e307 | 700 | |
1da177e4 LT |
701 | return ret; |
702 | } | |
703 | ||
25e18499 RW |
704 | static int platform_pm_prepare(struct device *dev) |
705 | { | |
706 | struct device_driver *drv = dev->driver; | |
707 | int ret = 0; | |
708 | ||
709 | if (drv && drv->pm && drv->pm->prepare) | |
710 | ret = drv->pm->prepare(dev); | |
711 | ||
712 | return ret; | |
713 | } | |
714 | ||
715 | static void platform_pm_complete(struct device *dev) | |
716 | { | |
717 | struct device_driver *drv = dev->driver; | |
718 | ||
719 | if (drv && drv->pm && drv->pm->complete) | |
720 | drv->pm->complete(dev); | |
721 | } | |
722 | ||
9d730229 MD |
723 | #else /* !CONFIG_PM_SLEEP */ |
724 | ||
725 | #define platform_pm_prepare NULL | |
726 | #define platform_pm_complete NULL | |
727 | ||
728 | #endif /* !CONFIG_PM_SLEEP */ | |
729 | ||
25e18499 RW |
730 | #ifdef CONFIG_SUSPEND |
731 | ||
732 | static int platform_pm_suspend(struct device *dev) | |
733 | { | |
734 | struct device_driver *drv = dev->driver; | |
735 | int ret = 0; | |
736 | ||
adf09493 RW |
737 | if (!drv) |
738 | return 0; | |
739 | ||
740 | if (drv->pm) { | |
25e18499 RW |
741 | if (drv->pm->suspend) |
742 | ret = drv->pm->suspend(dev); | |
743 | } else { | |
744 | ret = platform_legacy_suspend(dev, PMSG_SUSPEND); | |
745 | } | |
746 | ||
747 | return ret; | |
748 | } | |
749 | ||
750 | static int platform_pm_suspend_noirq(struct device *dev) | |
751 | { | |
adf09493 | 752 | struct device_driver *drv = dev->driver; |
25e18499 RW |
753 | int ret = 0; |
754 | ||
adf09493 | 755 | if (!drv) |
25e18499 RW |
756 | return 0; |
757 | ||
adf09493 RW |
758 | if (drv->pm) { |
759 | if (drv->pm->suspend_noirq) | |
760 | ret = drv->pm->suspend_noirq(dev); | |
25e18499 RW |
761 | } |
762 | ||
763 | return ret; | |
764 | } | |
765 | ||
766 | static int platform_pm_resume(struct device *dev) | |
767 | { | |
768 | struct device_driver *drv = dev->driver; | |
769 | int ret = 0; | |
770 | ||
adf09493 RW |
771 | if (!drv) |
772 | return 0; | |
773 | ||
774 | if (drv->pm) { | |
25e18499 RW |
775 | if (drv->pm->resume) |
776 | ret = drv->pm->resume(dev); | |
777 | } else { | |
778 | ret = platform_legacy_resume(dev); | |
779 | } | |
780 | ||
781 | return ret; | |
782 | } | |
783 | ||
784 | static int platform_pm_resume_noirq(struct device *dev) | |
785 | { | |
adf09493 | 786 | struct device_driver *drv = dev->driver; |
25e18499 RW |
787 | int ret = 0; |
788 | ||
adf09493 | 789 | if (!drv) |
25e18499 RW |
790 | return 0; |
791 | ||
adf09493 RW |
792 | if (drv->pm) { |
793 | if (drv->pm->resume_noirq) | |
794 | ret = drv->pm->resume_noirq(dev); | |
25e18499 RW |
795 | } |
796 | ||
797 | return ret; | |
798 | } | |
799 | ||
800 | #else /* !CONFIG_SUSPEND */ | |
801 | ||
802 | #define platform_pm_suspend NULL | |
803 | #define platform_pm_resume NULL | |
804 | #define platform_pm_suspend_noirq NULL | |
805 | #define platform_pm_resume_noirq NULL | |
806 | ||
807 | #endif /* !CONFIG_SUSPEND */ | |
808 | ||
809 | #ifdef CONFIG_HIBERNATION | |
810 | ||
811 | static int platform_pm_freeze(struct device *dev) | |
812 | { | |
813 | struct device_driver *drv = dev->driver; | |
814 | int ret = 0; | |
815 | ||
816 | if (!drv) | |
817 | return 0; | |
818 | ||
819 | if (drv->pm) { | |
820 | if (drv->pm->freeze) | |
821 | ret = drv->pm->freeze(dev); | |
822 | } else { | |
823 | ret = platform_legacy_suspend(dev, PMSG_FREEZE); | |
824 | } | |
825 | ||
826 | return ret; | |
827 | } | |
828 | ||
829 | static int platform_pm_freeze_noirq(struct device *dev) | |
830 | { | |
adf09493 | 831 | struct device_driver *drv = dev->driver; |
25e18499 RW |
832 | int ret = 0; |
833 | ||
adf09493 | 834 | if (!drv) |
25e18499 RW |
835 | return 0; |
836 | ||
adf09493 RW |
837 | if (drv->pm) { |
838 | if (drv->pm->freeze_noirq) | |
839 | ret = drv->pm->freeze_noirq(dev); | |
25e18499 RW |
840 | } |
841 | ||
842 | return ret; | |
843 | } | |
844 | ||
845 | static int platform_pm_thaw(struct device *dev) | |
846 | { | |
847 | struct device_driver *drv = dev->driver; | |
848 | int ret = 0; | |
849 | ||
adf09493 RW |
850 | if (!drv) |
851 | return 0; | |
852 | ||
853 | if (drv->pm) { | |
25e18499 RW |
854 | if (drv->pm->thaw) |
855 | ret = drv->pm->thaw(dev); | |
856 | } else { | |
857 | ret = platform_legacy_resume(dev); | |
858 | } | |
859 | ||
860 | return ret; | |
861 | } | |
862 | ||
863 | static int platform_pm_thaw_noirq(struct device *dev) | |
864 | { | |
adf09493 | 865 | struct device_driver *drv = dev->driver; |
25e18499 RW |
866 | int ret = 0; |
867 | ||
adf09493 | 868 | if (!drv) |
25e18499 RW |
869 | return 0; |
870 | ||
adf09493 RW |
871 | if (drv->pm) { |
872 | if (drv->pm->thaw_noirq) | |
873 | ret = drv->pm->thaw_noirq(dev); | |
25e18499 RW |
874 | } |
875 | ||
876 | return ret; | |
877 | } | |
878 | ||
879 | static int platform_pm_poweroff(struct device *dev) | |
880 | { | |
881 | struct device_driver *drv = dev->driver; | |
882 | int ret = 0; | |
883 | ||
adf09493 RW |
884 | if (!drv) |
885 | return 0; | |
886 | ||
887 | if (drv->pm) { | |
25e18499 RW |
888 | if (drv->pm->poweroff) |
889 | ret = drv->pm->poweroff(dev); | |
890 | } else { | |
891 | ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); | |
892 | } | |
893 | ||
894 | return ret; | |
895 | } | |
896 | ||
897 | static int platform_pm_poweroff_noirq(struct device *dev) | |
898 | { | |
adf09493 | 899 | struct device_driver *drv = dev->driver; |
25e18499 RW |
900 | int ret = 0; |
901 | ||
adf09493 | 902 | if (!drv) |
25e18499 RW |
903 | return 0; |
904 | ||
adf09493 RW |
905 | if (drv->pm) { |
906 | if (drv->pm->poweroff_noirq) | |
907 | ret = drv->pm->poweroff_noirq(dev); | |
25e18499 RW |
908 | } |
909 | ||
910 | return ret; | |
911 | } | |
912 | ||
913 | static int platform_pm_restore(struct device *dev) | |
914 | { | |
915 | struct device_driver *drv = dev->driver; | |
916 | int ret = 0; | |
917 | ||
adf09493 RW |
918 | if (!drv) |
919 | return 0; | |
920 | ||
921 | if (drv->pm) { | |
25e18499 RW |
922 | if (drv->pm->restore) |
923 | ret = drv->pm->restore(dev); | |
924 | } else { | |
925 | ret = platform_legacy_resume(dev); | |
926 | } | |
927 | ||
928 | return ret; | |
929 | } | |
930 | ||
931 | static int platform_pm_restore_noirq(struct device *dev) | |
932 | { | |
adf09493 | 933 | struct device_driver *drv = dev->driver; |
25e18499 RW |
934 | int ret = 0; |
935 | ||
adf09493 | 936 | if (!drv) |
25e18499 RW |
937 | return 0; |
938 | ||
adf09493 RW |
939 | if (drv->pm) { |
940 | if (drv->pm->restore_noirq) | |
941 | ret = drv->pm->restore_noirq(dev); | |
25e18499 RW |
942 | } |
943 | ||
944 | return ret; | |
945 | } | |
946 | ||
947 | #else /* !CONFIG_HIBERNATION */ | |
948 | ||
949 | #define platform_pm_freeze NULL | |
950 | #define platform_pm_thaw NULL | |
951 | #define platform_pm_poweroff NULL | |
952 | #define platform_pm_restore NULL | |
953 | #define platform_pm_freeze_noirq NULL | |
954 | #define platform_pm_thaw_noirq NULL | |
955 | #define platform_pm_poweroff_noirq NULL | |
956 | #define platform_pm_restore_noirq NULL | |
957 | ||
958 | #endif /* !CONFIG_HIBERNATION */ | |
959 | ||
9d730229 MD |
960 | #ifdef CONFIG_PM_RUNTIME |
961 | ||
962 | int __weak platform_pm_runtime_suspend(struct device *dev) | |
963 | { | |
964 | return -ENOSYS; | |
965 | }; | |
966 | ||
967 | int __weak platform_pm_runtime_resume(struct device *dev) | |
968 | { | |
969 | return -ENOSYS; | |
970 | }; | |
971 | ||
972 | int __weak platform_pm_runtime_idle(struct device *dev) | |
973 | { | |
974 | return -ENOSYS; | |
975 | }; | |
976 | ||
977 | #else /* !CONFIG_PM_RUNTIME */ | |
978 | ||
979 | #define platform_pm_runtime_suspend NULL | |
980 | #define platform_pm_runtime_resume NULL | |
981 | #define platform_pm_runtime_idle NULL | |
982 | ||
983 | #endif /* !CONFIG_PM_RUNTIME */ | |
984 | ||
d9ab7716 | 985 | static const struct dev_pm_ops platform_dev_pm_ops = { |
adf09493 RW |
986 | .prepare = platform_pm_prepare, |
987 | .complete = platform_pm_complete, | |
988 | .suspend = platform_pm_suspend, | |
989 | .resume = platform_pm_resume, | |
990 | .freeze = platform_pm_freeze, | |
991 | .thaw = platform_pm_thaw, | |
992 | .poweroff = platform_pm_poweroff, | |
993 | .restore = platform_pm_restore, | |
25e18499 RW |
994 | .suspend_noirq = platform_pm_suspend_noirq, |
995 | .resume_noirq = platform_pm_resume_noirq, | |
996 | .freeze_noirq = platform_pm_freeze_noirq, | |
997 | .thaw_noirq = platform_pm_thaw_noirq, | |
998 | .poweroff_noirq = platform_pm_poweroff_noirq, | |
999 | .restore_noirq = platform_pm_restore_noirq, | |
9d730229 MD |
1000 | .runtime_suspend = platform_pm_runtime_suspend, |
1001 | .runtime_resume = platform_pm_runtime_resume, | |
1002 | .runtime_idle = platform_pm_runtime_idle, | |
25e18499 RW |
1003 | }; |
1004 | ||
1da177e4 LT |
1005 | struct bus_type platform_bus_type = { |
1006 | .name = "platform", | |
a0245f7a | 1007 | .dev_attrs = platform_dev_attrs, |
1da177e4 | 1008 | .match = platform_match, |
a0245f7a | 1009 | .uevent = platform_uevent, |
9d730229 | 1010 | .pm = &platform_dev_pm_ops, |
1da177e4 | 1011 | }; |
a96b2042 | 1012 | EXPORT_SYMBOL_GPL(platform_bus_type); |
1da177e4 LT |
1013 | |
1014 | int __init platform_bus_init(void) | |
1015 | { | |
fbfb1445 CH |
1016 | int error; |
1017 | ||
13977091 MD |
1018 | early_platform_cleanup(); |
1019 | ||
fbfb1445 CH |
1020 | error = device_register(&platform_bus); |
1021 | if (error) | |
1022 | return error; | |
1023 | error = bus_register(&platform_bus_type); | |
1024 | if (error) | |
1025 | device_unregister(&platform_bus); | |
1026 | return error; | |
1da177e4 LT |
1027 | } |
1028 | ||
1029 | #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK | |
1030 | u64 dma_get_required_mask(struct device *dev) | |
1031 | { | |
1032 | u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); | |
1033 | u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); | |
1034 | u64 mask; | |
1035 | ||
1036 | if (!high_totalram) { | |
1037 | /* convert to mask just covering totalram */ | |
1038 | low_totalram = (1 << (fls(low_totalram) - 1)); | |
1039 | low_totalram += low_totalram - 1; | |
1040 | mask = low_totalram; | |
1041 | } else { | |
1042 | high_totalram = (1 << (fls(high_totalram) - 1)); | |
1043 | high_totalram += high_totalram - 1; | |
1044 | mask = (((u64)high_totalram) << 32) + 0xffffffff; | |
1045 | } | |
e88a0c2c | 1046 | return mask; |
1da177e4 LT |
1047 | } |
1048 | EXPORT_SYMBOL_GPL(dma_get_required_mask); | |
1049 | #endif | |
13977091 MD |
1050 | |
1051 | static __initdata LIST_HEAD(early_platform_driver_list); | |
1052 | static __initdata LIST_HEAD(early_platform_device_list); | |
1053 | ||
1054 | /** | |
4d26e139 | 1055 | * early_platform_driver_register - register early platform driver |
d86c1302 | 1056 | * @epdrv: early_platform driver structure |
13977091 | 1057 | * @buf: string passed from early_param() |
4d26e139 MD |
1058 | * |
1059 | * Helper function for early_platform_init() / early_platform_init_buffer() | |
13977091 MD |
1060 | */ |
1061 | int __init early_platform_driver_register(struct early_platform_driver *epdrv, | |
1062 | char *buf) | |
1063 | { | |
c60e0504 | 1064 | char *tmp; |
13977091 MD |
1065 | int n; |
1066 | ||
1067 | /* Simply add the driver to the end of the global list. | |
1068 | * Drivers will by default be put on the list in compiled-in order. | |
1069 | */ | |
1070 | if (!epdrv->list.next) { | |
1071 | INIT_LIST_HEAD(&epdrv->list); | |
1072 | list_add_tail(&epdrv->list, &early_platform_driver_list); | |
1073 | } | |
1074 | ||
1075 | /* If the user has specified device then make sure the driver | |
1076 | * gets prioritized. The driver of the last device specified on | |
1077 | * command line will be put first on the list. | |
1078 | */ | |
1079 | n = strlen(epdrv->pdrv->driver.name); | |
1080 | if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) { | |
1081 | list_move(&epdrv->list, &early_platform_driver_list); | |
1082 | ||
c60e0504 MD |
1083 | /* Allow passing parameters after device name */ |
1084 | if (buf[n] == '\0' || buf[n] == ',') | |
13977091 | 1085 | epdrv->requested_id = -1; |
c60e0504 MD |
1086 | else { |
1087 | epdrv->requested_id = simple_strtoul(&buf[n + 1], | |
1088 | &tmp, 10); | |
1089 | ||
1090 | if (buf[n] != '.' || (tmp == &buf[n + 1])) { | |
1091 | epdrv->requested_id = EARLY_PLATFORM_ID_ERROR; | |
1092 | n = 0; | |
1093 | } else | |
1094 | n += strcspn(&buf[n + 1], ",") + 1; | |
1095 | } | |
1096 | ||
1097 | if (buf[n] == ',') | |
1098 | n++; | |
1099 | ||
1100 | if (epdrv->bufsize) { | |
1101 | memcpy(epdrv->buffer, &buf[n], | |
1102 | min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1)); | |
1103 | epdrv->buffer[epdrv->bufsize - 1] = '\0'; | |
1104 | } | |
13977091 MD |
1105 | } |
1106 | ||
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | /** | |
4d26e139 | 1111 | * early_platform_add_devices - adds a number of early platform devices |
13977091 MD |
1112 | * @devs: array of early platform devices to add |
1113 | * @num: number of early platform devices in array | |
4d26e139 MD |
1114 | * |
1115 | * Used by early architecture code to register early platform devices and | |
1116 | * their platform data. | |
13977091 MD |
1117 | */ |
1118 | void __init early_platform_add_devices(struct platform_device **devs, int num) | |
1119 | { | |
1120 | struct device *dev; | |
1121 | int i; | |
1122 | ||
1123 | /* simply add the devices to list */ | |
1124 | for (i = 0; i < num; i++) { | |
1125 | dev = &devs[i]->dev; | |
1126 | ||
1127 | if (!dev->devres_head.next) { | |
1128 | INIT_LIST_HEAD(&dev->devres_head); | |
1129 | list_add_tail(&dev->devres_head, | |
1130 | &early_platform_device_list); | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | /** | |
4d26e139 | 1136 | * early_platform_driver_register_all - register early platform drivers |
13977091 | 1137 | * @class_str: string to identify early platform driver class |
4d26e139 MD |
1138 | * |
1139 | * Used by architecture code to register all early platform drivers | |
1140 | * for a certain class. If omitted then only early platform drivers | |
1141 | * with matching kernel command line class parameters will be registered. | |
13977091 MD |
1142 | */ |
1143 | void __init early_platform_driver_register_all(char *class_str) | |
1144 | { | |
1145 | /* The "class_str" parameter may or may not be present on the kernel | |
1146 | * command line. If it is present then there may be more than one | |
1147 | * matching parameter. | |
1148 | * | |
1149 | * Since we register our early platform drivers using early_param() | |
1150 | * we need to make sure that they also get registered in the case | |
1151 | * when the parameter is missing from the kernel command line. | |
1152 | * | |
1153 | * We use parse_early_options() to make sure the early_param() gets | |
1154 | * called at least once. The early_param() may be called more than | |
1155 | * once since the name of the preferred device may be specified on | |
1156 | * the kernel command line. early_platform_driver_register() handles | |
1157 | * this case for us. | |
1158 | */ | |
1159 | parse_early_options(class_str); | |
1160 | } | |
1161 | ||
1162 | /** | |
4d26e139 | 1163 | * early_platform_match - find early platform device matching driver |
d86c1302 | 1164 | * @epdrv: early platform driver structure |
13977091 MD |
1165 | * @id: id to match against |
1166 | */ | |
1167 | static __init struct platform_device * | |
1168 | early_platform_match(struct early_platform_driver *epdrv, int id) | |
1169 | { | |
1170 | struct platform_device *pd; | |
1171 | ||
1172 | list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) | |
1173 | if (platform_match(&pd->dev, &epdrv->pdrv->driver)) | |
1174 | if (pd->id == id) | |
1175 | return pd; | |
1176 | ||
1177 | return NULL; | |
1178 | } | |
1179 | ||
1180 | /** | |
4d26e139 | 1181 | * early_platform_left - check if early platform driver has matching devices |
d86c1302 | 1182 | * @epdrv: early platform driver structure |
13977091 MD |
1183 | * @id: return true if id or above exists |
1184 | */ | |
1185 | static __init int early_platform_left(struct early_platform_driver *epdrv, | |
1186 | int id) | |
1187 | { | |
1188 | struct platform_device *pd; | |
1189 | ||
1190 | list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) | |
1191 | if (platform_match(&pd->dev, &epdrv->pdrv->driver)) | |
1192 | if (pd->id >= id) | |
1193 | return 1; | |
1194 | ||
1195 | return 0; | |
1196 | } | |
1197 | ||
1198 | /** | |
4d26e139 | 1199 | * early_platform_driver_probe_id - probe drivers matching class_str and id |
13977091 MD |
1200 | * @class_str: string to identify early platform driver class |
1201 | * @id: id to match against | |
1202 | * @nr_probe: number of platform devices to successfully probe before exiting | |
1203 | */ | |
1204 | static int __init early_platform_driver_probe_id(char *class_str, | |
1205 | int id, | |
1206 | int nr_probe) | |
1207 | { | |
1208 | struct early_platform_driver *epdrv; | |
1209 | struct platform_device *match; | |
1210 | int match_id; | |
1211 | int n = 0; | |
1212 | int left = 0; | |
1213 | ||
1214 | list_for_each_entry(epdrv, &early_platform_driver_list, list) { | |
1215 | /* only use drivers matching our class_str */ | |
1216 | if (strcmp(class_str, epdrv->class_str)) | |
1217 | continue; | |
1218 | ||
1219 | if (id == -2) { | |
1220 | match_id = epdrv->requested_id; | |
1221 | left = 1; | |
1222 | ||
1223 | } else { | |
1224 | match_id = id; | |
1225 | left += early_platform_left(epdrv, id); | |
1226 | ||
1227 | /* skip requested id */ | |
1228 | switch (epdrv->requested_id) { | |
1229 | case EARLY_PLATFORM_ID_ERROR: | |
1230 | case EARLY_PLATFORM_ID_UNSET: | |
1231 | break; | |
1232 | default: | |
1233 | if (epdrv->requested_id == id) | |
1234 | match_id = EARLY_PLATFORM_ID_UNSET; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | switch (match_id) { | |
1239 | case EARLY_PLATFORM_ID_ERROR: | |
1240 | pr_warning("%s: unable to parse %s parameter\n", | |
1241 | class_str, epdrv->pdrv->driver.name); | |
1242 | /* fall-through */ | |
1243 | case EARLY_PLATFORM_ID_UNSET: | |
1244 | match = NULL; | |
1245 | break; | |
1246 | default: | |
1247 | match = early_platform_match(epdrv, match_id); | |
1248 | } | |
1249 | ||
1250 | if (match) { | |
1251 | if (epdrv->pdrv->probe(match)) | |
1252 | pr_warning("%s: unable to probe %s early.\n", | |
1253 | class_str, match->name); | |
1254 | else | |
1255 | n++; | |
1256 | } | |
1257 | ||
1258 | if (n >= nr_probe) | |
1259 | break; | |
1260 | } | |
1261 | ||
1262 | if (left) | |
1263 | return n; | |
1264 | else | |
1265 | return -ENODEV; | |
1266 | } | |
1267 | ||
1268 | /** | |
4d26e139 | 1269 | * early_platform_driver_probe - probe a class of registered drivers |
13977091 MD |
1270 | * @class_str: string to identify early platform driver class |
1271 | * @nr_probe: number of platform devices to successfully probe before exiting | |
1272 | * @user_only: only probe user specified early platform devices | |
4d26e139 MD |
1273 | * |
1274 | * Used by architecture code to probe registered early platform drivers | |
1275 | * within a certain class. For probe to happen a registered early platform | |
1276 | * device matching a registered early platform driver is needed. | |
13977091 MD |
1277 | */ |
1278 | int __init early_platform_driver_probe(char *class_str, | |
1279 | int nr_probe, | |
1280 | int user_only) | |
1281 | { | |
1282 | int k, n, i; | |
1283 | ||
1284 | n = 0; | |
1285 | for (i = -2; n < nr_probe; i++) { | |
1286 | k = early_platform_driver_probe_id(class_str, i, nr_probe - n); | |
1287 | ||
1288 | if (k < 0) | |
1289 | break; | |
1290 | ||
1291 | n += k; | |
1292 | ||
1293 | if (user_only) | |
1294 | break; | |
1295 | } | |
1296 | ||
1297 | return n; | |
1298 | } | |
1299 | ||
1300 | /** | |
1301 | * early_platform_cleanup - clean up early platform code | |
1302 | */ | |
1303 | void __init early_platform_cleanup(void) | |
1304 | { | |
1305 | struct platform_device *pd, *pd2; | |
1306 | ||
1307 | /* clean up the devres list used to chain devices */ | |
1308 | list_for_each_entry_safe(pd, pd2, &early_platform_device_list, | |
1309 | dev.devres_head) { | |
1310 | list_del(&pd->dev.devres_head); | |
1311 | memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head)); | |
1312 | } | |
1313 | } | |
1314 |