下面是一个简单的Linux字符设备驱动程序的示例,用于演示如何创建和注册一个字符设备。这个示例仅用于教学目的,它会创建一个设备,该设备能够接收写入的数据,并允许从中读取相同的数据。
请注意,开发内核模块需要具备一定的Linux内核知识,以及对C语言的熟悉。此外,编译内核模块需要安装相应的内核头文件。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#define DEVICE_NAME "mychardev"
#define CLASS_NAME "mychar"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("An Example Author");
MODULE_DESCRIPTION("A simple Linux char driver");
MODULE_VERSION("0.1");
static int majorNumber;
static char message[256] = {0};
static short size_of_message;
static int numberOpens = 0;
static struct class* charClass = NULL;
static struct device* charDevice = NULL;
static int dev_open(struct inode *, struct file *);
static int dev_release(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static struct file_operations fops =
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};
static int __init char_init(void){
printk(KERN_INFO "CharDevice: Initializing the CharDevice\n");
majorNumber = register_chrdev(0, DEVICE_NAME, &fops);
if (majorNumber<0){
printk(KERN_ALERT "CharDevice failed to register a major number\n");
return majorNumber;
}
printk(KERN_INFO "CharDevice: registered correctly with major number %d\n", majorNumber);
charClass = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(charClass)){
unregister_chrdev(majorNumber, DEVICE_NAME);
printk(KERN_ALERT "Failed to register device class\n");
return PTR_ERR(charClass);
}
printk(KERN_INFO "CharDevice: device class registered correctly\n");
charDevice = device_create(charClass, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME);
if (IS_ERR(charDevice)){
class_destroy(charClass);
unregister_chrdev(majorNumber, DEVICE_NAME);
printk(KERN_ALERT "Failed to create the device\n");
return PTR_ERR(charDevice);
}
printk(KERN_INFO "CharDevice: device class created correctly\n");
return 0;
}
static void __exit char_exit(void){
device_destroy(charClass, MKDEV(majorNumber, 0));
class_unregister(charClass);
class_destroy(charClass);
unregister_chrdev(majorNumber, DEVICE_NAME);
printk(KERN_INFO "CharDevice: Goodbye from the LKM!\n");
}
static int dev_open(struct inode *inodep, struct file *filep){
numberOpens++;
printk(KERN_INFO "CharDevice: Device has been opened %d time(s)\n", numberOpens);
return 0;
}
static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset){
int error_count = 0;
error_count = copy_to_user(buffer, message, size_of_message);
if (error_count==0){
printk(KERN_INFO "CharDevice: Sent %d characters to the user\n", size_of_message);
return (size_of_message=0);
}
else {
printk(KERN_INFO "CharDevice: Failed to send %d characters to the user\n", error_count);
return -EFAULT;
}
}
static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
sprintf(message, "%s(%zu letters)", buffer, len);
size_of_message = strlen(message);
printk(KERN_INFO "CharDevice: Received %zu characters from the user\n", len);
return len;
}
static int dev_release(struct inode *inodep, struct file *filep){
printk(KERN_INFO "CharDevice: Device successfully closed\n");
return 0;
}
//可以增添设备专属接口
module_init(char_init);
module_exit(char_exit);
驱动测试代码的实现
字符驱动测试程序通常指的是一个简单的程序,用于测试计算机系统中字符设备驱动的功能。这种类型的程序可以帮助开发者确保字符设备(如键盘、串口等)能够正确地接收和发送数据。下面是一个简单的字符设备驱动测试程序的例子,使用C语言编写,针对Linux系统。
这个例子假设你已经有一个字符设备驱动,并且设备文件是/dev/mychardev
。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd;
char buf[100];
// 打开设备文件
fd = open("/dev/mychardev", O_RDWR);
if (fd < 0) {
perror("Failed to open the device");
return errno;
}
// 向设备写数据
printf("Writing to the device...\n");
if (write(fd, "Hello, device!", strlen("Hello, device!")) < 0) {
perror("Failed to write to the device");
return errno;
}
// 从设备读数据
printf("Reading from the device...\n");
if (read(fd, buf, sizeof(buf)) < 0) {
perror("Failed to read from the device");
return errno;
}
printf("Received: %s\n", buf);
// 关闭设备文件
close(fd);
return 0;
}
这个程序首先尝试打开/dev/mychardev
设备文件。如果成功,它会向设备写入字符串"Hello, device!",然后尝试从设备读取数据到缓冲区buf
中,并将读取的数据打印出来。最后,它会关闭设备文件。