Chinaunix首页 | 论坛 | 博客
  • 博客访问: 132507
  • 博文数量: 75
  • 博客积分: 1485
  • 博客等级: 上尉
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-11 20:57
文章分类
文章存档

2011年(1)

2010年(15)

2009年(59)

我的朋友

分类: 嵌入式

2010-04-09 20:40:19

国嵌的驱动代码学习总结


 

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#include "memdev.h"

static mem_major = MEMDEV_MAJOR;   //主设备号

module_param(mem_major, int, S_IRUGO);

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

struct cdev cdev;     //cdev结构体用来描述字符设备

/*文件打开函数*/
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];    
/*将设备描述结构指针赋值给文件私有数据指针。大多数驱动工程师都将文件的私有数据private_data指向设备结构体,read,write,llseek,等函数通过private_data访问设备结构体*/
    filp->private_data = dev;  //通过private_data在open,read,write等

                                 函数中传递dev设备结构体的相关数据
    return 0;
}

/*文件释放函数*/
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;

  /*读数据到用户空间*/
  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\n", count, p);
  }

  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\n", count, p);
  }

  return ret;
}

/* seek文件定位函数 */
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;    //EIMVAL表示传给系统调用的参数不正确,错误的返回值一般是负数,所以前面加个负号。
        
    filp->f_pos = newpos;
    return newpos;

}

/*文件操作结构体*/
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,
};

/*设备驱动模块加载函数*/
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 /* 动态分配设备号 */
  {                         //devno动态分配的设备号 0表示起始次设备号  
    result = alloc_chrdev_region(&devno, 0, 2, "memdev");
    mem_major = MAJOR(devno);  //获取主设备号
  }
  
  if (result < 0)
    return result;

  /*初始化cdev结构*/
  cdev_init(&cdev, &mem_fops);
  cdev.owner = THIS_MODULE;   
  cdev.ops = &mem_fops;   //这两句个人感觉不要也行,因为cdev_init函数已经初始化了cdev的成员,并建立cdev和file_operations之间的连接。cdev_init定义见备注。  也可以不要cdev_init(),但要用cdev_alloc()动态申请一个cdev内存,struct cdev *cdev=cdev_alloc()
  
  /* 注册字符设备 */      //对应cdev_del  设备个数2
  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);
  }
    
  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 254 /*预设的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;  //存放数据,读或写memdev设备实质上是对data地址进行操作。
  unsigned long size;
};

#endif /* _MEMDEV_H_ */


字符设备驱动的编写步骤

1.申请设备号(分为动态alloc_chrdev_region和静态申请register_chrdev_region),注销时都用cdev_del

2.初始化cdev结构体(用来描述字符设备)cdev_init(),cdev结构体重要的成员:

    struct module *owner;

    struct file_operations *ops;

    dev_t dev;  设备号
3.注册字符设备结构体cdev_add()

4.动态申请自定义的设备结构体的内存

 
  struct {
           struct         f_list;
           struct            *f_dentry;
           struct          *f_vfsmnt;
           struct   *f_op;
                           f_count;
           unsigned int            f_flags;
                             f_mode;
                             f_pos;
           unsigned long           f_reada, f_ramax, f_raend, f_ralen, f_rawin;
           struct       f_owner;
           unsigned int            f_uid, f_gid;
           int                     f_error;
 
           unsigned long           f_version;
 
           /* needed for tty driver, and maybe others */
           void                    *private_data;
 
           /* preallocated helper kiobuf to speedup O_DIRECT */
           struct            *f_iobuf;
           long                    f_iobuf_lock;
  };
阅读(515) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~