1. 程式人生 > >linux 核心編譯驅動模組ko的配置以及載入模組解除安裝模組例項測試

linux 核心編譯驅動模組ko的配置以及載入模組解除安裝模組例項測試

linux 核心編譯驅動模組ko的配置以及載入模組、解除安裝模組例項測試

一、要讓linux系統支援動態載入驅動模組必須先對linux 核心進行相關的配置,不然編譯不過,載入模組也會載入失敗甚至導致裝置重啟。

1、勾選核心Enable loadable module support選項


2、按回車進入到Enable loadable module support選項子目錄,如下圖勾選對應選項。


簡單的解釋一下各選項:

Enable loadable module support 

開啟可載入模組支援,如果開啟它則必須通過”make modules_install”把核心模組安裝在/lib/modules/中 
Forced module loading 
允許模組強制載入 
Module unloading 
允許解除安裝已經載入的模組 
Forced module unloading 
允許強制解除安裝正在使用中的模組(比較危險) 
Module versioning support 
允許使用其他核心版本的模組(可能會出問題) 
Source checksum for all modules 
為所有的模組校驗原始碼,如果你不是自己編寫核心模組就不需要它 
Module signature verification 
模組簽名認證 
Require modules to be validly signed 
要求模組有效簽名 
Automatically sign all modules 
自動簽署所有模組 
Which hash algorithm should modules be signed with? (Sign m 
哪個雜湊演算法應該與模組簽署? (Sign m 
Compress modules on installation 
在安裝時壓縮模組 
Trim unused exported kernel symbols 
修剪未使用的匯出的核心符號

3、然後編譯核心一下,一定要編譯。


二、完成的上面的一步,現在可以編譯驅動模組檔案,生成ko檔案了。

1、新建一個Makefile檔案。KDIR是核心程式碼的路徑,我這裡的路徑是

/home/weifanghai/Android_4.4_git/xunwei/kernel/iTop4412_Kernel_3.0,修改到你放核心程式碼的路徑。

Makefile

obj-m += schedule_work.o 

KDIR := /home/weifanghai/Android_4.4_git/xunwei/kernel/iTop4412_Kernel_3.0
PWD ?= $(shell pwd)


all:
	make -C $(KDIR) M=$(PWD) modules
		
clean:
	rm -rf *.o

2、在同一個目錄下新建一個c檔案,比如schedule_work.c

我的測試檔案schedule_work.c 這是一個簡單的測試程式碼。

/* 
* LEDs driver for GPIOs 
* 
* Copyright (C) 2007 8D Technologies inc. 
* Raphael Assenat <[email protected]> 
* Copyright (C) 2008 Freescale Semiconductor, Inc. 
* 
* This program is free software; you can redistribute it and/or modify 
* it under the terms of the GNU General Public License version 2 as 
* published by the Free Software Foundation. 
* 
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/leds.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/delay.h>

static int steping_motor_probe(struct platform_device *pdev)
{  

   printk("steping_motor_probe 20180415\n");    

}
static int steping_motor_remove(struct platform_device *pdev)
	{   
	   printk("steping_motor_probe remove 20180415\n"); 
	   return 0;
	}


static struct platform_driver steping_motor_driver = {        
	.probe		= steping_motor_probe,        
	.remove		= steping_motor_remove,
	.driver		= {                
	          .name	= "love_tanghanyue",                
	          .owner	= THIS_MODULE,                       
	},
	};

static int motor_init(void)
{	
int DriverState;		
printk(KERN_EMERG "HELLO WORLD enter! hanyue\n");	
DriverState = platform_driver_register(&steping_motor_driver);		
printk(KERN_EMERG "\tDriverState is %d\n",DriverState);	
return 0;
}

static void motor_exit(void)
{	
printk(KERN_EMERG "HELLO WORLD exit!\n");		
platform_driver_unregister(&steping_motor_driver);	
}

module_init(motor_init);
module_exit(motor_exit);


MODULE_AUTHOR("
[email protected]
## tanghanyue.cc"); MODULE_DESCRIPTION("2-Phase Steping motor driver"); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:setping-motor");


3、編譯,直接在該路徑下make就可以了,就生成了ko檔案了。


三、做了前面的準備工作,已經得到模組ko檔案,現在可以開始測試載入和解除安裝驅動模組。

1、insmod載入模組命令 ;lsmod檢視模組命令;rmmod解除安裝模組命令。

2、把模組檔案(例如schedule_work.ko)放到裝置裡面,我這裡是在android系統上測試,核心都是linux,都一樣,我直接push到system/bin目錄下面。

3、載入核心、檢視模組命令、rmmod解除安裝模組命令


4、看列印log,測試ok,載入和解除安裝模組都ok。