On platform device template.
mind entropy
mindentropy at gmail.com
Sun Aug 10 03:57:37 EDT 2014
Hi,
I am testing out the platform driver and would like to know the
template for platform device register/unregister.
The following code causes a crash whenever the device gets unregistered.
---------------------------------------------------
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/device.h>
struct test_platform_data {
int x;
int y;
};
static struct test_platform_data test_pd = {
.x = 2,
.y = 3,
};
static struct platform_device test_platform_device = {
.name = "test-plat",
.id = -1,
.resource = NULL,
.num_resources = 0,
.dev = {
.platform_data = &test_pd,
}
};
static int __init platform_driver_test_init(void) {
int retval = 0;
printk(KERN_ALERT "Platform driver init\n");
retval = platform_device_register(&test_platform_device);
if(retval != 0) {
printk(KERN_ALERT "Could not register platform device\n");
return retval;
} else {
printk(KERN_ALERT "Registered platform device\n");
printk(KERN_ALERT "Unregistering\n");
platform_device_unregister(&test_platform_device);
}
return 0;
}
static void __exit platform_driver_test_exit(void) {
printk(KERN_ALERT "Platform driver exit\n");
}
module_init(platform_driver_test_init);
module_exit(platform_driver_test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("TEST");
MODULE_DESCRIPTION("Platform driver test");
---------------------------------------------------
Should the platform device be created using platform_device_alloc(..)
and added using platform_device_add(..) like the below code?
----------------------------------------------------
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/device.h>
struct test_platform_data {
int x;
int y;
};
static struct test_platform_data test_pd = {
.x = 2,
.y = 3,
};
static struct platform_device *test_platform_device;
static int __init platform_driver_test_init(void) {
int retval = 0;
printk(KERN_ALERT "Platform driver init\n");
test_platform_device = platform_device_alloc("test-plat",-1);
if(test_platform_device == NULL) {
printk(KERN_ALERT "Could not allocate platform device\n");
return 0;
}
platform_device_add_data(test_platform_device,&test_pd,sizeof(test_pd));
retval = platform_device_add(test_platform_device);
if(retval != 0) {
printk(KERN_ALERT "Could not register platform device\n");
return retval;
} else {
printk(KERN_ALERT "Registered platform device\n");
printk(KERN_ALERT "Unregistering\n");
platform_device_unregister(test_platform_device);
}
return 0;
}
static void __exit platform_driver_test_exit(void) {
printk(KERN_ALERT "Platform driver exit\n");
}
module_init(platform_driver_test_init);
module_exit(platform_driver_test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("TEST");
MODULE_DESCRIPTION("Platform driver test");
----------------------------------------------------
Thanks,
Gautam.
More information about the Kernelnewbies
mailing list