1. 程式人生 > >Linux USB 驅動開發(二)—— USB 驅動幾個重要資料結構

Linux USB 驅動開發(二)—— USB 驅動幾個重要資料結構

Linux USB 驅動開發(二)—— USB 驅動幾個重要資料結構

 

       前面我們學習了USB 驅動的一個描述符,下面來學習 USB 驅動的幾個重要資料結構

 

一、struct usb_interface  介面函式

 

 
  1. struct usb_interface

  2. {

  3.  
  4. struct usb_host_interface *altsetting;

  5.  
  6. struct usb_host_interface *cur_altsetting;

  7. unsigned num_altsetting;

  8.  
  9. int minor;

  10. enum usb_interface_condition condition;

  11. unsigned is_active:1;

  12. unsigned needs_remote_wakeup:1;

  13.  
  14. struct device dev;

  15. struct device *usb_dev;

  16. int pm_usage_cnt;

  17. };

 

    struct usb_interface中的struct usb_host_interface *cur_altsetting成員,表示當前正在使用的設定

1、struct usb_host_interface 

 

 
  1. struct usb_host_interface

  2. {

  3. struct usb_interface_descriptor desc;//usb描述符,主要有四種usb描述符,裝置描述符,配置描述符,介面描述符和端點描述符,協議裡規定一個usb裝置是必須支援這四大描述符的。

  4. //usb描述符放在usb裝置的eeprom裡邊

  5. /* array of desc.bNumEndpoint endpoints associated with this

  6. * interface setting. these will be in no particular order.

  7. */

  8. struct usb_host_endpoint *endpoint;//這個設定所使用的端點

  9.  
  10. char *string; /* iInterface string, if present */

  11. unsigned char *extra; /* Extra descriptors */關於額外描述符

  12. int extralen;

  13. };

       具體到介面描述符,它當然就是描述介面本身的資訊的。一個介面可以有多個設定,使用不同的設定,描述介面的資訊會有些不同,所以介面描述符並沒有放在struct usb_interface結構裡,而是放在表示介面設定的struct usb_host_interface結構裡。

 

二、struct usb_host_endpoint  端點函式

 

 
  1. struct usb_host_endpoint

  2. {

  3. struct usb_endpoint_descriptor desc;

  4. struct list_head urb_list;//端點要處理的urb佇列.urb是usb通訊的主角,裝置中的每個端點都可以處理一個urb佇列.要想和你的usb通訊,就得建立一個urb,並且為它賦好值,

  5. //交給咱們的usb core,它會找到合適的host controller,從而進行具體的資料傳輸

  6. void *hcpriv;//這是提供給HCD(host controller driver)用的

  7. struct ep_device *ep_dev; /* For sysfs info */

  8.  
  9. unsigned char *extra; /* Extra descriptors */

  10. int extralen;

  11. };

 

