導航:首頁 > 源碼編譯 > linux編譯驅動

linux編譯驅動

發布時間:2022-02-06 09:45:42

linux安裝設備驅動需要哪些編譯環境

和編譯一些開源代碼的環境一樣,就是make工具和GCC編譯器。

⑵ 怎麼編譯目標機linux設備驅動

在宿主機上安裝開發工具和下載linux源碼(要求版本號和目標機上的linux內核版本一致)。開發工具主要有gcc、gdb、make等,這些工具在redhat或fc中默認就安裝了,在debian或Ubuntu中可以通過下面這個命令安裝:
apt-get install build-essential
linux源碼可以通過以下幾種途徑獲得:
將源碼解壓到/usr/src/目錄後,進入linux-source-(版本號)目錄中執行下面幾個命令:
make oldconfig
make prepare
make scripts
直接去www.kernel.org下載
通過包管理工具下載源碼,在debian和Ubuntu中可以通過下面這個命令下載,
apt-get install linux-source-(版本號) ,下載後的文件在/usr/src目錄中,解壓到該目錄即可
編寫Linux驅動程序,以一個最簡單的hello.c為例,hello.c的內容如下:

#include "linux/init.h"
#include "linux/mole.h"

static int hello_init(void)
{
printk(KERN_ALERT "Hello World linux_driver_mole\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbey linux_driver_mole\n");
}

mole_init(hello_init);
mole_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lpj");

寫Makefile文件,一個示例如下,裡面各項參數根據實際情況更改:

#sample driver mole
obj-m := hello.o
KDIR = /usr/src/linux-source-2.6.24/

all:
$(MAKE) -C $(KDIR) M=$(PWD)

.PHONY:clean
clean:
rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions

編譯,在hello.c和Makefile所在目錄下執行 make 即可,編譯後在當前目錄生成hello.ko文件
載入並測試:載入使用insmod或modprobe命令來實現,如在當前路徑執行如下代碼:
insmod hello.ko 或 modprobe hello
注意,如果在虛擬終端載入內核的話,將看不到內核列印信息,因為內核列印信息不會輸出到虛擬終端,而是輸出到/proc/kmsg文件中,所以可以通過以下方式查看內核信息:
cat /proc/kmsg 會一直列印,需要Ctrl-C手動終止
dmesg 或 dmesg | tail -N ,N為一數字,表示顯示最後N行
卸載:使用rmmod命令卸載驅動模塊,如 rmmod hello

⑶ linux下怎麼編譯安裝驅動

linux 編譯安裝驅動有兩種,動態載入與靜態載入
動態載入
一,編譯,在指點內核樹下編譯,生成.o文件或.ko文件
二,將生成的.o或.ko文件拷到相應目錄,一般是/lib/mole/kernel下面
三,用insmod命令載入,用rmmod命令卸載
靜態載入
靜態載入主要就是編譯內核。就是將編寫好的驅動放進內核相應的目錄下面。然後編譯內核。然後運行編譯好的內核。

