快速链接:
.
👉👉👉 Linux内核驱动面试-百问百答-[目录] 👈👈👈
- 付费专栏-付费课程 【购买须知】
- 个人博客笔记导读目录(全部)
向Linux内核添加新的设备驱动通常涉及以下几个步骤:编写驱动程序代码、配置和编译内核、安装和加载驱动程序以及测试驱动程序。下面是详细的步骤:
1. 编写设备驱动程序代码
a. 包含必要的头文件
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
b. 定义必要的数据结构和变量
#define DEVICE_NAME "simple_char_device"
static int major_number;
static struct cdev my_cdev;
static char device_buffer[256];
c. 实现设备文件操作函数
static int device_open(struct inode *inode, struct file *file) {
printk(KERN_INFO "Device opened\n");
return 0;
}
static ssize_t device_read(struct file *file, char __user *user_buffer, size_t size, loff_t *offset) {
size_t to_copy, not_copied;
to_copy = min(size, sizeof(device_buffer));
not_copied = copy_to_user(user_buffer, device_buffer, to_copy);
return to_copy - not_copied;
}
static ssize_t device_write(struct file *file, const char __user *user_buffer, size_t size, loff_t *offset) {
size_t to_copy, not_copied;
to_copy = min(size, sizeof(device_buffer));
not_copied = copy_from_user(device_buffer, user_buffer, to_copy);
return to_copy - not_copied;
}
static int device_release(struct inode *inode, struct file *file) {
printk(KERN_INFO "Device closed\n");
return 0;
}
d. 定义文件操作结构体
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = device_open,
.read = device_read,
.write = device_write,
.release = device_release,
};
2. 初始化和清理模块
a. 初始化模块
static int __init simple_char_init(void) {
major_number = register_chrdev(0, DEVICE_NAME, &fops);
if (major_number < 0) {
printk(KERN_ALERT "Failed to register a major number\n");
return major_number;
}
printk(KERN_INFO "Registered correctly with major number %d\n", major_number);
cdev_init(&my_cdev, &fops);
if (cdev_add(&my_cdev, MKDEV(major_number, 0), 1) == -1) {
unregister_chrdev(major_number, DEVICE_NAME);
return -1;
}
printk(KERN_INFO "Device added successfully\n");
return 0;
}
b. 清理模块
static void __exit simple_char_exit(void) {
cdev_del(&my_cdev);
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_INFO "Device unregistered and module exited\n");
}
module_init(simple_char_init);
module_exit(simple_char_exit);
3. 模块信息
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple character device driver");
MODULE_VERSION("1.0");
4. 编写Makefile
编写一个Makefile来编译内核模块。
obj-m += simple_char.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
5. 编译和测试驱动程序
a. 编译驱动程序
在终端中运行make
命令编译内核模块。
make
b. 加载驱动程序
使用insmod
命令加载编译好的内核模块。
sudo insmod simple_char.ko
c. 创建设备文件
使用mknod
命令在/dev
目录下创建设备文件。
sudo mknod /dev/simple_char_device c <major_number> 0
<major_number>
是加载模块时获得的主设备号。
d. 测试驱动程序
使用echo
和cat
命令测试字符设备驱动的读写操作。
echo "Hello, World!" > /dev/simple_char_device
cat /dev/simple_char_device
e. 卸载驱动程序
使用rmmod
命令卸载内核模块。
sudo rmmod simple_char
并删除设备文件。
sudo rm /dev/simple_char_device
6. 将驱动集成到内核源代码中(可选)
如果你希望将驱动集成到内核源代码中,而不是作为外部模块加载,可以按照以下步骤操作:
a. 将驱动源文件复制到内核源代码目录中
假设内核源代码位于/usr/src/linux
,将驱动文件复制到合适的子目录中,例如drivers/char/
。
cp simple_char.c /usr/src/linux/drivers/char/
b. 修改Kconfig文件
在驱动目录下的Kconfig
文件中添加你的驱动配置选项。
config SIMPLE_CHAR
tristate "Simple character device driver"
default n
c. 修改Makefile
在驱动目录下的Makefile
中添加编译指令。
obj-$(CONFIG_SIMPLE_CHAR) += simple_char.o
d. 配置内核
运行内核配置菜单,选择你的驱动。
make menuconfig
导航到“Device Drivers”->“Character devices”,找到并启用你的驱动。
e. 编译内核
编译内核和模块。
make -j$(nproc)
make modules_install
make install
f. 重启系统
重启系统,加载新的内核,确认你的驱动已被集成并自动加载。
reboot
通过以上步骤,你可以创建、编译、加载、测试和集成一个简单的字符设备驱动程序到Linux内核中。