三、struct usb_device 裝置函式

 

 
  1. struct usb_device {

  2. int devnum; //devnum只是usb裝置在一條usb總線上的編號.一條usb_bus_type型別的總線上最多可以連上128個裝置

  3. char devpath [16]; /* Use in messages: /port/port/...*/ //對於root hub.會將dev->devpath[0]=’0’

  4. enum usb_device_state state; //裝置的狀態Attached,Powered,Default,Address,Configured,Suspended;

  5. //Attached表示裝置已經連線到usb介面上了,是hub檢測到裝置時的初始狀態。那麼這裡所謂的USB_STATE_NOTATTACHED就是表示裝置並沒有Attached。

  6. //Address狀態表示主機分配了一個唯一的地址給裝置,此時裝置可以使用預設管道響應主機的請求

  7. //Configured狀態表示裝置已經被主機配置過了,也就是協議裡說的處理了一個帶有非0值的SetConfiguration()請求,此時主機可以使用裝置提供的所有功能

  8. //Suspended掛起狀態,為了省電,裝置在指定的時間內,3ms吧,如果沒有發生匯流排傳輸,就要進入掛起狀態。此時,usb裝置要自己維護包括地址、配置在內的資訊

  9.  
  10. enum usb_device_speed speed; /* high/full/low (or error) */

  11.  
  12. struct usb_tt *tt; //如果一個高速裝置裡有這麼一個TT,那麼就可以連線低速/全速裝置,如不然,那低速/全速裝置沒法用,只能連線到OHCI/UHCI那邊出來的hub口裡。

  13. int ttport; //如果一個高速裝置裡有這麼一個TT,那麼就可以連線低速/全速裝置,如不然,那低速/全速裝置沒法用,只能連線到OHCI/UHCI那邊出來的hub口裡。

  14.  
  15. unsigned int toggle[2]; /* one bit for each endpoint //他實際上就是一個位圖.IN方向的是toggle[0].OUT方向的是toggle[1].其實,這個陣列中的每一位表示ep的toggle值

  16. * ([0] = IN, [1] = OUT) */它裡面的每一位表示的就是每個端點當前傳送或接收的資料包是DATA0還是DATA1

  17.  
  18. struct usb_device *parent; /* our hub, unless we're the root */

  19. //USB裝置是從Root Hub開始,一個一個往外面連的,比如Root Hub有4個口,每個口連一個USB裝置,比如其中有一個是Hub,那麼這個Hub有可以繼續有多個口,於是一級一級的往下連,

  20. //最終連成了一棵樹。

  21. struct usb_bus *bus; /* Bus we're part of */裝置所在的匯流排

  22. struct usb_host_endpoint ep0; //端點0的特殊地位決定了她必將受到特殊的待遇,在struct usb_device物件產生的時候它就要初始化

  23.  
  24. struct device dev; /* Generic device interface */嵌入到struct usb_device結構裡的struct device結構

  25.  
  26. struct usb_device_descriptor descriptor;/* Descriptor */裝置描述符,此結構體的bMaxPacketSize0 filed儲存了端點0的maximum packet size

  27. struct usb_host_config *config; //裝置擁有的所有配置

  28.  
  29. struct usb_host_config *actconfig;//裝置正在使用的配置

  30. struct usb_host_endpoint *ep_in[16];//ep_in[16],359行,ep_out[16],除了端點0,一個裝置即使在高速模式下也最多隻能再有15個IN端點和15個OUT端點,端點0太特殊了,

  31. struct usb_host_endpoint *ep_out[16];//對應的管道是Message管道,又能進又能出特能屈能伸的那種,所以這裡的ep_in和ep_out陣列都有16個值

  32.  
  33. char **rawdescriptors; /* Raw descriptors for each config */

  34.  
  35. unsigned short bus_mA; /* Current available from the bus */這個值是在host controller的驅動程式中設定的,通常來講,計算機的usb埠可以提供500mA的電流

  36. u8 portnum; //不管是root hub還是一般的hub,你的USB裝置總歸要插在一個hub的埠上才能用,portnum就是那個埠號。

  37. u8 level; //層次,也可以說是級別,表徵usb裝置樹的級連關係。Root Hub的level當然就是0,其下面一層就是level 1,再下面一層就是level 2,依此類推

  38.  
  39. unsigned discon_suspended:1; /* Disconnected while suspended */

  40. unsigned have_langid:1; /* whether string_langid is valid */

  41. int string_langid; /* language ID for strings */

  42.  
  43. /* static strings from the device */

  44. char *product; /* iProduct string, if present */

  45. char *manufacturer; /* iManufacturer string, if present */

  46. char *serial; /* iSerialNumber string, if present */

  47. //分別用來儲存產品、廠商和序列號對應的字串描述符資訊

  48. struct list_head filelist;

  49. #ifdef CONFIG_USB_DEVICE_CLASS

  50. struct device *usb_classdev;

  51. #endif

  52. #ifdef CONFIG_USB_DEVICEFS

  53. struct dentry *usbfs_dentry; /* usbfs dentry entry for the device */

  54. #endif

  55. /*

  56. * Child devices - these can be either new devices

  57. * (if this is a hub device), or different instances

  58. * of this same device.

  59. *

  60. * Each instance needs its own set of data structures.

  61. */

  62.  
  63. int maxchild; /* Number of ports if hub */

  64. struct usb_device *children[USB_MAXCHILDREN];

  65.  
  66. int pm_usage_cnt; /* usage counter for autosuspend */

  67. u32 quirks; //quirk就是用來判斷這些有毛病的產品啥毛病的

  68.  
  69. #ifdef CONFIG_PM

  70. struct delayed_work autosuspend; /* for delayed autosuspends */

  71. struct mutex pm_mutex; /* protects PM operations */

  72.  
  73. unsigned long last_busy; /* time of last use */

  74. int autosuspend_delay; /* in jiffies */

  75.  
  76. unsigned auto_pm:1; /* autosuspend/resume in progress */

  77. unsigned do_remote_wakeup:1; /* remote wakeup should be enabled */

  78. unsigned autosuspend_disabled:1; /* autosuspend and autoresume */

  79. unsigned autoresume_disabled:1; /* disabled by the user */

  80. #endif

  81. };

 

四、struct usb_host_config 配置函式

 

  1. struct usb_host_config {

  2. struct usb_config_descriptor desc;

  3.  
  4. char *string;

  5.  
  6. struct usb_interface *interface[USB_MAXINTERFACES]; //配置所包含的介面,這個陣列的順序未必是按照配置裡介面號的順序

  7.  
  8.  
  9. struct usb_interface_cache *intf_cache[USB_MAXINTERFACES]; //usb介面的快取

  10.  
  11. unsigned char *extra;

  12. int extralen;

  13. }