Linux USB驅動詳解
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
原文地址:http://blog.csdn.net/chenjin_zhong/article/details/6329316
1.Linux usb裝置驅動框架
USB是通用序列匯流排的總稱,Linux核心幾乎支援所有的usb裝置,包括鍵盤,滑鼠,印表機,modem,掃描器。Linux的usb驅動分為主機驅動與gadget驅動。前者是裝置連線到計算機上,通過主機驅動掃描usb裝置,控制所連線的裝置。而gadget驅動一般用於嵌入式裝置,gadget驅動用於控制嵌入式裝置。Linux的usb驅動兩種型別圖如下:
左側是usb的主機驅動,右側是gadget驅動。下面著重介紹一下usb的主機驅動:
(1)usb主機控制器-直接與硬體裝置互動。
(2)usb core-向usb裝置驅動提供API以及usb主機控制器驅動的程式。使用usb core所提供的函式,巨集來完成資料處理的功能。
(3)usb裝置驅動,即usb介面驅動,一般所說的usb驅動指的是usb介面驅動
2.usb系統的組成部分
usb系統一般由三個部分組成,主機,一個或多個usb hub,以及與之些hub連線的usb裝置。
(1)主機
在任何的usb系統中僅有一個主機,主機系統中的usb介面即上圖中的主機控制器,主機控制器可由硬體,軟體或韌體組成。主機主要負責:
a.檢測usb裝置的連線與拆除
b.管理主機與usb裝置之間的控制流
c.管理主機與usb裝置之間的資料流
d.收集狀態和活動的統計
e.為連線的usb裝置提供電源
(2)usb裝置
所有的usb裝置都是通過地址來存取的,這個地址在連線或列舉時分配。usb裝置對usb系統來說是端點的集合,一組端點實現一個介面。裝置端點是usb裝置中唯一可定址的部分。它是主機與裝置之間通訊流的結束點。一系列的相互獨立的端點構成了usb邏輯裝置。每個端點支援流進裝置或者是流出裝置。
主機與裝置端點上的usb資料傳輸是通過管道的方式。
(3)hub
所有的usb device都連線在hub埠上。
3. usb傳輸模式
(1)控制傳輸模式(Control)
控制傳輸模式支援雙向傳輸,用來處理從usb主機埠到usb裝置埠的資料傳輸,用於控制指令,裝置狀態查詢以及確認命令。
(2)等時傳輸方式(lsochronous)
等時傳輸是一種週期性的連續性的意向傳輸模式,通常用於對時間有著密切關係的資訊的傳輸,對準確性要求不高,但對時間要求極為敏感的裝置,如視訊,音訊的傳輸。
(3)中斷傳輸模式(Interrupt)
中斷傳輸模式用於非週期性的,自然發生的,資料量小的傳輸,資料傳輸的方向是從裝置到主機。如usb鍵盤和滑鼠
(4)批量傳輸模式(bulk)
批量傳輸模式是一種單向的,用於大量資料傳輸的模式,該方式用來傳輸正確無誤的資料。通常印表機,掃描器,數碼相機以這種方式與主機連線
4. usb裝置組成
(1)一個usb裝置由可以有一個或多個配置
(2)一個配置通常可以有一個或多個介面
(3)一個介面通常可以有一個或多個端點
通常所盡的usb裝置驅動是指介面驅動,即一個介面對應一個驅動。
所以Linux usb裝置有四大描述符,分別為裝置描述符,配置描述符,介面描述符,端點描述符。下面看一個這幾個描述符的相關資料結構:
struct usb_device_descriptor
{
_u8 bLength; //此描述符的位元組數
_u8 bDescriptorType; //描述符的種類為裝置
_u16 bcdUSB; //此裝置與描述符相容的usb裝置說明版本號(BCD碼)
_u8 bDeviceClass; //裝置類碼
_u8 bDeviceSubClass; //裝置子類碼
_u8 bDeviceProtocol; //協議碼
_u8 bMaxPacketSize0; //端點0的最大包大小
_u16 idVendor; //廠商標誌
_u16 idProduct; //產品標誌
_u16 bcdDevice; //裝置發行號
_u8 iManufacturer; //描述廠商的字串索引
_u8 iProduct; //描述產品資訊的字串索引
_u8 iSerialNumber; //描述裝置序列號資訊的字串索引
_u8 bNumConfigurations;//此裝置支援的配置數
}_attribute_ ((packed));
裝置類碼的典型值如下:
#define USB_CLASS_PER_INTERFACE 0
#define USB_CLAS_AUDIO 1 //聲音裝置
#define USB_CLASS_COMM 2 // 調變解調器,網絡卡,ISDN連線
#define USB_CLASS_HID 3 //HID裝置,如滑鼠,鍵盤
#define USB_CLASS_PHYSICAL 5 //物理裝置
#define USB_CLASS_STILL_IMAGE 6 //靜止影象捕捉裝置
#define USB_CLASS_PRINTER 7//印表機
#define USB_CLASS_MASS_STORAGE //8 批量儲存裝置
#define USB_CLASS_HUB 9 //USB HUBS
#define USB_CLASS_CSCID 0x0B //智慧卡
#define USB_CLASS_VIDEO 0X0E //視訊裝置,如網路攝像頭
#define USB_CLASS_VENDOR_SPEC 0xFF //廠商自定義的裝置
struct usb_config_descriptor{
_u8 bLength ;//此描述符的位元組數
_u8 bDescriptorType; //配置描述符型別
_u16 wTotalLength; //此配置資訊的總長(包括配置,介面,端點和裝置型別及廠商定義的描述符)
_u8 bNumInterfaces; //此配置所支援的介面數
_u8 bConfigurationValue ;//在setConfiguration()請求中用作引數來選定此配置
_u8 iConfiguration; //描述此配置的字串描述符索引
_u8 bmAttributes; //電源配置特性
_u8 bMaxpowe;r //此配置下的匯流排電源耗電量
}_attribute_ ((packed));
配置描述符給出了usb裝置配置資訊,以及此配置下的介面數。每個介面可能的獨立操作。
struct usb_interface_descriptor{
_u8 bLength ;//此描述符的位元組數
_u8 bDescriptorType;//介面描述符類
_u8 bInterfacNumber;//介面號,當前配置所支援的介面陣列索引,從0開始
_u8 bNumEndpoints ;//此介面用的端點數量,如果是0,說明此介面只有預設控制通道
_u8 bAlernateSetting;//可選裝置的索引值
_u8 bInterfaceClass;// 類值,0值作為將來保留使用如果是0FFH,此介面由廠商說明
_u8 bInterfaceSubClass;//子類碼
_u8 bInterfaceProtocol;//協議碼
_u8 iInterface;//描述此介面的字串描述符索引
}_attribute_ ((packed));
struct usb_endpoint_descriptor{
_u8 bLength ;//此描述符的位元組數
_u8 bDescriptorType;//端點描述符類
_u8 bEndpointAddress;此描述符所描述的端點的地址
_u8 bmAtrributes;//所指定的端點的特性,如果是00=控制傳送,01=等時傳送,10=批傳送,11=中斷傳送
_u8 wMaxPacketSize;//當前配置下端點能夠傳送與接收的最大資料包大小
_u8 bInterval;//輪詢資料傳送端點的時間間隙
_u8 bRefresh
_u8 bSynchAddress
}_attribute_ ((packed));
以上給出了usb中的裝置描述符,配置描述符,介面描述符和端點描述符。
5. usb裝置驅動的幾個重要的資料結構
usb_driver,usb_device,usb_bus.
/**
788* stru ctusb_driver - identifiesU SB interface driver tou sbcore
789* @name: The driver name shou ld beu niqu e amongU SB drivers,
790* and shou ld normally be the same as the modu le name.
791* @probe: Called to see if the driver is willing to manage a particu lar
792* interface on a device. If it is, probe retu rns zero andu ses
793* u sb_set_intfdata() to associate driver-specific data with the
794* interface. It may alsou seu sb_set_interface() to specify the
795* appropriate altsetting. Ifu nwilling to manage the interface,
796* retu rn -ENODEV, if genu ine IO errors occu red, an appropriate
797* negative errno valu e.
798* @disconnect: Called when the interface is no longer accessible,u su ally
799* becau se its device has been (or is being) disconnected or the
800* driver modu le is beingu nloaded.
801* @u nlocked_ioctl:U sed for drivers that want to talk tou serspace throu gh
802* the "u sbfs" filesystem. This lets devices provide ways to
803* expose information tou ser space regardless of where they
804* do (or don't) showu p otherwise in the filesystem.
805* @su spend: Called when the device is going to be su spended by the system.
806* @resu me: Called when the device is being resu med by the system.
807* @reset_resu me: Called when the su spended device has been reset instead
808* of being resu med.
809* @pre_reset: Called byu sb_reset_device() when the device
810* is abou t to be reset.
811* @post_reset: Called byu sb_reset_device() after the device
812* has been reset
813* @id_table:U SB driversu se ID table to su pport hotplu gging.
814* Export this with MODU LE_DEVICE_TABLE(u sb,...). This mu st be set
815* or you r driver's probe fu nction will never get called.
816* @dynids:u sed internally to hold the list of dynamically added device
817* ids for this driver.
818* @drvwrap: Driver-model core stru ctu re wrapper.
819* @no_dynamic_id: if set to 1, theU SB core will not allow dynamic ids to be
820* added to this driver by preventing the sysfs file from being created.
821* @su pports_au tosu spend: if set to 0, theU SB core will not allow au tosu spend
822* for interfaces bou nd to this driver.
823* @soft_u nbind: if set to 1, theU SB core will not killU RBs and disable
824* endpoints before calling the driver's disconnect method.
825*
826*U SB interface drivers mu st provide a name, probe() and disconnect()
827* methods, and an id_table. Other driver fields are optional.
828*
829* The id_table isu sed in hotplu gging. It holds a set of descriptors,
830* and specialized data may be associated with each entry. That table
831* isu sed by bothu ser and kernel mode hotplu gging su pport.
832*
833* The probe() and disconnect() methods are called in a context where
834* they can sleep, bu t they shou ld avoid abu sing the privilege. Most
835* work to connect to a device shou ld be done when the device is opened,
836* andu ndone at the last close. The disconnect code needs to address
837* concu rrency issu es with respect to open() and close() methods, as
838* well as forcing all pending I/O requ ests to complete (byu nlinking
839* them as necessary, and blockingu ntil theu nlinks complete).
840*/
841stru ctusb_driver{
842 const char *name;
843
844 int (*probe) (stru ct u sb_interface*intf,
845 const stru ctu sb_device_id*id);
846
847 void (*disconnect) (stru ct u sb_interface*intf);
848
849 int (*u nlocked_ioctl) (stru ctu sb_interface*intf,u nsigned intcode,
850 void *bu f);
851
852 int (*su spend) (stru ct u sb_interface *intf,pm_message_tmessage);
853 int (*resu me) (stru ct u sb_interface *intf);
854 int (*reset_resu me)(stru ctu sb_interface*intf);
855
856 int (*pre_reset)(stru ct u sb_interface*intf);
857 int (*post_reset)(stru ctu sb_interface*intf);
858
859 const stru ctu sb_device_id*id_table;
860
861 stru ctu sb_dynidsdynids;
862 stru ctu sbdrv_wrapdrvwrap;
863 u nsigned int no_dynamic_id:1;
864 u nsigned int su pports_au tosu spend:1;
865 u nsigned int soft_u nbind:1;
866};
usb_driver中的probe函式掃描連線到主機上的usb裝置,並且註冊usb介面驅動。
disconnect函式是當usb裝置移除時呼叫。
/*
310* Allocated per bu s (tree of devices) we have:
311*/
312stru ctu sb_bu s {
313 stru ctdevice *controller ; /* host/master side hardware */
314 int bu snu m; /* Bu s nu mber (in order of reg) */
315 const char *bu s_name; /* stable id (PCI slot_name etc) */
316 u 8u ses_dma; /* Does the host controlleru se DMA? */
317 u 8u ses_pio_for_control; /*
318 * Does the host controlleru se PIO
319 * for control transfers?
320 */
321 u 8 otg_port; /* 0, or nu mber of OTG/HNP port */
322 u nsigned is_b_host:1; /* tru e du ring some HNP roleswitches */
323 u nsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
324 u nsignedsg_tablesize ; /* 0 or largest nu mber of sg list entries */
325
326 int devnu m_next; /* Next open device nu mber in
327 * rou nd-robin allocation */
328
329 stru ctu sb_devmap devmap; /* device address allocation map */
330 stru ctusb_device * root_hu b; /* Root hu b */
331 stru ctu sb_bu s *hs_companion; /* Companion EHCI bu s, if any */
332 stru ctlist_head bu s_list; /* list of bu sses */
333
334 int bandwidth_allocated; /* on this bu s: how mu ch of the time
335 * reserved for periodic (intr/iso)
336 * requ ests isu sed, on average?
337 *U nits: microseconds/frame.
338 * Limits: Fu ll/low speed reserve 90%,
339 * while high speed reserves 80%.
340 */
341 int bandwidth_int_reqs; /* nu mber of Interru pt requ ests */
342 int bandwidth_isoc_reqs; /* nu mber of Isoc. requ ests */
343
344#ifdef CONFIG_USB_DEVICE FS
345 stru ctdentry *u sbfs_dentry; /*u sbfs dentry entry for the bu s */
346#endif
347
348#ifdefined (CONFIG_U SB_MON) ||defined(CONFIG_U SB_MON_MODU LE)
349 stru ctmon_bu s *mon_bu s ; /* non-nu ll when associated */
350 int monitored; /* non-zero when monitored */
351#endif
352};
353
**
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
驅動程式把驅動物件註冊到 USB 子系統中,之後使用供應商(idVendor)和裝置(idProduct)標識來判斷對應的硬體是否已經安裝.
驅動的裝置支援列表
struct usb_device_id 結構提供了這個驅動支援的不同型別 USB 裝置的列表. USB 核心通過此列表
linux 核心中的
USB 程式碼和所有的 USB 裝置通訊使用稱為 urb 的東西( USB request block). 這個請求塊用 struct urb 結構描述並且可在 include/linux/usb.h 中找到.
一個urb 用來發送或 裏的 end opacity 塊設備 dea 獲取request device geometry cat 通過上節的塊設備驅動分析,本節便通過內存來模擬塊設備驅動
參考內核自帶的塊設備驅動程序:
drivers/block /xd.c
drivers/bl
《5.linux驅動開發-第7部分-5.7.framebuffer驅動詳解》
第一部分、章節目錄 5.7.1.framebuffer介紹 5.7.2.framebuffer應用程式設計實踐1 5.7.3.framebuffer應用程式設計實踐2 5.7.4.framebuffer應用程式
由上一節的輸入子系統的框架分析可知,其分三層:裝置驅動層,核心層,事件驅動層
我們在為某種裝置的編寫驅動層,只需要關心裝置驅動層,即如何驅動裝置並獲得硬體資料(如按下的按鍵資料),然後呼叫核心層提供的介面,核心層就會自動把資料提交給事件處理層。在輸入子系統中,事件驅動是標準的,適用於所有輸入類的。
NVMe離不開PCIe,NVMe SSD是PCIe的endpoint。PCIe是x86平臺上一種流行的bus匯流排,由於其Plug and Play的特性,目前很多外設都通過PCI Bus與Host通訊,甚至不少CPU的整合外設都通過PCI Bus連線,如APIC等。 NV
為了實現串列埠通訊,需要在嵌入式linux下編寫相應的驅動程式。在嵌入式系統中,串列埠被看做終端裝置tty。終端裝置是unix體系中一個非常重要的物件,內容非常複雜,它是整個unix人機互動的基礎,其地位並不亞於檔案系統在作業系統中的作用。筆者muge0913在此對uar 請求佇列跟蹤等候的塊I/O請求,它儲存用於描述這個裝置能夠支援的請求的型別資訊、它們的最大大小、多少不同的段可進入一個請求、硬體扇區大小、對齊要求等引數,其結果是:如果請求佇列被配置正確了,它不會交給該裝置一個不能處理的請求。
請求佇列還實現一個插入介面,這個介面允許使用多個I/O排程器,I/
打算跟著友善之臂的《mini2440 linux移植開發指南》來做個LED驅動,雖然LED的原理簡單得不能再簡單了,但是要把kernel中針對於s3c24**的GPIO的一些資料結構,還有函式搞清楚也不是那麼輕鬆的事,所以本文主要簡單地說明下LED驅動中的相關資料結構以及 更多嵌入式Linux原創,請關注公眾號:一口Linux
# 一、模組硬體學習
## 1.1. Uart介紹
通用非同步收發傳輸器(Universal Asynchronous Receiver/Transmitter),通常稱為UART,是一種非同步收發傳輸器,是電腦硬體的一部分。它將要傳輸的資料在序列通訊 linux 詳解 啟動流程 grub mbr 內核 linux啟動流程第一部分 Linux啟動基礎知識1.1 linux centos6.8啟動流程圖 BIOS加電自檢à加載MBRà加載啟動grubà加載內核à啟動/sbin/i bytes tool head osi ngs 進行 pen 通信 詳細信息 簡介
Netstat 命令用於顯示各種網絡相關信息,如網絡連接,路由表,接口狀態 (Interface Statistics),masquerade 連接,多播成員 (Multicast Mem ctrl+ 一次 所有 使用方法 ase 隱藏 統計 ini 前臺 top命令是Linux下常用的性能分析工具,能夠實時顯示系統中各個進程的資源占用狀況,類似於Windows的任務管理器。下面詳細介紹它的使用方法。top - 01:06:48 up 1:22, 1 user column 環境變量 最後一行 工作流程 初始 文本文件 for循環 其中 cti
簡介
awk是一個強大的文本分析工具,相對於grep的查找,sed的編輯,awk在其對數據分析並生成報告時,顯得尤為強大。簡單來說awk就是把文件逐行的讀入,以空格為默認分隔符將每行切 -c 目錄 輸出 限制 普通 排序 當前 ls -l sna ls 命令可以說是Linux下最常用的命令之一。
-a 列出目錄下的所有文件,包括以 . 開頭的隱含文件。(後有詳解)-b 把文件名中不可輸出的字符用反斜杠加字符編號(就象在c語言裏一樣)的形式列出。-c 輸出 open 日誌 文件和目錄 delete 數據報 正在 某個文件 alt targe 簡介
lsof(list open files)是一個列出當前系統打開文件的工具。在linux環境下,任何事物都以文件的形式存在,通過文件不僅僅可以訪問常規數據,還可以訪問網絡連接和硬件 基本 linux中 file tail命令 方式 sed 缺省 顯示 有效 linux ---tail命令 linux中tail命令---用於查看文件內容 最基本的是cat、more和less。 1. 如果你只想看文件的前5行,可以使用head命令,如: head -5 / 大於 linux下 blog 根據 影響 stack 復制代碼 系統資源 代碼 線程
是計算機中獨立運行的最小單位,運行時占用很少的系統資源。可以把線程看成是操作系統分配CPU時間的基本單元。一個進程可以擁有一個至多個線程。它線程在進程內部共享地址空間、打開的文件描述符等資 文本 常用 小時 遠程終端 參考 編輯 load 調整 長時間 一、背景
系統管理員經常需要SSH 或者telent 遠程登錄到Linux 服務器,經常運行一些需要很長時間才能完成的任務,比如系統備份、ftp 傳輸等等。通常情況下我們都是為每一個這樣的任務開一個遠程終端窗口
370* stru ctusb_device - kernel's representation of aU SB device
371* @devnu m: device nu mber; address on aU SB bu s
372* @devpath: device ID string foru se in messages (e.g., /port/...)
373* @rou te: tree topology hex string foru se with xHCI
374* @state: device state: configu red, not attached, etc.
375* @speed: device speed: high/fu ll/low (or error)
376* @tt: Transaction Translator info;u sed with low/fu ll speed dev, highspeed hu b
377* @ttport: device port on that tt hu b
378* @toggle: one bit for each endpoint, with ([0] = IN, [1] = OU T) endpoints
379* @parent: ou r hu b,u nless we're the root
380* @bu s: bu s we're part of
381* @ep0: endpoint 0 data (defau lt control pipe)
382* @dev: generic device interface
383* @descriptor:U SB device descriptor
384* @config: all of the device's configs
385* @actconfig: the active configu ration
386* @ep_in: array of IN endpoints
387* @ep_ou t: array of OU T endpoints
388* @rawdescriptors: raw descriptors for each config
389* @bu s_mA: Cu rrent available from the bu s
390* @portnu m: parent port nu mber (origin 1)
391* @level: nu mber ofU SB hu b ancestors
392* @can_su bmit:U RBs may be su bmitted
393* @persist_enabled: U SB_PERSIST enabled for this device
394* @have_langid: whether string_langid is valid
395* @au thorized: policy has said we canu se it;
396* (u ser space) policy determines if we au thorize this device to be
397* u sed or not. By defau lt, wiredU SB devices are au thorized.
398* WU SB devices are not,u ntil we au thorize them fromu ser space.
399* FIXME -- complete doc
400* @au thenticated: Crypto au thentication passed
401* @wu sb: device is WirelessU SB
402* @string_langid: langu age ID for strings
403* @produ ct: iProdu ct string, if present (static)
404* @manu factu rer: iManu factu rer string, if present (static)
405* @serial: iSerialNu mber string, if present (static)
406* @filelist:u sbfs files that are open to this device
407* @u sb_classdev:U SB class device that was created foru sbfs device
408* access fromu serspace
409* @u sbfs_dentry:u sbfs dentry entry for the device
410* @maxchild: nu mber of ports if hu b
411* @children: child devices -U SB devices that are attached to this hu b
412* @qu irks: qu irks of the whole device
413* @u rbnu m: nu mber ofU RBs su bmitted for the whole device
414* @active_du ration: total time device is not su spended
415* @connect_time: time device was first connected
416* @do_remote_wakeu p: remote wakeu p shou ld be enabled
417* @reset_resu me: needs reset instead of resu me
418* @wu sb_dev: if this is a WirelessU SB device, link to the WU SB
419* specific data for the device.
420* @slot_id: Slot ID assigned by xHCI
421*
422* Notes:
423*U sbcore drivers shou ld not setu sbdev->state directly. Insteadu se
424*u sb_set_device_state().
425*/
426stru ctusb_device{
427 int devnu m;
428 char devpath[16];
429 u 32 rou te;
430 enu musb_device _state state;
431 enu musb_device _speed speed;
432
433 stru ctu sb_tt *tt;
434 int ttport;
435
436 u nsigned inttoggle[2];
437
438 stru ctusb_device*parent;
439 stru ctu sb_bu s*bu s;
440 stru ctu sb_host_endpointep0;
441
442 stru ctdevicedev;
443
444 stru ctusb_device _descriptordescriptor;
445 stru ctu sb_host_config*config;
446
447 stru ctu sb_host_config*actconfig;
448 stru ctu sb_host_endpoint*ep_in[16];
449 stru ctu sb_host_endpoint*ep_ou t[16];
450
相關推薦
Linux USB驅動詳解
linux usb 驅動詳解
linux usb驅動中的urb詳解
23.Linux-塊設備驅動(詳解)
《5.linux驅動開發-第7部分-5.7.framebuffer驅動詳解》
輸入子系統------鍵盤按鍵驅動程式 13.Linux鍵盤按鍵驅動 (詳解)
Linux中nvme驅動詳解
6410 實現 linux 串列埠驅動詳解
Linux塊裝置驅動詳解(一)
很好的linux下GPIO驅動詳解文章
基於Linux的tty架構及UART驅動詳解
Linux啟動流程詳解
Linux netstat命令詳解,高級面試必備
linux top 命令詳解
【轉】linux awk命令詳解
Linux ls命令詳解
linux lsof命令詳解
linux tail 命令詳解
linux 線程詳解
linux screen 命令詳解