linux usb 驅動詳解
|
|
|
|
建立一個簡單的 struct usb_driver 結構, 只有 4 個成員需要初始化:
|
|
探測和斷開的細節
在 struct usb_driver 結構中, 有 2 個 USB 核心在適當的時候呼叫的函式:
(1)當裝置安裝時, 如果 USB 核心認為這個驅動可以處理,則呼叫探測(probe)函式,探測函式檢查傳遞給它的裝置資訊, 並判斷驅動是否真正合適這個裝置.
(2)由於某些原因,裝置被移除或驅動不再控制裝置時,呼叫斷開(disconnect)函式,做適當清理.
探測和斷開回調函式都在 USB 集線器核心執行緒上下文中被呼叫, 因此它們休眠是合法的. 為了縮短 USB 探測時間,大部分工作儘可能在裝置開啟時完成.這是因為 USB 核心是在一個執行緒中處理 USB 裝置的新增和移除, 因此任何慢裝置驅動都可能使 USB 裝置探測時間變長。
探測函式分析 在探測回撥函式中, USB 驅動應當初始化它可能用來管理 USB 裝置的所有本地結構並儲存所有需要的裝置資訊到本地結構, 因為在此時做這些通常更容易.為了和裝置通訊,USB 驅動通常要探測裝置的端點地址和緩衝大小. 以下是usb-skeleton的probe函式中的探測程式碼:
|