写LED驱动
首先,我们要建立一个file_operator结构体:
/* 定义自己的file_operations结构体 */ static struct file_operations led_drv = { .owner = THIS_MODULE, //主设备号 .open = led_drv_open, // driver function .read = led_drv_read, .write = led_drv_write, .release = led_drv_close, };
编写相应的四个driver function:
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */ static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset) { printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); return 0; } /* write(fd, &val, 1); */ static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset) { int err; char status; //struct inode *inode = file_inode(file); //int minor = iminor(inode); printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); err = copy_from_user(&status, buf, 1); /* 根据次设备号和status控制LED */ gpiod_set_value(led_gpio, status); return 1; } static int led_drv_open (struct inode *node, struct file *file) { //int minor = iminor(node); printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); /* 根据次设备号初始化LED */ gpiod_direction_output(led_gpio, 0); return 0; } static int led_drv_close (struct inode *node, struct file *file) { printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); return 0; }
注册filer_operator: 告诉CPU:这里采用设备树的方式:
b
标签:__,led,struct,LED,file,驱动,编写,drv,inode From: https://www.cnblogs.com/lianglianglu/p/17418534.html