歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核模塊和Linux fs 與 sysfs

Linux內核模塊和Linux fs 與 sysfs

日期:2017/3/1 11:07:07   编辑:Linux內核
注: 參考整理自《Linux設備驅動開發詳解》下載見 http://www.linuxidc.com/Linux/2011-07/38211.htm

一 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. u64 f_version;
  22. #ifdef CONFIG_SECURITY
  23. void *f_security;
  24. #endif
  25. /* needed for tty driver, and maybe others */
  26. void *private_data;
  27. #ifdef CONFIG_EPOLL
  28. /* Used by fs/eventpoll.c to link all the hooks to this file */
  29. struct list_head f_ep_links;
  30. spinlock_t f_ep_lock;
  31. #endif /* #ifdef CONFIG_EPOLL */
  32. struct address_space *f_mapping;
  33. #ifdef CONFIG_DEBUG_WRITECOUNT
  34. unsigned long f_mnt_write_state;
  35. #endif
  36. };

另外可以參考一下 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) (int, struct file *, int);
  20. int (*lock) (struct file *, int, struct file_lock *);
  21. ssize_t (*sendpage) (struct file *, struct page *, int, size_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 *, int, struct 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 *, long, struct 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 ---包括內核中所有已注冊的設備驅動程序;

Copyright © Linux教程網 All Rights Reserved