复习字符设备驱动---写给自己看的

2019-07-13 03:19发布

字符设备驱动之复习
已经大半年没有搞过嵌入式linux的学习了,现在再次学,比第一次学的时候有了更加深刻的理解。
字符设备驱动:
包括1、最简单的字符设备驱动;2、含有ioctl、内核等待队列、阻塞类型、poll机制的字符设备驱动
不多说,先贴代码,这是包含了所有东西的字符设备驱动:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
#include
#include "memdev.h"
 
static mem_major = MEMDEV_MAJOR;
bool have_data = false; //用来标识有没有数据
 
module_param(mem_major, int, S_IRUGO);//在用户态下编程可以通过main()的来传递命令行参数,而编写一个内核模块则通过module_param() 使用 S_IRUGO 作为参数可以被所有人读取
 
struct mem_dev *mem_devp; //定义mem_dev结构体
 
struct cdev cdev; //定义cdev结构体,是描述一个字符设备的结构体
 
//定义open函数,因为在Linux中,每个设备都被当作特殊的文件处理
int mem_open(struct inode *inode, struct file *filp)
{
    struct mem_dev *dev;
     
    
    int num = MINOR(inode->i_rdev);//函数用来返回次设备号
 
    if (num >= MEMDEV_NR_DEVS)  
            return -ENODEV;
    dev = &mem_devp[num];
     
   /*将设备描述结构指针赋值给文件私有数据指针*/
    filp->private_data = dev;
     
    return 0;  
}
 
//设备release函数
int mem_release(struct inode *inode, struct file *filp)
{
  return 0;
}
 
//设备读函数
static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
  unsigned long p =  *ppos;//定义文件游标指针
  unsigned int count = size;
  int ret = 0;
  struct mem_dev *dev = filp->private_data; //获得设备结构体指针
 
 //判断要读的位置有没有越界
  if (p >= MEMDEV_SIZE)
    return 0;
  if (count > MEMDEV_SIZE - p)
    count = MEMDEV_SIZE - p;
     
  while (!have_data) //如果没有数据可以读,则等待
  {
        if (filp->f_flags & O_NONBLOCK)
            return -EAGAIN;
    
    wait_event_interruptible(dev->inq,have_data);//等待函数
  }
 
 //把数据读到用户空间
  if (copy_to_user(buf, (void*)(dev->data + p), count))
  {
    ret =  - EFAULT;
  }
  else
  {
    *ppos += count;
    ret = count;
    
    printk(KERN_INFO "read %d bytes(s) from %d ", count, p);
  }
   
  have_data = false; //没有数据可读
 
  return ret;
}
 
//写函数
static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
  unsigned long p =  *ppos;
  unsigned int count = size;
  int ret = 0;
  struct mem_dev *dev = filp->private_data; //获取设备指针
   
//判断要写的位置有没有越界
  if (p >= MEMDEV_SIZE)
    return 0;
  if (count > MEMDEV_SIZE - p)
    count = MEMDEV_SIZE - p;
 
  //把用户空间的数据写到设备
  if (copy_from_user(dev->data + p, buf, count))
    ret =  - EFAULT;
  else
  {
    *ppos += count;
    ret = count;
     
    printk(KERN_INFO "written %d bytes(s) from %d ", count, p);
  }
   
  have_data = true; //表明有数据可读
     
   //唤醒队列
    wake_up(&(dev->inq));
 
  return ret;
}
 
//文件定位函数
static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
{  
    loff_t newpos;
 
    switch(whence) {
      case 0: /* SEEK_SET */
        newpos = offset;
        break;
 
      case 1: /* SEEK_CUR */
        newpos = filp->f_pos + offset;
        break;
 
      case 2: /* SEEK_END */
        newpos = MEMDEV_SIZE -1 + offset;
        break;
 
      default: /* can't happen */
        return -EINVAL;
    }
    if ((newpos<0) || (newpos>MEMDEV_SIZE))
        return -EINVAL;
        
    filp->f_pos = newpos;
    return newpos;
 
}
 
