From: mindentropy@gmail.com (mind entropy)
To: kernelnewbies@lists.kernelnewbies.org
Subject: On platform device template.
Date: Sun, 10 Aug 2014 13:27:37 +0530 [thread overview]
Message-ID: <CAM2a4uwBxGgOPWp1P0zAXdv-QX6A=Smy4xDBLYb_hqsgJsXkyw@mail.gmail.com> (raw)
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.
reply other threads:[~2014-08-10 7:57 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAM2a4uwBxGgOPWp1P0zAXdv-QX6A=Smy4xDBLYb_hqsgJsXkyw@mail.gmail.com' \
--to=mindentropy@gmail.com \
--cc=kernelnewbies@lists.kernelnewbies.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).