一:read,write
<asm-generic/uaccess.h> 应用层调用 read —>>> 应用层想读取数据 —>>> 这件事情是内核中驱动完成 —>>> copy_to_user 应用层调用 write —>>> 应用层想写取数据 —>>> 这件事情是内核中驱动完成 —>>> copy_from_user
//驱动中的read、write接口实现 ssize_t demo_read(struct file *filp, char __user *buf, size_t size, loff_t *off){ if(size > kbufcount) { size = kbufcount; } if(copy_to_user(buf, k_buf, size)){ printk(KERN_ERR "copy_to_user failed...\n"); return -EAGAIN; } kbufcount = 0; printk(KERN_DEBUG "---%s---%s---%d---\n",__FILE__,__func__,__LINE__); return size;}ssize_t demo_write(struct file *filp, const char __user *buf, size_t size, loff_t *off){ if(size > KBUFSIZE-1){ size = KBUFSIZE-1; } if(copy_from_user(k_buf,buf, size)){ printk(KERN_ERR "copy_from_user failed...\n"); return -EAGAIN; } kbufcount = size; printk(KERN_INFO "demo_write->copy_from_user:%s\n",k_buf); printk(KERN_DEBUG "---%s---%s---%d---\n",__FILE__,__func__,__LINE__); return size; } //应用层测试代码char buf[] = "aaaaaaaaaaaaa"; char r_buf[32]; int main(int argc, const char *argv[]){ int fd = open("/dev/cdev_demo2",O_RDWR); if(fd < 0){ perror("open"); return -1; } write(fd,buf,strlen(buf)); close(fd); return 0;}