美文网首页
Ubuntu驱动demo

Ubuntu驱动demo

作者: 王振 | 来源:发表于2018-07-10 19:59 被阅读0次
    1. 查看系统版本号:
        evidence@evidence-VirtualBox:~/DriverTest$ uname -r
    3.2.0-64-generic-pae
    
    1. 编写Hello World模块

       #include <linux/init.h>  
       #include <linux/module.h>  
       
       MODULE_LICENSE("Dual BSD/GPL");  
         
       static int hellodriver_init(void)  
       {  
             printk(KERN_ALERT "hellodriver.driver world \n");  
             return 0;  
       }  
         
       static void hellodriver_exit(void)  
       {  
             printk(KERN_ALERT "Goodbye,hellodriver");  
       }  
         
       module_init(hellodriver_init);  
       module_exit(hellodriver_exit);
      
    2. 编写Makefile文件

       obj-m := hellodriver.o
       KERNELDIR := /lib/modules/3.2.0-64-generic-pae/build/  
       PWD := $(shell pwd)  
       modules:  
           $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
       modules_install:  
           $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
      
    3. 编译

       evidence@evidence-VirtualBox:~/DriverTest$ cd /home/evidence/DriverTest 
       evidence@evidence-VirtualBox:~/DriverTest$ make
      

    编译结果如下:

        evidence@evidence-VirtualBox:~/DriverTest$ ls -la
        total 100
        drwxrwxr-x  3 evidence evidence  4096 May 27 03:03 .
        drwxr-xr-x 28 evidence evidence  4096 May 27 02:50 ..
        -rw-rw-r--  1 evidence evidence   377 May 27 02:56 hellodriver.c
        -rw-rw-r--  1 evidence evidence  2602 May 27 03:03 hellodriver.ko
        -rw-rw-r--  1 evidence evidence   279 May 27 03:03 .hellodriver.ko.cmd
        -rw-rw-r--  1 evidence evidence   690 May 27 03:03 hellodriver.mod.c
        -rw-rw-r--  1 evidence evidence  1832 May 27 03:03 hellodriver.mod.o
        -rw-rw-r--  1 evidence evidence 26346 May 27 03:03 .hellodriver.mod.o.cmd
        -rw-rw-r--  1 evidence evidence  1932 May 27 03:03 hellodriver.o
        -rw-rw-r--  1 evidence evidence 26243 May 27 03:03 .hellodriver.o.cmd
        -rw-rw-r--  1 evidence evidence   224 May 27 04:11 Makefile
        -rw-rw-r--  1 evidence evidence    48 May 27 03:03 modules.order
        -rw-rw-r--  1 evidence evidence     0 May 27 03:03 Module.symvers
        drwxrwxr-x  2 evidence evidence  4096 May 27 03:03 .tmp_versions
    
    1. 安装

       evidence@evidence-VirtualBox:~/DriverTest$ sudo  insmod  ./hellodriver.ko 
       evidence@evidence-VirtualBox:~/DriverTest$ sudo dmesg -c
       
       <1>[128873.191644] hellodriver.driver world 
       evidence@evidence-VirtualBox:~/DriverTest$ tree -a /sys/module/hellodriver/
       /sys/module/hellodriver/
       ├── holders
       ├── initstate
       ├── notes
       │   └── .note.gnu.build-id
       ├── refcnt
       ├── sections
       │   ├── .gnu.linkonce.this_module
       │   ├── __mcount_loc
       │   ├── .note.gnu.build-id
       │   ├── .rodata.str1.1
       │   ├── .strtab
       │   ├── .symtab
       │   └── .text
       ├── srcversion
       └── uevent
       
       3 directories, 12 files
       
       evidence@evidence-VirtualBox:~/DriverTest$ sudo  rmmod  hellodriver 
       evidence@evidence-VirtualBox:~/DriverTest$ sudo dmesg -c
       [128887.792820] Goodbye,hellodriver
       evidence@evidence-VirtualBox:~/DriverTest$

    相关文章

      网友评论

          本文标题:Ubuntu驱动demo

          本文链接:https://www.haomeiwen.com/subject/cbmqpftx.html