《[arm驱动]注册函数相关内容》本文涉及内核驱动函数七个,内核结构体二个,分析了内核驱动函数七个;可参考的相关应用程序模板或内核驱动模板零个,可参考的相关应用程序模板或内核驱动两个

一、三个注册设备函数
1、三个注册函数定义

内核驱动函数一)

1.1、新版内核的注册函数(2.6几的)

int    register_chrdev_region(dev_t first,unsigned int count,char *name);
int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,unsigned int count,char *name);

   First :要分配的设备编号范围的初始值(次设备号常设为0);
   Count:连续编号范围.
   Name:编号相关联的设备名称. (/proc/devices);

Firstminor : 通常为0;


内核驱动函数二)1.2释放设备函数:

void unregist_chrdev_region(dev_t first,unsigned int count);

内核驱动函数三)1、3、旧版内核的注册函数

int register_chrdev(unsigned int major, const char *name, const struct    file_operations *fops)

   Return Value:主设备号

   释放设备函数:

unregister_chrdev(unsigned int major, const char *name);

2、三个注册函数简单区别
  2、1、register_chrdev_region()是为提前知道设备的主次设备号的设备分配设备编号。
   2.2、alloc_chrdev_region() 是动态分配主次设备号。
   2.3、register_chrdev()。是老版本的设备号注册方式,他只分配主设备号。从设备号在mknod的时候指定。

3、深入理解
3.1、注册函数相关struct结构
   内核中所有已分配的字符设备编号都记录在一个名为 chrdevs 散列表里。该散列表中的每一个元素是一个 char_device_struct 结构,它的定义如下:

结构体一)

static struct char_device_struct {
      struct char_device_struct *next;    // 指向散列冲突链表中的下一个元素的指针
      unsigned int major;                 // 主设备号
      unsigned int baseminor;             // 起始次设备号
      int minorct;                        // 设备编号的范围大小
      char name[64];                      // 处理该设备编号范围内的设备驱动的名称
      struct file_operations *fops;       // 没有使用
      struct cdev *cdev;                  // 指向字符设备驱动程序描述符的指针
  } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];

注意:
   内核并不是为每一个字符设备编号定义一个 char_device_struct 结构,而是为一组对应同一个字符设备驱动的设备编号范围定义一个 char_device_struct 结构。chrdevs 散列表的大小是 255,散列算法是把每组字符设备编号范围的主设备号以 255 取模插入相应的散列桶中。同一个散列桶中的字符设备编号范围是按起始次设备号递增排序的。
4、注册设备相关函数分析

内核驱动源码一)

4.1、register_chrdev_region()内核源码

int register_chrdev_region(dev_t from, unsigned count, const char *name)
{
   struct char_device_struct *cd;
   dev_t to = from + count;
   dev_t n, next;
   for (n = from; n < to; n = next) {
       next = MKDEV(MAJOR(n)+1, 0);
       if (next > to)
           next = to;
       cd = __register_chrdev_region(MAJOR(n), MINOR(n), next - n, name);
       if (IS_ERR(cd))
           goto fail;
   }
   return 0;
fail:
   to = n;
   for (n = from; n < to; n = next) {
       next = MKDEV(MAJOR(n)+1, 0);
       kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
   }
   return PTR_ERR(cd);
}


总结:register_chrdev_region()数用于分配已知的设备编号范围。
细说:register_chrdev_region() 函数用于分配指定的设备编号范围。如果申请的设备编号范围跨越了主设备号,它会把分配范围内的编号按主设备号分割成较小的子范围,并在每个子范围上调用 __register_chrdev_region() 。如果其中有一次分配失败的话,那会把之前成功分配的都全部退回。

内核驱动源码二)

4.2、alloc_chrdev_region()内核源码

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
{
   struct char_device_struct *cd;
   cd = __register_chrdev_region(0, baseminor, count, name);
   if (IS_ERR(cd))
       return PTR_ERR(cd);
   *dev = MKDEV(cd->major, cd->baseminor);
   return 0;
}

