当前位置:   article > 正文

linux usb ehci controller driver_qtd_sts_babble :90008d50

qtd_sts_babble :90008d50

1 数据结构

这里描述的所有结构的第一个word的结构都是相同的:Next Link Pointer + Type+T,即都是由指向下个结构体的物理地址+结构体类型指示+Terminate来构成。 这样构成有个好处,用c++的基类和派生类来类比非常贴切,这里的第一个word就相当于基类,而这些结构体的其余部分就相当于不同的派生类(在这里就是qtd,itd,qh,sitd...)。诚如c++那样操作,可以不管这些派生类的区别,而直接操作它的基类(Word0),如果需要,可由基类中的Type类型,从基类转成对应类型的派生类:即可以根据word0中的Type字段,来判断所指向的结构体是qtd、itd、qh、sitd中的哪一个。

1.1 queue element transfer descriptor(qtd)

用于intr、bulk、control传输类型向hc描述要传输的数据,即包含如下信息:hc从哪里取数据(C_Page,Current Offset,Buffer Pointer),取多长的数据(Total Bytes to Transfer),传输的状态和结果信息(status),传输完成后是否产生中断(ioc),使用何令牌包来发起数据传输(PID Code: IN OUT SETUP...)


该结构在linux ehcd驱动中的表示:

  1. /*
  2. * EHCI Specification 0.95 Section 3.5
  3. * QTD: describe data transfer components (buffer, direction, ...)
  4. * See Fig 3-6 "Queue Element Transfer Descriptor Block Diagram".
  5. *
  6. * These are associated only with "QH" (Queue Head) structures,
  7. * used with control, bulk, and interrupt transfers.
  8. */
  9. struct ehci_qtd {
  10. /* first part defined by EHCI spec */
  11. __hc32 hw_next; /* see EHCI 3.5.1 */
  12. __hc32 hw_alt_next; /* see EHCI 3.5.2 */
  13. __hc32 hw_token; /* see EHCI 3.5.3 */
  14. #define QTD_TOGGLE (1 << 31) /* data toggle */
  15. #define QTD_LENGTH(tok) (((tok)>>16) & 0x7fff)
  16. #define QTD_IOC (1 << 15) /* interrupt on complete */
  17. #define QTD_CERR(tok) (((tok)>>10) & 0x3)
  18. #define QTD_PID(tok) (((tok)>>8) & 0x3)
  19. #define QTD_STS_ACTIVE (1 << 7) /* HC may execute this */
  20. #define QTD_STS_HALT (1 << 6) /* halted on error */
  21. #define QTD_STS_DBE (1 << 5) /* data buffer error (in HC) */
  22. #define QTD_STS_BABBLE (1 << 4) /* device was babbling (qtd halted) */
  23. #define QTD_STS_XACT (1 << 3) /* device gave illegal response */
  24. #define QTD_STS_MMF (1 << 2) /* incomplete split transaction */
  25. #define QTD_STS_STS (1 << 1) /* split transaction state */
  26. #define QTD_STS_PING (1 << 0) /* issue PING? */
  27. #define ACTIVE_BIT(ehci) cpu_to_hc32(ehci, QTD_STS_ACTIVE)
  28. #define HALT_BIT(ehci) cpu_to_hc32(ehci, QTD_STS_HALT)
  29. #define STATUS_BIT(ehci) cpu_to_hc32(ehci, QTD_STS_STS)
  30. __hc32 hw_buf [5]; /* see EHCI 3.5.4 */
  31. __hc32 hw_buf_hi [5]; /* Appendix B */
  32. /* the rest is HCD-private */
  33. dma_addr_t qtd_dma; /* qtd address */
  34. struct list_head qtd_list; /* sw qtd list */
  35. struct urb *urb; /* qtd's urb */
  36. size_t length; /* length of buffer */
  37. } __attribute__ ((aligned (32)));
该结构是数据传输组件,通过qtd->qtd_list成员挂在qh队列头下面。

1.2 queue head 结构(qh)

一个queue head结构对应一个端点,所以queue head结构中的word1和word2字都是包含些跟端点相关的静态信息:最大包长度(Maximum Packet Length),hub地址(Hub Addr),hub port号(Port Number),设备地址(Device Address),端口号(EndPt),端口速度(EPS),是否是控制端点(C),RL(Nakcount的初值),data toggle 控制位(dtc),倍乘器(Mult). 另外qh是组织成一个循环队列的,而为了hc能够区分在遍历完当前循环队列一圈后,是否有事务传输发生,需要一个队列头,由于是循环队列,所以里每个qh都可以作为队列头,所以需要一个标志位H,用来指是当前哪个QH用来做该循环队列的头。
uFrame C-mask用于指是在哪几个微帧(1<=uframe<=8)会有complete split transsiton,该字段仅对全速中断的分裂传输由效,其他情况下,应该都是0。
uFrame S-mask用于指示在哪几个微帧(1<=uframe<=8)发起中断传输(针对高速的中断端点)或者发起start split传输(针对全速、低速的中断端点)。 
黄色标识的区域是overlay area, 即跟qtd交叠区域,所以里面包含的内容基本跟qtd都相同
queue head结构用来管理用于中断、bulk、control传输的数据。而对于中断传输,qh是挂在周期调度列表中的, 而对于bulk、control传输,qh是挂在异步循环调度列表中。


该结构在linux ehcd驱动中的表示:

  1. /*
  2. * EHCI Specification 0.95 Section 3.6
  3. * QH: describes control/bulk/interrupt endpoints
  4. * See Fig 3-7 "Queue Head Structure Layout".
  5. *
  6. * These appear in both the async and (for interrupt) periodic schedules.
  7. */
  8. /* first part defined by EHCI spec */
  9. struct ehci_qh_hw {
  10. __hc32 hw_next; /* see EHCI 3.6.1 */
  11. __hc32 hw_info1; /* see EHCI 3.6.2 */
  12. #define QH_HEAD 0x00008000
  13. __hc32 hw_info2; /* see EHCI 3.6.2 */
  14. #define QH_SMASK 0x000000ff
  15. #define QH_CMASK 0x0000ff00
  16. #define QH_HUBADDR 0x007f0000
  17. #define QH_HUBPORT 0x3f800000
  18. #define QH_MULT 0xc0000000
  19. __hc32 hw_current; /* qtd list - see EHCI 3.6.4 */
  20. /* qtd overlay (hardware parts of a struct ehci_qtd) */
  21. __hc32 hw_qtd_next;
  22. __hc32 hw_alt_next;
  23. __hc32 hw_token;
  24. __hc32 hw_buf [5];
  25. __hc32 hw_buf_hi [5];
  26. } __attribute__ ((aligned(32)));
实际在程序中,是使用的struct ehci_qh结构体,它跟struct ehci_qh_hw*hw的关系就是:struct ehci_qh结构体的开头部分存储的就是指向struct ehci_qh_hw*hw结构的指针,struct ehci_qh_hw结构是供hc来使用的,而struct ehci_qh结构则供我们的hcd来维护和管理这样的结构的。

1.3 itd结构

word1到word8,表示8个transaction,用于描述同步传输,连入对应的周期列表(periodic list),周期列表含有1024个表项(items),而每个item表示1 ms的时间内usb所进行的周期传输;而对于高速设备,1ms的frame又被分为8个微帧uframe,所以每个微帧对应的时间是125us。如下的8个transaction就各自对应一个微帧uframe的传输。 具体到每个微帧字段的含义:
  • status 是由hc在transaction(事务)完成后,自动填充,表示传输的结果
  • Transaction Length,表示该transaction的数据长度
  • ioc表示该transaction完成后,是否产生中断
  • PG用于选择下面的哪个buffer Pointer(指向一个虚拟页面,所以低12位都是0,所以低12位可用作其他用途)
  • Transaction offset表示页内的偏移量
另外itd结构就是表示一个同步端点在periodic list的某个表项(item,对应一个大帧frame)中所要进行的传输,而在这个大帧中,可能还包含了其他同步端点所对应的itd结构,并且还可能包含中断端点的qh结构。而同属于这个表项()的所有端点的传输,他们都会通过各自结构体的第一个字段(word0)进行连接起来。这样就可以让hc对每个挂在这个列表上的qh,itd都是reachable的, 诚如刚才所说,一个itd就表示一个同步端点在某个periodic list节点所需要进行的传输,所以需要在该结构体中包含端点的信息,以便于hc来执行相应的事务传输。即包含如下端点相关信息:endpt(端点号),device address(设备地址), max package size(最大包长度), mult(倍乘) , I/O(方向)等。



同步传输的带宽,由以下因素即可计算得出:  最大包长度(mmax package size)、每个微帧中传输的次数(multi)、帧间隔(interval),则就可以算出该高速同步端点对应所需要的带宽,计算代码如下:
  1. maxp = usb_maxpacket(dev, pipe, !is_input);
  2. unsigned multi = hb_mult(maxp);
  3. maxp = max_packet(maxp)*multi//计算一次同步传输,所包含的最大包的长度
  4. stream->usecs = HS_USECS_ISO (maxp);//由包长换算成带宽时间
  5. //bandwidth = stream->usecs * 8;
  6. //bandwidth /= interval;


long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)函数的功能: 对于给定的usb总线速度(高速、全速、低速),给定传输类型(同步传输、中断传输)和给定传输长度,所需要占用多少总线时间(即带宽)。


Isochronous Transaction Descriptor (iTD) 在linux中的hcd驱动中对应的数据结构体为:struct ehci_itd

  1. /*
  2. * EHCI Specification 0.95 Section 3.3
  3. * Fig 3-4 "Isochronous Transaction Descriptor (iTD)"
  4. *
  5. * Schedule records for high speed iso xfers
  6. */
  7. struct ehci_itd {
  8. /* first part defined by EHCI spec */
  9. __hc32 hw_next; /* see EHCI 3.3.1 */
  10. __hc32 hw_transaction [8]; /* see EHCI 3.3.2 */
  11. #define EHCI_ISOC_ACTIVE (1<<31) /* activate transfer this slot */
  12. #define EHCI_ISOC_BUF_ERR (1<<30) /* Data buffer error */
  13. #define EHCI_ISOC_BABBLE (1<<29) /* babble detected */
  14. #define EHCI_ISOC_XACTERR (1<<28) /* XactErr - transaction error */
  15. #define EHCI_ITD_LENGTH(tok) (((tok)>>16) & 0x0fff)
  16. #define EHCI_ITD_IOC (1 << 15) /* interrupt on complete */
  17. #define ITD_ACTIVE(ehci) cpu_to_hc32(ehci, EHCI_ISOC_ACTIVE)
  18. __hc32 hw_bufp [7]; /* see EHCI 3.3.3 */
  19. __hc32 hw_bufp_hi [7]; /* Appendix B */
  20. /* the rest is HCD-private */
  21. dma_addr_t itd_dma; /* for this itd */
  22. union ehci_shadow itd_next; /* ptr to periodic q entry */
  23. struct urb *urb;
  24. struct ehci_iso_stream *stream; /* endpoint's queue */
  25. struct list_head itd_list; /* list of stream's itds */
  26. /* any/all hw_transactions here may be used by that urb */
  27. unsigned frame; /* where scheduled */
  28. unsigned pg;
  29. unsigned index[8]; /* in urb->iso_frame_desc */
  30. } __attribute__ ((aligned (32)));
该结构的前半部分用于hc,后半部分用于hcd来管理这个结构。

1.4 sitd结构

通过带TT(transfer translator)功能的高速hub,可以在高速的ehci controller下挂上full/slow-speed的设备。其中该full/slow-speed的设备又分为中断/控制传输、同步传输。
TT 在 USB HUB 中主要负责从高速到全/低速传输事务的转发,完成速度的匹配。从传输的角度来说,主要就是完成分离事务传输(Split Transaction)。下图为 TT
的基本结构。 



