在Linux中驅動的加載方式有動態加載和靜態加載。
動態加載,即驅動不添加到內核中,在內核啟動完成后,僅在用到這一驅動時才會進行加載
靜態加載,驅動編譯進內核中,隨內核的啟動而完成驅動的加載。
添加字符驅動代碼到內核的方式:
1、在Linux-3.0.8/drivers/char/Kconfig中為charDev添加一個config條目,比如config TEST_CHAR
2、修改Linux-3.0.8/drivers/char/Makefile,添加如下代碼:obj-$(CONFIG_TEST_CHAR)+=charDev.o
3、把charDev.c拷到Linux-3.0.8/drivers/char下
4、make menuconfig,選中CONFIG_TEST_CHAR項目,設定為【M】或者【Y】
5、重新編譯即可(設定為M:編譯后得到的為動態加載模塊*.ko;設定為Y:編譯到內核中)
驅動動態加載過程:(以字符按鍵驅動為例:charButtons.ko,dev_t = 232 0 name= "buttons")
1、編輯並編譯應用程序:buttons.c (gcc -c buttons.c -o buttons)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/ioctl.h> 5 #include <sys/types.h> 6 #include <sys/stat.h> 7 #include <fcntl.h> 8 9 int main(int argc, char **argv) 10 { 11 int on; 12 int led_no; 13 int fd; 14 15 16 fd = open("/dev/buttons", 0); 17 if (fd < 0) { 18 perror("open device buttons"); 19 exit(1); 20 } 21 22 close(fd); 23 24 return 0; 25 }
2、創建設備節點:mknod /dev/buttons c 232 0 刪除設備節點:rm /dev/buttons
3、加載/移除驅動模塊:insmod/rmmod charButtons.ko
4、執行應用程序:./buttons即可調用按鍵驅動