device_create_file的用法详解

本文介绍了如何在Linux内核设备驱动中使用`device_create_file`函数在`sysfs`目录下创建设备属性文件。代码示例展示了在设备初始化时创建名为`version`的属性文件,该文件具有读写权限,并在设备移除时删除。创建的文件路径为`/sys/devices/platform/my_platform_device003/version`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>

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

逐行解释代码:#include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/uaccess.h> #include <linux/ctype.h> #define DEVICE_NAME "mydev" #define CLASS_NAME "myclass" MODULE_LICENSE("GPL"); MODULE_AUTHOR("YourName"); MODULE_DESCRIPTION("Character device driver with uppercase conversion"); static int cap = 0; module_param(cap, int, S_IRUGO | S_IWUSR); MODULE_PARM_DESC(cap, "Enable uppercase conversion (1=enabled)"); static int major_num; static struct class* dev_class = NULL; static struct cdev my_cdev; static int device_open(struct inode *inode, struct file *file) { return 0; } static int device_release(struct inode *inode, struct file *file) { return 0; } static ssize_t device_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) { char kernel_buffer[1024]; size_t bytes_to_copy = min(count, sizeof(kernel_buffer) - 1); // Copy data from userspace if (copy_from_user(kernel_buffer, buf, bytes_to_copy)) { return -EFAULT; } kernel_buffer[bytes_to_copy] = '\0'; // Uppercase conversion if enabled if (cap) { for (int i = 0; kernel_buffer[i]; i++) { kernel_buffer[i] = toupper(kernel_buffer[i]); } } // Print to kernel log printk(KERN_INFO "mydev: %s\n", kernel_buffer); return bytes_to_copy; } static struct file_operations fops = { .owner = THIS_MODULE, .open = device_open, .release = device_release, .write = device_write, }; static int __init mydev_init(void) { // Allocate device numbers if (alloc_chrdev_region(&major_num, 0, 1, DEVICE_NAME) < 0) { return -1; } // Create character device cdev_init(&my_cdev, &fops); if (cdev_add(&my_cdev, major_num, 1) < 0) { unregister_chrdev_region(major_num, 1); return -1; } // Create device class dev_class = class_create(THIS_MODULE, CLASS_NAME); if (IS_ERR(dev_class)) { cdev_del(&my_cdev); unregister_chrdev_region(major_num, 1); return PTR_ERR(dev_class); } // Create device node device_create(dev_class, NULL, major_num, NULL, DEVICE_NAME); printk(KERN_INFO "mydev: Loaded with cap=%d\n", cap); return 0; } static void __exit mydev_exit(void) { device_destroy(dev_class, major_num); class_destroy(dev_class); cdev_del(&my_cdev); unregister_chrdev_region(major_num, 1); printk(KERN_INFO "mydev: Unloaded\n"); } module_init(mydev_init); module_exit(mydev_exit);
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值