总结:alloc_chrdev_region() 函数用于设备号未知,向系统动态申请未被占用的设备的情况
细说:alloc_chrdev_region() 函数用于动态申请设备编号范围,这个函数好像并没有检查范围过大的情况,不过动态分配总是找个空的散列桶,所以问题也不大。通过指针参数返回实际获得的起始设备编号。
总总结:alloc_chrdev_region()与register_chrdev_region()相比,优点在于它会自动避开设备号重复冲突

内核驱动源码三)
4.3、 register_chrdev() 内核源码

int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
{
   struct char_device_struct *cd;
   struct cdev *cdev;
   char *s;
   int err = -ENOMEM;
   cd = __register_chrdev_region(major, 0, 256, name);
   if (IS_ERR(cd))
       return PTR_ERR(cd);
   cdev = cdev_alloc();
   if (!cdev)
       goto out2;
   cdev->owner = fops->owner;
   cdev->ops = fops;
   kobject_set_name(&cdev->kobj, "%s", name);
   for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
       *s = '!';
   err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
   if (err)
       goto out;
   cd->cdev = cdev;
   return major ? 0 : cd->major;
out:
   kobject_put(&cdev->kobj);
out2:
   kfree(__unregister_chrdev_region(cd->major, 0, 256));
   return err;
}

   细说:最后一个 register_chrdev() 是一个老式分配设备编号范围的函数。它分配一个单独主设备号和 0 ~ 255 的次设备号范围。如果申请的主设备号为 0 则动态分配一个该函数还需传入一个 file_operations 结构的指针,函数内部自动分配了一个新的 cdev 结构。

4.4、unregister_chrdev_region() 和 unregister_chrdev() 。它们都调用了 __unregister_chrdev_region() 函数



二、几个注册相关函数和变量类型

内核驱动函数四)

1、注册相关的函数
   1.1、dev_t ,dev_t MKDEV(int major, int minor);int MAJOR(dev_t dev);MINOR(dev_t dev)
   1.2、cdev加载模块卸载模块初始化相关

void cdev_init(struct cdev *cdev, const struct file_operations *fops);
int cdev_add(struct cdev *p, dev_t dev, unsigned count);
void cdev_del(struct cdev *p);
struct cdev *cdev_alloc(void);

2、函数简单分析
   2.1dev_t类型解析
       unsigned int 类型,32位
,用于在驱动程序中定义设备编号,高12位为主设备号,低20位为次设备号,在程序中用宏MAJOR(dev_t dev)可以解析出主设备号,用宏MINOR(dev_t dev)可以解析出次设备号

       你在/dev目录下,用命令ll就可以看到那些设备文件的主次设备号.如(31,1)组成一个dev_t,

crwxrwxrwx  1 zhangjianlin root           5,  1  1月 22  2008 console
brwxrwxrwx  1 zhangjianlin root          31,  0  1月 22  2008 mtdblock0
brwxrwxrwx  1 zhangjianlin root          31,  1  1月 22  2008 mtdblock1
brwxrwxrwx  1 zhangjianlin root          31,  2  1月 22  2008 mtdblock2
crwxrwxrwx  1 zhangjianlin root           1,  3  1月 22  2008 null
crwxrwxrwx  1 zhangjianlin root         204, 64  1月 22  2008 ttySAC0


   2.2、加载模块卸载模块初始化相关
       a)cdev_init()、cdev_add()以及卸载模块中的cdev_del()函数(常用)
       b)这些函数就是对cdev的一些操作,其实也就是对你的字符设备的一些操作
c)cdev_init()是用于初始化cedv的成员,并建立与file_operations之间的联系,这个结构体会在后面说明。
3、深入函数内部解析
结构体二)3.1、dev_t相关struct结构
   linux-2.6.22/include/linux/cdev.h

struct cdev {
struct kobject                kobj;
struct module                 *owner;
const struct file_operations -*ops;
struct list_head              list;
dev_t                         dev;
unsigned int                  count;
};

   linux-2.6.22/include/linux/types.h

