Linux内核模块和Linux fs 与 sysfs


注: 参考整理自《Linux设备驱动开发详解》下载见

一 Linux 内核模块 :

    1. 查找系统中加载的模块: lsmod ; 卸载模块命令 : rmmod ;  装载命令: insmod/modprobe(modprobe 会自动分析模块间的依赖关系);

    2. 模块加载函数:利用__init 标识

             static int __init  ***_init(void)

             {

                   ................

              }

              module_init(***_init);

            返回0表示加载成功, 失败则返回一些错误代码,参考<linux/errno.h>

           在加载函数中一般用于初始化硬件,申请资源.....

    3. 模块卸载函数:利用__exit标识

            static void __exit ***_exit(void)

            {

                  .............

            }

            module_exit(***_exit);

      注: 在卸载模块的函数中,一般需要完成以下的几件事情:

          # 若模块加载函数中注册了***,则在模块卸载函数中需注销;

          # 若在模块加载函数中动态申请了内存,则在此亦需要释放;

          # 若在模块加载中申请了一些硬件资源(如:IRQ, DMA channel, I/O ,memory),则在此需释放;

    4. 模块参数 : module_param(参数名, 参数类型, 参数读/写权限)

           注: 可以利用上述的方法在加载模块时传递需要的参数。

     5. 导出符号 : 记录了该符号以及该符号所在的内存地址。

           可以使用:

                          EXPORT_SYMBOL(符号名);

                          EXPORT_SYMBOL_GPL(符号名);

           该功能在内核中使用较多,只要导出该符号,则其他的模块在调用该符号时,只需声明则可;这有点类似于跨文件的函数间调用。

     6. 模块声明与描述:

          MODULE_AUTHOR(author);

          MODULE_DESCRIPTION(description);

          MODULE_VERSION(version_string);

          MODULE_DEVICE_TABLE(table_info);

          MODULE_ALIAS(alternate_name);

     7.  模块的编译 :

           下面演示一个 Makefile :

       #Makefile for wm8350_rtc
        obj-m           := wm8350_rtc.o

        all:
                     make -C ../../../linux-2.6.26 M=`pwd` modules

       clean:
                     make -C ../../../linux-2.6.26 M=`pwd` clean
                     rm -f modules.orde

        分析: -C 表示Linux内核所在的目录,需注意的是,这个目录是之前设置好的用来编译zImage的源码;

                  M 表示指定的需要编译成模块的代码源文件和Makefile 文件;

                  modules ----表示编译为内核模块。 

二, Linux文件系统:

       1. C中的文件操作: open , write, read, close,

       2.在Linux的驱动开发中,需要关注的是如下两个结构:

            1) file_operations :应用程序和 VFS(virtual FS)之间通过系统调用链接,而VFS 和普通设备之间则通过file_operation的成员函数(如: read ,write, open ,close ,ioctl ...)

             file 结构体 :参考: /linux/include/linux/fs.h:            

 
  1. struct file {  
  2.     /* 
  3.      * fu_list becomes invalid after file_free is called and queued via 
  4.      * fu_rcuhead for RCU freeing 
  5.      */  
  6.     union {  
  7.         struct list_head    fu_list;  
  8.         struct rcu_head     fu_rcuhead;  
  9.     } f_u;  
  10.     struct path     f_path;  
  11. #define f_dentry    f_path.dentry   
  12. #define f_vfsmnt    f_path.mnt   
  13.     const struct file_operations    *f_op;  
  14.     atomic_t        f_count;  
  15.     unsigned int        f_flags;  
  16.     mode_t          f_mode;  
  17.     loff_t          f_pos;  
  18.     struct fown_struct  f_owner;  
  19.     unsigned int        f_uid, f_gid;  
  20.     struct file_ra_state    f_ra;  
  21.   
  22.     u64         f_version;  
  23. #ifdef CONFIG_SECURITY   
  24.     void            *f_security;  
  25. #endif   
  26.     /* needed for tty driver, and maybe others */  
  27.     void            *private_data;  
  28.   
  29. #ifdef CONFIG_EPOLL   
  30.     /* Used by fs/eventpoll.c to link all the hooks to this file */  
  31.     struct list_head    f_ep_links;  
  32.     spinlock_t      f_ep_lock;  
  33. #endif /* #ifdef CONFIG_EPOLL */   
  34.     struct address_space    *f_mapping;  
  35. #ifdef CONFIG_DEBUG_WRITECOUNT   
  36.     unsigned long f_mnt_write_state;  
  37. #endif   
  38. };  

另外可以参考一下 file_operation 的描述:

  1. struct file_operations {  
  2.     struct module *owner;  
  3.     loff_t (*llseek) (struct file *, loff_t, int);  
  4.     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);  
  5.     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);  
  6.     ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);  
  7.     ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);  
  8.     int (*readdir) (struct file *, void *, filldir_t);  
  9.     unsigned int (*poll) (struct file *, struct poll_table_struct *);  
  10.     int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);  
  11.     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);  
  12.     long (*compat_ioctl) (struct file *, unsigned int, unsigned long);  
  13.     int (*mmap) (struct file *, struct vm_area_struct *);  
  14.     int (*open) (struct inode *, struct file *);  
  15.     int (*flush) (struct file *, fl_owner_t id);  
  16.     int (*release) (struct inode *, struct file *);  
  17.     int (*fsync) (struct file *, struct dentry *, int datasync);  
  18.     int (*aio_fsync) (struct kiocb *, int datasync);  
  19.     int (*fasync) (intstruct file *, int);  
  20.     int (*lock) (struct file *, intstruct file_lock *);  
  21.     ssize_t (*sendpage) (struct file *, struct page *, intsize_t, loff_t *, int);  
  22.     unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);  
  23.     int (*check_flags)(int);  
  24.     int (*dir_notify)(struct file *filp, unsigned long arg);  
  25.     int (*flock) (struct file *, intstruct file_lock *);  
  26.     ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);  
  27.     ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);  
  28.     int (*setlease)(struct file *, longstruct file_lock **);  
  29. }  

2) inode : 包括文件访问权限,属主,组,大小,生成时间,访问时间,最后修改时间等...

inode的结构体定义也是在include/linux/fs.h

3. ps: 可以使用 cat /proc/devices 查询系统中注册的设备。

三, sysfs  --- Linux驱动开发使用得较多,尤其是需要显示一些设备的信息时,而且Android中,sysfs的使用也很多。

1. 目的: sysfs 显示设备驱动模型中各个模块间的层次关系: ls /sys ---可以看到其顶级目录 包括 :

block  bus  class  devices  firmware  module  power

其中: block ----显示块设备;

bus   ----显示系统中所有的总线;

class ----显示系统中的设备类型;

device --显示系统中所有的设备并根据设备挂接的总线类型组织成层次关系;

drivers ---包括内核中所有已注册的设备驱动程序;

  • 1
  • 2
  • 下一页

相关内容