关于Linux driver中device_create()使用的注意事项

本文介绍了在Linux中通过灵活使用device_create()函数避免加载内核时出现oops错误的方法,特别关注了参数使用不当可能导致的问题,并提供了正确的调用方式。
    通过驱动模块的加载在/dev下创建设备文件,在驱动模块卸载时又自动的删除在/dev下创建的设备文件非常方便。而这个过程就是通过device_create()和device_destroy()内核函数完成的,在Linux2.6.27之前是class_device_create()和class_device_destroy()。最近在开发过程中发现device_create()参数使用很灵活,一不小心就会导致加载时内核报oops错误(vsnprintf引起)。google上搜到的一些文章也有着问题。如如下使用就会出错:
    device_create(my_class, NULL, MKDEV(major,minor), "hello" "%d",0);

    在"hello" "%d"之间加上","能解决insmod时oops错误,但是却不能在/dev目录下创建想要的设备文件。正确的调用方法如下:
    device_create(my_class, NULL, MKDEV(major,minor), NULL,"hello%d",0);

逐行解释代码:#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、付费专栏及课程。

余额充值