typedef __u32             __kernel_dev_t;
typedef __kernel_dev_t    dev_t;

   细说:其结构的成员分别为kobject对象、所属模块的指针、文件操作结构体、链表、设备号等。大家再看在加载模块中的cdev_init()、cdev_add()以及卸载模块中的cdev_del()函数,这些函数就是对cdev的一些操作,其实也就是对你的字符设备的一些操作,cdev_init()是用于初始化cedv的成员,并建立与file_operations之间的联系,这个结构体会在后面说明。
 3.2dev_t MKDEV(int major, int minor);int MAJOR(dev_t dev);MINOR(dev_t dev)等函数的定义
   在内核源码中如此定义:

内核驱动源码四)

#define MINORBITS       20
#define MINORMASK       ((1U << MINORBITS) - 1)
//(1<<20 -1) 此操作后,MINORMASK宏的低20位为1,高12位为0
#define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))

 3.3、加载模块卸载模块初始化相关
内核驱动函数五)a)void cdev_init(struct cdev *cdev, const struct file_operations *fops)  

内核驱动源码五)

void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
    memset(cdev, 0, sizeof *cdev);
    INIT_LIST_HEAD(&cdev->list);
    kobject_init(&cdev->kobj, &ktype_cdev_default);  
    cdev->ops = fops;
}


细说:cdev_init()是用于初始化cedv的成员,并建立与file_operations之间的联系
内核驱动函数六)b)int cdev_add(struct cdev *p, dev_t dev, unsigned count)和void cdev_del(struct cdev *p)

内核驱动源码六)

int cdev_add(struct cdev *p, dev_t dev, unsigned count)
{
p->dev = dev;
p->count = count;
return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
}
void cdev_del(struct cdev *p)
{
cdev_unmap(p->dev, p->count);
kobject_put(&p->kobj);
}

       细说:cdev_add()和cdev_del()函数是向系统添加和删除一个cdev,也就是完成字符设备的注册与注销。
           TIP:这里我们都看到kobjet这个结构体和他的一些操作,这个结构体也是比较重要的
内核驱动函数七)c)struct cdev *cdev_alloc(void)

内核驱动源码七)

struct cdev *cdev_alloc(void)
{
struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
    if (p) {
        INIT_LIST_HEAD(&p->list);
        kobject_init(&p->kobj, &ktype_cdev_dynamic);
    }
return p;
}

       细说:用于动态的申请一个cdev内存
       d)cdev_put()函数
       细说:模块的引用计数

模板一)老式注册写法register_chrdev

//“myled_drv”,"myled_","myled"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
static struct class *myled_class;
static struct class_device  *myled_class_dev;
static int myled_drv_open(struct inode *inode, struct file *file)
{
    printk("myled_dev read\n");
    return 0;
}
static ssize_t myled_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    printk("myled_dev write\n");
    return 0;
}
static struct file_operations myled_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   myled_drv_open,  
    .write  =   myled_drv_write,    
};
int major;
static int myled_drv_init(void)
{
    major = register_chrdev(0, "myled_drv", &myled_drv_fops); // 注册, 告诉内核
    if (major < 0) {
      printk(" can't register major number\n");
      return major;
    }
    myled_class = class_create(THIS_MODULE, "myled_drv");
    if (IS_ERR(myled_class))
        return PTR_ERR(myled_class);
    myled_class_dev = class_device_create(myled_class, NULL, MKDEV(major, 0), NULL, "myled"); /* /dev/xyz */
    if (IS_ERR(myled_class_dev))
        return PTR_ERR(myled_class_dev);
    return 0;
}
static void myled_drv_exit(void)
{
    unregister_chrdev(major, "myled_drv"); // 卸载
    class_device_unregister(myled_class_dev);
    class_destroy(myled_class);
}
module_init(myled_drv_init);
module_exit(myled_drv_exit);
MODULE_LICENSE("GPL");

模板2)新式注册写法

