萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 字元裝置驅動開發實戰:手動載入、Kconfig 選單、動態編譯、裝置節點建立

一、手動載入/卸載測試

insmod ./key_test.ko           # 載入模組
cat /proc/modules | grep key_test  # 查看載入位址
lsmod                          # 列出所有模組與主/次裝置號
rmmod key_test                 # 卸載模組

二、整合進核心原始碼樹動態編譯

  1. 放置原始碼:key_test.c → /drivers/char/
  2. 修改 Kconfig:在 /drivers/char/Kconfig 新增:
    config KEY_TEST
        bool "key test"
        ---help---
        This key test help.
    bool 為勾選框;改 tristate 可支援模組化編譯(產生 .ko)。
  3. 修改 Makefile:在 /drivers/char/Makefile 加入:
    obj-$(CONFIG_KEY_TEST) += key_test.o
  4. 編譯核心:make menuconfig 勾選 Device Drivers → Character devices → key test,make zImage 產生新核心,重啟載入。

三、建立裝置節點並測試

mknod /dev/key_test c 121 0   # c=字元裝置,121=主裝置號,0=次裝置號
cat /dev/key_test               # 相當於 open() 測試
# 或撰寫程式呼叫 open()/write() 等系統呼叫

驅動載入後必須建立對應裝置節點才能被使用者空間存取。

copyright © 萬盛學電腦網 all rights reserved