勇闖新世界︰ 《 Kernel 4.X 》之整裝蓄勢‧事件驅動‧二

說到作業系統中的『中斷處理』 與『事件驅動』程序,最具代表性的就是『裝置驅動』程式。維基百科詞條說︰

Device driver

In computing, a device driver (commonly referred to as a driver) is a computer program that operates or controls a particular type of device that is attached to a computer.[1] A driver provides a software interface to hardware devices, enabling operating systems and other computer programs to access hardware functions without needing to know precise details of the hardware being used.

A driver typically communicates with the device through the computer bus or communications subsystem to which the hardware connects. When a calling program invokes a routine in the driver, the driver issues commands to the device. Once the device sends data back to the driver, the driver may invoke routines in the original calling program. Drivers are hardware-dependent and operating-system-specific. They usually provide the interrupt handling required for any necessary asynchronous time-dependent hardware interface.[2]

若講到系統整體啟動程序,就像《大樹底下好乘涼 ︰ 《歷史觀》》所講,過去

每一台 PC 主機板上都有一個『BIOS 』 Basic Input Output System 掌管開機動作。這個『第一啟動』韌體一直隱藏在『保密合約』 NDA  non-disclosure agreement 幕後。曾經 Linux Bios 計畫努力於打開黑箱,直到今天稱之為 coreboot 。在那個應許的年代,已走入歷史的『 SUN Microsystems 』之『 Open Bios 』計畫裡的『 open firmware 』架構,就是『設備樹』的原點︰

IEEE Std 1275-1994
IEEE Standard for Boot (Initialization Configuration) Firmware: Core Requirements and Practices
……
3.2 Device tree
The device tree is a hierarchical data structure that describes the system hardware, describes user configuration choices, contains firmware device drivers for hardware devices, and contains support routines for use by those drivers.

The device tree’s structure mimics the organization of the system hardware, viewed as a hierarchy of interconnected buses and their attached devices. The device tree consists of a set of device nodes that are interconnected to form a tree. An individual device node
represents either a hardware bus, a hardware device, or a set of interrelated software procedures. ……

二零零八年起 Power.org^{TM} Standard for Embedded Power Architecture^{TM} Platform Requirements (ePAPR) 開始推廣 open firmware 架構︰
……
1.2 Relationship to IEEETM 1275

The ePAPR is loosely related to the IEEE 1275 Open Firmware standard—IEEE Standard for Boot (Initialization Configuration) Firmware: Core Requirements and Practices [2].
The original IEEE 1275 specification and its derivatives such as CHRP [10] and PAPR [16] address problems of general purpose computers, such as how a single version of an operating system can work on several different computers within the same family and the problem of loading an operating system from user-installed I/O devices. ……

當『裝置樹』遇上『作業系統』後,對『裝置驅動程式』的寫作帶來了改變及好處。為什麼呢?舉例來講,一個 USB 裝置的驅動程式載入,通常依賴『USB Vendor ID』。這是因為許多產品也許使用的主要晶片相同,但是 I/O 選擇地址、LED 有幾個、附加功能 …… 不同。於是裝置的『驅動程式』大同小異,為了『管理』上的方便!於是傳統上就得靠著那個 USB ID 編號來作區分。只不過常常有的廠商更新了產品,卻未必申請新的 USB ID ,因此發生裝置驅動程式新舊衝突的情況。反倒是搞不清楚到底誰對誰錯的哩??隨著樹莓派可用的 GPIO 裝置越來越多,基金會或許正是及早防範未然,未雨綢繆的耶!!

 

由此可知未來如何閱讀和寫作『裝置樹』文檔,將會是一個實務上要緊的『課題』,所以特別在繼續探究『事件驅動』程式點滴之前 ,先列出相關重點文本摘要,方便讀者參考︰

【裝置樹工具程式安裝】

大樹底下好乘涼 ︰ 《大哉問》──

