写一个灯的字符驱动设备
时间: 2024-05-24 22:12:27 浏览: 10
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#define DEVICE_NAME "myled"
#define CLASS_NAME "myled_class"
MODULE_LICENSE("GPL");
static int major_number;
static struct class* myled_class = NULL;
static struct device* myled_device = NULL;
static int led_status = 0;
static int myled_open(struct inode*, struct file*);
static int myled_release(struct inode*, struct file*);
static ssize_t myled_read(struct file*, char*, size_t, loff_t*);
static ssize_t myled_write(struct file*, const char*, size_t, loff_t*);
static struct file_operations fops = {
.open = myled_open,
.release = myled_release,
.read = myled_read,
.write = myled_write,
};
// 初始化函数
static int __init myled_init(void) {
printk(KERN_INFO "myled: Initializing the myled LKM\n");
// allocate a major number dynamically
major_number = register_chrdev(0, DEVICE_NAME, &fops);
if (major_number < 0) {
printk(KERN_ALERT "myled failed to register a major number\n");
return major_number;
}
printk(KERN_INFO "myled: registered correctly with major number %d\n", major_number);
// register the device class
myled_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(myled_class)) {
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "Failed to register device class\n");
return PTR_ERR(myled_class);
}
printk(KERN_INFO "myled: device class registered correctly\n");
// register the device driver
myled_device = device_create(myled_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
if (IS_ERR(myled_device)) {
class_destroy(myled_class);
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "Failed to create the device\n");
return PTR_ERR(myled_device);
}
printk(KERN_INFO "myled: device class created correctly\n");
return 0;
}
// 清理函数
static void __exit myled_exit(void) {
device_destroy(myled_class, MKDEV(major_number, 0));
class_unregister(myled_class);
class_destroy(myled_class);
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_INFO "myled: Goodbye from the myled LKM!\n");
}
// open 函数
static int myled_open(struct inode* inodep, struct file* filep) {
printk(KERN_INFO "myled: Device has been opened\n");
return 0;
}
// release 函数
static int myled_release(struct inode* inodep, struct file* filep) {
printk(KERN_INFO "myled: Device successfully closed\n");
return 0;
}
// read 函数
static ssize_t myled_read(struct file* filep, char* buffer, size_t len, loff_t* offset) {
int error_count = 0;
error_count = copy_to_user(buffer, &led_status, sizeof(led_status));
if (error_count == 0) {
printk(KERN_INFO "myled: Sent %d bytes to the user\n", sizeof(led_status));
return 0;
} else {
printk(KERN_INFO "myled: Failed to send %d bytes to the user\n", sizeof(led_status));
return -EFAULT;
}
}
// write 函数
static ssize_t myled_write(struct file* filep, const char* buffer, size_t len, loff_t* offset) {
int error_count = 0;
error_count = copy_from_user(&led_status, buffer, sizeof(led_status));
if (error_count == 0) {
printk(KERN_INFO "myled: Received %d bytes from the user\n", sizeof(led_status));
return len;
} else {
printk(KERN_INFO "myled: Failed to receive %d bytes from the user\n", sizeof(led_status));
return -EFAULT;
}
}
module_init(myled_init);
module_exit(myled_exit);
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)