linux如何通过ioctl调用驱动的

10 篇文章 2 订阅

ioctl作用:应用层的ioctl函数传入的cmd和arg参数会直接传入驱动层的ioctl接口,在对应驱动文件里会对相应的命令进行操作

对于传递的ioctl命令有一定的规范,具体可以参考:/include/asm/ioctl.h,/Documentation/ioctl-number.txt 这两个文件

 

应用层和驱动程序联系如下:

最终ioctl是通过系统调用sys_ioctl软中断陷入到内核,通过系统中断号最终调用到内核态的ioctl函数。

 

构造ioctl命令linux已经给我们提供了宏命令:

_IO    an ioctl with no parameters
_IOW   an ioctl with write parameters (copy_from_user)
_IOR   an ioctl with read parameters  (copy_to_user)
_IOWR  an ioctl with both write and read parameters


相关参数:
/*
    type:    幻数(设备相关的一个字母,避免和内核冲突)
    nr:      命令编号
    datatype:数据类型
*/

_IO(type,nr)           //无参数的命令

_IOR(type,nr,datatype)  //应用从驱动中读数据

_IOW(type,nr,datatype)  //应用从驱动中写数据

举一个简单的例子:

//应用程序

#define MOTOR_CMD_BASE'M'  
#define SET_MOTOR_STEP _IOW(MOTOR_CMD_BASE, 1u, int)
#define GET_MOTOR_STEP _IOW(MOTOR_CMD_BASE, 2u, int)
...

int step= 0;
int value = 0;
int fd = -1;
 
fd = open("/dev/motor",O_RDWR);   
if (fd== -1) {   
    printf("open device error!/n");   
    return 0;   
}  
   .  
   .  
   printf("Please input the motor step:/n"  
   scanf("%d",&step);    
   if(ioctl(fd, SET_MOTOR_STEP, &step)<0){  
      perror("ioctl error");  
      exit(1);  
   }  
 
   if(ioctl(fd, GET_MOTOR_STEP, &value)<0){  
      perror("ioctl error");  
      exit(1);  
   }
     
... 

 

//驱动程序
//假设该驱动程序建立了名为motor的字符设备

#define MOTOR_CMD_BASE'M'  
#define SET_MOTOR_STEP _IOW(MOTOR_CMD_BASE, 1u, int)
#define GET_MOTOR_STEP _IOW(MOTOR_CMD_BASE, 2u, int)
 

int motor_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)  
{
      int step=0;   
      int value = 0;
      switch (cmd) {  
          case SET_MOTOR_STEP :  
                if(copy_from_user(&step, (int*)arg, sizeof(int)))
                    return fail;

                //处理程序

                break;
        case GET_MOTOR_STEP :
            value = 100;

            if(copy_to_user((int*)arg, &value, sizeof(int)))
                return fail;
    
            break;
}  

 

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值