一顆 Broadcom BCM2835 SoC 可以用來設計樹莓派 B, A, B+, A+ 與 Computer Module 等主版,那麼它們之間的軟硬體差異,從 kernel OS 的角度來看,差異到底有多大呢?假使每種不同的板子,都需要特定的 kernel ,不但非常麻煩,該說是不智之舉的吧!再說因於 GPIO 上的 SPI, I2C, I2S 等等介面所產生的各類擴充板,難到不能有更好的規劃,使得驅動程式的安裝、設定和管理更加簡易的嗎??如此總總實務上的考量,促使著應用『 Device Tree 』、『 Overlay 』與『 Parameter 』來描述『系統硬體』,達成化繁為簡的目的。這個『樹狀結構』的『硬體裝置描述』 .dts 方式類似於 C 語言的 struct 資料結構,也有 C 語言相仿的 /include/ 子樹 .dtsi 引用,再加上部份『 Fragment 』 零碎資料構成的『 Overlay 』 .dts 組成了整體硬體的『設備樹』。經由 dtc 編譯程式編成二進制的『 device tree blob 』 dtb ,系統啟動時由 start.elf 程式傳給 kernel

在此讓我們先安裝那個 dtc 編譯工具︰

# 不支援 overlay
pi@raspberrypi ~ apt-cache search device-tree device-tree-compiler - Device Tree Compiler for Flat Device Trees  # 安裝新版 dtc 及 fdt** 工具 wget -c https://raw.githubusercontent.com/RobertCNelson/tools/master/pkgs/dtc.sh chmod +x dtc.sh ./dtc.sh  # 版本檢查 pi@raspberrypi ~ dtc -v
Version: DTC 1.4.0-gf345d9e4

───

 

【裝置樹工具程式說明】

大樹底下好乘涼 ︰ 《拜樹頭》 ? ※ 碩果不食──

假使仔細閱讀《 Device Tree on ARCH_BCM2708 》,留心一下『那個人』── notro ── 與『那個時間』── on Jul 30, 2014 ──,也許我們不只對樹莓派『裝置樹』有更多的了解,更能明白許多的貢獻其來有自,正所謂『德不孤,必有鄰』乎!!

此處將僅簡單說說幾個工具程式,以及簡介它們的用法。

常用縮寫︰

DT 】︰  Device Tree
FDT 】︰ Flattended Device Tree: a DT representation used in the dtb blob.
.dtb 】︰ Device Tree Binary
.dts 】︰ Device Tree Source
.dtsi 】︰ Device Tree Source Include
dtc 】︰ Device Tree Compiler

───

 

【核心設備樹支援】

大樹底下好乘涼 ︰ 《拜樹頭》 ? ※成而不居──

在一篇名為《 Device trees everywhere 》的文章裡,談及了二零零五年五月時,  IBM 的 Benjamin Herrenschmidt 建置了一套新的『設備樹』之 linux kernel 啟動機制,即使一個沒有『 OF 』 Open Firmware 的系統,也可以使用『設備樹』得到好處。此事記載於 Linux 《 booting-without-of.txt 》 文件之中,那時 ── May 18, 2005: Rev 0.1 – Initial draft, no chapter III yet. ── ,到了

November 21, 2005: Rev 0.5

– Additions/generalizations for 32-bit
– Changed to reflect the new arch/powerpc structure
– Added chapter VI

此刻的差異實在說並不大。『術語』與『架構』延襲至今,正是深入理解『 /proc/device-tree 』、『 The DT block format 』、『 Required content of the device tree 』以及一些『建議』的基礎『文本』。俗話講︰『十年磨一劍』,正此之謂耶??

雖說『設備樹』是種系統連接『硬體裝置』的簡易『描述語言』 ,但要三言兩語講清楚它與『第一啟動』 BIOS 韌體或是『 Boot Loader 』與『 kernel os 』之裝置『驅動程式』間的關係,其實很不容易。因此這裡也只能借花獻佛,請讀者自讀《 Device Tree for DUMMIES 》的了。

□︰ phandle 是什麼阿?
○︰ p + Handle 的吧!
□︰能說文解字嗎?
○︰ Handle 把柄, p (ointer) 乃指標是也!
□︰有些八卦呢??
○︰音譯成『ㄈㄟㄢ ㄉㄡ』才八卦哩!!

vray-bsp-tree-acceleration-structure

象棋‧仙人指路

既然『設備樹』 與『檔案樹』都是『樹狀結構』,系統開機後用『 /proc/device-tree 』來表現啟始 dtb 之結果,正是一一對應︰

設備根節點 Device Root  \Longleftrightarrow /proc/device-tree 目錄

設備節點 Device Node  \Longleftrightarrow 子目錄

設備屬性 Device Property  \Longleftrightarrow 檔案

───