和大部分速度匹配逻辑一样,TT 也采用 Buffer 的方 式 来 完 成 速 度 匹 配 。Buffer 的一端是一个高速Handler ,处理高速数据收发 ; 另 一 端 是 全 速 / 低 速Handler, 处理全速/低速数据的收发。针对传输类型的不同,TT 采用多个 Buffer 来缓存数据。中断传输的 Start-Split(S-Split)阶段和 Complete-Split(C-Split)阶段分别用一个 Buffer,批量传输和中断传输的 S-Split 和 C-Split 不分开存储,但是在一个 TT 中至少有 2 个用于批量传输和中断传输的 Buffer分离传输仅在 HOST 和 HUB 之间发生,对于设备全速/低速设备来说是透明的。一个USB HUB 可以有多个 TT,这在 USB 协议中是允许的按照处理方式的区别,TT 中的分离传输可以分为两类——周期分离传输和非周期分离传输。周期分离传输指的是同步分离传输和中断分离传输,非周期分离传输指的是批量分离传输和控制分离传输。
而针对同步分离传输,设计了一种的新的数据结构sitd来管理这种同步分裂传输。
该结构各字段的内容如下:
其中word0用于连接下一个itd或qh结构,word1用于描述该结构对应的端点信息,word2中c-mask表示在哪些微帧位置发起complete传输,s-mask表示在哪些位置发起start complete传输;word3表示传输长度和传输结构,word4、5表示传输对应的buffer地址。
在linux驱动中的结构表示:
  1. /*
  2. * EHCI Specification 0.95 Section 3.4
  3. * siTD, aka split-transaction isochronous Transfer Descriptor
  4. * ... describe full speed iso xfers through TT in hubs
  5. * see Figure 3-5 "Split-transaction Isochronous Transaction Descriptor (siTD)
  6. */
  7. struct ehci_sitd {
  8. /* first part defined by EHCI spec */
  9. __hc32 hw_next;
  10. /* uses bit field macros above - see EHCI 0.95 Table 3-8 */
  11. __hc32 hw_fullspeed_ep; /* EHCI table 3-9 */
  12. __hc32 hw_uframe; /* EHCI table 3-10 */
  13. __hc32 hw_results; /* EHCI table 3-11 */
  14. #define SITD_IOC (1 << 31) /* interrupt on completion */
  15. #define SITD_PAGE (1 << 30) /* buffer 0/1 */
  16. #define SITD_LENGTH(x) (0x3ff & ((x)>>16))
  17. #define SITD_STS_ACTIVE (1 << 7) /* HC may execute this */
  18. #define SITD_STS_ERR (1 << 6) /* error from TT */
  19. #define SITD_STS_DBE (1 << 5) /* data buffer error (in HC) */
  20. #define SITD_STS_BABBLE (1 << 4) /* device was babbling */
  21. #define SITD_STS_XACT (1 << 3) /* illegal IN response */
  22. #define SITD_STS_MMF (1 << 2) /* incomplete split transaction */
  23. #define SITD_STS_STS (1 << 1) /* split transaction state */
  24. #define SITD_ACTIVE(ehci) cpu_to_hc32(ehci, SITD_STS_ACTIVE)
  25. __hc32 hw_buf [2]; /* EHCI table 3-12 */
  26. __hc32 hw_backpointer; /* EHCI table 3-13 */
  27. __hc32 hw_buf_hi [2]; /* Appendix B */
  28. /* the rest is HCD-private */
  29. dma_addr_t sitd_dma;
  30. union ehci_shadow sitd_next; /* ptr to periodic q entry */
  31. struct urb *urb;
  32. struct ehci_iso_stream *stream; /* endpoint's queue */
  33. struct list_head sitd_list; /* list of stream's sitds */
  34. unsigned frame;
  35. unsigned index;
  36. } __attribute__ ((aligned (32)));
该结构的开始部分是用于host controller来访问的,后面是hcd使用的,其中sitd_list 成员将该sitd结构连入到列表中。这样便于hcd来遍历这个列表(我们称为软件列表)。而该结构的hw_next则是用于连接硬件列表,是专供host controller来访问的。

1.5 周期调度队列 vs 异步调度队列

