zoukankan      html  css  js  c++  java
  • linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)

    原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html

    驱动常用头文件介绍

    #include <linux/***.h> 是在linux-2.6.29/include/linux下面寻找源文件。
    #include <asm/***.h> 是在linux-2.6.29/arch/arm/include/asm下面寻找源文件。
    #include <mach/***.h> 是在linux-2.6.29/arch/arm/mach-s3c2410/include/mach下面寻找源文件。

    #include <plat/regs-adc.h>在linux-2.6.31_TX2440A20100510/linux-2.6.31_TX2440A/arch/arm/plat-s3c/include/plat

    #include <linux/module.h> //最基本的文件,支持动态添加和卸载模块。Hello World驱动要这一个文件就可以了
    #include <linux/fs.h> //包含了文件操作相关struct的定义,例如大名鼎鼎的struct file_operations

                                      //包含了struct inode 的定义,MINOR、MAJOR的头文件。
    #include <linux/errno.h> //包含了对返回值的宏定义,这样用户程序可以用perror输出错误信息。
    #include <linux/types.h> //对一些特殊类型的定义,例如dev_t, off_t, pid_t.其实这些类型大部分都是unsigned int型通过一连串的typedef变过来的,只是为了方便阅读。
    #include <linux/cdev.h> //对字符设备结构cdev以及一系列的操作函数的定义。//包含了cdev 结构及相关函数的定义。
    #include <linux/wait.h> //等代队列相关头文件//内核等待队列,它包含了自旋锁的头文件

    #include <linux/init.h>
    #include <linux/kernel.h>            

    #include <linux/slab.h>              //包含了kcalloc、kzalloc内存分配函数的定义。
    #include <linux/uaccess.h>        //包含了copy_to_user、copy_from_user等内核访问用户进程内存地址的函数定义。
    #include <linux/device.h>           //包含了device、class 等结构的定义
    #include <linux/io.h>                  //包含了ioremap、iowrite等内核访问IO内存等函数的定义。
    #include <linux/miscdevice.h>    //包含了miscdevice结构的定义及相关的操作函数。
    #include <linux/interrupt.h>        //使用中断必须的头文件
    #include <mach/irqs.h>             //使用中断必须的头文件
    #include <asm/bitops.h>           //包含set_bit等位操作函数,实现Input子系统时可用。
    #include <linux/semaphore.h>   //使用信号量必须的头文件
    #include <linux/spinlock.h>       //自旋锁

    #include <linux/sched.h>          //内核等待队列中要使用的TASK_NORMAL、TASK_INTERRUPTIBLE包含在这个头文件
    #include <linux/kfifo.h>             //fifo环形队列
    #include <linux/timer.h>           //内核定时器
    #include <linux/input.h>           //中断处理

    头文件主目录include

    头文件目录中总共有32个.h头文件。其中主目录下有13个,asm子目录中有4个,linux子目录中有10个,sys子目录中有5个。这些头文件各自的功能如下,具体的作用和所包含的信息请参见第14章。

    <a.out.h>:a.out头文件,定义了a.out执行文件格式和一些宏。
    <const.h>:常数符号头文件,目前仅定义了i节点中i_mode字段的各标志位。
    <ctype.h>:字符类型头文件,定义了一些有关字符类型判断和转换的宏。
    <errno.h>:错误号头文件,包含系统中各种出错号。(Linus从minix中引进的)。
    <fcntl.h>:文件控制头文件,用于文件及其描述符的操作控制常数符号的定义。
    <signal.h>:信号头文件,定义信号符号常量,信号结构以及信号操作函数原型。
    <stdarg.h>:标准参数头文件,以宏的形式定义变量参数列表。主要说明了一个类型(va_list)和3个宏(va_start, va_arg和va_end),用于vsprintf、vprintf、vfprintf函数。
    <stddef.h>:标准定义头文件,定义了NULL, offsetof(TYPE, MEMBER)。
    <string.h>:字符串头文件,主要定义了一些有关字符串操作的嵌入函数。
    <termios.h>:终端输入输出函数头文件,主要定义控制异步通信口的终端接口。
    <time.h>:时间类型头文件,主要定义了tm结构和一些有关时间的函数原形。
    <unistd.h>:Linux标准头文件,定义了各种符号常数和类型,并声明了各种函数。如,定义了__LIBRARY__,则还包括系统调用号和内嵌汇编_syscall0()等。
    <utime.h>:用户时间头文件,定义了访问和修改时间结构以及utime()原型。

    (1)体系结构相关头文件子目录include/asm

    这些头文件主要定义了一些与CPU体系结构密切相关的数据结构、宏函数和变量。共4个文件。

    <asm/io.h>:I/O头文件,以宏的嵌入汇编程序形式定义对I/O端口操作的函数。
    <asm/memory.h>:内存拷贝头文件,含有memcpy()嵌入式汇编宏函数。
    <asm/segment.h>:段操作头文件,定义了有关段寄存器操作的嵌入式汇编函数。
    <asm/system.h>:系统头文件,定义了设置或修改描述符/中断门等的嵌入式汇编宏。

    (2)Linux内核专用头文件子目录include/linux

    <linux/config.h>:内核配置头文件,定义键盘语言和硬盘类型(HD_TYPE)可选项。
    <linux/fdreg.h>:软驱头文件,含有软盘控制器参数的一些定义。
    <linux/fs.h>:文件系统头文件,定义文件表结构(file,buffer_head,m_inode等)。
    <linux/hdreg.h>:硬盘参数头文件,定义访问硬盘寄存器端口、状态码和分区表等信息。
    <linux/head.h>:head头文件,定义了段描述符的简单结构,和几个选择符常量。
    <linux/kernel.h>:内核头文件,含有一些内核常用函数的原形定义。
    <linux/mm.h>:内存管理头文件,含有页面大小定义和一些页面释放函数原型。
    <linux/sched.h>: 调度程序头文件,定义了任务结构task_struct、初始任务0的数据,
    以及一些有关描述符参数设置和获取的嵌入式汇编函数宏语句。
    <linux/sys.h>:系统调用头文件,含有72个系统调用C函数处理程序,以"sys_"开头。
    <linux/tty.h>:tty头文件,定义了有关tty_io,串行通信方面的参数、常数。

    (3)系统专用数据结构子目录include/sys

    <sys/stat.h>: 文件状态头文件,含有文件或文件系统状态结构stat{}和常量。
    <sys/times.h>:定义了进程中运行时间结构tms以及times()函数原型。
    <sys/types.h>:类型头文件,定义了基本的系统数据类型。
    <sys/utsname.h>:系统名称结构头文件。
    <sys/wait.h>:等待调用头文件,定义系统调用wait()和waitpid()及相关常数符号。

    驱动如何合理添加头文件

    在编写linux设备驱动程序的时候,如果在不参考已有驱动程序的情况下,我们该如何不多不少地用#include包含所需的头文件呢? 
    下面,来通过一个实验来介绍。
    运行环境:Source  Insight  @  Windows  xp
    内核代码:Linux  2.6.32
    实验代码:  简单ramblock实验

     1 #define DEV_MAJOR 251
     2 static struct gendisk *my_rmbk;
     3 static struct request_queue *my_rq;
     4 DEFINE_SPINLOCK(rmbk_lock);
     5 #define RAMBLOCK_SIZE (1024*1024)
     6 const struct block_device_operations rmbk_fops = {
     7 .owner = THIS_MODULE,
     8 };
     9 static void do_rmbk_request(struct request_queue *q)
    10 {
    11 printk("Do_ramdisk_requestn");
    12 }
    13 static int ramdisk_init(void)
    14 {
    15 /* 注册块设备 */
    16 register_blkdev(DEV_MAJOR, "ramdisk");
    17 /* 分配gendisk结构体*/
    18 my_rmbk = alloc_disk(16);
    19 /* 为块设备准备一个请求队列 */
    20 my_rq = blk_init_queue(do_rmbk_request, &rmbk_lock);
    21 sprintf(my_rmbk, "ramdisk");
    22 my_rmbk->major = DEV_MAJOR;
    23 my_rmbk->first_minor = 0;
    24 my_rmbk->fops = &rmbk_fops;
    25 my_rmbk->queue = my_rq;
    26 set_capacity(my_rmbk, RAMBLOCK_SIZE / 512);
    27 /* 添加分区信息到内核列表 */
    28 add_disk(my_rmbk);
    29 return 0;
    30 }
    31 static void ramdisk_exit(void)
    32 {
    33 del_gendisk(my_rmbk);
    34 blk_cleanup_queue(my_rq);
    35 put_disk(my_rmbk);
    36 unregister_blkdev(DEV_MAJOR, "ramdisk");
    37 return;
    38 }
    39 module_init(ramdisk_init);
    40 module_exit(ramdisk_exit);
    41 MODULE_LICENSE("GPL");
    View Code

    上面是没有写#include语句的源代码,直接编译程序,看会报告那些函数未知 

    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:6:  warning:  data  definition  has  no  type  or  storage  class
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:6:  warning:  type  defaults  to  'int'  in  declaration  of  'DEFINE_SPINLOCK'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:6:  warning:  parameter  names  (without  types)  in  function  declaration
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:10:  error:  variable  'rmbk_fops'  has  initializer  but  incomplete  type
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11:  error:  unknown  field  'owner'  specified  in  initializer
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11:  error:  'THIS_MODULE'  undeclared  here  (not  in  a  function)
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11:  warning:  excess  elements  in  struct  initializer
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:11:  warning:  (near  initialization  for  'rmbk_fops')
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:  In  function  'do_rmbk_request':
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:16:  error:  implicit  declaration  of  function  'printk'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:  In  function  'ramdisk_init':
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:22:  error:  implicit  declaration  of  function  'register_blkdev'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:25:  error:  implicit  declaration  of  function  'alloc_disk'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:25:  warning:  assignment  makes  pointer  from  integer  without  a  cast
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28:  error:  implicit  declaration  of  function  'blk_init_queue'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28:  error:  'rmbk_lock'  undeclared  (first  use  in  this  function)
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28:  error:  (Each  undeclared  identifier  is  reported  only  once
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:28:  error:  for  each  function  it  appears  in.)
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:32:  error:  implicit  declaration  of  function  'sprintf'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:32:  warning:  incompatible  implicit  declaration  of  built-in  function  'sprintf'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:32:  error:  dereferencing  pointer  to  incomplete  type
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:33:  error:  dereferencing  pointer  to  incomplete  type
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:34:  error:  dereferencing  pointer  to  incomplete  type
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:35:  error:  dereferencing  pointer  to  incomplete  type
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:36:  error:  dereferencing  pointer  to  incomplete  type
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:38:  error:  implicit  declaration  of  function  'set_capacity'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:41:  error:  implicit  declaration  of  function  'add_disk'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:  In  function  'ramdisk_exit':
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:50:  error:  implicit  declaration  of  function  'del_gendisk'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:51:  error:  implicit  declaration  of  function  'blk_cleanup_queue'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:52:  error:  implicit  declaration  of  function  'put_disk'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:53:  error:  implicit  declaration  of  function  'unregister_blkdev'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:  At  top  level:
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:58:  warning:  data  definition  has  no  type  or  storage  class
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:58:  warning:  type  defaults  to  'int'  in  declaration  of  'module_init'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:58:  warning:  parameter  names  (without  types)  in  function  declaration
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:59:  warning:  data  definition  has  no  type  or  storage  class
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:59:  warning:  type  defaults  to  'int'  in  declaration  of  'module_exit'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:59:  warning:  parameter  names  (without  types)  in  function  declaration
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61:  error:  expected  declaration  specifiers  or  '...'  before  string  constant
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61:  warning:  data  definition  has  no  type  or  storage  class
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61:  warning:  type  defaults  to  'int'  in  declaration  of  'MODULE_LICENSE'
    /home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.c:61:  warning:  function  declaration  isn't a prototype
    make[2]:  ***  [/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk/ramblock.o]  Error  1
    make[1]:  ***  [_module_/home/profiles/2440/rootfs/exps_wqkfsc/ramdisk]  Error  2
    make[1]:  Leaving  directory  `/home/profiles/2440/linux-2.6.32.2'
    make:  ***  [all]  Error  2

    第6行 warning DECLARE_SPINLOCK
    第10行 error rmbk_fops 为struct  block_device_operations
    第11行 error .owner =  THIS_MODULE
    第16行 error printk
    第22行 error register_blkdev
    第25行 error alloc_disk
    第28行 error blk_init_queue、rmbk_lock
    第32行 error sprintf
    第32-36行 error 都是上面引起的连带错误
    第38行 error set_capacity
    第41行 error add_disk
    第50行 error del_gendisk
    第51行 error blk_cleanup_queue
    第52行 error put_disk
    第53行 error unregister_blkdev
    第58-59行 warning module_init,module_exit的警告
    第61行 error&warning MODULE_LICENSE发出的

    先说明一下source  insight里面经常用到的两个快捷键
    Jump  to  difinition(跳至定义处):  CTRL+=或者CTRL+左键单击关键词
    Lookup  reference(查找引用):CTRL+  搜索关键词

    步骤:
    第6行
    DECLARE_SPINLOCK是一个宏,要Lookup  reference(查找引用):#define   DECLARE_MUTEX
    结果:
    Spinlock_types.h  (e:friendly2.6.32.2includelinux):#define   DEFINE_SPINLOCK(x) spinlock_t  x  =  __SPIN_LOCK_UNLOCKED(x)
    包含:#define   <linux/spinlock_types.h>
    第10行
    block_device_operations,Jump  to  difinition,跳到定义处
    怎么查看这个文件的路径呢?
    Project->Add  and  Remove  Project  Files...->右击被选中的文件->Copy  List->Copy  selected  item(s)
    e:friendly2.6.32.2includelinuxBlkdev.h
    包含:#include  <linux/blkdev.h>
    第11行
    THIS_MODULE,  同样的,按住鼠标左键,单击这个词
    路径:e:friendly2.6.32.2includelinuxModule.h
    包含:#include  <linux/module.h>
    第16行
    Jump  to  difinition: printk
    路径: e:friendly2.6.32.2includelinuxKernel.h
    包含:#include  <linux/kernel.h>
    第22行
    Jump  to  difinition: register_blkdev
    路径: e:friendly2.6.32.2lockGenhd.c
    包含:???
    出现问题了,文件并不在include目录或者子目录中,包含这个文件也没用,而我们用Jump  to  difinition也只能跳转到Genhd.c
    出现int  register_blkdev(unsigned  int  major,  const  char  *name)
    这个时候就需要命令Lookup  reference(查找引用)。并且搜索的词也是关键,我们需要搜索这个函数的声明,就需要尽量包含整个函数声明的语句。
    如int  register_blkdev(unsigned  int  major,  const  char  *name)这整段话搜索的话,很可能不成功,因为包含了变量的名字major,  name,不一定能匹配到。
    我们应该搜索如"register_blkdev(unsigned int"或者"register_blkdev("这样的关键字,那么就试一试吧。
    Fs.h  (e:friendly2.6.32.2includelinux):extern  int  register_blkdev(unsigned  int,  const  char  *);
    Genhd.c  (e:friendly2.6.32.2lock):int  register_blkdev(unsigned  int  major,  const  char  *name)
    找到了e:friendly2.6.32.2includelinuxFs.h
    那我们就包含#include  <linux/fs.h>
    第25行
    Jump  to  difinition: alloc_disk
    路径: e:friendly2.6.32.2lockGenhd.c
    找到:struct  gendisk  *alloc_disk(int  minors)
    Lookup  reference关键字:alloc_disk(int
    结果:
    Genhd.c  (e:friendly2.6.32.2lock):struct  gendisk  *alloc_disk(int  minors)
    Genhd.h  (e:friendly2.6.32.2includelinux):extern  struct  gendisk  *alloc_disk(int  minors);
    包含:#include  <linux/genhd.h>
    我们可以在这个文件里看看,应该和一般的硬盘操作有关的函数都在里面,可以找到
    set_capacity、add_disk、del_gendisk、put_disk
    第28行
    Jump  to  difinition: blk_init_queue
    路径: e:friendly2.6.32.2lockBlk-core.c
    找到:struct  request_queue  *blk_init_queue(request_fn_proc  *rfn,  spinlock_t  *lock)
    Lookup  reference关键字:blk_init_queue(request_fn_proc
    结果:
    Blk-core.c  (e:friendly2.6.32.2lock):struct  request_queue  *blk_init_queue(request_fn_proc  *rfn,  spinlock_t  *lock)
    Blkdev.h  (e:friendly2.6.32.2includelinux):extern  struct  request_queue  *blk_init_queue(request_fn_proc  *,  spinlock_t  *);
    包含:#include  <linux/blkdev.h>,这里还可以找到blk_cleanup_queue
    rmbk_lock,第6行已解决
    第32行
    Jump  to  difinition: sprintf
    路径: e:friendly2.6.32.2libVsprintf.c
    找到:int  sprintf(char  *  buf,  const  char  *fmt,  ...)
    Lookup  reference关键字:sprintf(char  *  buf
    结果:
    Kernel.h  (e:friendly2.6.32.2includelinux):extern  int  sprintf(char  *  buf,  const  char  *  fmt,  ...)
    Vsprintf.c  (e:friendly2.6.32.2lib):int  sprintf(char  *  buf,  const  char  *fmt,  ...)
    包含:#include  <linux/kernel.h>,其实和printk在一个文件里面
    第32-36行
    上面已解决
    第38行
    set_capacity,第25行已解决
    第41行
    add_disk,第25行已解决
    第50行
    del_gendisk,第25行已解决
    第51行 error blk_cleanup_queue
    第52行
    put_disk,第25行已解决
    第53行
    unregister_blkdev,第28行已解决
    第58-59行
    Jump  to  difinition: module_init是找不到的,我们知道它是一个宏
    所以只能Lookup  reference关键字:#define   module_init
    结果:
    Init.h  (e:friendly2.6.32.2includelinux):#define   module_init(x) __initcall(x);
    Init.h  (e:friendly2.6.32.2includelinux):#define   module_init(initfn)
    包含:#include  <linux/init.h>,  module_exit也包含在里面
    第61行
    同样的只能Lookup  reference关键字:MODULE_LICENSE
    结果:
    Module.h  (e:friendly2.6.32.2includelinux):#define   MODULE_LICENSE(_license)  MODULE_INFO(license,  _license)
    Nand_ecc.c  (e:friendly2.6.32.2driversmtd and):#define   MODULE_LICENSE(x) /* x */
    Pq.h  (e:friendly2.6.32.2includelinux aid):#define   MODULE_LICENSE(licence)
    包含:#include  <linux/module.h>

    添加上头文件


    1. #include <linux/spinlock_types.h>
    2. #include <linux/blkdev.h>
    3. #include <linux/module.h>
    4. #include <linux/kernel.h>
    5. #include <linux/fs.h>
    6. #include <linux/genhd.h>
    7. #include <linux/init.h>



    再次编译,Finished!

  • 相关阅读:
    api接口出现Provisional headers are shown,
    yii消息提示扩展
    php中普通类 接口类 抽象类 浅谈
    读韭菜的自我修养
    读李笑来的《把时间当作朋友》
    Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
    Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
    Spring框架整合Struts2框架的传统方法
    ecplice中代码使用快捷键无法格式化,使用其他方法将代码格式化的步骤
    SSH框架整合jar包时的注意事项
  • 原文地址:https://www.cnblogs.com/kwseeker-bolgs/p/4477892.html
Copyright © 2011-2022 走看看