int memdev_ioctl(struct inode *inode, struct file *filp,
                 unsigned int cmd, unsigned long arg)
{
 
    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---> ");
        break;
       
      //输出当前信息
      case MEMDEV_IOCGETDATA:  
        ioarg = 4099;
        ret = __put_user(ioarg, (int *)arg);
        break;
       
      //设置当前信息
      case MEMDEV_IOCSETDATA:  
        ret = __get_user(ioarg, (int *)arg);
        printk("<--- In Kernel MEMDEV_IOCSETDATA ioarg = %d ---> ",ioarg);
        break;
 
      default:   
        return -EINVAL;
    }
    return ret;
 
}
unsigned int mem_poll(struct file *filp, poll_table *wait)
{
    struct mem_dev  *dev = filp->private_data;  
    
  加入等待队列
    poll_wait(filp, &dev->inq,  wait);
 
     
     
}
 
 
//这个不解析了,相信很多人懂的
static const struct file_operations mem_fops =
{
  .owner = THIS_MODULE,
  .llseek = mem_llseek,
  .read = mem_read,
  .write = mem_write,
  .open = mem_open,
  .release = mem_release,
  .poll = mem_poll,
  .ioctl = memdev_ioctl,
};
 

static int memdev_init(void)
{
  int result;
  int i;
 
  dev_t devno = MKDEV(mem_major, 0);
 
  if (mem_major)
    result = register_chrdev_region(devno, 2, "memdev");
  else  
  {
    result = alloc_chrdev_region(&devno, 0, 2, "memdev");
    mem_major = MAJOR(devno);
  }   
   
  if (result < 0)
    return result;
 
 
  cdev_init(&cdev, &mem_fops);
  cdev.owner = THIS_MODULE;
  cdev.ops = &mem_fops;
   

  cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS);
    
 
  mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL);
  if (!mem_devp)    
  {
    result =  - ENOMEM;
    goto fail_malloc;
  }
  memset(mem_devp, 0, sizeof(struct mem_dev));
   
 
  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);
   
      
     init_waitqueue_head(&(mem_devp[i].inq));
     //init_waitqueue_head(&(mem_devp[i].outq));
  }
   
   myclass = class_create(THIS_MODULE,"test_char");  
   device_create(myclass, NULL, MKDEV(mem_major,0), NULL, "memdev0");    
  return 0;
 
  fail_malloc:  
  unregister_chrdev_region(devno, 1);
   
  return result;
}
 

static void memdev_exit(void)
{
  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);


头文件:

#ifndef _MEMDEV_H_
#define _MEMDEV_H_
 
#ifndef MEMDEV_MAJOR
#define MEMDEV_MAJOR 0  
#endif
 
#ifndef MEMDEV_NR_DEVS
#define MEMDEV_NR_DEVS 2    
#endif
 
#ifndef MEMDEV_SIZE
#define MEMDEV_SIZE 4096
#endif
 

struct mem_dev                                      
{                                                         
  char *data;                       
  unsigned long size;  
  wait_queue_head_t inq;   
};
 
#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, 2, int)
 
#define MEMDEV_IOC_MAXNR 3
 
#endif /* _MEMDEV_H_ */

以上代码都是参考国嵌的
解析:
ioctl:这个基本是用来设置设备参数的,因为它是用来执行命令,传入和传出内核的书基本不是大规模的数据,如是一个int或者char
内核阻塞:
本驱动程序中,在设备没有东西读的情况下,read函数会阻塞,进入睡眠状态,直到有东西写进被唤醒
poll:在应用程序中是select()函数,用来监控传入的文件集,如果文件集没有符合要求的文件,则阻塞调用进程
poll的详细可以参考:http://blog.chinaunix.net/uid-12461657-id-3191217.html