请编写一个简单的字符设备驱动程序,它可以读取设备的状态并将其显示在终端上
时间: 2024-05-16 20:17:25 浏览: 22
以下是一个简单的字符设备驱动程序,它可以读取设备的状态并将其显示在终端上:
```c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#define DEVICE_NAME "mydevice"
#define CLASS_NAME "myclass"
static dev_t dev_num;
static struct cdev my_cdev;
static struct class *my_class = NULL;
static char message[256] = "Device is working\n";
static int message_len = 0;
static int my_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "mydevice: device opened\n");
return 0;
}
static ssize_t my_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
{
ssize_t ret;
if (*offset >= message_len)
return 0;
if (count > message_len - *offset)
count = message_len - *offset;
ret = copy_to_user(buf, message + *offset, count);
if (ret == count) {
printk(KERN_INFO "mydevice: read %ld bytes\n", count);
return -EFAULT;
}
*offset += count - ret;
return count - ret;
}
static int my_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO "mydevice: device closed\n");
return 0;
}
static struct file_operations my_fops = {
.owner = THIS_MODULE,
.open = my_open,
.read = my_read,
.release = my_release,
};
static int __init my_init(void)
{
int ret;
ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME);
if (ret < 0) {
printk(KERN_ERR "mydevice: failed to allocate major number\n");
return ret;
}
cdev_init(&my_cdev, &my_fops);
my_cdev.owner = THIS_MODULE;
ret = cdev_add(&my_cdev, dev_num, 1);
if (ret < 0) {
printk(KERN_ERR "mydevice: failed to add device\n");
goto cdev_fail;
}
my_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(my_class)) {
printk(KERN_ERR "mydevice: failed to create class\n");
ret = PTR_ERR(my_class);
goto class_fail;
}
device_create(my_class, NULL, dev_num, NULL, DEVICE_NAME);
printk(KERN_INFO "mydevice: device registered\n");
message_len = strlen(message);
return 0;
class_fail:
cdev_del(&my_cdev);
cdev_fail:
unregister_chrdev_region(dev_num, 1);
return ret;
}
static void __exit my_exit(void)
{
device_destroy(my_class, dev_num);
class_destroy(my_class);
cdev_del(&my_cdev);
unregister_chrdev_region(dev_num, 1);
printk(KERN_INFO "mydevice: device unregistered\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple character device driver for displaying device status");
```
该驱动程序实现了一个字符设备,当用户打开设备时,它会显示一条消息,当用户读取设备时,它会将设备状态消息返回给用户。该驱动程序使用了内核中的字符设备 API 来进行注册和添加设备,以及实现 open、read 和 release 操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)