device_create_file的用法详解

device_create_file 在当前设备的sys目录下创建一个属性对应的文件。
这个方法只能一次创建一个属性文件。
以下是演示代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct device *dev;
int version = 159;

static DEVICE_INT_ATTR(version,S_IWUSR | S_IRUGO,version);

int myprobe(struct platform_device *pdev) {
	dev = &pdev->dev;
	device_create_file(dev, &dev_attr_version.attr);
	pr_info("myplatformdriver myprobe \n");
	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	device_remove_file(dev, &dev_attr_version.attr);
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver =
		{ .of_match_table = &my_of_match_table, .name = "my-platform-driver",
				.owner = THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

以上创建的文件的路径为 : /sys/devices/platform/my_platform_device003/version

你可能感兴趣的:(内核4.4.194,linux)