TOC

  • 查看本机是否有linux源码

    查看目录/usr/src
    以ubuntu14.04为例,linux源码目录为/usr/src/linux-headers-3.13.0-24-generic

  • 若是本机没有源码就需要自己去下载相应的源码

    同样以ubuntu 14.04为例
    首先查看本机内核版本:uname -r
    然后去官网下载linux源码,网址如下:[1] :https://www.kernel.org/pub/linux/kernel/
    在上面的网址找到对应版本的源码

2.编写一个简单的驱动程序

hello.c

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

MODULE_LICENSE("GPL"); 

static int __init hello_init(void) 
{ 
    printk("init hello module\n"); 
    return 0; 
} 

static void __exit hello_exit(void) 
{ 
    printk("exit hello module\n"); 
} 

module_init(hello_init); 
module_exit(hello_exit);

Mekefile

KERNEL_DIR=/usr/src/linux-headers-3.13.0-24-generic 
all: 
    make -C $(KERNEL_DIR) M=`pwd` modules 

clean: 
    make -C $(KERNEL_DIR) M=`pwd` clean 

obj-m += hello.o

3.编译驱动及加载

  • 编译驱动

    执行 $ make,编译后会在当前目录生成hello.ko

  • 驱动加载及卸载

    加载驱动 # insmod hello.ko,加载驱动

查看驱动加载 lsmod | grep hello

卸载驱动 # rmmod hello

查看驱动打印信息 $ dmesg | tail

相关文章:

  • 2021-11-23
  • 2022-01-18
  • 2022-12-23
  • 2022-03-01
  • 2021-08-21
  • 2021-10-18
  • 2021-10-25
  • 2021-12-29
猜你喜欢
  • 2021-11-07
  • 2021-04-06
  • 2021-08-15
  • 2022-12-23
  • 2021-10-20
  • 2021-11-10
相关资源
相似解决方案