⑷ 如何編寫Linux的驅動程序

};  //IO功能選項,硬體上拉輸出  static unsigned int gpio_cfg_table[] = {      S3C2410_GPB5_OUTP,    S3C2410_GPB6_OUTP,    S3C2410_GPB7_OUTP,    S3C2410_GPB8_OUTP, };  //編寫一個ioctl函數,這個函數提供給用戶端使用(也就是用戶態使用)  static int my_ioctl(struct inode *inode,struct file* file,unsigned int cmd,           unsigned long arg) {                 if (arg > 4)        {            return -EINVAL;        }         if (cmd == 1) //led ON        {             s3c2410_gpio_setpin(gpio_table[arg],0);            return 0;        }         if (cmd == 0) //led OFF        {            s3c2410_gpio_setpin(gpio_table[arg],1);           return 0;        }        else        {             return -EINVAL;        }  }  //一個和文件設備相關的結構體。  static struct file_operations dev_fops =  {         .owner = THIS_MODULE,        .ioctl = my_ioctl,         //.read  = my_read,   //這個暫時屏蔽,一會我們再加入一個讀操作的函數 };  //linux中設備的注冊結構體 static struct miscdevice misc = 
{         .minor = MISC_DYNAMIC_MINOR,        .name  = DEVICE_NAME,        .fops  = &dev_fops, };  //設備初始化(包括注冊)函數 static int __init dev_init(void) {         int ret;        int i;         for (i=0;i<4;i++)        {             s3c2410_gpio_cfgpin(gpio_table[i],gpio_cfg_table[i]);            s3c2410_gpio_setpin(gpio_table[i],0);            mdelay(500);             s3c2410_gpio_setpin(gpio_table[i],1);        }         ret = misc_register(&misc);         printk(DEVICE_NAME"MY_LED_DRIVER init ok\n");        return ret; }  //設備注銷函數   static void __exit dev_exit(void) {         misc_deregister(&misc); }  //與模塊相關的函數 mole_init(dev_init); mole_exit(dev_exit); MODULE_LICENSE("GPL");  MODULE_AUTHOR("blog.ednchina.com/itspy"); 
MODULE_DESCRIPTION("MY LED DRIVER");  到此,上面就完成了一個簡單的驅動(別急,下面我們再會稍微增加點復雜的東西),以上代碼的可以簡單概括為:像自己寫51單片機或者ARM的裸奔程序一樣操作IO函數,然後再linux系統中進行相關必須的函數關聯和注冊。 為什麼要關聯呢,為什麼注冊呢? 因為這是必須的,從以下這些結構體就知道了。 stuct file_operations{  struct mole *owner;  loff_t (*llseek) (struct file *, loff_t, int);  ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);  ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);  ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);  ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);  int (*readdir) (struct file *, void *, filldir_t); 
 unsigned int (*poll) (struct file *, struct poll_table_struct *);  int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);  long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); … }  file_operations 結構體中包括了很多與設備相關的函數指針,指向了驅動所提供的函數。 struct inode{  struct hlist_node i_hash;  struct list_head i_list;  struct list_head i_sb_list;  struct list_head i_dentry;  unsigned long  i_ino;  atomic_t  i_count;  unsigned int  i_nlink;  uid_t   i_uid;  gid_t   i_gid;  dev_t   i_rdev;  u64   i_version;  loff_t   i_size; … }     inode 是 UNIX 操作系統中的一種數據結構,它包含了與文件系統中各個文件相關的一些重要信息。在 UNIX 中創建文件系統時,同時將會創建大量的 inode 。通常,文件系統磁碟空間中大約百分之一空間分配給了 inode 表。  大略了解以上信息之後,我們只需把我們所要實現的功能和結構體關聯起來。上例中已經完成IO寫操作的函數,現在我們再添加一個讀的函數。基於這種原理,我們想實現各種功能的驅動也就很簡單了。  //添加讀函數示意, 用戶層可以通過 read函數來操作。  static int my_read(struct file* fp, char __user *dat,size_t cnt) {        size_t i;         printk("now read the hardware...\n");       for(i=0;i<cnt;i++)           dat[i] = 'A';       dat[i] = '\0';       return cnt;  }  這樣,完成驅動編寫。編譯之後,本驅動可以通過直接嵌入內核中,也可以以模塊的嵌入的形式載入到linux內核中去。  完成了驅動,寫個應用程序了驗證一下吧:  int main(int argc,char ** argv) {  
    int on;     int led_no;     int fd;     char str[10];     int cnt =0;      fd = open("/dev/MY_LED_DRIVER",0);     if (fd < 0)     {         printf("can't open dev\n");        exit(1);         }      printf("read process\n");     cnt = read(fd,str,10);      printf("get data from driver:\n%s\ncount = %d\n",str,cnt);     printf("read process end \n");     cnt = 0;      printf("running...\n");     while(cnt++<1000)     {        ioctl(fd,0,0);  //led off        ioctl(fd,0,1);       ioctl(fd,0,2);       ioctl(fd,0,3);       sleep(1);   //printf("sdfdsfdsfdsfds...\n");       ioctl(fd,1,0);  //led on       ioctl(fd,1,1);       ioctl(fd,1,2);       ioctl(fd,1,3);       sleep(1);        printf("%d\b",cnt);     }      close(fd);     return 0; }

⑸ linux 查看有哪些驅動編譯進了內核

一、 驅動程序編譯進內核的步驟
在 linux 內核中增加程序需要完成以下三項工作:
1. 將編寫的源代碼復制到 Linux 內核源代碼的相應目錄;
2. 在目錄的 Kconfig 文件中增加新源代碼對應項目的編譯配置選項;
3. 在目錄的 Makefile 文件中增加對新源代碼的編譯條目。

bq27501驅動編譯到內核中具體步驟如下:
1. 先將驅動代碼bq27501文件夾復制到 ti-davinci/drivers/ 目錄下。
確定bq27501驅動模塊應在內核源代碼樹中處於何處。
設備驅動程序存放在內核源碼樹根目錄 drivers/ 的子目錄下,在其內部,設備驅動文件進一步按照類別,類型等有序地組織起來。
a. 字元設備存在於 drivers/char/ 目錄下
b. 塊設備存放在 drivers/block/ 目錄下
c. USB 設備則存放在 drivers/usb/ 目錄下。
注意:
(1) 此處的文件組織規則並非絕對不變,例如: USB 設備也屬於字元設備,也可以存放在 drivers/usb/ 目錄下。
(2) 在 drivers/char/ 目錄下,在該目錄下同時存在大量的 C 源代碼文件和許多其他目錄。所有對於僅僅只有一兩個源文件的設備驅動程序,可以直接存放在該目錄下,但如果驅動程序包含許多源文件和其他輔助文件,那麼可以創建一個新子目錄。
(3) bq27501的驅動是屬於字元設備驅動類別,雖然驅動相關的文件只有兩個,但是為了方面查看,將相關文件放在了bq27501的文件夾中。在drivers/char/目錄下增加新的設備過程比較簡單,但是在drivers/下直接添加新的設備稍微復雜點。所以下面首先給出在drivers/下添加bq27501驅動的過程,然後再簡單說明在drivers/char/目錄下添加的過程。

2. 在/bq27501下面新建一個Makefile文件。向裡面添加代碼:
obj-$(CONFIG_BQ27501)+=bq27501.o
此時,構建系統運行就將會進入 bq27501/ 目錄下,並且將bq27501.c 編譯為 bq27501.o
3. 在/bq27501下面新建Kconfig文件。添加代碼:
menu "bq27501 driver"

config BQ27501
tristate"BQ27501"
default y
---help---
Say 'Y' here, it will be compiled into thekernel; If you choose 'M', it will be compiled into a mole named asbq27501.ko.
endmenu
注意:help中的文字不能加回車符,否則make menuconfig編譯的時候會報錯。
4. 修改/drivers目錄下的Kconfig文件,在endmenu之前添加一條語句『source drivers/bq27501/Kconfig』 對於驅動程序,Kconfig 通常和源代碼處於同一目錄。 若建立了一個新的目錄,而且也希望 Kconfig 文件存在於該目錄中的話,那麼就必須在一個已存在的 Kconfig 文件中將它引入,需要用上面的語句將其掛接在 drivers 目錄中的Kconfig 中。

5. 修改/drivers目下Makefile文件,添加『obj-$(CONFIG_BQ27501) +=bq27501/』。這行編譯指令告訴模塊構建系統在編譯模塊時需要進入 bq27501/ 子目錄中。此時的驅動程序的編譯取決於一個特殊配置 CONFIG_BQ27501 配置選項。

6. 修改arch/arm目錄下的Kconfig文件,在menu "Device Drivers……endmenu"直接添加語句
source "drivers/bq27501/Kconfig" 樓主 學習linux可以看看http://www.linuxprobe.com/chapter-00.html。

⑹ 如何編譯一個linux下的驅動模塊

按照《linux設備驅動開發詳解》一書中的步驟實現經典例子"hello,world!"的例子。
具體步驟如下:
=============================================
1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers

⑺ linux 驅動程序開發,為什麼要編譯內核源碼樹

您好,這樣的情況建議您下載最新版本的驅動精靈,或是直接在線升級一下驅動精靈。希望可以幫到您。

⑻ Linux驅動編譯

這文件目錄下面根本就沒有需要編譯的文件,所以make的時候提示,命令沒有找到(沒有那個文件)
「README_STA_usb」,你看看這個文檔,是安裝說明,裡面應該有講解。

⑼ linux怎麼編譯進驅動進內核

一、 驅動程序編譯進內核的步驟
在 linux 內核中增加程序需要完成以下三項工作:
1. 將編寫的源代碼復制到 Linux 內核源代碼的相應目錄;
2. 在目錄的 Kconfig 文件中增加新源代碼對應項目的編譯配置選項;
3. 在目錄的 Makefile 文件中增加對新源代碼的編譯條目。

bq27501驅動編譯到內核中具體步驟如下:
1. 先將驅動代碼bq27501文件夾復制到 ti-davinci/drivers/ 目錄下。
確定bq27501驅動模塊應在內核源代碼樹中處於何處。
設備驅動程序存放在內核源碼樹根目錄 drivers/ 的子目錄下,在其內部,設備驅動文件進一步按照類別,類型等有序地組織起來。
a. 字元設備存在於 drivers/char/ 目錄下
b. 塊設備存放在 drivers/block/ 目錄下
c. USB 設備則存放在 drivers/usb/ 目錄下。
注意:
(1) 此處的文件組織規則並非絕對不變,例如: USB 設備也屬於字元設備,也可以存放在 drivers/usb/ 目錄下。
(2) 在 drivers/char/ 目錄下,在該目錄下同時存在大量的 C 源代碼文件和許多其他目錄。所有對於僅僅只有一兩個源文件的設備驅動程序,可以直接存放在該目錄下,但如果驅動程序包含許多源文件和其他輔助文件,那麼可以創建一個新子目錄。
(3) bq27501的驅動是屬於字元設備驅動類別,雖然驅動相關的文件只有兩個,但是為了方面查看,將相關文件放在了bq27501的文件夾中。在drivers/char/目錄下增加新的設備過程比較簡單,但是在drivers/下直接添加新的設備稍微復雜點。所以下面首先給出在drivers/下添加bq27501驅動的過程,然後再簡單說明在drivers/char/目錄下添加的過程。

2. 在/bq27501下面新建一個Makefile文件。向裡面添加代碼:
obj-$(CONFIG_BQ27501)+=bq27501.o
此時,構建系統運行就將會進入 bq27501/ 目錄下,並且將bq27501.c 編譯為 bq27501.o
3. 在/bq27501下面新建Kconfig文件。添加代碼:
menu "bq27501 driver"

config BQ27501
tristate"BQ27501"
default y
---help---
Say 'Y' here, it will be compiled into thekernel; If you choose 'M', it will be compiled into a mole named asbq27501.ko.
endmenu
注意:help中的文字不能加回車符,否則make menuconfig編譯的時候會報錯。
4. 修改/drivers目錄下的Kconfig文件,在endmenu之前添加一條語句『source drivers/bq27501/Kconfig』 對於驅動程序,Kconfig 通常和源代碼處於同一目錄。 若建立了一個新的目錄,而且也希望 Kconfig 文件存在於該目錄中的話,那麼就必須在一個已存在的 Kconfig 文件中將它引入,需要用上面的語句將其掛接在 drivers 目錄中的Kconfig 中。

5. 修改/drivers目下Makefile文件,添加『obj-$(CONFIG_BQ27501) +=bq27501/』。這行編譯指令告訴模塊構建系統在編譯模塊時需要進入 bq27501/ 子目錄中。此時的驅動程序的編譯取決於一個特殊配置 CONFIG_BQ27501 配置選項。

6. 修改arch/arm目錄下的Kconfig文件,在menu "Device Drivers……endmenu"直接添加語句
source "drivers/bq27501/Kconfig"

⑽ 如何編譯linux usb驅動

得自己寫makefile文件

ifneq($(KERNELRELEASE),)
obj-m:=xxx.o
else
KERNELDIR?=/lib/moles/$(shelluname-r)/build
PWD:=$(shellpwd)
default:
$(MAKE)-C$(KERNELDIR)M=$(PWD)moles
endif
clean:
rm-rf*.mod.**.o*.ko.*.ko.*.tmp*.*.mod.o.*.*.o.*


其 中xxx是源文件的文件名,在linux下直接執行make就可以生成驅動模塊(xxx.ko)了。生成驅動模塊後使用insmod xxx.ko就可以插入到內核中運行了,用lsmod可以看到你插入到內核中的模塊,也可以從系統中用命令rmmod xxx把模塊卸載掉;如果把編譯出來的驅動模塊拷貝到/lib/moles/~/kernel/drivers/usb/下,然後depmod一下, 那麼你在插入USB設備的時候,系統就會自動為你載入驅動模塊的;當然這個得有hotplug的支持;載入驅動模塊成功後就會在/dev/下生成設備文件 了,如果用命令cat /proc/bus/usb/devices,我們可以看到驅動程序已經綁定到介面上了:
T: Bus=03 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#= 2 Spd=12 MxCh= 0
D: Ver= 1.10 Cls=02(comm.) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=1234 ProdID=2345 Rev= 1.10
C:* #Ifs= 1 Cfg#= 1 Atr=c0 MxPwr= 0mA
I: If#= 1 Alt= 0 #EPs= 2 Cls=0a(data ) Sub=00 Prot=00 Driver=test_usb_driver /*我們的驅動*/
E: Ad=01(O) Atr=02(Bulk) MxPS= 64 Ivl=0ms
E: Ad=82(I) Atr=02(Bulk) MxPS= 64 Ivl=0ms
此框架程序生成的是skel0(可以自由修改)的設備文件,現在就可以對這個設備文件進行打開、讀寫、關閉等的操作了。

閱讀全文

與linux編譯驅動相關的資料

熱點內容
如何對電腦的d盤加密 瀏覽:98
刀片式伺服器怎麼連接電腦 瀏覽:79
矩陣計算java 瀏覽:231
如何把各銀行app整合 瀏覽:877
方舟生存進化手游版如何建伺服器 瀏覽:288
哪裡購買黃金app 瀏覽:661
中國聯通app優惠購功能在哪裡 瀏覽:227
dream伺服器密碼是什麼 瀏覽:222
程序員自救 瀏覽:486
cmd殺死進程命令 瀏覽:236
ipad激活伺服器地址 瀏覽:452
單片機開始直流電機壓降問題 瀏覽:17
伺服器地址失敗怎麼辦 瀏覽:147
安卓手機怎麼下載蘇聯游戲 瀏覽:132
主教的命令 瀏覽:807
php判斷變數為空 瀏覽:745
你演我猜安卓版本怎麼用 瀏覽:910
已夠app哪裡看 瀏覽:1007
程序員怎麼學會開車的技巧 瀏覽:785
網易郵箱如何刪除伺服器郵件 瀏覽:154