Writing Pseudo Platform device and Drivers :
#include < linux/init.h >
#include < linux/module.h >
#include < linux/kernel.h >
#include < linux/device.h >
#include < linux/platform_device.h >
#include < linux/slab.h >
struct my_platform_data {
int a;
int b;
};
static struct my_platform_data sample_data = {
.a = 100,
.b = 20,
};
static struct platform_device mydevice_device = {
.name = "mydevice",
.id = 0,
.dev = {
.platform_data = &sample_data,
},
};
struct pvt_data {
int a;
int b;
};
static int my_device_probe(struct platform_device *pdev)
{
struct pvt_data *pvt;
pvt = kmalloc(sizeof(struct pvt_data),GFP_KERNEL);
if(pvt == NULL) {
printk("memory allocation failed\n");
}
struct my_platform_data *pdata;
pdata = pdev->dev.platform_data;
pvt->a = pdata->a;
pvt->b = pdata->b;
platform_set_drvdata(pdev, pvt);
return 0;
}
static int my_device_remove(struct platform_device *dev)
{
struct pvt_data *pvt = platform_get_drvdata(dev);
kfree(pvt);
return 0;
}
static struct platform_driver my_driver = {
.driver = {
.name = "mydevice",
.owner = THIS_MODULE,
},
.probe = my_device_probe,
.remove = my_device_remove,
};
static int __init my_device_init(void)
{
printk("In Init\n");
int ret;
ret = platform_device_register(&mydevice_device);
if(ret != 0) {
printk("Platform device registered successfuly\n");
}
ret = platform_driver_register(&my_driver);
if(ret != 0) {
printk("platform driver registerd successfuly\n");
}
return 0;
}
static void mydevice_exit(void)
{
printk("In exit\n");
platform_device_unregister(&mydevice_device);
platform_driver_unregister(&my_driver);
}
module_init(my_device_init);
module_exit(mydevice_exit);
MODULE_LICENSE("GPL");
________________________________________________________________________
Platform Generic driver
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/init.h >
#include < linux/platform_device.h >
static int __devinit hello_probe(struct platform_device *pdev)
{
printk("hello: %s entry\n", __func__);
return 0;
}
static int __devexit hello_remove(struct platform_device *pdev)
{
printk("hello: %s entry\n", __func__);
return 0;
}
#ifdef CONFIG_PM
int hello_suspend(struct platform_device *pdev, pm_message_t state)
{
return 0;
}
int hello_resume(struct platform_device *pdev, pm_message_t state)
{
return 0;
}
#else
#define hello_suspend NULL
#define hello_resume NULL
#endif
static struct platform_driver hello_driver = {
.driver = {
.name = "hello",
.owner = THIS_MODULE,
},
.probe = hello_probe,
.remove = __devexit_p(hello_remove),
.suspend = hello_suspend,
.resume = hello_resume,
};
static struct platform_device *hello_device;
static int __init hello_init(void)
{
int ret;
/* register the platform device */
hello_device = platform_device_alloc("hello", -1);
if (NULL == hello_device) {
printk("hello: platform device allocation failed\n");
}
ret = platform_device_add(hello_device);
if (ret < 0) {
printk("hello: unable to add platform device\n");
goto err_platform_device_add;
}
ret = platform_driver_register(&hello_driver);
if (ret < 0) {
printk("hello: unable to register platform driver\n");
goto err_platform_driver_register;
}
printk("hello: driver initalized\n");
return 0;
err_platform_device_add:
platform_device_put(hello_device);
err_platform_driver_register:
platform_device_unregister(hello_device);
return ret;
}
static void __exit hello_exit(void)
{
platform_driver_unregister(&hello_driver);
platform_device_unregister(hello_device);
printk("hello: driver exited\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_DESCRIPTION("example: Hello LDM");
MODULE_LICENSE("GPL");
#include < linux/init.h >
#include < linux/module.h >
#include < linux/kernel.h >
#include < linux/device.h >
#include < linux/platform_device.h >
#include < linux/slab.h >
struct my_platform_data {
int a;
int b;
};
static struct my_platform_data sample_data = {
.a = 100,
.b = 20,
};
static struct platform_device mydevice_device = {
.name = "mydevice",
.id = 0,
.dev = {
.platform_data = &sample_data,
},
};
struct pvt_data {
int a;
int b;
};
static int my_device_probe(struct platform_device *pdev)
{
struct pvt_data *pvt;
pvt = kmalloc(sizeof(struct pvt_data),GFP_KERNEL);
if(pvt == NULL) {
printk("memory allocation failed\n");
}
struct my_platform_data *pdata;
pdata = pdev->dev.platform_data;
pvt->a = pdata->a;
pvt->b = pdata->b;
platform_set_drvdata(pdev, pvt);
return 0;
}
static int my_device_remove(struct platform_device *dev)
{
struct pvt_data *pvt = platform_get_drvdata(dev);
kfree(pvt);
return 0;
}
static struct platform_driver my_driver = {
.driver = {
.name = "mydevice",
.owner = THIS_MODULE,
},
.probe = my_device_probe,
.remove = my_device_remove,
};
static int __init my_device_init(void)
{
printk("In Init\n");
int ret;
ret = platform_device_register(&mydevice_device);
if(ret != 0) {
printk("Platform device registered successfuly\n");
}
ret = platform_driver_register(&my_driver);
if(ret != 0) {
printk("platform driver registerd successfuly\n");
}
return 0;
}
static void mydevice_exit(void)
{
printk("In exit\n");
platform_device_unregister(&mydevice_device);
platform_driver_unregister(&my_driver);
}
module_init(my_device_init);
module_exit(mydevice_exit);
MODULE_LICENSE("GPL");
________________________________________________________________________
Platform Generic driver
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/init.h >
#include < linux/platform_device.h >
static int __devinit hello_probe(struct platform_device *pdev)
{
printk("hello: %s entry\n", __func__);
return 0;
}
static int __devexit hello_remove(struct platform_device *pdev)
{
printk("hello: %s entry\n", __func__);
return 0;
}
#ifdef CONFIG_PM
int hello_suspend(struct platform_device *pdev, pm_message_t state)
{
return 0;
}
int hello_resume(struct platform_device *pdev, pm_message_t state)
{
return 0;
}
#else
#define hello_suspend NULL
#define hello_resume NULL
#endif
static struct platform_driver hello_driver = {
.driver = {
.name = "hello",
.owner = THIS_MODULE,
},
.probe = hello_probe,
.remove = __devexit_p(hello_remove),
.suspend = hello_suspend,
.resume = hello_resume,
};
static struct platform_device *hello_device;
static int __init hello_init(void)
{
int ret;
/* register the platform device */
hello_device = platform_device_alloc("hello", -1);
if (NULL == hello_device) {
printk("hello: platform device allocation failed\n");
}
ret = platform_device_add(hello_device);
if (ret < 0) {
printk("hello: unable to add platform device\n");
goto err_platform_device_add;
}
ret = platform_driver_register(&hello_driver);
if (ret < 0) {
printk("hello: unable to register platform driver\n");
goto err_platform_driver_register;
}
printk("hello: driver initalized\n");
return 0;
err_platform_device_add:
platform_device_put(hello_device);
err_platform_driver_register:
platform_device_unregister(hello_device);
return ret;
}
static void __exit hello_exit(void)
{
platform_driver_unregister(&hello_driver);
platform_device_unregister(hello_device);
printk("hello: driver exited\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_DESCRIPTION("example: Hello LDM");
MODULE_LICENSE("GPL");
No comments:
Post a Comment