在有bios的情况下要测试某个驱动是否可以正常工作的话,经常要改bios比较麻烦,在drivers/rtc/rtc-test.c 中提供了一个方法自己注册platform_device,这样可以避免需修改bios的情况.
static int __init test_init(void)
{
int err;
//注册platform_driver
if ((err = platform_driver_register(&test_driver)))
return err;
//由于有两个device,因此调用platform_device_alloc 来申请两个static struct platform_device *test0 = NULL, *test1 = NULL;
也可以直接调用platform_device_register 来注册,注意这里匹配的name为rtc-test 和 test_driver 中定义的name 一致
if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
err = -ENOMEM;
goto exit_driver_unregister;
}
if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
err = -ENOMEM;
goto exit_put_test0;
}
//调用platform_device_alloc 申请到platform_device 后需要调用platform_device_add 来添加device.
if ((err = platform_device_add(test0)))
goto exit_put_test1;
if ((err = platform_device_add(test1)))
goto exit_del_test0;
return 0;
exit_del_test0:
platform_device_del(test0);
exit_put_test1:
platform_device_put(test1);
exit_put_test0:
platform_device_put(test0);
exit_driver_unregister:
platform_driver_unregister(&test_driver);
return err;
}
module_init(test_init);
static struct platform_driver test_driver = {
.probe = test_probe,
.remove = test_remove,
.driver = {
.name = "rtc-test",
},
};
device和driver 匹配后就会调用test_probe
static int test_probe(struct platform_device *plat_dev)
{
int err;
struct rtc_device *rtc;
if (test_mmss64) {
test_rtc_ops.set_mmss64 = test_rtc_set_mmss64;
test_rtc_ops.set_mmss = NULL;
}
//调用devm_rtc_device_register 来注册rtc设备.
rtc = devm_rtc_device_register(&plat_dev->dev, "test",
&test_rtc_ops, THIS_MODULE);
if (IS_ERR(rtc)) {
return PTR_ERR(rtc);
}
//调用device_create_file 来在sys下为rtc建立入口
err = device_create_file(&plat_dev->dev, &dev_attr_irq);
if (err)
dev_err(&plat_dev->dev, "Unable to create sysfs entry: %s\n",
dev_attr_irq.attr.name);
platform_set_drvdata(plat_dev, rtc);
return 0;
}
static ssize_t test_irq_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", 42);
}
static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
我们以test_irq_show 为例,就是会显示42
static int __init test_init(void)
{
int err;
//注册platform_driver
if ((err = platform_driver_register(&test_driver)))
return err;
//由于有两个device,因此调用platform_device_alloc 来申请两个static struct platform_device *test0 = NULL, *test1 = NULL;
也可以直接调用platform_device_register 来注册,注意这里匹配的name为rtc-test 和 test_driver 中定义的name 一致
if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
err = -ENOMEM;
goto exit_driver_unregister;
}
if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
err = -ENOMEM;
goto exit_put_test0;
}
//调用platform_device_alloc 申请到platform_device 后需要调用platform_device_add 来添加device.
if ((err = platform_device_add(test0)))
goto exit_put_test1;
if ((err = platform_device_add(test1)))
goto exit_del_test0;
return 0;
exit_del_test0:
platform_device_del(test0);
exit_put_test1:
platform_device_put(test1);
exit_put_test0:
platform_device_put(test0);
exit_driver_unregister:
platform_driver_unregister(&test_driver);
return err;
}
module_init(test_init);
static struct platform_driver test_driver = {
.probe = test_probe,
.remove = test_remove,
.driver = {
.name = "rtc-test",
},
};
device和driver 匹配后就会调用test_probe
static int test_probe(struct platform_device *plat_dev)
{
int err;
struct rtc_device *rtc;
if (test_mmss64) {
test_rtc_ops.set_mmss64 = test_rtc_set_mmss64;
test_rtc_ops.set_mmss = NULL;
}
//调用devm_rtc_device_register 来注册rtc设备.
rtc = devm_rtc_device_register(&plat_dev->dev, "test",
&test_rtc_ops, THIS_MODULE);
if (IS_ERR(rtc)) {
return PTR_ERR(rtc);
}
//调用device_create_file 来在sys下为rtc建立入口
err = device_create_file(&plat_dev->dev, &dev_attr_irq);
if (err)
dev_err(&plat_dev->dev, "Unable to create sysfs entry: %s\n",
dev_attr_irq.attr.name);
platform_set_drvdata(plat_dev, rtc);
return 0;
}
static ssize_t test_irq_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", 42);
}
static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
我们以test_irq_show 为例,就是会显示42
本文介绍了一种在不修改BIOS的情况下测试RTC驱动的方法。通过在Linux内核中使用platform_device_alloc和platform_driver_register等函数,可以创建并注册RTC设备进行测试。

258

被折叠的 条评论
为什么被折叠?



