驱动与应用端交互最小demo示例

原文出处:https://www.cnblogs.com/cyyljw/p/8477594.html

补充:补充了小白知识,原文中没有说明要做字符设备的文件创建,,,这个小白一般不知道。未提供ko文件的编译脚本,,这个小白不一定会,,原文未适配3.xx之后的linux内核,,原文代码未加打印

【一】driver的代码

memdev.h

#ifndef _MEMDEV_H_
#define _MEMDEV_H_

#include 

#ifndef MEMDEV_MAJOR
#define MEMDEV_MAJOR 0   /*预设的mem的主设备号*/
#endif

#ifndef MEMDEV_NR_DEVS
#define MEMDEV_NR_DEVS 2    /*设备数*/
#endif

#ifndef MEMDEV_SIZE
#define MEMDEV_SIZE 4096
#endif

/*mem设备描述结构体*/
struct mem_dev                                     
{                                                        
  char *data;                      
  unsigned long size;       
};

/* 定义幻数 */
#define MEMDEV_IOC_MAGIC  'k'

/* 定义命令 */
#define MEMDEV_IOCPRINT   _IO(MEMDEV_IOC_MAGIC, 1)
#define MEMDEV_IOCGETDATA _IOR(MEMDEV_IOC_MAGIC, 2, int)
#define MEMDEV_IOCSETDATA _IOW(MEMDEV_IOC_MAGIC, 3, int)

#define MEMDEV_IOC_MAXNR 3

#endif /* _MEMDEV_H_ */

memdev.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
        #include 
#else
        #include 
#endif

#include 
#include 

#include "memdev.h"

static int mem_major = MEMDEV_MAJOR;

module_param(mem_major, int, S_IRUGO);

struct mem_dev *mem_devp; /*设备结构体指针*/

struct cdev cdev; 

/*文件打开函数*/
int mem_open(struct inode *inode, struct file *filp)
{
    struct mem_dev *dev;
    
    /*获取次设备号*/
    int num = MINOR(inode->i_rdev);

    printk("mem_open");
    if (num >= MEMDEV_NR_DEVS) 
            return -ENODEV;
    dev = &mem_devp[num];
    
    /*将设备描述结构指针赋值给文件私有数据指针*/
    filp->private_data = dev;

    return 0; 
}

/*文件释放函数*/
int mem_release(struct inode *inode, struct file *filp)
{
  return 0;
}

/*IO操作*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
static int memdev_ioctl(
        struct inode *indoe,
        struct file *filp,
        unsigned int cmd,
        unsigned long arg)
{
#else
static long memdev_unlocked_ioctl(
        struct file *filp,
        unsigned int cmd,
        unsigned long arg)
{
    struct inode *inode = inode = file_inode(filp);
#endif
    int err = 0;
    int ret = 0;
    int ioarg = 0;
    
    /* 检测命令的有效性 */
    if (_IOC_TYPE(cmd) != MEMDEV_IOC_MAGIC) 
        return -EINVAL;
    if (_IOC_NR(cmd) > MEMDEV_IOC_MAXNR) 
        return -EINVAL;

    /* 根据命令类型,检测参数空间是否可以访问 */
    if (_IOC_DIR(cmd) & _IOC_READ)
        err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
    else if (_IOC_DIR(cmd) & _IOC_WRITE)
        err = !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
    if (err) 
        return -EFAULT;

    /* 根据命令,执行相应的操作 */
    switch(cmd) {

      /* 打印当前设备信息 */
      case MEMDEV_IOCPRINT:
          printk("<--- CMD MEMDEV_IOCPRINT Done--->\n\n");
        break;
      
      /* 获取参数 */
      case MEMDEV_IOCGETDATA: 
        ioarg = 1101;
        ret = __put_user(ioarg, (int *)arg);
        break;
      
      /* 设置参数 */
      case MEMDEV_IOCSETDATA: 
        ret = __get_user(ioarg, (int *)arg);
        printk("<--- In Kernel MEMDEV_IOCSETDATA ioarg = %d --->\n\n",ioarg);
        break;

      default:  
        return -EINVAL;
    }
    return ret;

}

/*文件操作结构体*/
static const struct file_operations mem_fops =
{
  .owner = THIS_MODULE,
  .open = mem_open,
  .release = mem_release,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
  .ioctl = memdev_ioctl,
#else
  .unlocked_ioctl = memdev_unlocked_ioctl,
#endif
};