//“timerlist_drv”,"timerlist_","timerlist_dev"
#include <linux/module.h>//模块所需的大量符号和函数定义
#include <linux/kernel.h>
#include <linux/fs.h>//文件系统相关的函数和头文件
#include <linux/init.h> //指定初始化和清除函数
#include <linux/delay.h>
#include <linux/cdev.h> //cdev结构的头文件包含<linux/kdev_t.h>
#include <linux/device.h>
#include <linux/mm.h>
//#include <linux/sched.h>//包含驱动程序使用的大部分内核API的定义,包括睡眠函数以及各种变量声明
#include <asm/uaccess.h>//在内核和用户空间中移动数据的函数
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/timer.h>  /*timer*/
#include <asm/uaccess.h>  /*jiffies*/
#define VIRTUALDISK_SIZE  0x1000//4k
#define MEM_CLEAR 0x1
#define VIRTUALDISK_MAJOR 250
/******timer *******/
#define TIMERDELAY 2
int VirtualDisk_major = VIRTUALDISK_MAJOR;
struct cdev cdev;//详细看cdev机制
static struct class *timerlist_class;
static struct class_device  *timerlist_class_dev;
static int timerlist_drv_open(struct inode *inode, struct file *file)
{
    printk("timerlist_dev read\n");
    return 0;
}
static int timerlist_drv_release(struct inode *inode, struct file *file)
{
    printk("timerlist_dev release\n");
    return 0;
}
/*读函数:读写函数主要是让设备结构体的mem[]数组与用户空间交互数据,并随着访问字节数变更返回用户的文件读写偏移位置*/
static ssize_t timerlist_drv_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
   printk("timerlist_dev read\n");
  return 0;
}
/*
 file 是文件指针,count 是请求的传输数据长度,buff 参数是指向用户空间的缓冲区,这个缓冲区或者保存要写入的数据,或者是一个存放新读入数据的空缓冲区,该地址在内核空间不能直接读写,ppos 是一个指针指向一个"long offset type"对象, 它指出用户正在存取的文件位置. 返回值是一个"signed size type。写的位置相对于文件开头的偏移。
 */
static ssize_t timerlist_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    return 0;
}
static struct file_operations timerlist_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .read = timerlist_drv_read,
    .write  =   timerlist_drv_write,    
    .open   =   timerlist_drv_open, 
    .release = timerlist_drv_release,
};
 /*将 cdev 结构嵌入一个你自己的设备特定的结构,你应当初始化你已经分配的结构使用以上函数,有一个其他的 struct cdev 成员你需要初始化. 象 file_operations 结构,struct cdev 有一个拥有者成员,应当设置为 THIS_MODULE,一旦 cdev 结构建立, 最后的步骤是把它告诉内核, 调用:
   cdev_add(&dev->cdev, devno, 1);*/
static int timerlist_drv_init(void)
{
    int result;
    int err;
    dev_t devno = MKDEV(VirtualDisk_major, 0);
    if(VirtualDisk_major){
    result = register_chrdev_region(devno, 1, "timerlist_dev");
    }else{
    result = alloc_chrdev_region(&devno, 0, 1, "timerlist_dev");
    VirtualDisk_major = MAJOR(devno);
    } 
    if(result < 0 ){
    return result;
    }
    cdev_init(&cdev, &timerlist_drv_fops);
    cdev.owner = THIS_MODULE;
    err = cdev_add(&cdev, devno, 1);
    if(err){
    printk("error %d cdev file added\n", err);
    }
    timerlist_class = class_create(THIS_MODULE, "timerlist_drv");
    if (IS_ERR(timerlist_class))
        return PTR_ERR(timerlist_class);
    timerlist_class_dev = class_device_create(timerlist_class, NULL, MKDEV(VirtualDisk_major, 0), NULL, "timerlist_dev"); /* /dev/xyz */
    if (IS_ERR(timerlist_class_dev))
        return PTR_ERR(timerlist_class_dev);
                           
    return 0;
}
static void timerlist_drv_exit(void)
{
    cdev_del(&cdev);
    unregister_chrdev_region(MKDEV(VirtualDisk_major, 0), 1);
    class_device_unregister(timerlist_class_dev);
    class_destroy(timerlist_class);
}
module_init(timerlist_drv_init);
module_exit(timerlist_drv_exit);
MODULE_LICENSE("GPL");


favicon.icofavicon.ico 0 (number)1e87eb1b.jpgWikipedia: 0 (zero; BrE:  or AmE:) is both a number and the numerical digit used to represent that number in numerals.