Device Driver : Char Device

Book: Essential Linux Device Drivers

以下皆出自書中截錄,若違反版權請速告知,將盡速刪除。

About init() function : init之中可能需要的步驟
1.
API : alloc_chrdev_region
Desp : Request dynamic allocation of a device major number
此法為動態分配一個主編號,另有register_chrdev()為靜態的方式,編號為使用者傳入,但也需要遵循裝置的所屬做配置。

第一個步驟做完之後可以透過以下command來檢視產生出來的裝置

cat /proc/devices | grep 裝置名稱

在fops中的owner欄位,通常設定為THIS_MODULE,用來表示正在撰寫的驅動程式模組的位址


kernel常使用cdev來表示字元裝置,但是我們很常看到許多驅動程式會將cdev包含於個別裝置的資料結構中。也就是將cdev包裝在更大的結構內。

2.
API : class_create
Desp : Populate sysfs entries, kernel所提供的函式,用來建立一個類別,這個類別存在於/sys下,也就是為此裝置建構sysfs的進入點。
3.
kmalloc 分配記憶體給device's structure
4.
API : request_region
Desp : Request I/O region
5.
API : cdev_init
Desp : connect the file operations with the cdev
6.
API : cdev_add
Desp : Connect the major/minor number to the cdev
7.
API : class_device_create
Desp : Send uevents to udev, so it'll create /dev nodes

從code中看到device_create似乎取代了7.這個API
此API會產生uevent,由udevd來監聽並做處理而建立裝置節點,udev的規則目錄(/etc/udev/rules.d),如此在inser module的時候,user space中的udev會根據規則目錄去/sysfs下尋找對應的class而建立device node。

在使用device_create這個函數之前要include /include/linux/device.h

留言

熱門文章