当前位置: 首页 > java >正文

Linux驱动学习day2

APP和驱动交互方式

app与驱动之间,通过下面两个函数交互数据

  1. copy_to_user
  2. copy_from_user
/* 函数原型 */
static inline unsigned long copy_from_user(void* to , const char* from , unsigned long n);static inline unsigned long copy_to_user(void* to , const char* from , unsigned long n);

驱动和硬件之间交互

  1. 可以使用各个子系统函数(iic and so on)
  2. 寄存器(但是Linux中不能直接使用addr ,要使用内存映射ioremap)
int *p = ioremap(addr , size);

改进hello_drv.c驱动代码

为了实现在开发板上能够自动生成nod,不需要使用mknod显示创建nod,在入口函数的时候创建类class_create , 并且注册设备device_create();下图是insmod hello_drv.ko之后的结果,可以cat dev,就能看到其驱动的主设备号和次设备号。

入口函数 

/* register_chrdev */
static int __init hello_init(void)
{major = register_chrdev(0 , "hello_drv", &hello_drv);/* do not need mknod */hello_class = class_create(THIS_MODULE, "hello_class");if (IS_ERR(hello_class)) {printk("failed to allocate class\n");unregister_chrdev(major, "hello_class");return PTR_ERR(hello_class);}/* struct device *device_create(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt, ...) */device_create(hello_class , NULL , MKDEV(major, 0) , NULL , "hello"); /* create node /dev/hello */return 0;
}

出口函数 

/* entry function */
static void __exit  hello_exit(void)
{/* distroy void device_destroy(struct class *class, dev_t devt)*/device_destroy(hello_class , MKDEV(major, 0));class_destroy(hello_class);unregister_chrdev(major, "hello_drv");return;
}
#include <linux/module.h>     // 最基本模块宏
#include <linux/kernel.h>     // printk
#include <linux/init.h>       // __init/__exit
#include <linux/fs.h>         // register_chrdev 等
#include <linux/uaccess.h>    // copy_to_user, copy_from_user
#include <linux/types.h>      // dev_t, bool 等类型
#include <linux/device.h>static int major = 0;
static char hello_buf[100];static struct class *hello_class;/* funciton */static ssize_t hello_drv_read (struct file *file, char __user *buf , size_t size, loff_t * offset)
{unsigned long len = (size < 100 ? size : 100);//printk("%s %s %d\n" , __FILE__ , __FUNCTION__ , __LINE__);if(copy_to_user(buf, hello_buf, len) != 0){printk("copy_to_user error\n");return -1;}printk("read from drivers:%s %s %s\n" , __FILE__ , __FUNCTION__ , hello_buf);return len;
}static ssize_t hello_drv_write (struct file *file, const char __user *buf , size_t size, loff_t * offset)
{unsigned long len = (size < 100 ? size : 100);// printk("%s %s %d\n" , __FILE__ , __FUNCTION__ , __LINE__);if(copy_from_user(hello_buf, buf, len) != 0){printk("copy_from_user error\n");return -1;}printk("write from user:%s %s %s\n" , __FILE__ , __FUNCTION__ , hello_buf);return len;  
}static int hello_drv_open (struct inode *node, struct file *file)
{// printk("%s %s %d\n" , __FILE__ , __FUNCTION__ , __LINE__);return 0;
}static int hello_drv_close (struct inode *node, struct file *file)
{// printk("%s %s %d\n" , __FILE__ , __FUNCTION__ , __LINE__);return 0;
}/*  create struct file_operations */
static struct file_operations hello_drv = 
{.owner          = THIS_MODULE,.open           = hello_drv_open,.read           = hello_drv_read,.write          = hello_drv_write,.release        = hello_drv_close,
};/* register_chrdev */
static int __init hello_init(void)
{major = register_chrdev(0 , "hello_drv", &hello_drv);/* do not need mknod */hello_class = class_create(THIS_MODULE, "hello_class");if (IS_ERR(hello_class)) {printk("failed to allocate class\n");unregister_chrdev(major, "hello_class");return PTR_ERR(hello_class);}/* struct device *device_create(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt, ...) */device_create(hello_class , NULL , MKDEV(major, 0) , NULL , "hello"); /* create node /dev/hello */return 0;
}/* entry function */
static void __exit  hello_exit(void)
{/* distroy void device_destroy(struct class *class, dev_t devt)*/device_destroy(hello_class , MKDEV(major, 0));class_destroy(hello_class);unregister_chrdev(major, "hello_drv");return;
}module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

使用命令echo "4 4 1 7" > /proc/sys/kernel/printk,可以取消掉内核驱动的打印。

使用命令echo "7 4 1 7" > /proc/sys/kernel/printk,可以开启内核驱动的打印。

从上图结果看,可以成功写入驱动并且读出来。成功! 

http://www.xdnf.cn/news/12442.html

相关文章:

  • Python——day46通道注意力(SE注意力)
  • EM储能网关ZWS智慧储能云应用(11) — 一级架构主从架构
  • Pycharm中添加不了新建的Conda环境(此篇专门给Daidai写的)
  • 【Linux】文件操作
  • 大数据学习(132)-HIve数据分析
  • VMware 安装 CentOS8详细教程 (附步骤截图)附连接公网、虚拟机yum源等系统配置
  • 【各种主流消息队列(MQ)对比指南】
  • Appium+python自动化(十)- 元素定位
  • 消息队列高级特性与原理:解锁分布式系统的底层逻辑
  • springcloud SpringAmqp消息队列 简单使用
  • Semaphore - 信号量
  • JavaScript 中的单例内置对象:Global 与 Math 的深度解析
  • 护网行动面试试题(1)
  • 【芯片设计- RTL 数字逻辑设计入门 4.2 -- 组合逻辑赋值 + 时序逻辑状态保持】
  • 电脑要不要经常更新系统
  • SpringBoot自动配置原理深度解析
  • JAVA毕业设计224—基于Java+Springboot+vue的家政服务系统(源代码+数据库)
  • JS实现OSS断点续传
  • 第二届智慧教育与计算机技术国际学术会议(IECT 2025)
  • 抢占2025短剧风口!专业短剧系统开发,打造您的爆款内容平台
  • vm+ubuntu24.04扩展磁盘
  • [环境搭建篇] Windows家庭版如何安装Docker工具
  • 5.3 Spring Boot整合JPA
  • IoT/HCIP实验-4/单片机基础实验(LCD/LED/按键操作/GPIO/EXTI中断服务)
  • 深入理解 Linux 进程控制
  • Vue 3 Teleport 实战:优雅实现模态框、通知和全局组件
  • CMake GLOB返回路径规则及示例
  • 什么是零镜头泛化(Zero-Shot Generalization)
  • 微软推出SQL Server 2025技术预览版,深化人工智能应用集成
  • DDD架构实战 充血模型 电商订单