Skip to content

Commit 44f28bd

Browse files
Uwe Kleine-Königgregkh
authored andcommitted
Driver core: reduce duplicated code for platform_device creation
This makes the two similar functions platform_device_register_simple and platform_device_register_data one line inline functions using a new generic function platform_device_register_resndata. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 3e61dfd commit 44f28bd

3 files changed

Lines changed: 85 additions & 82 deletions

File tree

‎Documentation/DocBook/device-drivers.tmpl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ X!Edrivers/base/attribute_container.c
111111
<!--
112112
X!Edrivers/base/interface.c
113113
-->
114+
!Iinclude/linux/platform_device.h
114115
!Edrivers/base/platform.c
115116
!Edrivers/base/bus.c
116117
</sect1>

‎drivers/base/platform.c‎

Lines changed: 26 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -344,108 +344,56 @@ void platform_device_unregister(struct platform_device *pdev)
344344
EXPORT_SYMBOL_GPL(platform_device_unregister);
345345

346346
/**
347-
* platform_device_register_simple - add a platform-level device and its resources
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
352-
*
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.
347+
* platform_device_register_resndata - add a platform-level device with
348+
* resources and platform-specific data
358349
*
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".
365-
*
366-
* Returns &struct platform_device pointer on success, or ERR_PTR() on error.
367-
*/
368-
struct platform_device *platform_device_register_simple(const char *name,
369-
int id,
370-
const struct resource *res,
371-
unsigned int num)
372-
{
373-
struct platform_device *pdev;
374-
int retval;
375-
376-
pdev = platform_device_alloc(name, id);
377-
if (!pdev) {
378-
retval = -ENOMEM;
379-
goto error;
380-
}
381-
382-
if (num) {
383-
retval = platform_device_add_resources(pdev, res, num);
384-
if (retval)
385-
goto error;
386-
}
387-
388-
retval = platform_device_add(pdev);
389-
if (retval)
390-
goto error;
391-
392-
return pdev;
393-
394-
error:
395-
platform_device_put(pdev);
396-
return ERR_PTR(retval);
397-
}
398-
EXPORT_SYMBOL_GPL(platform_device_register_simple);
399-
400-
/**
401-
* platform_device_register_data - add a platform-level device with platform-specific data
402350
* @parent: parent device for the device we're adding
403351
* @name: base name of the device we're adding
404352
* @id: instance id
353+
* @res: set of resources that needs to be allocated for the device
354+
* @num: number of resources
405355
* @data: platform specific data for this platform device
406356
* @size: size of platform specific data
407357
*
408-
* This function creates a simple platform device that requires minimal
409-
* resource and memory management. Canned release function freeing memory
410-
* allocated for the device allows drivers using such devices to be
411-
* unloaded without waiting for the last reference to the device to be
412-
* dropped.
413-
*
414358
* Returns &struct platform_device pointer on success, or ERR_PTR() on error.
415359
*/
416-
struct platform_device *platform_device_register_data(
360+
struct platform_device *platform_device_register_resndata(
417361
struct device *parent,
418362
const char *name, int id,
363+
const struct resource *res, unsigned int num,
419364
const void *data, size_t size)
420365
{
366+
int ret = -ENOMEM;
421367
struct platform_device *pdev;
422-
int retval;
423368

424369
pdev = platform_device_alloc(name, id);
425-
if (!pdev) {
426-
retval = -ENOMEM;
427-
goto error;
428-
}
370+
if (!pdev)
371+
goto err;
429372

430373
pdev->dev.parent = parent;
431374

432-
if (size) {
433-
retval = platform_device_add_data(pdev, data, size);
434-
if (retval)
435-
goto error;
375+
if (res) {
376+
ret = platform_device_add_resources(pdev, res, num);
377+
if (ret)
378+
goto err;
436379
}
437380

438-
retval = platform_device_add(pdev);
439-
if (retval)
440-
goto error;
381+
if (data) {
382+
ret = platform_device_add_data(pdev, data, size);
383+
if (ret)
384+
goto err;
385+
}
441386

442-
return pdev;
387+
ret = platform_device_add(pdev);
388+
if (ret) {
389+
err:
390+
platform_device_put(pdev);
391+
return ERR_PTR(ret);
392+
}
443393

444-
error:
445-
platform_device_put(pdev);
446-
return ERR_PTR(retval);
394+
return pdev;
447395
}
448-
EXPORT_SYMBOL_GPL(platform_device_register_data);
396+
EXPORT_SYMBOL_GPL(platform_device_register_resndata);
449397

450398
static int platform_drv_probe(struct device *_dev)
451399
{

‎include/linux/platform_device.h‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,64 @@ extern struct resource *platform_get_resource_byname(struct platform_device *, u
4343
extern int platform_get_irq_byname(struct platform_device *, const char *);
4444
extern int platform_add_devices(struct platform_device **, int);
4545

46-
extern struct platform_device *platform_device_register_simple(const char *, int id,
47-
const struct resource *, unsigned int);
48-
extern struct platform_device *platform_device_register_data(struct device *,
49-
const char *, int, const void *, size_t);
46+
extern struct platform_device *platform_device_register_resndata(
47+
struct device *parent, const char *name, int id,
48+
const struct resource *res, unsigned int num,
49+
const void *data, size_t size);
50+
51+
/**
52+
* platform_device_register_simple - add a platform-level device and its resources
53+
* @name: base name of the device we're adding
54+
* @id: instance id
55+
* @res: set of resources that needs to be allocated for the device
56+
* @num: number of resources
57+
*
58+
* This function creates a simple platform device that requires minimal
59+
* resource and memory management. Canned release function freeing memory
60+
* allocated for the device allows drivers using such devices to be
61+
* unloaded without waiting for the last reference to the device to be
62+
* dropped.
63+
*
64+
* This interface is primarily intended for use with legacy drivers which
65+
* probe hardware directly. Because such drivers create sysfs device nodes
66+
* themselves, rather than letting system infrastructure handle such device
67+
* enumeration tasks, they don't fully conform to the Linux driver model.
68+
* In particular, when such drivers are built as modules, they can't be
69+
* "hotplugged".
70+
*
71+
* Returns &struct platform_device pointer on success, or ERR_PTR() on error.
72+
*/
73+
static inline struct platform_device *platform_device_register_simple(
74+
const char *name, int id,
75+
const struct resource *res, unsigned int num)
76+
{
77+
return platform_device_register_resndata(NULL, name, id,
78+
res, num, NULL, 0);
79+
}
80+
81+
/**
82+
* platform_device_register_data - add a platform-level device with platform-specific data
83+
* @parent: parent device for the device we're adding
84+
* @name: base name of the device we're adding
85+
* @id: instance id
86+
* @data: platform specific data for this platform device
87+
* @size: size of platform specific data
88+
*
89+
* This function creates a simple platform device that requires minimal
90+
* resource and memory management. Canned release function freeing memory
91+
* allocated for the device allows drivers using such devices to be
92+
* unloaded without waiting for the last reference to the device to be
93+
* dropped.
94+
*
95+
* Returns &struct platform_device pointer on success, or ERR_PTR() on error.
96+
*/
97+
static inline struct platform_device *platform_device_register_data(
98+
struct device *parent, const char *name, int id,
99+
const void *data, size_t size)
100+
{
101+
return platform_device_register_resndata(parent, name, id,
102+
NULL, 0, data, size);
103+
}
50104

51105
extern struct platform_device *platform_device_alloc(const char *name, int id);
52106
extern int platform_device_add_resources(struct platform_device *pdev,

0 commit comments

Comments
 (0)