设备驱动开发_2

编译和调试

主要内容

  • 写一个可加载模块
  • 编译和加载一个可加载模块
  • 使用printk 进行跟踪和调试
  • 使用跟踪和调试

1 写一个可加载模块

内核头文件 linux/module.h
module_init(e1000_init_module)
static int __init e1000_init_module(void)
module_exit (e1000_exit_module)
static void _exit e1000_exit_module(void)

tan@ubuntu:~/samba_workspaces/linux-5.4.26$ cat -n ./include/linux/module.h   | grep "module_init("
    78   * module_init() - driver initialization entry point
    81   * module_init() will either be called during do_initcalls() (if
    85  #define module_init(x)  __initcall(x);
   108  #define early_initcall(fn)              module_init(fn)
   109  #define core_initcall(fn)               module_init(fn)
   110  #define core_initcall_sync(fn)          module_init(fn)
   111  #define postcore_initcall(fn)           module_init(fn)
   112  #define postcore_initcall_sync(fn)      module_init(fn)
   113  #define arch_initcall(fn)               module_init(fn)
   114  #define subsys_initcall(fn)             module_init(fn)
   115  #define subsys_initcall_sync(fn)        module_init(fn)
   116  #define fs_initcall(fn)                 module_init(fn)
   117  #define fs_initcall_sync(fn)            module_init(fn)
   118  #define rootfs_initcall(fn)             module_init(fn)
   119  #define device_initcall(fn)             module_init(fn)
   120  #define device_initcall_sync(fn)        module_init(fn)
   121  #define late_initcall(fn)               module_init(fn)
   122  #define late_initcall_sync(fn)          module_init(fn)
   124  #define console_initcall(fn)            module_init(fn)
   126  /* Each module must use one module_init(). */
   127  #define module_init(initfn)                                     \
   546  static inline bool within_module_init(unsigned long addr,
   555          return within_module_init(addr, mod) || within_module_core(addr, mod);
   725  static inline bool within_module_init(unsigned long addr,

module_init(e1000_init_module);

/**
 * e1000_exit_module - Driver Exit Cleanup Routine
 *
 * e1000_exit_module is called just before the driver is removed
 * from memory.
 **/
static void __exit e1000_exit_module(void)
{
   
        pci_unregister_driver(&e1000_driver);
}
module_exit(e1000_exit_module);

MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
MODULE_LICENSE("GPL v2");
MODULE_VERSION(DRV_VERSION);
"drivers/net/ethernet/intel/e1000e/netdev.c" 7927L, 227450C 

2 编译和加载一个可加载模块

obj-m
cat skeleton.c
cat makefile.1
cat makefile.2 —> 多文件 指向内核参数
/lib/modules/uname -r/build M= P W D m o d u l e s l n − s f m a k e f i l e . 1 m a k e f i l e m a k e − C / l i b / m o d u l e s / ‘ u n a m e − r ‘ / b u i l d M = PWD modules ln -sf makefile.1 makefile make -C /lib/modules/`uname -r`/build M= PWDmoduleslnsfmakefile.1make

  • 19
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值