* On platform device template.
@ 2014-08-10 7:57 mind entropy
0 siblings, 0 replies; only message in thread
From: mind entropy @ 2014-08-10 7:57 UTC (permalink / raw)
To: kernelnewbies
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.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2014-08-10 7:57 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-10 7:57 On platform device template mind entropy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).