/*设备驱动模块加载函数*/
static int memdev_init(void)
{
  int result;
  int i;
  dev_t devno = MKDEV(mem_major, 0);

  printk("memdev_init\n");
  /* 静态申请设备号*/
  if (mem_major){
    printk("begin register_chrdev_region\n");
    result = register_chrdev_region(devno, 2, "memdev");
  }
  else  /* 动态分配设备号 */
  {
    printk("begin alloc_chrdev_region\n");
    result = alloc_chrdev_region(&devno, 0, 2, "memdev");
    printk("alloc_chrdev_region result=%i\n",result);
    mem_major = MAJOR(devno);
  }  
  printk("alloc result=%i\n",result);
  if (result < 0)
    return result;

  /*初始化cdev结构*/
  cdev_init(&cdev, &mem_fops);
  cdev.owner = THIS_MODULE;
  cdev.ops = &mem_fops;
  
  /* 注册字符设备 */
  cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS);
  printk("cdev_add finished\n");
   
  /* 为设备描述结构分配内存*/
  mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL);
  if (!mem_devp)    /*申请失败*/
  {
     printk("kmalloc for mem_devp failed\n");
    result =  - ENOMEM;
    goto fail_malloc;
  }
  printk("kmalloc for mem_devp sucess\n");
  memset(mem_devp, 0, sizeof(struct mem_dev));
  
  /*为设备分配内存*/
  printk("kmalloc for devices\n");
  for (i=0; i < MEMDEV_NR_DEVS; i++) 
  {
        mem_devp[i].size = MEMDEV_SIZE;
        mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL);
        memset(mem_devp[i].data, 0, MEMDEV_SIZE);
  }
  //ret = device_register(dev);
  return 0;

  fail_malloc: 
  unregister_chrdev_region(devno, 1);
  
  return result;
}

/*模块卸载函数*/
static void memdev_exit(void)
{
  printk("memdev_exit\n");
  cdev_del(&cdev);   /*注销设备*/
  kfree(mem_devp);     /*释放设备结构体内存*/
  unregister_chrdev_region(MKDEV(mem_major, 0), 2); /*释放设备号*/
}

MODULE_AUTHOR("David Xie");
MODULE_LICENSE("GPL");

module_init(memdev_init);
module_exit(memdev_exit);

【ap端】

app-ioctl.c

#include 
#include
#include
#include  
#include       
#include      
#include 
#include "../driver/memdev.h"  /* 包含命令定义 */

int main()
{
    int fd = 0;
    int cmd;
    int arg = 0;
    char Buf[4096];
    
    
    /*打开设备文件*/
    fd = open("/dev/memdev0",O_RDWR);
    if (fd < 0)
    {
        printf("Open Dev Mem0 Error!,%i\n",fd);
        return -1;
    }
    
    /* 调用命令MEMDEV_IOCPRINT */
    printf("<--- Call MEMDEV_IOCPRINT --->\n");
    cmd = MEMDEV_IOCPRINT;
    if (ioctl(fd, cmd, &arg) < 0)
        {
            printf("Call cmd MEMDEV_IOCPRINT fail\n");
            return -1;
    }
    
    
    /* 调用命令MEMDEV_IOCSETDATA */
    printf("<--- Call MEMDEV_IOCSETDATA --->\n");
    cmd = MEMDEV_IOCSETDATA;
    arg = 2007;
    if (ioctl(fd, cmd, &arg) < 0)
        {
            printf("Call cmd MEMDEV_IOCSETDATA fail\n");
            return -1;
    }

    
    /* 调用命令MEMDEV_IOCGETDATA */
    printf("<--- Call MEMDEV_IOCGETDATA --->\n");
    cmd = MEMDEV_IOCGETDATA;
    if (ioctl(fd, cmd, &arg) < 0)
        {
            printf("Call cmd dme fail\n");
            return -1;
    }
    printf("<--- In User Space MEMDEV_IOCGETDATA Get Data is %d --->\n\n",arg);    
    
    close(fd);
    return 0;    
}

【驱动编译脚本】

和memdev.c同目录放置

Makefile

ifneq ($(KERNELRELEASE),)

    obj-m := memdev.o

else

    KERN_DIR ?= /usr/src/linux-headers-$(shell uname -r)/
    PWD := $(shell pwd)

default:
	$(MAKE) -C $(KERN_DIR) M=$(PWD) modules

endif


clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

 

【app侧编译可执行文件】

在app-ioctl.c目录下执行  $gcc *.c

执行:./a.out

 

【创建字符设备】

1.ko文件加载 insmod memdev.ko

2.查看设备的major ID: $cat /proc/devices

假定有一行 245 memdev

3.创建字符设备 $sudo mknod /dev/memdev0 c 245 0

创建成功后查看/dev/目录

附:这里选的是手动创建字符设备,但其实是还可以在init代码里调用create-device()来创建创建的。这块后续测试通过后补充进来

【流程顺序】

1.完成代码编译

2.编译构建驱动ko文件,app的可执行文件

3.创建字符设备

4.执行app的可执行文件查看效果。如果执行报错,有可能是权限不够导致,试下sudo xx

5.执行dmesg查看驱动打印。若驱动日志过多 执行dmesg -c

【驱动开发相关资料链接汇总】

https://blog.csdn.net/zqixiao_09/article/details/50888795 Linux 设备驱动开发 —— platform设备驱动应用实例解析

http://v4l.videotechnology.com/dwg/v4l2.pdf v4l2开发api

https://blog.csdn.net/simonforfuture/article/details/78743800

http://www.yellowmax2001.com/2018/07/02/V4L2%E6%A1%86%E6%9E%B6-media-device/ V4L2框架-media-device media_device,media_entity,media_link,media_pad

你可能感兴趣的:(ioctl,driver,驱动,linux,driver和应用交互)