周期调度队列:
该周期调度帧列表,是传输数据接口(指qtd,qh,itd,sitd等)在内存中的组织结构,这个组织结构将被host controller自动访问。但需要软件(hcd driver)来初始化好,并动态维护他(往这个结构中添加传输数据接口(对应enqueue操作),删除传输数据接口(dequeue操作)

异步调度队列:
该结构用于管理和连接控制、块传输,该队列是单向循环队列,队列的节点就是qh,而每个qh描述一个端点的传输(可能是:高速/全速控制端点,高速/全速块端点),并且属于这个端点的所有待传数据都是通过qtd来描述,挂在qh节点对应的->qtd_list列表下。下图标有H的节点,则是这个循环调度队列的头节点。

需要注意的地方是,hw_next字段连接起来的都是硬件队列,而qtd/itd/sitd_list等成员连接起来的列表都是软件列表,他们有些其实就是同在一个结构体中。其中硬件队列是供host controller使用,软件队列是供hcd驱动来使用的。前者存储的是物理地址,后者存储的是虚拟地址。

2 操作模型

2.1 echi control对异步调度队列的处理过程

详细的请查看ehci规范的4.10节,这个描述的过程,就是host controller对qh、qtd结构的操作和管理过程。

2.2 echi control对异步调度中的QH的删除算法(摘自ehci spec 4.8.2)

下图摘自ehci spec的4.8.2小结,描述怎么删除正在被hc访问的队列中的节点的方法,核心就是要避免hc和hcd对这个队列的节点产生竞争性的访问。该算法会被用于dequeue过程。


2.3 echi的ehci_urb_enqueue过程

2.3.1 for usb bulk、control transfrer urb enqueue过程



/*
 * create a list of filled qtds for this URB; won't link into qh.
* URBs map to sequences of QTDs:  one logical transaction
 */
static struct list_head *qh_urb_transaction (struct ehci_hcd*ehci,struct urb*urb,struct list_head*head,gfp_tflags)
  1. 该函数将一个urb传输映射成若干个qtd结构,并把他们关联起来。他们的关联分为两部分
      1. 一个是将这若干个qtd按他们被执行的顺序,通过qtd->qtd_list域连接到head列表头中,该列表主要用于软件来访问这些qtd
        1. 一个是通过qtd->hw_next指针,将这些qtd连接起来,这个连接是给host constroller使用的,所以->hw_next里存的是物理地址
      2. 该函数根据urb->transfer_buffer_length的长度,来决定分配具体几个qtd,即一个qtd存不下所需传输的内容时,就调用ehci_qtd_alloc函数再分配一个qtd,然后再调用qtd_fill将剩余的数据的长度和buffer开始地址填充到qtd结构中。
      3. 该函数如果发现待传输的数据是控制传输,则为该控制传输,添加setup数据建立阶段所对应的qtd和状态阶段所对应的qtd
      1. if (usb_pipecontrol (urb->pipe)) {//为控制的setup数据建立阶段,分配和填充qtd结构,并将它联入到head list列表头中
      2. /* SETUP pid */
      3. qtd_fill(ehci, qtd, urb->setup_dma,
      4. sizeof (struct usb_ctrlrequest),
      5. token | (2 /* "setup" */ << 8), 8);
      6. /* ... and always at least one more pid */
      7. token ^= QTD_TOGGLE;
      8. qtd_prev = qtd;
      9. qtd = ehci_qtd_alloc (ehci, flags);
      10. if (unlikely (!qtd))
      11. goto cleanup;
      12. qtd->urb = urb;
      13. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
      14. list_add_tail (&qtd->qtd_list, head);
      15. /* for zero length DATA stages, STATUS is always IN */
      16. if (len == 0)
      17. token |= (1 /* "in" */ << 8);
      18. }

      如下代码为控制传输的状态阶段和以短包表示结束(URB_ZERO_PACKET)的bulk out传输分配和初始化qtd结构,并连入head list列表中
      1. if (one_more) {
      2. qtd_prev = qtd;
      3. qtd = ehci_qtd_alloc (ehci, flags);
      4. if (unlikely (!qtd))
      5. goto cleanup;
      6. qtd->urb = urb;
      7. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
      8. list_add_tail (&qtd->qtd_list, head);
      9. /* never any data in such packets */
      10. qtd_fill(ehci, qtd, 0, 0, token, 0);
      11. }


      static struct ehci_qtd *ehci_qtd_alloc (struct ehci_hcd *ehci, gfp_t flags)
      分配一个qtd结构

      /* fill a qtd, returning how much of the buffer we were able to queue up */
      static int qtd_fill(struct ehci_hcd *ehci, struct ehci_qtd *qtd, dma_addr_t buf, size_t len, int token, int maxpacket)
      填充一个qtd结构,一个qtd结构最多可以存储16K to 20K的数据,如果urb包含的数据长度大于这个长度,则需要调用ehci_qtd_alloc 另外再分配一个qtd。


      static int submit_async (struct ehci_hcd*ehci,struct urb*urb,struct list_head*qtd_list,gfp_tmem_flags)
       该函数将上面的qh_urb_transaction 初始化好的qtd列表链接到异步循环调度列表中。该函数将如下两个重要函数:qh_append_tds, qh_link_async 

      /*
       * For control/bulk/interrupt, return QH with these TDs appended.
       * Allocates and initializes the QH if necessary.
       * Returns null if it can't allocate a QH it needs to.
       * If the QH has TDs (urbs) already, that's great.
       */
      static struct ehci_qh *qh_append_tds (struct ehci_hcd*ehci,struct urb*urb,struct list_head*qtd_list,intepnum,void**ptr)
      该函数的功能就是:将qtd_list添加对应的队列头上,如果没有对应的队列头,则分配初始化一个队列头。
      1. static struct ehci_qh *qh_append_tds (
      2. struct ehci_hcd *ehci,
      3. struct urb *urb,
      4. struct list_head *qtd_list,
      5. int epnum,
      6. void **ptr
      7. )
      8. {
      9. struct ehci_qh *qh = NULL;
      10. __hc32 qh_addr_mask = cpu_to_hc32(ehci, 0x7f);
      11. qh = (struct ehci_qh *) *ptr;
      12. if (unlikely (qh == NULL)) { //如果该端点还没有对应的qh队列头,则就调用<span style="font-family:Arial, Helvetica, sans-serif;">qh_make函数</span>分配一个QH队列头
      13. /* can't sleep here, we have ehci->lock... */
      14. qh = qh_make (ehci, urb, GFP_ATOMIC);
      15. *ptr = qh;
      16. }
      17. if (likely (qh != NULL)) {
      18. struct ehci_qtd *qtd;
      19. if (unlikely (list_empty (qtd_list)))
      20. qtd = NULL;
      21. else
      22. qtd = list_entry (qtd_list->next, struct ehci_qtd,//获得列表节点所在的qtd结构
      23. qtd_list);
      24. /* control qh may need patching ... */
      25. if (unlikely (epnum == 0)) {
      26. /* usb_reset_device() briefly reverts to address 0 */
      27. if (usb_pipedevice (urb->pipe) == 0)
      28. qh->hw->hw_info1 &= ~qh_addr_mask;
      29. }
      30. /* just one way to queue requests: swap with the dummy qtd.
      31. * only hc or qh_refresh() ever modify the overlay.
      32. */
      33. if (likely (qtd != NULL)) {
      34. struct ehci_qtd *dummy;
      35. dma_addr_t dma;
      36. __hc32 token;
      37. /* to avoid racing the HC, use the dummy td instead of
      38. * the first td of our list (becomes new dummy). both
      39. * tds stay deactivated until we're done, when the
      40. * HC is allowed to fetch the old dummy (4.10.2).
      41. */
      42. token = qtd->hw_token;
      43. qtd->hw_token = HALT_BIT(ehci);
      44. dummy = qh->dummy;
      45. dma = dummy->qtd_dma;
      46. *dummy = *qtd;
      47. dummy->qtd_dma = dma;
      48. list_del (&qtd->qtd_list);//将qtd_list列表中的第一个元素删除,将dummy添加到这个位置,此时dummy->hw_next是指向qtd->hw_next的
      49. list_add (&dummy->qtd_list, qtd_list);
      50. list_splice_tail(qtd_list, &qh->qtd_list);//将带dummy的qtd list列表添加到qh->qtd_list列表的末尾
      51. ehci_qtd_init(ehci, qtd, qtd->qtd_dma);
      52. qh->dummy = qtd; //将qh->dummy指向原qtd list列表的第一个元素(已从list删除下来)
      53. /* hc must see the new dummy at list end */
      54. dma = qtd->qtd_dma;
      55. qtd = list_entry (qh->qtd_list.prev,
      56. struct ehci_qtd, qtd_list);//
      57. qtd->hw_next = QTD_NEXT(ehci, dma);//让qh->qtd_list列表的尾端链上这个新的dummy元素
      58. /* let the hc process these next qtds */
      59. wmb ();
      60. dummy->hw_token = token;
      61. urb->hcpriv = qh_get (qh);
      62. }
      63. }
      64. return qh;
      65. }

      上面函数涉及的操作说明见如下图列表操作示意:(上半部分,是列表操作前的情况,下半部是列表操作后的情况,从下图可以看出qtd和dummy进行了互换)






      /* move qh (and its qtds) onto async queue; maybe enable queue.  */
      static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)

      1. static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
      2. {
      3. __hc32 dma = QH_NEXT(ehci, qh->qh_dma);
      4. struct ehci_qh *head;
      5. /* Don't link a QH if there's a Clear-TT-Buffer pending */
      6. if (unlikely(qh->clearing_tt))
      7. return;
      8. WARN_ON(qh->qh_state != QH_STATE_IDLE);
      9. /* (re)start the async schedule? */
      10. head = ehci->async;//软件上的异步循环调度队列的头,并不是host controller所标识的队列头
      11. timer_action_done (ehci, TIMER_ASYNC_OFF);
      12. if (!head->qh_next.qh) {
      13. u32 cmd = ehci_readl(ehci, &ehci->regs->command);
      14. if (!(cmd & CMD_ASE)) {//对cmd寄存器中的cmd_ase位的控制,并不是立即使能的,需要结合status寄存器来最终确定。
      15. /* in case a clear of CMD_ASE didn't take yet */
      16. (void)handshake(ehci, &ehci->regs->status,
      17. STS_ASS, 0, 150);
      18. cmd |= CMD_ASE;
      19. ehci_writel(ehci, cmd, &ehci->regs->command);
      20. /* posted write need not be known to HC yet ... */
      21. }
      22. }
      23. /* clear halt and/or toggle; and maybe recover from silicon quirk */
      24. qh_refresh(ehci, qh);
      25. /* splice right after start */
      26. qh->qh_next = head->qh_next;
      27. qh->hw->hw_next = head->hw->hw_next;
      28. wmb ();
      29. head->qh_next.qh = qh;
      30. head->hw->hw_next = dma;
      31. qh_get(qh);
      32. qh->xacterrs = 0;
      33. qh->qh_state = QH_STATE_LINKED;
      34. /* qtd completions reported later by interrupt */
      35. }
      上述函数功能是将qh添加到异步循环调度队列中,必要时使能该异步调度使能位。上述操作见下面的示意图(注意每个QH对应一个端点的传输):

      2.3.2 for PIPE_INTERRUPT urb enqueue过程(包括高速中断传输和分裂中断传输)

      中断的分裂IN传输过程:


      中断的分裂OUT传输过程:


      分裂传输时的frame和microframe的时隙关系


      中断传输的enqueue过程的流程图:


      该函数的功能:将urb中断传输对应的qtd列表连入到对应的时隙中。
      1. static int intr_submit (
      2. struct ehci_hcd *ehci,
      3. struct urb *urb,
      4. struct list_head *qtd_list,
      5. gfp_t mem_flags
      6. ) {
      7. unsigned epnum;
      8. unsigned long flags;
      9. struct ehci_qh *qh;
      10. int status;
      11. struct list_head empty;
      12. /* get endpoint and transfer/schedule data */
      13. epnum = urb->ep->desc.bEndpointAddress;
      14. spin_lock_irqsave (&ehci->lock, flags);
      15. if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
      16. status = -ESHUTDOWN;
      17. goto done_not_linked;
      18. }
      19. status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
      20. if (unlikely(status))
      21. goto done_not_linked;
      22. /* get qh and force any scheduling errors */
      23. INIT_LIST_HEAD (&empty);
      24. qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
      25. if (qh == NULL) {
      26. status = -ENOMEM;
      27. goto done;
      28. }
      29. if (qh->qh_state == QH_STATE_IDLE) {
      30. if ((status = qh_schedule (ehci, qh)) != 0)
      31. goto done;
      32. }
      33. /* then queue the urb's tds to the qh */
      34. qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
      35. BUG_ON (qh == NULL);
      36. /* ... update usbfs periodic stats */
      37. ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;
      38. done:
      39. if (unlikely(status))
      40. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
      41. done_not_linked:
      42. spin_unlock_irqrestore (&ehci->lock, flags);
      43. if (status)
      44. qtd_list_free (ehci, urb, qtd_list);
      45. return status;
      46. }

      接下来的描述总线带宽,都是使用时间单位us(微秒)(总线带宽的描述,可以从时域和频域来描述),一个periodic frame list中的一frame的时间是1ms,而一个frame里包含了8个uframe,每个uframe对应一个总线的时隙(time slot),其带宽是固定的1000us/8=125us,并且usb2.0规定:周期传输的带宽不能超过总线带宽的80%,即不能超过125us*80%=100us。usb1.0中规定,同步传输的总线带宽不能够超过总线带宽的90%,但是low/full-speed设别,他们的总线时隙是1ms,即刚好对应一个frame帧的时间。

      QH的一些重要成员域的含义:
      • qh->start:该成员仅对中断传输有意义,表示该QH被挂在周期微帧的什么位置,即在哪个周期微帧有中断传输,取值范围: 0..(qh->period - 1), or NO_FRAME(表示poll rate为1次/微帧) 
      • qh->usecs: 分裂传输分为split阶段和 csplit阶段,而该字段就是在split阶段所需要的总线带宽
      • qh->c_usecs:表示在csplit阶段,所需要的总线带宽
      • qh->tt_usecs:表示端点最大包长度在下行的全速总线上传输所需要占用的时间
      • qh->gap_uf:表示该端点的最大包长度在下行的全速总线上传输完,该段时间对应多少个高速总线时隙
      • qh->period:表示该中断传输的poll rate,表示每隔多少个uframe产生一次中断传输,单位为微帧


      /*
       * Each QH holds a qtd list; a QH is used for everything except iso.
       *
       * For interrupt urbs, the scheduler must set the microframe scheduling
       * mask(s) each time the QH gets scheduled.  For highspeed, that's
       * just one microframe in the s-mask.  For split interrupt transactions
       * there are additional complications: c-mask, maybe FSTNs.
       */
      该函数的主要功能,就是分配qh结构的空间,并初始化他们。
      static struct ehci_qh *qh_make ( struct ehci_hcd *ehci, struct urb *urb, gfp_t flags)
      1. {
      2. struct ehci_qh *qh = ehci_qh_alloc (ehci, flags);
      3. u32 info1 = 0, info2 = 0;
      4. int is_input, type;
      5. int maxp = 0;
      6. struct usb_tt *tt = urb->dev->tt;
      7. struct ehci_qh_hw *hw;
      8. if (!qh)
      9. return qh;
      10. /*
      11. * init endpoint/device data for this QH
      12. */
      13. info1 |= usb_pipeendpoint (urb->pipe) << 8;
      14. info1 |= usb_pipedevice (urb->pipe) << 0;
      15. is_input = usb_pipein (urb->pipe);
      16. type = usb_pipetype (urb->pipe);
      17. maxp = usb_maxpacket (urb->dev, urb->pipe, !is_input);
      18. /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
      19. * acts like up to 3KB, but is built from smaller packets.
      20. */
      21. if (max_packet(maxp) > 1024) {
      22. ehci_dbg(ehci, "bogus qh maxpacket %d\n", max_packet(maxp));
      23. goto done;
      24. }
      25. <span style="white-space:pre"> </span>//下面这段英文的注释,就是在初始化中断端点对应的QH时,需要考虑的问题,是难点!!!
      26. /* Compute interrupt scheduling parameters just once, and save.
      27. * - allowing for high bandwidth, how many nsec/uframe are used?
      28. * - split transactions need a second CSPLIT uframe; same question
      29. * - splits also need a schedule gap (for full/low speed I/O)
      30. * - qh has a polling interval
      31. *
      32. * For control/bulk requests, the HC or TT handles these.
      33. */
      34. if (type == PIPE_INTERRUPT) {//for 中断传输
      35. qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
      36. is_input, 0,
      37. hb_mult(maxp) * max_packet(maxp)));//计算该中断端点对应的最大包长度,在高速总线下需要的带宽()
      38. qh->start = NO_FRAME;
      39. qh->stamp = ehci->periodic_stamp;
      40. if (urb->dev->speed == USB_SPEED_HIGH) {//高速中断传输
      41. qh->c_usecs = 0;
      42. qh->gap_uf = 0;
      43. qh->period = urb->interval >> 3;
      44. if (qh->period == 0 && urb->interval != 1) {//对于那些中断间隔为2,4个uframe的中断传输,直接都upscale到间隔为1个uframe的中断传输
      45. /* NOTE interval 2 or 4 uframes could work.//这样更便于管理,带宽更高
      46. * But interval 1 scheduling is simpler, and
      47. * includes high bandwidth.
      48. */
      49. urb->interval = 1;
      50. } else if (qh->period > ehci->periodic_size) {//对于那些周期超过1024*8的中断传输,则都直接upscale到间隔为1024*8个uframe的中断传输
      51. qh->period = ehci->periodic_size;
      52. urb->interval = qh->period << 3;
      53. }
      54. } else { //全速中断传输
      55. int think_time;
      56. /* gap is f(FS/LS transfer times) */
      57. qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,//针对全速和低速设备的中断传输,首先计算出该中断端点的最大包长度在全速总线上所占的总线带宽
      58. is_input, 0, maxp) / (125 * 1000); //并计算出该总线带宽对应几个高速总线时隙,即对应几个uframe
      59. /* FIXME this just approximates SPLIT/CSPLIT times */
      60. if (is_input) { // 分裂中断IN传输的操作模型:SPLIT(对应带宽:HS_USECS(1)), gap, CSPLIT+DATA(对应带宽:qh->c_usecs)<span style="font-family: Arial, Helvetica, sans-serif;">qh->c_usecs)</span>
      61. qh->c_usecs = qh->usecs + HS_USECS (0);
      62. qh->usecs = HS_USECS (1);
      63. } else { //分裂中断OUT传输的操作模型: SPLIT+DATA, gap, CSPLIT
      64. qh->usecs += HS_USECS (1);
      65. qh->c_usecs = HS_USECS (0);
      66. }
      67. think_time = tt ? tt->think_time : 0;
      68. qh->tt_usecs = NS_TO_US (think_time +
      69. usb_calc_bus_time (urb->dev->speed,
      70. is_input, 0, max_packet (maxp)));//即这个长度的包在全速总线上传输,所需要的时间
      71. qh->period = urb->interval;
      72. if (qh->period > ehci->periodic_size) {
      73. qh->period = ehci->periodic_size;
      74. urb->interval = qh->period;
      75. }
      76. }
      77. }
      78. /* support for tt scheduling, and access to toggles */
      79. qh->dev = urb->dev;
      80. /* using TT? */
      81. switch (urb->dev->speed) {
      82. case USB_SPEED_LOW:
      83. info1 |= (1 << 12); /*更新QH结构中的 EPS域到 "low" */
      84. /* FALL THROUGH */
      85. case USB_SPEED_FULL:
      86. /* EPS 0 means "full" */
      87. if (type != PIPE_INTERRUPT)
      88. info1 |= (EHCI_TUNE_RL_TT << 28);
      89. if (type == PIPE_CONTROL) {
      90. info1 |= (1 << 27); /*field:"C" for TT */
      91. info1 |= 1 << 14; /*field:"dtc" toggle from qtd */
      92. }
      93. info1 |= maxp << 16; /*field:"Maximum package length"*/
      94. info2 |= (EHCI_TUNE_MULT_TT << 30); /*field:"Mult"*/
      95. /* Some Freescale processors have an erratum in which the
      96. * port number in the queue head was 0..N-1 instead of 1..N.
      97. */
      98. if (ehci_has_fsl_portno_bug(ehci))/*Port number*/
      99. info2 |= (urb->dev->ttport-1) << 23;
      100. else
      101. info2 |= urb->dev->ttport << 23;
      102. /* set the address of the TT; for TDI's integrated
      103. * root hub tt, leave it zeroed.
      104. */
      105. if (tt && tt->hub != ehci_to_hcd(ehci)->self.root_hub)/*Hub address*/
      106. info2 |= tt->hub->devnum << 16;
      107. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
      108. break;
      109. case USB_SPEED_HIGH: /* no TT involved */
      110. info1 |= (2 << 12); /* EPS "high" */
      111. if (type == PIPE_CONTROL) {
      112. info1 |= (EHCI_TUNE_RL_HS << 28);
      113. info1 |= 64 << 16; /* usb2 fixed maxpacket */
      114. info1 |= 1 << 14; /* toggle from qtd */
      115. info2 |= (EHCI_TUNE_MULT_HS << 30);
      116. } else if (type == PIPE_BULK) {
      117. info1 |= (EHCI_TUNE_RL_HS << 28);
      118. /* The USB spec says that high speed bulk endpoints
      119. * always use 512 byte maxpacket. But some device
      120. * vendors decided to ignore that, and MSFT is happy
      121. * to help them do so. So now people expect to use
      122. * such nonconformant devices with Linux too; sigh.
      123. */
      124. info1 |= max_packet(maxp) << 16;
      125. info2 |= (EHCI_TUNE_MULT_HS << 30);
      126. } else { /* PIPE_INTERRUPT */
      127. info1 |= max_packet (maxp) << 16;
      128. info2 |= hb_mult (maxp) << 30;
      129. }
      130. break;
      131. default:
      132. dbg ("bogus dev %p speed %d", urb->dev, urb->dev->speed);
      133. done:
      134. qh_put (qh);
      135. return NULL;
      136. }
      137. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
      138. /* init as live, toggle clear, advance to dummy */
      139. qh->qh_state = QH_STATE_IDLE;
      140. hw = qh->hw;
      141. hw->hw_info1 = cpu_to_hc32(ehci, info1);
      142. hw->hw_info2 = cpu_to_hc32(ehci, info2);
      143. qh->is_out = !is_input;
      144. usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
      145. qh_refresh (ehci, qh);
      146. return qh;
      147. }

      该函数的主要功能就是确定qh->start的值,而该值合理的取值范围为: 0..(qh->period - 1), or NO_FRAME,即该函数的功能就是:扫描整个周期调度uframe列表,从0...(qh->period - 1)中,选择第一个可用的值,使该值对应的那组时隙都有可用的同步带宽。
      另外需要注意的是,QH结构体中的S-mask的含义:
      • 首先该值不为0,则说明是中断端点,然后再根据EPS域
        • 如果是高速端点,则smask则表示在哪些对应的微帧发起中断传输
        • 如果是低速、全速端点,则smask则表示在哪个微帧发起ssplit传输
      • 如果该值为0,说明是在异步传输队列中,只能是控制、bulk传输
      最后c-mask则只有在低速、全速的中断端点时,才有意义,表示在哪几个微帧发起csplit传输
      1. /* "first fit" scheduling policy used the first time through,
      2. * or when the previous schedule slot can't be re-used.
      3. */
      4. static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
      5. {
      6. int status;
      7. unsigned uframe;
      8. __hc32 c_mask;
      9. unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
      10. struct ehci_qh_hw *hw = qh->hw;
      11. qh_refresh(ehci, qh);
      12. hw->hw_next = EHCI_LIST_END(ehci);
      13. frame = qh->start;
      14. /* reuse the previous schedule slots, if we can */
      15. if (frame < qh->period) {
      16. uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
      17. status = check_intr_schedule (ehci, frame, --uframe,
      18. qh, &c_mask);
      19. } else {
      20. uframe = 0;
      21. c_mask = 0;
      22. status = -ENOSPC;
      23. }
      24. /* else scan the schedule to find a group of slots such that all
      25. * uframes have enough periodic bandwidth available.
      26. */
      27. if (status) {
      28. /* "normal" case, uframing flexible except with splits */
      29. if (qh->period) {
      30. int i;
      31. for (i = qh->period; status && i > 0; --i) {
      32. frame = ++ehci->random_frame % qh->period;
      33. for (uframe = 0; uframe < 8; uframe++) {
      34. status = check_intr_schedule (ehci,
      35. frame, uframe, qh,
      36. &c_mask);
      37. if (status == 0)
      38. break;
      39. }
      40. }
      41. /* qh->period == 0 means every uframe */
      42. } else {
      43. frame = 0;
      44. status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
      45. }
      46. if (status)
      47. goto done;
      48. qh->start = frame; //选定了该frame帧对应的uframe微帧,是符合条件的。
      49. /* reset S-frame and (maybe) C-frame masks */
      50. hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
      51. hw->hw_info2 |= qh->period
      52. ? cpu_to_hc32(ehci, 1 << uframe) //qh->period == 0 means every uframe都有中断传输,
      53. : cpu_to_hc32(ehci, QH_SMASK); //不为0,则意味着只有制定的那个微帧才有中断传输或start split传输
      54. hw->hw_info2 |= c_mask;//制定csplit传输的位置,只有全速、低速中断才有意义,如果其他情况,应该都为0.
      55. } else
      56. ehci_dbg (ehci, "reused qh %p schedule\n", qh);
      57. /* stuff into the periodic schedule */
      58. status = qh_link_periodic (ehci, qh);
      59. done:
      60. return status;
      61. }

      分裂传输虽然可以跨H帧的边界,但是分裂传输绝不能夸B帧的边界,由于B帧滞后于H帧一个slot,所以如果要跨H帧支持分裂传输,那也只可能在下一个H帧的0、1uframe有csplit传输。
      上面函数就是通过调用check_intr_schedule函数确定frame帧中的uframe微帧对应的时隙组是否有可用的周期总线带宽(包括中断、同步传输)。
      由于分裂传输,是有ssplit和csplit传输组成,所以需要分别判断ssplit和csplit所在的微帧是否都有可用的周期带宽。
      在这里需要关键弄清楚qh->usecs的含义:
      • 如果EPS指示是高速中断端点,则qh->usecs表示该中断IN、OUT传输所需的最大带宽
      • 如果EPS指示是全速、低速中断端点,且是OUT中断传输,则qh->usecs表示:ssplit+data所需最大的带宽
      • 如果EPS指示是全速、低速中断端点,且是IN中断传输,则qh->usecs为0

      1. static int check_intr_schedule (
      2. struct ehci_hcd *ehci,
      3. unsigned frame,
      4. unsigned uframe,
      5. const struct ehci_qh *qh,
      6. __hc32 *c_maskp
      7. )
      8. {
      9. int retval = -ENOSPC;
      10. u8 mask = 0;
      11. if (qh->c_usecs && uframe >= 6) /* FSTN territory? */目前usb驱动还不支持分裂传输跨H帧边界,所以需要这样一个判断
      12. goto done;
      13. if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))//计算frame帧中的uframe微帧对应的时隙组,是否有足够多可用的同步带宽来容纳qh->usecs对应的带宽
      14. goto done;
      15. if (!qh->c_usecs) {//对于全速、低速设备而言,如果中断OUT传输,则qh->c_usecs为0,如果是中断IN传输,则不为0
      16. retval = 0;
      17. *c_maskp = 0;
      18. goto done;
      19. }
      20. #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
      21. if (tt_available (ehci, qh->period, qh->dev, frame, uframe,//该函数判断tt下行的fs、ls设备对应于frame帧的时隙组,是否有足够同步带宽来容纳qh->tt_usecs
      22. qh->tt_usecs)) {
      23. unsigned i;
      24. /* TODO : this may need FSTN for SSPLIT in uframe 5. */
      25. for (i=uframe+1; i<8 && i<uframe+4; i++)
      26. if (!check_period (ehci, frame, i,
      27. qh->period, qh->c_usecs))//判断对应的uframe微帧所对应的时隙组,是否有足够带宽发起csplit传输
      28. goto done;
      29. else
      30. mask |= 1 << i;
      31. retval = 0;
      32. *c_maskp = cpu_to_hc32(ehci, mask << 8);//设定在哪些帧发起csplit传输,固定为3个csplit传输。
      33. }
      34. #else
      35. /* Make sure this tt's buffer is also available for CSPLITs.
      36. * We pessimize a bit; probably the typical full speed case
      37. * doesn't need the second CSPLIT.
      38. *
      39. * NOTE: both SPLIT and CSPLIT could be checked in just
      40. * one smart pass...
      41. */
      42. mask = 0x03 << (uframe + qh->gap_uf);
      43. *c_maskp = cpu_to_hc32(ehci, mask << 8);
      44. mask |= 1 << uframe;
      45. if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
      46. if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
      47. qh->period, qh->c_usecs))
      48. goto done;
      49. if (!check_period (ehci, frame, uframe + qh->gap_uf,
      50. qh->period, qh->c_usecs))
      51. goto done;
      52. retval = 0;
      53. }
      54. #endif
      55. done:
      56. return retval;
      57. }

      确定qh->start的值后,就可以通过qh_link_periodic函数,将qh以qh->period为间隔,链接到对应的时隙中。
      struct ehci_hcd结构体中,有如下几个重要的成员:
      • ehci->periodic[i]: 对应hc使用的periodic frame list,里面存储的是指向下一个接口数据的开始物理地址
      • ehci->pshadow[i]:里面存储的是hc使用的接口数据对应的虚拟地址,与ehci->pshadow[i]的关系就是:
        • 他们共相同的物理空间,ehci->periodic[i]是接口数据的ehci->pshadow[i]的开始部分,是hc在执行传输时需要的接口数据单元
        • ehci->pshadow[i]中除去ehci->periodic[i]部分,还有剩余的私有部分,是用于软件来管理这些数据结构的。hc不会访问到。
        • ehci->periodic为硬件周期列表,共有1024项,供host constroller访问;ehci->pshadow是对应的软件结构列表,共hcd driver来访问。
          其实ehci->pshadow中前半部分,即为硬件表的内容,只是ehci->periodic种存的是物理地址,而ehci->pshadow
          使用的是物理地址对应的虚拟地址。

      1. /* periodic schedule slots have iso tds (normal or split) first, then a
      2. * sparse tree for active interrupt transfers.
      3. *
      4. * this just links in a qh; caller guarantees uframe masks are set right.
      5. * no FSTN support (yet; ehci 0.96+)
      6. */
      7. static int qh_link_periodic (struct ehci_hcd *ehci, struct ehci_qh *qh)
      8. {
      9. unsigned i;
      10. unsigned period = qh->period;
      11. dev_dbg (&qh->dev->dev,
      12. "link qh%d-%04x/%p start %d [%d/%d us]\n",
      13. period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
      14. & (QH_CMASK | QH_SMASK),
      15. qh, qh->start, qh->usecs, qh->c_usecs);
      16. /* high bandwidth, or otherwise every microframe */
      17. if (period == 0)
      18. period = 1;
      19. for (i = qh->start; i < ehci->periodic_size; i += period) {
      20. union ehci_shadow *prev = &ehci->pshadow[i];
      21. __hc32 *hw_p = &ehci->periodic[i];
      22. union ehci_shadow here = *prev;
      23. __hc32 type = 0;
      24. /* skip the iso nodes at list head */跳过前面的itd和sitd节点
      25. while (here.ptr) {
      26. type = Q_NEXT_TYPE(ehci, *hw_p);
      27. if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
      28. break;
      29. prev = periodic_next_shadow(ehci, prev, type);
      30. hw_p = shadow_next_periodic(ehci, &here, type);
      31. here = *prev;
      32. }
      33. /* sorting each branch by period (slow-->fast)//由于中断端点对应的qh在数据结构的组织上,根据poll rate从低到高连接在一起的。
      34. * enables sharing interior tree nodes//所以要根据他们的qh->period来确定合适的插入位置
      35. */
      36. while (here.ptr && qh != here.qh) {
      37. if (qh->period > here.qh->period)
      38. break;
      39. prev = &here.qh->qh_next;
      40. hw_p = &here.qh->hw->hw_next;
      41. here = *prev; //在这里需要特别注意的是:here不是指针,而prev是指针,prev指向的是列表的前一个,而here指向的列表的当前节点
      42. }
      43. /* link in this qh, unless some earlier pass did that */
      44. if (qh != here.qh) { // before insert: prev--->here ; after insert: prev-->qh--->here
      45. qh->qh_next = here;
      46. if (here.qh)
      47. qh->hw->hw_next = *hw_p;
      48. wmb ();
      49. prev->qh = qh;
      50. *hw_p = QH_NEXT (ehci, qh->qh_dma);
      51. }
      52. }
      53. qh->qh_state = QH_STATE_LINKED;
      54. qh->xacterrs = 0;
      55. qh_get (qh);
      56. /* update per-qh bandwidth for usbfs */
      57. ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
      58. ? ((qh->usecs + qh->c_usecs) / qh->period)
      59. : (qh->usecs * 8);
      60. /* maybe enable periodic schedule processing */
      61. return enable_periodic(ehci);
      62. }
      上面操作的示意图如下:
      上图中,需要强调的是:周期列表中,一个QH对应一个中断端点,QH/xx,"xx"为该中断端点的poll rate。

      2.3.3 for PIPE_ISOCHRONOUS urb enqueue过程(包括高速同步和分裂同步传输)

      struct ehci_iso_stream*stream  该结构用来将属于某个同步端点的所有sitds/itds组织在一起,通过stream->td_list列表。stream的作用类似于异步传输结构中的qh作用。因此stream也包含同步端点的一些静态内容,譬如:该同步端口所在设备的设备地址,该同步端口的端口号,该设备对应的hub的地址,hub的端口号,最大包长度。stream结构中,有如下几个重要的成员:
      • stream->usecs:表示传输该端口所允许的最大包所需要的带宽(用us单位计量,call HS_USECS_ISO())
      • stream->c_usecs:对应同步分裂传输的csplit传输阶段所需的带宽
      • stream->raw_mask:该成员存储了在哪些微帧需要发起ssplist和csplist传输,注意是相对于0uframe的,如果是从非0uframe开始,则整体左移。
      • stream->next_uframe: 对应该端点的下次链入interface schedule data structure在periodic list列表的什么地方
      • stream->splits:该成员是stream->raw_mask在左移uframe为后的值。(uframe为同步预算调度计算出来的对应的插入该同步传输的位置,0<=uframe<8
      • stream->interval:类似urb->interval的含义
      为什么全速设备在一个microframe的时间内,只能传输188个字节的数据?
      A microframe of time allows at most 187.5 raw bytes of signaling on a full-speed bus. In order to estimate when full-/low-speed transactions appear on a downstream bus, the host must calculate a best case fullspeed budget. This budget tracks in which microframes a full-/low-speed transaction appears. The best case full-speed budget assumes that 188 full-speed bytes occur in each microframe. Figure 11-60 shows how a1 ms frame subdivided into microframes of budget time. This estimate assumes that no bit stuffing occurs to lengthen the time required to move transactions over the bus.(摘自usb2.0 11.18.1)

      The maximum number of bytes in a 1 ms frame is calculated as:
      1157 maximum_periodic_bytes_per_frame = 12 Mb/s * 1 ms / 8 bits_per_byte *6 data_bits / 7 bit-stuffed_data_bits * 90% maximum_periodic_data_per_frame



      同步传输的enqueue流程图如下:


      该函数初始化 struct ehci_iso_stream *stream结构,重点需要关注stream结构中的如上红色高亮部分的成员的含义和设置。

      1. static void
      2. iso_stream_init (
      3. struct ehci_hcd *ehci,
      4. struct ehci_iso_stream *stream,
      5. struct usb_device *dev,
      6. int pipe,
      7. unsigned interval
      8. )
      9. {
      10. static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };//该数组表示以microframe的数目为index,相应的值为在哪些uframe slot需要发起start split传输
      11. u32 buf1;
      12. unsigned epnum, maxp;
      13. int is_input;
      14. long bandwidth;
      15. /*
      16. * this might be a "high bandwidth" highspeed endpoint,
      17. * as encoded in the ep descriptor's wMaxPacket field
      18. */
      19. epnum = usb_pipeendpoint (pipe);
      20. is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
      21. maxp = usb_maxpacket(dev, pipe, !is_input);
      22. if (is_input) {
      23. buf1 = (1 << 11);
      24. } else {
      25. buf1 = 0;
      26. }
      27. /* knows about ITD vs SITD */
      28. if (dev->speed == USB_SPEED_HIGH) {//高速同步传输
      29. unsigned multi = hb_mult(maxp);//陪乘数,表示在一个uframe中连续发起multi个IN/OUT传输
      30. stream->highspeed = 1;
      31. maxp = max_packet(maxp);
      32. buf1 |= maxp;
      33. maxp *= multi;
      34. stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
      35. stream->buf1 = cpu_to_hc32(ehci, buf1);
      36. stream->buf2 = cpu_to_hc32(ehci, multi);
      37. /* usbfs wants to report the average usecs per frame tied up
      38. * when transfers on this endpoint are scheduled ...
      39. */
      40. stream->usecs = HS_USECS_ISO (maxp);//同步带宽
      41. bandwidth = stream->usecs * 8;
      42. bandwidth /= interval;
      43. } else {//全速同步分裂传输
      44. u32 addr;
      45. int think_time;
      46. int hs_transfers;
      47. addr = dev->ttport << 24;
      48. if (!ehci_is_TDI(ehci)
      49. || (dev->tt->hub !=
      50. ehci_to_hcd(ehci)->self.root_hub))
      51. addr |= dev->tt->hub->devnum << 16;
      52. addr |= epnum << 8;
      53. addr |= dev->devnum;
      54. stream->usecs = HS_USECS_ISO (maxp);//同步带宽
      55. think_time = dev->tt ? dev->tt->think_time : 0;
      56. stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (//这个最大包长度在全速总线上,所占的总线带宽(以时间us计量)
      57. dev->speed, is_input, 1, maxp));
      58. hs_transfers = max (1u, (maxp + 187) / 188);//这个全速同步传输时间,跨越了几个uframe微帧,由于在一个微帧(125us)的时间里,全速同步传输最多可以传送188个字节。
      59. if (is_input) {// 同步端点IN传输的操作模式:SSplit+IN , CSplit+IN+DATA
      60. u32 tmp;
      61. addr |= 1 << 31;
      62. stream->c_usecs = stream->usecs;//同步IN传输csplist阶段所需的最大带宽
      63. stream->usecs = HS_USECS_ISO (1);//同步In传输ssplit阶段所需的最大带宽
      64. stream->raw_mask = 1;
      65. /* c-mask as specified in USB 2.0 11.18.4 3.c */该语句的含义,见下面的示意图
      66. tmp = (1 << (hs_transfers + 2)) - 1;
      67. stream->raw_mask |= tmp << (8 + 2);
      68. } else //同步端点OUT传输的操作模式:SSplist+OUT+DATA,没有csplist阶段,因为同步传输是不不可靠传输,不需要响应
      69. stream->raw_mask = smask_out [hs_transfers - 1];//对应那些位置需要发起ssplist out传输,如果数据包大于188byte,则需要多个ssplist
      70. bandwidth = stream->usecs + stream->c_usecs;
      71. bandwidth /= interval << 3;
      72. /* stream->splits gets created from raw_mask later */
      73. stream->address = cpu_to_hc32(ehci, addr);
      74. }
      75. stream->bandwidth = bandwidth;
      76. stream->udev = dev;
      77. stream->bEndpointAddress = is_input | epnum;
      78. stream->interval = interval;
      79. stream->maxp = maxp;
      80. }
      下图演示了周期传输的csplist和ssplist是如何设置的:


      struct ehci_iso_packet结构体用来描述一个同步端点在某个microframe(0<uframe<7)中的传输,对应itd结构体中的word0到word7中的某一个字。一个itd结构对应一个同步端点在某个frame帧(包含8个microframe)中的传输,而frame帧可以对应多个itd,即可以服务多个不同的同步端点(每个端点对应一个itd,在同一个周期列表元素中。)。
      1. /* description of one iso transaction (up to 3 KB data if highspeed) */
      2. struct ehci_iso_packet {
      3. /* These will be copied to iTD when scheduling */
      4. u64 bufp; /* itd->hw_bufp{,_hi}[pg] |= */
      5. __hc32 transaction; /* itd->hw_transaction[i] |= */
      6. u8 cross; /* buf crosses pages */
      7. /* for full speed OUT splits */
      8. u32 buf1;
      9. };
      struct ehci_iso_sched结构对应一个urb的传输,他是一个暂存结构,暂存这个urb所对应的itd中所需的信息;注意一个urb可能会跨多个itd结构。
      1. /* temporary schedule data for packets from iso urbs (both speeds)
      2. * each packet is one logical usb transaction to the device (not TT),
      3. * beginning at stream->next_uframe
      4. */
      5. struct ehci_iso_sched {
      6. struct list_head td_list;
      7. unsigned span;
      8. struct ehci_iso_packet packet [0];
      9. };

      urb->number_of_packets与itd的关系:一个poll rate为N的同步端点,一个urb包含了在uframe,uframe+N,uframe+2N三个时隙的同步传输buffer,则urb->number_of_packets就为3.当N大于8时,itd的数目就跟urb->number_of_packets的数目相同。而当N小于8时,则一个urb所需的itd的数据就小于urb->number_of_packets,因为一个itd是对应一个frame的,而在一个frame中,在N小于8时,就会有多个同步传输发生,即会消耗多个urb->iso_frame_desc[i](0<i<urb->number_of_packets)
      itd_urb_transaction 函数,就是根据urb的urb->number_of_packets和urb->interval来确定需要分配多少个itd结构体,并初始化他们,并挂在sched->td_list列表上。

      1. static int
      2. itd_urb_transaction (
      3. struct ehci_iso_stream *stream,
      4. struct ehci_hcd *ehci,
      5. struct urb *urb,
      6. gfp_t mem_flags
      7. )
      8. {
      9. struct ehci_itd *itd;
      10. dma_addr_t itd_dma;
      11. int i;
      12. unsigned num_itds;
      13. struct ehci_iso_sched *sched;
      14. unsigned long flags;
      15. sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
      16. if (unlikely (sched == NULL))
      17. return -ENOMEM;
      18. itd_sched_init(ehci, sched, stream, urb);
      19. if (urb->interval < 8)
      20. num_itds = 1 + (sched->span + 7) / 8;//计算一个urb需要多少个itd接口数据
      21. else
      22. num_itds = urb->number_of_packets;
      23. /* allocate/init ITDs */
      24. spin_lock_irqsave (&ehci->lock, flags);
      25. for (i = 0; i < num_itds; i++) {
      26. /* free_list.next might be cache-hot ... but maybe
      27. * the HC caches it too. avoid that issue for now.
      28. */
      29. /* prefer previously-allocated itds */
      30. if (likely (!list_empty(&stream->free_list))) {
      31. itd = list_entry (stream->free_list.prev,
      32. struct ehci_itd, itd_list);
      33. list_del (&itd->itd_list);
      34. itd_dma = itd->itd_dma;
      35. } else {
      36. spin_unlock_irqrestore (&ehci->lock, flags);
      37. itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
      38. &itd_dma);
      39. spin_lock_irqsave (&ehci->lock, flags);
      40. if (!itd) {
      41. iso_sched_free(stream, sched);
      42. spin_unlock_irqrestore(&ehci->lock, flags);
      43. return -ENOMEM;
      44. }
      45. }
      46. memset (itd, 0, sizeof *itd);
      47. itd->itd_dma = itd_dma;
      48. list_add (&itd->itd_list, &sched->td_list);
      49. }
      50. spin_unlock_irqrestore (&ehci->lock, flags);
      51. /* temporarily store schedule info in hcpriv */
      52. urb->hcpriv = sched;
      53. urb->error_count = 0;
      54. return 0;
      55. }


      下面这个图示意了urb和ehci_iso_stream, itd, iso_frame_desc等结构体之间的关系。


      iso_stream_schedule:(struct ehci_hcd *ehci, struct urb*urb, struct ehci_iso_stream*stream):该函数决定此次提交的urb将被放在周期微帧列表的什么位置,并将该位置保存到stream->next_uframe变量中,供以后使用。
      周期微帧列表: 是1024*8个微帧,每帧为125us, 所以上述0<next_uframe<1024*8
      1. /*
      2. * This scheduler plans almost as far into the future as it has actual
      3. * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
      4. * "as small as possible" to be cache-friendlier.) That limits the size
      5. * transfers you can stream reliably; avoid more than 64 msec per urb.
      6. * Also avoid queue depths of less than ehci's worst irq latency (affected
      7. * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
      8. * and other factors); or more than about 230 msec total (for portability,
      9. * given EHCI_TUNE_FLS and the slop). Or, write a smarter scheduler!
      10. */
      11. #define SCHEDULE_SLOP 80 /* microframes */
      12. static int
      13. iso_stream_schedule (
      14. struct ehci_hcd *ehci,
      15. struct urb *urb,
      16. struct ehci_iso_stream *stream
      17. )
      18. {
      19. u32 now, next, start, period, span;
      20. int status;
      21. unsigned mod = ehci->periodic_size << 3;
      22. struct ehci_iso_sched *sched = urb->hcpriv;
      23. period = urb->interval;
      24. span = sched->span;
      25. if (!stream->highspeed) {
      26. period <<= 3;
      27. span <<= 3;
      28. }
      29. if (span > mod - SCHEDULE_SLOP) {
      30. ehci_dbg (ehci, "iso request %p too long\n", urb);
      31. status = -EFBIG;
      32. goto fail;
      33. }
      34. now = ehci_read_frame_index(ehci) & (mod - 1);//当前hc在哪个周期列表元素中执行
      35. /* Typical case: reuse current schedule, stream is still active.
      36. * Hopefully there are no gaps from the host falling behind
      37. * (irq delays etc), but if there are we'll take the next
      38. * slot in the schedule, implicitly assuming URB_ISO_ASAP.
      39. */
      40. if (likely (!list_empty (&stream->td_list))) {//这个说明这个周期端点,之前已经被调度过了只要按对应的时隙间隔填充,不用再去判断对应的时隙组是否有足够可用的总线带宽,因为已经判断过了。
      41. u32 excess;
      42. /* For high speed devices, allow scheduling within the
      43. * isochronous scheduling threshold. For full speed devices
      44. * and Intel PCI-based controllers, don't (work around for
      45. * Intel ICH9 bug).
      46. */
      47. if (!stream->highspeed && ehci->fs_i_thresh)
      48. next = now + ehci->i_thresh;
      49. else
      50. next = now;
      51. /* Fell behind (by up to twice the slop amount)?
      52. * We decide based on the time of the last currently-scheduled
      53. * slot, not the time of the next available slot.
      54. */
      55. excess = (stream->next_uframe - period - next) & (mod - 1);
      56. if (excess >= mod - 2 * SCHEDULE_SLOP)
      57. start = next + excess - mod + period *
      58. DIV_ROUND_UP(mod - excess, period);//保证在对应的间隔为period的时隙slot中。
      59. else
      60. start = next + excess + period; //基于上次被调度的地方,而不是最近hc被访问的地方
      61. if (start - now >= mod) {
      62. ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
      63. urb, start - now - period, period,
      64. mod);
      65. status = -EFBIG;
      66. goto fail;
      67. }
      68. }
      69. /* need to schedule; when's the next (u)frame we could start?
      70. * this is bigger than ehci->i_thresh allows; scheduling itself
      71. * isn't free, the slop should handle reasonably slow cpus. it
      72. * can also help high bandwidth if the dma and irq loads don't
      73. * jump until after the queue is primed.
      74. */
      75. else {
      76. int done = 0;
      77. start = SCHEDULE_SLOP + (now & ~0x07);
      78. /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
      79. /* find a uframe slot with enough bandwidth.
      80. * Early uframes are more precious because full-speed
      81. * iso IN transfers can't use late uframes,
      82. * and therefore they should be allocated last.
      83. */
      84. next = start;
      85. start += period;
      86. do {
      87. start--;
      88. /* check schedule: enough space? */
      89. if (stream->highspeed) {
      90. if (itd_slot_ok(ehci, mod, start,
      91. stream->usecs, period))//详细见下面的解析
      92. done = 1;
      93. } else {
      94. if ((start % 8) >= 6)
      95. continue;
      96. if (sitd_slot_ok(ehci, mod, stream,
      97. start, sched, period))//详细见下面的解析
      98. done = 1;
      99. }
      100. } while (start > next && !done);
      101. /* no room in the schedule */
      102. if (!done) {
      103. ehci_dbg(ehci, "iso resched full %p (now %d max %d)\n",
      104. urb, now, now + mod);
      105. status = -ENOSPC;
      106. goto fail;
      107. }
      108. }
      109. /* Tried to schedule too far into the future? */
      110. if (unlikely(start - now + span - period
      111. >= mod - 2 * SCHEDULE_SLOP)) {
      112. ehci_dbg(ehci, "request %p would overflow (%d+%d >= %d)\n",
      113. urb, start - now, span - period,
      114. mod - 2 * SCHEDULE_SLOP);
      115. status = -EFBIG;
      116. goto fail;
      117. }
      118. stream->next_uframe = start & (mod - 1);//该值在新的itd或sitd被连入到这个周期列表后,会被更新(详见itd_link_urb和sitd_link_urb函数)
      119. /* report high speed start in uframes; full speed, in frames */
      120. urb->start_frame = stream->next_uframe;
      121. if (!stream->highspeed)
      122. urb->start_frame >>= 3;
      123. return 0;
      124. fail:
      125. iso_sched_free(stream, sched);
      126. urb->hcpriv = NULL;
      127. return status;
      128. }

      periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe): 用来计算某个帧(frame指定)中的某个微帧(uframe指定)目前已经占用的周期总线带宽
      参数的取值范围:1<frame<1024,  0<uframe<7
      对于全速设备,每个frame帧中的,同步带宽不能超过90%;而对于高速设备,每个微帧中,同步带宽不能超过微帧总线带宽的80%,而ehci->uframe_periodic_max变量就保存了在高速设备的一个微帧中,最多可以有多少时间的同步带宽可用(以us单位计量).

      1. /* how many of the uframe's 125 usecs are allocated? */
      2. static unsigned short
      3. periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
      4. {
      5. __hc32 *hw_p = &ehci->periodic [frame];
      6. union ehci_shadow *q = &ehci->pshadow [frame];
      7. unsigned usecs = 0;
      8. struct ehci_qh_hw *hw;
      9. while (q->ptr) {
      10. switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
      11. case Q_TYPE_QH: //该QH只能对应的就是中断端点,
      12. hw = q->qh->hw;
      13. /* is it in the S-mask? */
      14. if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))//对于高速中断设备,表示在S-mask对应的位置发起中断传输,对应的最大可能的带宽包存在qh->usecs变量中。
      15. usecs += q->qh->usecs;
      16. /* ... or C-mask? */
      17. if (hw->hw_info2 & cpu_to_hc32(ehci,
      18. 1 << (8 + uframe)))//对于高速中断设备而言,该域为0,而对于全速设备而言,该域就是csplist传输,对应的带宽
      19. usecs += q->qh->c_usecs;
      20. hw_p = &hw->hw_next;
      21. q = &q->qh->qh_next;
      22. break;
      23. // case Q_TYPE_FSTN:
      24. default:
      25. /* for "save place" FSTNs, count the relevant INTR
      26. * bandwidth from the previous frame
      27. */
      28. if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
      29. ehci_dbg (ehci, "ignoring FSTN cost ...\n");
      30. }
      31. hw_p = &q->fstn->hw_next;
      32. q = &q->fstn->fstn_next;
      33. break;
      34. case Q_TYPE_ITD:
      35. if (q->itd->hw_transaction[uframe])//高速同步端点,一个itd保存了8个微帧的传输,如果对应的微帧位置没有事务传输,则应该为0,如果有对应的传输,则最大可能的带宽保存在stream->usecs变量中
      36. usecs += q->itd->stream->usecs;
      37. hw_p = &q->itd->hw_next;
      38. q = &q->itd->itd_next;
      39. break;
      40. case Q_TYPE_SITD:
      41. /* is it in the S-mask? (count SPLIT, DATA) */
      42. if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
      43. 1 << uframe)) {
      44. if (q->sitd->hw_fullspeed_ep &
      45. cpu_to_hc32(ehci, 1<<31))
      46. usecs += q->sitd->stream->usecs;// 全速设备的,In同步端点传输,对应的start split传输,最大可能的带宽保存在stream->usecs里
      47. else /* worst case for OUT start-split */
      48. usecs += HS_USECS_ISO (188);//全速设备的OUT同步端点传输,只有start-split+data传输,没有cspllist传输,out传输所允许最大的包大小就是188个字节
      49. }
      50. /* ... C-mask? (count CSPLIT, DATA) */
      51. if (q->sitd->hw_uframe &
      52. cpu_to_hc32(ehci, 1 << (8 + uframe))) {
      53. /* worst case for IN complete-split */
      54. usecs += q->sitd->stream->c_usecs;//全速设备的IN同步端点,他的csplit传输,包含了data阶段,所允许的最大带宽保存在stream->usecs里。
      55. }
      56. hw_p = &q->sitd->hw_next;
      57. q = &q->sitd->sitd_next;
      58. break;
      59. }
      60. }
      61. #ifdef DEBUG
      62. if (usecs > ehci->uframe_periodic_max)
      63. ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
      64. frame * 8 + uframe, usecs);
      65. #endif
      66. return usecs;
      67. }

      itd_slot_ok函数确定在uframe>>3的大帧中的第uframe&0x7个微帧位置,预算调度一个带宽为usecs时长的传输,是否符合同步带宽的约束条件(即总得周期带宽不能超过整个微帧总线带宽的80%)
      1. static inline int
      2. itd_slot_ok (
      3. struct ehci_hcd *ehci,
      4. u32 mod, //mod即为1024*8
      5. u32 uframe,
      6. u8 usecs,
      7. u32 period //每隔多少个微帧发起一个同步传输
      8. )
      9. {
      10. uframe %= period;//uframe的范围为0...(period-1)
      11. do {
      12. /* can't commit more than uframe_periodic_max usec */
      13. if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
      14. > (ehci->uframe_periodic_max - usecs))
      15. return 0;
      16. /* we know urb->interval is 2^N uframes */
      17. uframe += period;
      18. } while (uframe < mod);//遍历整个周期帧列表,即有个1024个列表,而每个列表中的每项又包含了8个微帧的带宽分布情况
      19. return 1;
      20. }

      sitd_slot_ok函数的功能:确定在frame大帧中的第uframe微帧,预算一个分裂传输stream,是否ok?
      该函数的理解确实是比较艰辛的,我相信如果读者对这个函数已经彻底理解了,那他对分裂传输也就理解的差不多了。应该具备了自己创造的基础了。预算调度高速的同步传输和预算调度低速同步分裂传输相比,预算低速同步分裂传输的调度要复杂的多。
      为了把这个复杂的问题清楚的描述出来,我们分如下几个步骤:
      • 分裂传输,涉及到TT,那也就是说,会同时涉及高速周期传输和低速周期传输,那要往这个frame大帧上预算分配一个调度(用stream来表述这个调度,类似qh的功能,一个同步端点对应一个stream),则这个调度要同时满足两个约束:
        • 在高速总线带宽这边,要满足在对应的微帧中(125us),周期传输不能超过总线带宽的80%
        • 同时在低速总线带宽这边,也要满足在对应的大帧中(1000us即1ms),周期传输不能超过总线带宽的90%
      • 如果是OUT同步分裂传输,由于他只有start split+data阶段,没有csplit阶段,并且在data长度大于188字节时,会包含多个连续的start split+data阶段,就要保证每个split阶段对应的微帧的周期带宽都符合约束条件
      • 如果是IN同步分裂传输,他是由:start split , csplit +IN + data阶段组成,所以主要是检查在发起csplist传输的微帧,它的周期带宽是否符合周期带宽的约束,由于会在多个微帧发起这个cplit传输,所以要根据cmask位检查多个对应的微帧
      • stream->raw_mask是未移位的smask和cmask位掩码。在微帧uframe的位置确定后,就需要将stream->raw_mask集体向左移位uframe & 7位,即:stream->splits = stream->raw_mask << (uframe & 7)
      1. static inline int
      2. sitd_slot_ok (
      3. struct ehci_hcd *ehci,
      4. u32 mod,
      5. struct ehci_iso_stream *stream,
      6. u32 uframe,
      7. struct ehci_iso_sched *sched,
      8. u32 period_uframes
      9. )
      10. {
      11. u32 mask, tmp;
      12. u32 frame, uf;
      13. mask = stream->raw_mask << (uframe & 7);
      14. /* for IN, don't wrap CSPLIT into the next frame */
      15. if (mask & ~0xffff) //目前尚不支持分裂传输跨H frame边界
      16. return 0;
      17. /* this multi-pass logic is simple, but performance may
      18. * suffer when the schedule data isn't cached.
      19. */
      20. /* check bandwidth */
      21. uframe %= period_uframes;// 范围:0<uframe<(period_uframes-1),单位是微帧
      22. do {
      23. u32 max_used;
      24. frame = uframe >> 3;//计算对应的大帧
      25. uf = uframe & 7; //计算对应的微帧
      26. #ifdef CONFIG_USB_EHCI_TT_NEWSCHED
      27. /* The tt's fullspeed bus bandwidth must be available.
      28. * tt_available scheduling guarantees 10+% for control/bulk.
      29. */
      30. if (!tt_available (ehci, period_uframes << 3,
      31. stream->udev, frame, uf, stream->tt_usecs))//确定在对应的大帧是否有足够可用的低速周期带宽来发起这次分裂周期传输
      32. return 0;
      33. #else
      34. /* tt must be idle for start(s), any gap, and csplit.
      35. * assume scheduling slop leaves 10+% for control/bulk.
      36. */
      37. if (!tt_no_collision (ehci, period_uframes << 3,
      38. stream->udev, frame, mask))
      39. return 0;
      40. #endif
      41. /* check starts (OUT uses more than one) */
      42. max_used = ehci->uframe_periodic_max - stream->usecs;
      43. for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {//如果是OUT分裂传输,我们要确定在连续的这几个分裂OUT周期传输所对应的微帧是否有足够可用的周期带宽
      44. if (periodic_usecs (ehci, frame, uf) > max_used)
      45. return 0;
      46. }
      47. /* for IN, check CSPLIT */
      48. if (stream->c_usecs) { //如果是IN分裂传输,我们要判断,在多个csplit传输对应的微帧是否都有足够可用的周期带宽
      49. uf = uframe & 7;
      50. max_used = ehci->uframe_periodic_max - stream->c_usecs;
      51. do {
      52. tmp = 1 << uf;
      53. tmp <<= 8;
      54. if ((stream->raw_mask & tmp) == 0)
      55. continue;
      56. if (periodic_usecs (ehci, frame, uf)
      57. > max_used)
      58. return 0;
      59. } while (++uf < 8);
      60. }
      61. /* we know urb->interval is 2^N uframes */
      62. uframe += period_uframes;
      63. } while (uframe < mod);
      64. stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));//保存移位后的smask和cmask
      65. return 1;
      66. }

      tt_available函数用来确定TT在frame/uframe(第frame(0<frame<1024)个大帧中的第uframe(0<uframe<8)个微帧位置)的微帧位置,发起一个总线时长为usecs的同步传输,是否违反了全速设备的总线带宽约束(即不能超过全速总线带宽的90%)。
      1. /*
      2. * Return true if the device's tt's downstream bus is available for a
      3. * periodic transfer of the specified length (usecs), starting at the
      4. * specified frame/uframe. Note that (as summarized in section 11.19
      5. * of the usb 2.0 spec) TTs can buffer multiple transactions for each
      6. * uframe.
      7. *
      8. * The uframe parameter is when the fullspeed/lowspeed transfer
      9. * should be executed in "B-frame" terms, which is the same as the
      10. * highspeed ssplit's uframe (which is in "H-frame" terms). For example
      11. * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
      12. * See the EHCI spec sec 4.5 and fig 4.7.
      13. *
      14. * This checks if the full/lowspeed bus, at the specified starting uframe,
      15. * has the specified bandwidth available, according to rules listed
      16. * in USB 2.0 spec section 11.18.1 fig 11-60.
      17. *
      18. * This does not check if the transfer would exceed the max ssplit
      19. * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
      20. * since proper scheduling limits ssplits to less than 16 per uframe.
      21. */
      22. static int tt_available (
      23. struct ehci_hcd *ehci,
      24. unsigned period,
      25. struct usb_device *dev,
      26. unsigned frame,
      27. unsigned uframe,
      28. u16 usecs
      29. )
      30. {
      31. if ((period == 0) || (uframe >= 7)) /* error */
      32. return 0;
      33. for (; frame < ehci->periodic_size; frame += period) {
      34. unsigned short tt_usecs[8];
      35. periodic_tt_usecs (ehci, dev, frame, tt_usecs);//确定在frame大帧中(对应1000us),有多少带宽已经被全速周期传输占用了,并将他们记录在tt_usecs数组中。
      36. ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
      37. " schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
      38. frame, usecs, uframe,
      39. tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
      40. tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);
      41. if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
      42. ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
      43. frame, uframe);
      44. return 0;
      45. }
      46. /* special case for isoc transfers larger than 125us:
      47. * the first and each subsequent fully used uframe
      48. * must be empty, so as to not illegally delay
      49. * already scheduled transactions
      50. */
      51. if (125 < usecs) {//对于那些带宽大于125us的周期传输,我们要求第一个和随后的几个微帧都必须是空的,不然当前的周期传输就会被非法的延迟。
      52. int ufs = (usecs / 125);
      53. int i;
      54. for (i = uframe; i < (uframe + ufs) && i < 8; i++)
      55. if (0 < tt_usecs[i]) {
      56. ehci_vdbg(ehci,
      57. "multi-uframe xfer can't fit "
      58. "in frame %d uframe %d\n",
      59. frame, i);
      60. return 0;
      61. }
      62. }
      63. tt_usecs[uframe] += usecs;
      64. carryover_tt_bandwidth(tt_usecs);
      65. /* fail if the carryover pushed bw past the last uframe's limit */
      66. if (max_tt_usecs[7] < tt_usecs[7]) {
      67. ehci_vdbg(ehci,
      68. "tt unavailable usecs %d frame %d uframe %d\n",
      69. usecs, frame, uframe);
      70. return 0;
      71. }
      72. }
      73. return 1;
      74. }
      下面这个数组,是表示周期传输在全速总线带宽的约束下,在8个微帧中的最佳总线带宽分配时间。
      1. static const unsigned char
      2. max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 30, 0 };

      periodic_tt_usecs函数用来计算目前这个大帧中(刚好对应1000us=1ms),TT设备中有多少全速、低速周期传输已经被占用了。由于在高速总线下,只可能存在TT类型的全速和低速设备,所以我们统计全速设备在当前大帧中的总线带宽,我们只需统计中断分裂传输和同步分裂传输。而每个全速、低速设备的中断端点所占的最大总线带宽保存在qh->tt_usecs中,而同步高速端点所占的最大总线带宽保存在stream->tt_usecs中。
      如下面函数开头的英文注释所说,低速设备是没有微帧的概念的,所以他们的一个传输是可以跨微帧的边界的。

      另外需要注意的是:对于同一个全速、低速中断端点而言,在一个大帧中,最多只有一次传输(即qh->smask只可能有一个bit位为1),因为他们的最低分辨率就是1ms,但对于同一个高速中断端点,则不然,他可以在一个大帧中,最多可以发起8个中断传输,即一个微帧一次。

      为了加深对下面函数的理解,我们可以假设这样一个全速设备:它由1个同步IN端点,1个同步OUT端点,1个中断IN端点,1个中断OUT端点,总计4个端点构成。则他们在周期列表frame帧中,对应的列表构成如下:
      sitd(IN,smask:0000 0001)-->sitd(OUT,smask:0000 0010)-->qh(IN,smask:0001 0000)--->qh(OUT,smask:0001 0000)
      1. /* How many of the tt's periodic downstream 1000 usecs are allocated?
      2. *
      3. * While this measures the bandwidth in terms of usecs/uframe,
      4. * the low/fullspeed bus has no notion of uframes, so any particular
      5. * low/fullspeed transfer can "carry over" from one uframe to the next,
      6. * since the TT just performs downstream transfers in sequence.
      7. *
      8. * For example two separate 100 usec transfers can start in the same uframe,
      9. * and the second one would "carry over" 75 usecs into the next uframe.
      10. */
      11. static void
      12. periodic_tt_usecs (
      13. struct ehci_hcd *ehci,
      14. struct usb_device *dev,
      15. unsigned frame,
      16. unsigned short tt_usecs[8]
      17. )
      18. {
      19. __hc32 *hw_p = &ehci->periodic [frame];
      20. union ehci_shadow *q = &ehci->pshadow [frame];
      21. unsigned char uf;
      22. memset(tt_usecs, 0, 16);
      23. while (q->ptr) {
      24. switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
      25. case Q_TYPE_ITD:
      26. hw_p = &q->itd->hw_next;
      27. q = &q->itd->itd_next;
      28. continue;
      29. case Q_TYPE_QH:
      30. if (same_tt(dev, q->qh->dev)) {
      31. uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);//通过查看s-mask位,全速、低速中断传输将在哪个微帧被发起
      32. tt_usecs[uf] += q->qh->tt_usecs;//对应的全速、低速中断传输的最大带宽
      33. }
      34. hw_p = &q->qh->hw->hw_next;
      35. q = &q->qh->qh_next;
      36. continue;
      37. case Q_TYPE_SITD:
      38. if (same_tt(dev, q->sitd->urb->dev)) {
      39. uf = tt_start_uframe(ehci, q->sitd->hw_uframe);//通过查看s-mask位,全速、低速同步传输在哪个微帧被发起
      40. tt_usecs[uf] += q->sitd->stream->tt_usecs;//低速同步带宽的最大带宽
      41. }
      42. hw_p = &q->sitd->hw_next;
      43. q = &q->sitd->sitd_next;
      44. continue;
      45. // case Q_TYPE_FSTN:
      46. default:
      47. ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
      48. frame);
      49. hw_p = &q->fstn->hw_next;
      50. q = &q->fstn->fstn_next;
      51. }
      52. }
      53. carryover_tt_bandwidth(tt_usecs);
      54. if (max_tt_usecs[7] < tt_usecs[7])
      55. ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
      56. frame, tt_usecs[7] - max_tt_usecs[7]);
      57. }

      itd_link_urb函数: 将urb对应的itd链接到硬件对应的周期列表中
      1. /* fit urb's itds into the selected schedule slot; activate as needed */
      2. static int
      3. itd_link_urb (
      4. struct ehci_hcd *ehci,
      5. struct urb *urb,
      6. unsigned mod,
      7. struct ehci_iso_stream *stream
      8. )
      9. {
      10. int packet;
      11. unsigned next_uframe, uframe, frame;
      12. struct ehci_iso_sched *iso_sched = urb->hcpriv;
      13. struct ehci_itd *itd;
      14. next_uframe = stream->next_uframe & (mod - 1);//itd被连入的位置
      15. if (unlikely (list_empty(&stream->td_list))) {
      16. ehci_to_hcd(ehci)->self.bandwidth_allocated
      17. += stream->bandwidth;
      18. ehci_vdbg (ehci,
      19. "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
      20. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
      21. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
      22. urb->interval,
      23. next_uframe >> 3, next_uframe & 0x7);
      24. }
      25. if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
      26. if (ehci->amd_pll_fix == 1)
      27. usb_amd_quirk_pll_disable();
      28. }
      29. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
      30. /* fill iTDs uframe by uframe */
      31. for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {//注意一个itd可能包含多个struct ehci_iso_packet包,
      32. if (itd == NULL) {
      33. /* ASSERT: we have all necessary itds */
      34. // BUG_ON (list_empty (&iso_sched->td_list));
      35. /* ASSERT: no itds for this endpoint in this uframe */
      36. itd = list_entry (iso_sched->td_list.next,
      37. struct ehci_itd, itd_list); //iso_sched结构对应一个urb(是一一对应的),里面保存了用于填充itd的成员,iso_sched结构可以对应多个itd成员(1对多)。
      38. list_move_tail (&itd->itd_list, &stream->td_list);//将这个itd连入到stream->td_list列表中
      39. itd->stream = iso_stream_get (stream);
      40. itd->urb = urb;
      41. itd_init (ehci, stream, itd);//初始化itd,主要是包含一些同步端点的信息
      42. }
      43. uframe = next_uframe & 0x07;
      44. frame = next_uframe >> 3;
      45. itd_patch(ehci, itd, iso_sched, packet, uframe);//初始化同步端点在uframe微帧位置对应的传输描述,包括:itd->hw_transaction,pg,itd->hw_bufp
      46. next_uframe += stream->interval;//跳到下一个填充微帧的位置
      47. next_uframe &= mod - 1;
      48. packet++;
      49. /* link completed itds into the schedule */
      50. if (((next_uframe >> 3) != frame)
      51. || packet == urb->number_of_packets) {//如果越过了itd结构对应的大帧frame,则这个itd连入到周期调度列表中
      52. itd_link(ehci, frame & (ehci->periodic_size - 1), itd);//详细见下面的描述
      53. itd = NULL;
      54. }
      55. }
      56. stream->next_uframe = next_uframe;//用于下次调度的插入位置
      57. /* don't need that schedule data any more */
      58. iso_sched_free (stream, iso_sched);
      59. urb->hcpriv = NULL;
      60. timer_action (ehci, TIMER_IO_WATCHDOG);
      61. return enable_periodic(ehci);
      62. }

      该函数将itd链入到frame参数(0<frame<1024)指定的周期列表中。
      1. static inline void
      2. itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
      3. {
      4. union ehci_shadow *prev = &ehci->pshadow[frame];//取得这个周期调度列表对应的软件部分
      5. __hc32 *hw_p = &ehci->periodic[frame];//取得这个周期调度列表对应的硬件部分
      6. union ehci_shadow here = *prev;
      7. __hc32 type = 0;
      8. /* skip any iso nodes which might belong to previous microframes */ //注意要跳过前面已经链入的所有同步节点。因为可以肯定的是,他们都是在当前要连入的微帧之前
      9. while (here.ptr) {
      10. type = Q_NEXT_TYPE(ehci, *hw_p);
      11. if (type == cpu_to_hc32(ehci, Q_TYPE_QH))//已连入的同步节点之后,中断节点之前: itd--itd--itd(new added node)---qh---qh
      12. break;
      13. prev = periodic_next_shadow(ehci, prev, type);
      14. hw_p = shadow_next_periodic(ehci, &here, type);
      15. here = *prev;
      16. }
      17. itd->itd_next = here;//操作如下:itd--itd(prev)--itd(new added node)--qh(here)
      18. itd->hw_next = *hw_p;
      19. prev->itd = itd;
      20. itd->frame = frame;
      21. wmb ();
      22. *hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
      23. }

      该函数的功能就是将urb对应的sitds插入到指定的周期调度时隙中。其中这个urb对应sitd数目等于urb->number_of_packets,即sitd跟ehci_iso_packet结构是一一对应的。对于同一个端点,sitd跟大帧是一一对应的,即一个sitd对应一个大帧。而高速同步端点,一个itd可能对应多个ehci_iso_packet结构
      1. /* fit urb's sitds into the selected schedule slot; activate as needed */
      2. static int
      3. sitd_link_urb (
      4. struct ehci_hcd *ehci,
      5. struct urb *urb,
      6. unsigned mod,
      7. struct ehci_iso_stream *stream
      8. )
      9. {
      10. int packet;
      11. unsigned next_uframe;
      12. struct ehci_iso_sched *sched = urb->hcpriv;
      13. struct ehci_sitd *sitd;
      14. next_uframe = stream->next_uframe; //该sitds列表插入的开始位置
      15. if (list_empty(&stream->td_list)) {
      16. /* usbfs ignores TT bandwidth */
      17. ehci_to_hcd(ehci)->self.bandwidth_allocated
      18. += stream->bandwidth;
      19. ehci_vdbg (ehci,
      20. "sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
      21. urb->dev->devpath, stream->bEndpointAddress & 0x0f,
      22. (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
      23. (next_uframe >> 3) & (ehci->periodic_size - 1),
      24. stream->interval, hc32_to_cpu(ehci, stream->splits));
      25. }
      26. if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
      27. if (ehci->amd_pll_fix == 1)
      28. usb_amd_quirk_pll_disable();
      29. }
      30. ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;
      31. /* fill sITDs frame by frame *///将urb中的数据填充到对应的sitd结构体中。
      32. for (packet = 0, sitd = NULL;
      33. packet < urb->number_of_packets;
      34. packet++) {//需要注意的是:
      35. /* ASSERT: we have all necessary sitds */
      36. BUG_ON (list_empty (&sched->td_list));
      37. /* ASSERT: no itds for this endpoint in this frame */
      38. sitd = list_entry (sched->td_list.next,
      39. struct ehci_sitd, sitd_list);
      40. list_move_tail (&sitd->sitd_list, &stream->td_list);
      41. sitd->stream = iso_stream_get (stream);
      42. sitd->urb = urb;
      43. sitd_patch(ehci, stream, sitd, sched, packet);//填充sitd结构
      44. sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),//将该sitd连入到周期帧列表中
      45. sitd);
      46. next_uframe += stream->interval << 3;//指向下次调度插入的地方
      47. }
      48. stream->next_uframe = next_uframe & (mod - 1);
      49. /* don't need that schedule data any more */
      50. iso_sched_free (stream, sched);
      51. urb->hcpriv = NULL;
      52. timer_action (ehci, TIMER_IO_WATCHDOG);
      53. return enable_periodic(ehci);

      2.4 分裂传输过程

      TT 采用流水的方式处理周期型的分离传输,一次周期型的分离传输在 TT 中共有四种状态:New、Pending、Ready 以及 Old。一次分离传输依次经历这四种状态。其中 New 态为这次分离传输在 TT 中建立的状态,是一个暂态、最长不能超过 1 个微帧的时间; Pending为等待传输完成的状态,最长不能超过 4 个微帧;Ready 为传输已经完成,等待 Host 取回结果的状态,最长不能超过 2 个微帧;Old 表示传输已经全部完成,TT 中该传输所占用的Buffer 可以重新利用。

      由于速度不匹配,在一个微帧里,最多可以在全速总线上传输 188 字节的数据,在传输的数据量较大的情况下,例如同步传输的最大包长度为 1023 字节,如果等到整个包传完再响应 HOST 的 C-Split,不但要求 TT 有更多的 Buffer,并且会使 HOST 浪费较多的时间在等待数据传输完成。因此 TT 采用了如下的处理方式:

      上行方向上,即 IN 型的传输:无论何时,只要 TT 收到超过 2 个字节的数据,就响应HOST 的 C-Split,向 HOST 发回数据,并在 PID 中以 MDATA,DATA1/0 表明还有没有未传完的数据。如果还有数据,则以 MDATA 发送,否则以 DATA1/0 发送。 HOST 在收到 MDATA后,应继续在下一个微帧里发起 C-Split 传输,向 HUB 请求数据,知道收到一个 DATA0/1
      的数据包。

      下行方向上,即 OUT 型的传输:HOST 把一个大的数据包拆成最大 188 字节的若干个小包,在连续的多个微帧里用 S-Split 向 TT 发送数据

      针对iso的同步传输,如下图所示,B:表示begin;M:表示中间数据;E:表示最后的数据;N:表示nyet;0:表示data0,即最后一个数据包。


      2.4.1  同步分裂传输的过程

      OUT情况:

      OUT 型的同步分离传输只有 S-Split 阶段,而没有 C-Split 阶段,因为同步传输是不可靠的传输, HOST不需要确认传输是否成功完成。

      IN情况:
      IN 型的同步分离传输对应的数据阶段在C-Split 阶段,并且数据阶段包含mdata、data1、data0类型的数据。

      2.4.2  控制/中断分裂传输的过程

      IN的中断、控制分裂传输:


      OUT的中断、控制分裂传输:


      2.4.3 分裂传输时的H frame与B frame




      2.5 urb的重要成员域

      • urb->setup_dma/urb->setup_packet
      • urb->transfer_buffer_length/urb->transfer_buffer
      • urb->transfer_flags    
        • URB_SHORT_NOT_OK:report short reads as errors
        • URB_ZERO_PACKET:Finish bulk OUT with short packet
        • URB_NO_INTERRUPT:HINT: no non-error interrupt
        • URB_NO_TRANSFER_DMA_MAP: urb->transfer_dma valid on submit
      • urb->interval:针对周期传输由意义,表示每隔多少个uframe发起一次中断或同步传输
      • urb->number_of_packets:该字段主要用来同个一个urb来提交多个同步传输,每个同步传输对应一个微帧,这些微帧之间的间隔为urb->interval
      • urb->iso_frame_desc[i]:0<i<(urb->number_of_packets),每个usb_iso_packet_descriptor元素描述一个赌赢的同步传输所包含的数据buffer(开始地址和长度)

      3 参考资料

      1: Ehci_spec_for_usb.pdf
      2: usb2.0 spec
      3: kernel3.4.5 source code (kernel3.4.5/trunk/drivers/usb/)


      声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/247006
      推荐阅读
      相关标签
        

      闽ICP备14008679号