usb.c
上传用户:qiulin1960
上传日期:2013-10-16
资源大小:2844k
文件大小:35k
源码类别:

Windows CE

开发平台:

Windows_Unix

  1. //
  2. // Copyright (c) Microsoft Corporation.  All rights reserved.
  3. //
  4. //
  5. // Use of this source code is subject to the terms of the Microsoft end-user
  6. // license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
  7. // If you did not accept the terms of the EULA, you are not authorized to use
  8. // this source code. For a copy of the EULA, please see the LICENSE.RTF on your
  9. // install media.
  10. //
  11. #include <windows.h>
  12. #include <halether.h>
  13. #include <s2440.h>
  14. // #include <S3C2440EVBD.h>
  15. #include "loader.h"
  16. #include <drv_glob.h>
  17. #define USB_BASE 0xB1200140
  18. volatile struct udcreg  * pUSBCtrlAddr;
  19. int g_DMAbufIndex = 0;
  20. int debugM = 0;
  21. volatile int DMADone = 0;
  22. #define REAL_PHYSICAL_ADDR_EP3_FIFO (0x520001CC) //Endpoint 3 FIFO
  23. ULONG realPhysicalAddr_UsbdRxBuf=0;
  24. //OUT_CSR2
  25. #define EPO_OUT_DMA_INT_MASK (1<<5)
  26. #define EPO_ISO   (1<<6)
  27. #define EPO_BULK   (0<<6)
  28. #define EPO_AUTO_CLR (1<<7)
  29. //USB DMA control register
  30. #define UDMA_IN_RUN_OB (1<<7)
  31. #define UDMA_IGNORE_TTC (1<<7)
  32. #define UDMA_DEMAND_MODE (1<<3)
  33. #define UDMA_OUT_RUN_OB (1<<2)
  34. #define UDMA_OUT_DMA_RUN (1<<2)
  35. #define UDMA_IN_DMA_RUN (1<<1)
  36. #define UDMA_DMA_MODE_EN (1<<0)
  37. #define pISR (*(unsigned *)(0x30000000+0x18)) // Virtual Address 0x0 is mapped to 0x30000000, ISR Address is VA 0x18
  38. #define U8 unsigned char
  39. #define U32 unsigned int
  40. #define BUFARRAYSIZE 64
  41. volatile unsigned char downArr[BUFARRAYSIZE];
  42. volatile unsigned char *downPt;
  43. #define DMABUFFER 0x32000000
  44. volatile unsigned int downPtIndex = DMABUFFER;
  45. volatile unsigned int readPtIndex = DMABUFFER;
  46. #define BULK_PKT_SIZE 64
  47. #define EP0_PKT_SIZE 8
  48. #define EP1_PKT_SIZE BULK_PKT_SIZE
  49. #define EP3_PKT_SIZE BULK_PKT_SIZE
  50. U8 ep1Buf[EP1_PKT_SIZE];
  51. int transferIndex=0;
  52. void Isr_Init(void);
  53. void IsrUsbd(unsigned int val);
  54. void IsrHandler(void);
  55. void Ep3Handler(void);
  56. void ConfigEp3DmaMode(U32 bufAddr,U32 count);
  57. void DMA2Handler(void);
  58. #define rEP3_DMA_TTC_L     (*(volatile unsigned char *)0x5200024c) //EP3 DMA total Tx counter
  59. #define rEP3_DMA_TTC_M     (*(volatile unsigned char *)0x52000250)
  60. #define rEP3_DMA_TTC_H     (*(volatile unsigned char *)0x52000254)
  61. #define rEP3_DMA_TTC (pUSBCtrlAddr->EP3DTL.ep3_ttl_l+(pUSBCtrlAddr->EP3DTM.ep3_ttl_m<<8)+(pUSBCtrlAddr->EP3DTH.ep3_ttl_h<<16))
  62. #define SKIP 1
  63. #define DOIT 0
  64. // Standard bmRequestType (Recipient)
  65. #define DEVICE_RECIPIENT            (0)
  66. #define INTERFACE_RECIPIENT         (1)
  67. #define ENDPOINT_RECIPIENT          (2)
  68. #define OTHER_RECIPIENT             (3)
  69. #define EP0_STATE_INIT (0)  
  70. #define EP0_STATE_GD_DEV_0 (10)  //10-10=0 
  71. #define EP0_STATE_GD_DEV_1 (11)  //11-10=1
  72. #define EP0_STATE_GD_DEV_2 (12)  //12-10=2
  73. #define EP0_STATE_GD_CFG_0 (20)
  74. #define EP0_STATE_GD_CFG_1 (21)
  75. #define EP0_STATE_GD_CFG_2 (22)
  76. #define EP0_STATE_GD_CFG_3 (23)
  77. #define EP0_STATE_GD_CFG_4 (24)
  78. #define EP0_STATE_GD_CFG_ONLY_0 (40)
  79. #define EP0_STATE_GD_CFG_ONLY_1 (41)
  80. #define EP0_STATE_GD_IF_ONLY_0 (42)
  81. #define EP0_STATE_GD_IF_ONLY_1 (43)
  82. #define EP0_STATE_GD_EP0_ONLY_0 (44)
  83. #define EP0_STATE_GD_EP1_ONLY_0 (45)
  84. #define EP0_INTERFACE_GET (46)
  85. #define EP0_STATE_GD_STR_I0 (30)  
  86. #define EP0_STATE_GD_STR_I1 (31)  
  87. #define EP0_STATE_GD_STR_I2 (32)  
  88. #define EP0_CONFIG_SET (33)
  89. #define EP0_GET_STATUS0 (35)
  90. #define EP0_GET_STATUS1 (36)
  91. #define EP0_GET_STATUS2 (37)
  92. #define EP0_GET_STATUS3 (38)
  93. #define EP0_GET_STATUS4 (39)
  94. #define CLR_EP0_OUT_PKT_RDY() pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1; pUSBCtrlAddr->EP0ICSR1.sse_ = 0;
  95. #define CLR_EP0_OUTPKTRDY_DATAEND() pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1; pUSBCtrlAddr->EP0ICSR1.de_ff = 1;; pUSBCtrlAddr->EP0ICSR1.sse_ = 0;
  96. #define SET_EP0_IN_PKT_RDY()  pUSBCtrlAddr->EP0ICSR1.ipr_ = 1; pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 0; pUSBCtrlAddr->EP0ICSR1.sse_ = 0;
  97. #define SET_EP0_INPKTRDY_DATAEND()  pUSBCtrlAddr->EP0ICSR1.ipr_ = 1; pUSBCtrlAddr->EP0ICSR1.de_ff = 1; pUSBCtrlAddr->EP0ICSR1.sse_ = 0; pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 0;
  98. #define CLR_EP0_SETUP_END()  pUSBCtrlAddr->EP0ICSR1.sse_ = 1; pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 0;
  99. #define CLR_EP0_SENT_STALL()  pUSBCtrlAddr->EP0ICSR1.sts_ur = 0; pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 0; pUSBCtrlAddr->EP0ICSR1.sse_ = 0;
  100. #define FLUSH_EP0_FIFO()  {while(pUSBCtrlAddr->OFCR1.out_cnt_low) pUSBCtrlAddr->EP0F.fifo_data;}
  101. #define CLR_EP3_OUT_PKT_READY() pUSBCtrlAddr->OCSR1.fifo_flush = 0; pUSBCtrlAddr->OCSR1.send_stall = 0; pUSBCtrlAddr->OCSR1.clr_data_tog = 0; pUSBCtrlAddr->OCSR1.out_pkt_rdy = 0;
  102. #define CLR_EP3_SENT_STALL() pUSBCtrlAddr->OCSR1.fifo_flush = 0; pUSBCtrlAddr->OCSR1.send_stall = 0; pUSBCtrlAddr->OCSR1.clr_data_tog = 0; pUSBCtrlAddr->OCSR1.sent_stall = 0;
  103. volatile U8 Rwuen;
  104. volatile U8 Selfpwr=TRUE;
  105. U32 ep0State;
  106. struct USB_SETUP_DATA{
  107. U8 bmRequestType;    
  108. U8 bRequest;         
  109. U8 bValueL;          
  110. U8 bValueH;          
  111. U8 bIndexL;          
  112. U8 bIndexH;          
  113. U8 bLengthL;         
  114. U8 bLengthH;         
  115. };
  116. struct USB_DEVICE_DESCRIPTOR{
  117. U8 bLength;    
  118. U8 bDescriptorType;         
  119. U8 bcdUSBL;
  120. U8 bcdUSBH;
  121. U8 bDeviceClass;
  122. U8 bDeviceSubClass;
  123. U8 bDeviceProtocol;
  124. U8 bMaxPacketSize0;
  125. U8 idVendorL;
  126. U8 idVendorH;
  127. U8 idProductL;
  128. U8 idProductH;
  129. U8 bcdDeviceL;
  130. U8 bcdDeviceH;
  131. U8 iManufacturer;
  132. U8 iProduct;
  133. U8 iSerialNumber;
  134. U8 bNumConfigurations;
  135. };
  136. struct USB_CONFIGURATION_DESCRIPTOR{
  137. U8 bLength;    
  138. U8 bDescriptorType;         
  139. U8 wTotalLengthL;
  140. U8 wTotalLengthH;
  141. U8 bNumInterfaces;
  142. U8 bConfigurationValue;
  143. U8 iConfiguration;
  144. U8 bmAttributes;
  145. U8 maxPower;          
  146. };
  147. struct USB_INTERFACE_DESCRIPTOR{
  148. U8 bLength;    
  149. U8 bDescriptorType;         
  150. U8 bInterfaceNumber;
  151. U8 bAlternateSetting;
  152. U8 bNumEndpoints;
  153. U8 bInterfaceClass;
  154. U8 bInterfaceSubClass;
  155. U8 bInterfaceProtocol;
  156. U8 iInterface;
  157. };
  158. struct USB_ENDPOINT_DESCRIPTOR{
  159. U8 bLength;    
  160. U8 bDescriptorType;         
  161. U8 bEndpointAddress;
  162. U8 bmAttributes;
  163. U8 wMaxPacketSizeL;
  164. U8 wMaxPacketSizeH;
  165. U8 bInterval;
  166. };
  167. struct USB_CONFIGURATION_SET{
  168. U8 ConfigurationValue;
  169. };
  170. struct USB_GET_STATUS{
  171. U8 Device;
  172. U8 Interface;
  173. U8 Endpoint0;
  174. U8 Endpoint1;
  175. U8 Endpoint3;
  176. };
  177. struct USB_INTERFACE_GET{
  178. U8 AlternateSetting;
  179. };
  180. struct USB_SETUP_DATA descSetup;
  181. struct USB_DEVICE_DESCRIPTOR descDev;
  182. struct USB_CONFIGURATION_DESCRIPTOR descConf;
  183. struct USB_INTERFACE_DESCRIPTOR descIf;
  184. struct USB_ENDPOINT_DESCRIPTOR descEndpt0;
  185. struct USB_ENDPOINT_DESCRIPTOR descEndpt1;
  186. struct USB_CONFIGURATION_SET ConfigSet;
  187. struct USB_INTERFACE_GET InterfaceGet;
  188. struct USB_GET_STATUS StatusGet;   //={0,0,0,0,0};
  189. // Descriptor Types
  190. #define USB_DEVICE_TYPE                 (1)
  191. #define CONFIGURATION_TYPE          (2)
  192. #define STRING_TYPE                 (3)
  193. #define INTERFACE_TYPE              (4)
  194. #define ENDPOINT_TYPE               (5)
  195. //string descriptor
  196. #define LANGID_US_L      (0x09)  
  197. #define LANGID_US_H      (0x04)
  198. // Standard Request Codes 
  199. #define GET_STATUS                  (0)
  200. #define CLEAR_FEATURE               (1)
  201. #define SET_FEATURE                 (3)
  202. #define SET_ADDRESS                 (5)
  203. #define GET_DESCRIPTOR              (6)
  204. #define SET_DESCRIPTOR              (7)
  205. #define GET_CONFIGURATION           (8)
  206. #define SET_CONFIGURATION           (9)
  207. #define GET_INTERFACE               (10)
  208. #define SET_INTERFACE               (11)
  209. #define SYNCH_FRAME                 (12)
  210. //configuration descriptor: bmAttributes 
  211. #define CONF_ATTR_DEFAULT     (0x80) //Spec 1.0 it was BUSPOWERED bit.
  212. #define CONF_ATTR_REMOTE_WAKEUP     (0x20)
  213. #define CONF_ATTR_SELFPOWERED       (0x40)
  214. //endpoint descriptor
  215. #define EP_ADDR_IN     (0x80)
  216. #define EP_ADDR_OUT     (0x00)
  217. #define EP_ATTR_CONTROL     (0x0)
  218. #define EP_ATTR_ISOCHRONOUS     (0x1)
  219. #define EP_ATTR_BULK     (0x2)
  220. #define EP_ATTR_INTERRUPT     (0x3)
  221. static const U8 descStr0[]={
  222. 4,STRING_TYPE,LANGID_US_L,LANGID_US_H,  //codes representing languages
  223.     };
  224. static const U8 descStr1[]={  //Manufacturer  
  225.         (0x14+2),STRING_TYPE, 
  226.         'S',0x0,'y',0x0,'s',0x0,'t',0x0,'e',0x0,'m',0x0,' ',0x0,'M',0x0,
  227.         'C',0x0,'U',0x0,
  228.     };
  229.     
  230. static const U8 descStr2[]={  //Product  
  231.         (0x2a+2),STRING_TYPE, 
  232.         'S',0x0,'E',0x0,'C',0x0,' ',0x0,'S',0x0,'3',0x0,'C',0x0,'2',0x0,
  233.         '4',0x0,'1',0x0,'0',0x0,'X',0x0,' ',0x0,'T',0x0,'e',0x0,'s',0x0,
  234.         't',0x0,' ',0x0,'B',0x0,'/',0x0,'D',0x0
  235.     };
  236. void InitDescriptorTable(void)
  237. {
  238.     //Standard device descriptor
  239.     descDev.bLength=0x12; //EP0_DEV_DESC_SIZE=0x12 bytes    
  240.     descDev.bDescriptorType=USB_DEVICE_TYPE;
  241.     descDev.bcdUSBL=0x10;
  242.     descDev.bcdUSBH=0x01;  //Ver 1.10
  243.     descDev.bDeviceClass=0xFF; //0x0          
  244.     descDev.bDeviceSubClass=0x0;          
  245.     descDev.bDeviceProtocol=0x0;          
  246.     descDev.bMaxPacketSize0=0x8;         
  247.     descDev.idVendorL=0x45;
  248.     descDev.idVendorH=0x53;
  249.     descDev.idProductL=0x34;
  250.     descDev.idProductH=0x12;
  251.     descDev.bcdDeviceL=0x00;
  252.     descDev.bcdDeviceH=0x01;
  253.     descDev.iManufacturer=0x1;  //index of string descriptor
  254.     descDev.iProduct=0x2; //index of string descriptor 
  255.     descDev.iSerialNumber=0x0;
  256.     descDev.bNumConfigurations=0x1;
  257.     //Standard configuration descriptor
  258.     descConf.bLength=0x9;    
  259.     descConf.bDescriptorType=CONFIGURATION_TYPE;         
  260.     descConf.wTotalLengthL=0x20; //<cfg desc>+<if desc>+<endp0 desc>+<endp1 desc>
  261.     descConf.wTotalLengthH=0;
  262.     descConf.bNumInterfaces=1;
  263. //dbg    descConf.bConfigurationValue=2;  //why 2? There's no reason.
  264.     descConf.bConfigurationValue=1;  
  265.     descConf.iConfiguration=0;
  266.     descConf.bmAttributes=CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED;  //bus powered only.
  267.     descConf.maxPower=25; //draws 50mA current from the USB bus.          
  268.     //Standard interface descriptor
  269.     descIf.bLength=0x9;    
  270.     descIf.bDescriptorType=INTERFACE_TYPE;         
  271.     descIf.bInterfaceNumber=0x0;
  272.     descIf.bAlternateSetting=0x0; //?
  273.     descIf.bNumEndpoints=2; //# of endpoints except EP0
  274.     descIf.bInterfaceClass=0xff; //0x0 ?
  275.     descIf.bInterfaceSubClass=0x0;  
  276.     descIf.bInterfaceProtocol=0x0;
  277.     descIf.iInterface=0x0;
  278.     //Standard endpoint0 descriptor
  279.     descEndpt0.bLength=0x7;    
  280.     descEndpt0.bDescriptorType=ENDPOINT_TYPE;         
  281.     descEndpt0.bEndpointAddress=1|EP_ADDR_IN;   // 2400Xendpoint 1 is IN endpoint.
  282.     descEndpt0.bmAttributes=EP_ATTR_BULK;
  283.     descEndpt0.wMaxPacketSizeL=EP1_PKT_SIZE; //64
  284.     descEndpt0.wMaxPacketSizeH=0x0;
  285.     descEndpt0.bInterval=0x0; //not used
  286.     //Standard endpoint1 descriptor
  287.     descEndpt1.bLength=0x7;    
  288.     descEndpt1.bDescriptorType=ENDPOINT_TYPE;         
  289.     descEndpt1.bEndpointAddress=3|EP_ADDR_OUT;   // 2400X endpoint 3 is OUT endpoint.
  290.     descEndpt1.bmAttributes=EP_ATTR_BULK;
  291.     descEndpt1.wMaxPacketSizeL=EP3_PKT_SIZE; //64
  292.     descEndpt1.wMaxPacketSizeH=0x0;
  293.     descEndpt1.bInterval=0x0; //not used 
  294. }
  295. BOOL InitUSB()
  296. {
  297. BYTE index;
  298. UCHAR cRxChar;
  299. volatile INTreg *s2440INT = (INTreg *)INT_BASE;
  300. pUSBCtrlAddr = (PUSHORT)(USB_BASE);
  301.     InitDescriptorTable();
  302.     //
  303.     // Initialize the USBD Controller
  304.     //
  305.     index = pUSBCtrlAddr->INDEX.index;
  306.     // suspend mode disable
  307.     pUSBCtrlAddr->PMR.sus_en = 0x0;
  308.     // setup endpoint 0
  309.     pUSBCtrlAddr->INDEX.index = 0;
  310.     pUSBCtrlAddr->MAXP.maxp = 0x1;         // 8 BYTE
  311.     pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;   // OUT_PKT_RDY
  312.     pUSBCtrlAddr->EP0ICSR1.sse_ = 1;       // SETUP_END
  313.     // setup endpoint 1
  314.     pUSBCtrlAddr->INDEX.index = 1;
  315.     pUSBCtrlAddr->MAXP.maxp = 0x8;     // 64 BYTE
  316. pUSBCtrlAddr->EP0ICSR1.de_ff = 1;
  317. pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;
  318.     pUSBCtrlAddr->ICSR2.mode_in = 1;   // IN
  319.     pUSBCtrlAddr->ICSR2.iso = 0;       // BULK
  320.     // setup endpoint 3
  321.     pUSBCtrlAddr->INDEX.index = 3;
  322.     pUSBCtrlAddr->MAXP.maxp = 0x8;     // 64 BYTE
  323. pUSBCtrlAddr->EP0ICSR1.de_ff = 1;
  324. pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;
  325.     pUSBCtrlAddr->ICSR2.mode_in = 0;   // OUT
  326.     pUSBCtrlAddr->OCSR2.iso = 0;       // BULK
  327.     // clear all EP interrupts
  328.     pUSBCtrlAddr->EIR.ep0_int = 0x1;
  329.     pUSBCtrlAddr->EIR.ep1_int = 0x1;
  330.     pUSBCtrlAddr->EIR.ep2_int = 0x1;
  331.     pUSBCtrlAddr->EIR.ep3_int = 0x1;
  332.     pUSBCtrlAddr->EIR.ep4_int = 0x1;
  333.     // clear reset int
  334.     pUSBCtrlAddr->UIR.reset_int = 0x1;
  335.     // EP0, 1, & 3 Enabled, EP2, 4 Disabled
  336.     pUSBCtrlAddr->EIER.ep0_int_en = 0x1;
  337.     pUSBCtrlAddr->EIER.ep2_int_en = 0x0;
  338.     pUSBCtrlAddr->EIER.ep4_int_en = 0x0;
  339.     pUSBCtrlAddr->EIER.ep1_int_en = 0x1;
  340.     pUSBCtrlAddr->EIER.ep3_int_en = 0x1;
  341.     // enable reset int 
  342.     pUSBCtrlAddr->UIER.reset_int_en = 0x1;
  343. return TRUE;
  344. }
  345. void PrintEp0Pkt(U8 *pt)
  346. {
  347.     int i;
  348. EdbgOutputDebugString("[RCV:");
  349. for(i=0;i<EP0_PKT_SIZE;i++)
  350. EdbgOutputDebugString("%x,",pt[i]);
  351. EdbgOutputDebugString("]rn");
  352. }
  353. void WrPktEp0(U8 *buf,int num)
  354. {
  355. int i;
  356.     
  357. for(i=0;i<num;i++)
  358. {
  359. pUSBCtrlAddr->EP0F.fifo_data = buf[i];
  360. }
  361. }
  362. void WrPktEp1(U8 *buf,int num)
  363. {
  364.     int i;
  365.     
  366.     for(i=0;i<num;i++)
  367.     {
  368.         pUSBCtrlAddr->EP1F.fifo_data = buf[i];
  369.     }
  370. }
  371. void RdPktEp0(U8 *buf,int num)
  372. {
  373.     int i;
  374.     
  375.     for(i=0;i<num;i++)
  376.     {
  377.         buf[i]=(U8)pUSBCtrlAddr->EP0F.fifo_data;
  378.     }
  379. }
  380. void RdPktEp3(U8 *buf,int num)
  381. {
  382. int i;
  383. for(i=0;i<num;i++)
  384. {
  385. buf[i]=(U8)(pUSBCtrlAddr->EP3F.fifo_data);
  386.     }
  387. }
  388. void PrepareEp1Fifo(void) 
  389. {
  390.     int i;
  391.     pUSBCtrlAddr->INDEX.index=1;
  392.     
  393.     for(i=0;i<EP1_PKT_SIZE;i++)ep1Buf[i]=(U8)(transferIndex+i);
  394.     WrPktEp1(ep1Buf,EP1_PKT_SIZE);
  395. pUSBCtrlAddr->EP0ICSR1.opr_ipr = 1;
  396. pUSBCtrlAddr->EP0ICSR1.sts_ur = 0;
  397. pUSBCtrlAddr->EP0ICSR1.de_ff = 0;
  398. }
  399. void Ep1Handler(void)
  400. {
  401.     pUSBCtrlAddr->INDEX.index=1;
  402.     
  403.     if(pUSBCtrlAddr->EP0ICSR1.sds_sts)
  404.     {   
  405. pUSBCtrlAddr->EP0ICSR1.opr_ipr = 0;
  406. pUSBCtrlAddr->EP0ICSR1.sts_ur = 0;
  407. pUSBCtrlAddr->EP0ICSR1.de_ff = 0;
  408. pUSBCtrlAddr->EP0ICSR1.sds_sts = 0;
  409.     return;
  410.     }
  411.     transferIndex++;
  412.     PrepareEp1Fifo(); 
  413.     return;
  414. }
  415. void Ep0Handler(void)
  416. {
  417. static int ep0SubState;
  418. pUSBCtrlAddr->INDEX.index=0;
  419. //DATAEND interrupt(ep0_csr==0x0) will be ignored 
  420. //because ep0State==EP0_STATE_INIT when the DATAEND interrupt is issued.
  421. // EdbgOutputDebugString("INFO : Ep0Handlerrn");
  422. if(pUSBCtrlAddr->EP0ICSR1.se_sds)
  423. {   
  424. // Host may end GET_DESCRIPTOR operation without completing the IN data stage.
  425. // If host does that, SETUP_END bit will be set.
  426. // OUT_PKT_RDY has to be also cleared because status stage sets OUT_PKT_RDY to 1.
  427. CLR_EP0_SETUP_END();
  428. if(pUSBCtrlAddr->EP0ICSR1.opr_ipr) 
  429. {
  430. FLUSH_EP0_FIFO(); //(???)
  431. //I think this isn't needed because EP0 flush is done automatically.   
  432. CLR_EP0_OUT_PKT_RDY();
  433. }
  434. ep0State=EP0_STATE_INIT;
  435. return;
  436. }
  437. //I think that EP0_SENT_STALL will not be set to 1.
  438. if(pUSBCtrlAddr->EP0ICSR1.sts_ur)
  439. {   
  440. EdbgOutputDebugString("[STALL]rn");
  441. CLR_EP0_SENT_STALL();
  442. if(pUSBCtrlAddr->EP0ICSR1.opr_ipr)
  443. {
  444. CLR_EP0_OUT_PKT_RDY();
  445. }
  446. ep0State=EP0_STATE_INIT;
  447. return;
  448. }
  449. if((pUSBCtrlAddr->EP0ICSR1.opr_ipr) && (ep0State==EP0_STATE_INIT))
  450. {
  451. RdPktEp0((U8 *)&descSetup,EP0_PKT_SIZE);
  452. switch(descSetup.bRequest)
  453. {
  454. case GET_DESCRIPTOR:
  455. switch(descSetup.bValueH)        
  456. {
  457.             case USB_DEVICE_TYPE:
  458. CLR_EP0_OUT_PKT_RDY();
  459. ep0State=EP0_STATE_GD_DEV_0;         
  460. break;
  461. case CONFIGURATION_TYPE:
  462. CLR_EP0_OUT_PKT_RDY();
  463. if((descSetup.bLengthL+(descSetup.bLengthH<<8))>0x9)
  464. //bLengthH should be used for bLength=0x209 at WIN2K.    
  465. ep0State=EP0_STATE_GD_CFG_0; //for WIN98,WIN2K
  466. else          
  467. ep0State=EP0_STATE_GD_CFG_ONLY_0; //for WIN2K
  468. break;
  469. case STRING_TYPE:
  470. CLR_EP0_OUT_PKT_RDY();
  471. switch(descSetup.bValueL)
  472. {
  473. case 0:
  474. ep0State=EP0_STATE_GD_STR_I0;
  475. break;
  476. case 1:
  477. ep0State=EP0_STATE_GD_STR_I1;
  478. break;
  479. case 2:
  480. ep0State=EP0_STATE_GD_STR_I2;
  481. break;
  482. default:
  483. EdbgOutputDebugString("[UE:STRI?]rn");
  484. break;
  485. }
  486. ep0SubState=0;
  487. break;
  488. case INTERFACE_TYPE:
  489. EdbgOutputDebugString("[GDI]rn");
  490. CLR_EP0_OUT_PKT_RDY();
  491. ep0State=EP0_STATE_GD_IF_ONLY_0; //for WIN98
  492. break;
  493. case ENDPOINT_TYPE:     
  494. EdbgOutputDebugString("[GDE]rn");
  495. CLR_EP0_OUT_PKT_RDY();
  496. switch(descSetup.bValueL&0xf)
  497. {
  498. case 0:
  499. ep0State=EP0_STATE_GD_EP0_ONLY_0;
  500. break;
  501. case 1:
  502. ep0State=EP0_STATE_GD_EP1_ONLY_0;
  503. break;
  504. default:
  505. EdbgOutputDebugString("[UE:GDE?]rn");
  506. break;
  507. }
  508. break;
  509. default:
  510. EdbgOutputDebugString("[UE:GD?]rn");
  511. break;
  512. }
  513. break;
  514. case SET_ADDRESS:
  515. pUSBCtrlAddr->udcFAR.func_addr = descSetup.bValueL;
  516. pUSBCtrlAddr->udcFAR.addr_up = 1;
  517. CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
  518. ep0State=EP0_STATE_INIT;
  519. break;
  520.     
  521. case SET_CONFIGURATION:
  522. ConfigSet.ConfigurationValue=descSetup.bValueL;
  523. CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
  524. ep0State=EP0_STATE_INIT;
  525. break;
  526. //////////////////////// For chapter 9 test ////////////////////
  527. case CLEAR_FEATURE:
  528. switch (descSetup.bmRequestType)
  529. {
  530. case DEVICE_RECIPIENT:
  531. if (descSetup.bValueL == 1)
  532. Rwuen = FALSE;
  533. break;
  534. case ENDPOINT_RECIPIENT:
  535. if (descSetup.bValueL == 0)
  536. {
  537. if((descSetup.bIndexL & 0x7f) == 0x00){
  538. StatusGet.Endpoint0= 0;    
  539. }
  540. if((descSetup.bIndexL & 0x8f) == 0x81){           // IN  Endpoint 1
  541. StatusGet.Endpoint1= 0;           
  542. }
  543. if((descSetup.bIndexL & 0x8f) == 0x03){          // OUT Endpoint 3
  544. StatusGet.Endpoint3= 0;      
  545. }
  546. }
  547. break;
  548. default:
  549. break;
  550. }
  551. CLR_EP0_OUTPKTRDY_DATAEND();
  552. ep0State=EP0_STATE_INIT;
  553. break;
  554. case GET_CONFIGURATION:
  555. CLR_EP0_OUT_PKT_RDY();
  556. ep0State=EP0_CONFIG_SET;
  557. break;
  558. case GET_INTERFACE:
  559. CLR_EP0_OUT_PKT_RDY();
  560. ep0State=EP0_INTERFACE_GET;
  561. break;
  562. case GET_STATUS:
  563. switch(descSetup.bmRequestType)
  564. {
  565. case  (0x80):
  566. CLR_EP0_OUT_PKT_RDY();
  567. StatusGet.Device=((U8)Rwuen<<1)|(U8)Selfpwr;
  568. ep0State=EP0_GET_STATUS0;
  569. break;
  570. case  (0x81):
  571. CLR_EP0_OUT_PKT_RDY();
  572. StatusGet.Interface=0;
  573. ep0State=EP0_GET_STATUS1;
  574. break;
  575. case  (0x82):
  576. CLR_EP0_OUT_PKT_RDY();
  577. if((descSetup.bIndexL & 0x7f) == 0x00){
  578. ep0State=EP0_GET_STATUS2;
  579. }
  580. if((descSetup.bIndexL & 0x8f) == 0x81){
  581. ep0State=EP0_GET_STATUS3;
  582. }
  583. if((descSetup.bIndexL & 0x8f) == 0x03){
  584. ep0State=EP0_GET_STATUS4;
  585. }
  586. break;
  587. default:
  588. break;
  589.         }
  590.         break;
  591. case SET_DESCRIPTOR:
  592. CLR_EP0_OUTPKTRDY_DATAEND();
  593. ep0State=EP0_STATE_INIT;
  594. break;
  595. case SET_FEATURE:
  596. switch (descSetup.bmRequestType)
  597. {
  598. case DEVICE_RECIPIENT:
  599. if (descSetup.bValueL == 1)
  600. Rwuen = TRUE;
  601. break;
  602. case ENDPOINT_RECIPIENT:
  603. if (descSetup.bValueL == 0)
  604. {
  605. if((descSetup.bIndexL & 0x7f) == 0x00){
  606. StatusGet.Endpoint0= 1;
  607. }
  608. if((descSetup.bIndexL & 0x8f) == 0x81){
  609. StatusGet.Endpoint1= 1;
  610. }
  611. if((descSetup.bIndexL & 0x8f) == 0x03){
  612. StatusGet.Endpoint3= 1;
  613. }
  614. }
  615. break;
  616. default:
  617. break;
  618. }
  619. CLR_EP0_OUTPKTRDY_DATAEND();
  620. ep0State=EP0_STATE_INIT;
  621. break;
  622. case SET_INTERFACE:
  623. InterfaceGet.AlternateSetting= descSetup.bValueL;
  624. CLR_EP0_OUTPKTRDY_DATAEND(); 
  625. ep0State=EP0_STATE_INIT;
  626. break;
  627. case SYNCH_FRAME:
  628. ep0State=EP0_STATE_INIT;
  629. break;
  630.        //////////////////////////////////////////////////////////////
  631. default:
  632. EdbgOutputDebugString("[UE:SETUP=%x]rn",descSetup.bRequest);
  633. CLR_EP0_OUTPKTRDY_DATAEND(); //Because of no data control transfers.
  634. ep0State=EP0_STATE_INIT;
  635. break;
  636. }
  637. }
  638. switch(ep0State)
  639. {
  640. case EP0_STATE_INIT:
  641. break; 
  642. //=== GET_DESCRIPTOR:DEVICE ===
  643. case EP0_STATE_GD_DEV_0:
  644. WrPktEp0((U8 *)&descDev+0,8); //EP0_PKT_SIZE
  645. SET_EP0_IN_PKT_RDY();
  646. ep0State=EP0_STATE_GD_DEV_1;
  647. break;
  648. case EP0_STATE_GD_DEV_1:
  649. WrPktEp0((U8 *)&descDev+0x8,8); 
  650. SET_EP0_IN_PKT_RDY();
  651. ep0State=EP0_STATE_GD_DEV_2;
  652. break;
  653. case EP0_STATE_GD_DEV_2:
  654. WrPktEp0((U8 *)&descDev+0x10,2);   //8+8+2=0x12
  655. SET_EP0_INPKTRDY_DATAEND();
  656. ep0State=EP0_STATE_INIT;
  657. break;
  658. //=== GET_DESCRIPTOR:CONFIGURATION+INTERFACE+ENDPOINT0+ENDPOINT1 ===
  659. //Windows98 gets these 4 descriptors all together by issuing only a request.
  660. //Windows2000 gets each descriptor seperately.
  661. case EP0_STATE_GD_CFG_0:
  662. WrPktEp0((U8 *)&descConf+0,8); //EP0_PKT_SIZE
  663. SET_EP0_IN_PKT_RDY();
  664. ep0State=EP0_STATE_GD_CFG_1;
  665. break;
  666. case EP0_STATE_GD_CFG_1:
  667. WrPktEp0((U8 *)&descConf+8,1); 
  668. WrPktEp0((U8 *)&descIf+0,7); 
  669. SET_EP0_IN_PKT_RDY();
  670. ep0State=EP0_STATE_GD_CFG_2;
  671. break;
  672. case EP0_STATE_GD_CFG_2:
  673. WrPktEp0((U8 *)&descIf+7,2); 
  674. WrPktEp0((U8 *)&descEndpt0+0,6); 
  675. SET_EP0_IN_PKT_RDY();
  676. ep0State=EP0_STATE_GD_CFG_3;
  677. break;
  678. case EP0_STATE_GD_CFG_3:
  679. WrPktEp0((U8 *)&descEndpt0+6,1); 
  680. WrPktEp0((U8 *)&descEndpt1+0,7); 
  681. SET_EP0_IN_PKT_RDY();
  682. ep0State=EP0_STATE_GD_CFG_4;            
  683. break;
  684. case EP0_STATE_GD_CFG_4:
  685.  //zero length data packit 
  686. SET_EP0_INPKTRDY_DATAEND();
  687. ep0State=EP0_STATE_INIT;            
  688. break;
  689. //=== GET_DESCRIPTOR:CONFIGURATION ONLY===
  690. case EP0_STATE_GD_CFG_ONLY_0:
  691. WrPktEp0((U8 *)&descConf+0,8); //EP0_PKT_SIZE
  692. SET_EP0_IN_PKT_RDY();
  693. ep0State=EP0_STATE_GD_CFG_ONLY_1;
  694. break;
  695.     
  696. case EP0_STATE_GD_CFG_ONLY_1:
  697. WrPktEp0((U8 *)&descConf+8,1); 
  698. SET_EP0_INPKTRDY_DATAEND();
  699. ep0State=EP0_STATE_INIT;            
  700. break;
  701. //=== GET_DESCRIPTOR:INTERFACE ONLY===
  702. case EP0_STATE_GD_IF_ONLY_0:
  703. EdbgOutputDebugString("[GDI0]rn");
  704. WrPktEp0((U8 *)&descIf+0,8); 
  705. SET_EP0_IN_PKT_RDY();
  706. ep0State=EP0_STATE_GD_IF_ONLY_1;
  707. break;
  708. case EP0_STATE_GD_IF_ONLY_1:
  709. EdbgOutputDebugString("[GDI1]rn");
  710. WrPktEp0((U8 *)&descIf+8,1); 
  711. SET_EP0_INPKTRDY_DATAEND();
  712. ep0State=EP0_STATE_INIT;            
  713. break;
  714. //=== GET_DESCRIPTOR:ENDPOINT 0 ONLY===
  715. case EP0_STATE_GD_EP0_ONLY_0:
  716. EdbgOutputDebugString("[GDE00]rn");
  717. WrPktEp0((U8 *)&descEndpt0+0,7); 
  718. SET_EP0_INPKTRDY_DATAEND();
  719. ep0State=EP0_STATE_INIT;            
  720. break;
  721.             
  722. //=== GET_DESCRIPTOR:ENDPOINT 1 ONLY===
  723. case EP0_STATE_GD_EP1_ONLY_0:
  724. EdbgOutputDebugString("[GDE10]rn");
  725. WrPktEp0((U8 *)&descEndpt1+0,7); 
  726. SET_EP0_INPKTRDY_DATAEND();
  727. ep0State=EP0_STATE_INIT;            
  728. break;
  729.             
  730. ////////////////////////////////////////////
  731. case EP0_INTERFACE_GET:
  732. WrPktEp0((U8 *)&InterfaceGet+0,1);
  733. SET_EP0_INPKTRDY_DATAEND();
  734. ep0State=EP0_STATE_INIT;      
  735. break;
  736.  
  737. //=== GET_DESCRIPTOR:STRING ===
  738. case EP0_STATE_GD_STR_I0:
  739. WrPktEp0((U8 *)descStr0, 4 );  
  740. SET_EP0_INPKTRDY_DATAEND();
  741. ep0State=EP0_STATE_INIT;     
  742. ep0SubState=0;
  743. break;
  744. case EP0_STATE_GD_STR_I1:
  745. if( (ep0SubState*EP0_PKT_SIZE+EP0_PKT_SIZE)<sizeof(descStr1) )
  746. {
  747. WrPktEp0((U8 *)descStr1+(ep0SubState*EP0_PKT_SIZE),EP0_PKT_SIZE); 
  748. SET_EP0_IN_PKT_RDY();
  749. ep0State=EP0_STATE_GD_STR_I1;
  750. ep0SubState++;
  751. }
  752. else
  753. {
  754. WrPktEp0((U8 *)descStr1+(ep0SubState*EP0_PKT_SIZE),
  755. sizeof(descStr1)-(ep0SubState*EP0_PKT_SIZE)); 
  756. SET_EP0_INPKTRDY_DATAEND();
  757. ep0State=EP0_STATE_INIT;     
  758. ep0SubState=0;
  759. }
  760. break;
  761. case EP0_STATE_GD_STR_I2:
  762. if( (ep0SubState*EP0_PKT_SIZE+EP0_PKT_SIZE)<sizeof(descStr2) )
  763. {
  764. WrPktEp0((U8 *)descStr2+(ep0SubState*EP0_PKT_SIZE),EP0_PKT_SIZE); 
  765. SET_EP0_IN_PKT_RDY();
  766. ep0State=EP0_STATE_GD_STR_I2;
  767. ep0SubState++;
  768. }
  769. else
  770. {
  771. WrPktEp0((U8 *)descStr2+(ep0SubState*EP0_PKT_SIZE),
  772. sizeof(descStr2)-(ep0SubState*EP0_PKT_SIZE)); 
  773. SET_EP0_INPKTRDY_DATAEND();
  774. ep0State=EP0_STATE_INIT;     
  775. ep0SubState=0;
  776. }
  777. break;
  778. case EP0_CONFIG_SET:
  779. WrPktEp0((U8 *)&ConfigSet+0,1); 
  780. SET_EP0_INPKTRDY_DATAEND();
  781. ep0State=EP0_STATE_INIT;      
  782. break;
  783. case EP0_GET_STATUS0:
  784. WrPktEp0((U8 *)&StatusGet+0,1);
  785. SET_EP0_INPKTRDY_DATAEND();
  786. ep0State=EP0_STATE_INIT;      
  787. break;
  788. case EP0_GET_STATUS1:
  789. WrPktEp0((U8 *)&StatusGet+1,1);
  790. SET_EP0_INPKTRDY_DATAEND();
  791. ep0State=EP0_STATE_INIT;      
  792. break;
  793. case EP0_GET_STATUS2:
  794. WrPktEp0((U8 *)&StatusGet+2,1);
  795. SET_EP0_INPKTRDY_DATAEND();
  796. ep0State=EP0_STATE_INIT;      
  797. break;
  798. case EP0_GET_STATUS3:
  799. WrPktEp0((U8 *)&StatusGet+3,1);
  800. SET_EP0_INPKTRDY_DATAEND();
  801. ep0State=EP0_STATE_INIT;      
  802. break;
  803. case EP0_GET_STATUS4:
  804. WrPktEp0((U8 *)&StatusGet+4,1);
  805. SET_EP0_INPKTRDY_DATAEND();
  806. ep0State=EP0_STATE_INIT;      
  807. break;
  808. default:
  809. // DbgPrintf("UE:G?D");
  810. EdbgOutputDebugString("[UE:G?D]rn");
  811. break;
  812. }
  813. }
  814. void ReconfigUsbd(void)
  815. {
  816. // *** End point information ***
  817. //   EP0: control
  818. //   EP1: bulk in end point
  819. //   EP2: not used
  820. //   EP3: bulk out end point
  821. //   EP4: not used
  822.     
  823. pUSBCtrlAddr->PMR.sus_en = 0;
  824. pUSBCtrlAddr->PMR.sus_mo = 0;
  825. pUSBCtrlAddr->PMR.muc_res = 0;
  826. pUSBCtrlAddr->PMR.usb_re = 0;
  827. pUSBCtrlAddr->PMR.iso_up = 0;
  828. pUSBCtrlAddr->INDEX.index = 0;
  829. pUSBCtrlAddr->MAXP.maxp = 0x01; //EP0 max packit size = 8 
  830. pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;
  831. pUSBCtrlAddr->EP0ICSR1.sse_ = 1;
  832. //EP0:clear OUT_PKT_RDY & SETUP_END
  833. pUSBCtrlAddr->INDEX.index = 1;
  834. pUSBCtrlAddr->MAXP.maxp = 0x08; //EP1 max packit size = 64 
  835. pUSBCtrlAddr->EP0ICSR1.de_ff = 1;
  836. pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;
  837. pUSBCtrlAddr->ICSR2.mode_in = 1;
  838. pUSBCtrlAddr->ICSR2.in_dma_int_en = 1;
  839. pUSBCtrlAddr->ICSR2.iso = 0;
  840. pUSBCtrlAddr->OCSR1.clr_data_tog = 1;
  841. pUSBCtrlAddr->OCSR2.iso = 0;
  842. pUSBCtrlAddr->OCSR2.out_dma_int_en = 1;
  843. pUSBCtrlAddr->INDEX.index = 2;
  844. pUSBCtrlAddr->MAXP.maxp = 0x08; //EP2 max packit size = 64 
  845. pUSBCtrlAddr->EP0ICSR1.de_ff = 1;
  846. pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;
  847. pUSBCtrlAddr->ICSR2.mode_in = 1;
  848. pUSBCtrlAddr->ICSR2.in_dma_int_en = 1;
  849. pUSBCtrlAddr->ICSR2.iso = 0;
  850. pUSBCtrlAddr->OCSR1.clr_data_tog = 1;
  851. pUSBCtrlAddr->OCSR2.iso = 0;
  852. pUSBCtrlAddr->OCSR2.out_dma_int_en = 1;
  853. pUSBCtrlAddr->INDEX.index = 3;
  854. pUSBCtrlAddr->MAXP.maxp = 0x08; //EP3 max packit size = 64 
  855. pUSBCtrlAddr->EP0ICSR1.de_ff = 1;
  856. pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;
  857. pUSBCtrlAddr->ICSR2.mode_in = 0;
  858. pUSBCtrlAddr->ICSR2.in_dma_int_en = 0;
  859. pUSBCtrlAddr->ICSR2.iso = 0;
  860. pUSBCtrlAddr->OCSR1.clr_data_tog = 1;
  861. //clear OUT_PKT_RDY, data_toggle_bit.
  862. //The data toggle bit should be cleared when initialization.
  863. pUSBCtrlAddr->OCSR2.iso = 0;
  864. pUSBCtrlAddr->OCSR2.out_dma_int_en = 1;
  865. pUSBCtrlAddr->INDEX.index = 4;
  866. pUSBCtrlAddr->MAXP.maxp = 0x08; //EP4 max packit size = 64 
  867. pUSBCtrlAddr->EP0ICSR1.de_ff = 1;
  868. pUSBCtrlAddr->EP0ICSR1.sopr_cdt = 1;
  869. pUSBCtrlAddr->ICSR2.mode_in = 0;
  870. pUSBCtrlAddr->ICSR2.in_dma_int_en = 1;
  871. pUSBCtrlAddr->ICSR2.iso = 0;
  872. pUSBCtrlAddr->OCSR1.clr_data_tog = 1;
  873. //clear OUT_PKT_RDY, data_toggle_bit.
  874. //The data toggle bit should be cleared when initialization.
  875. pUSBCtrlAddr->OCSR2.iso = 0;
  876. pUSBCtrlAddr->OCSR2.out_dma_int_en = 1;
  877. pUSBCtrlAddr->EIR.ep0_int=1;
  878. pUSBCtrlAddr->EIR.ep1_int=1;
  879. pUSBCtrlAddr->EIR.ep2_int=1;
  880. pUSBCtrlAddr->EIR.ep3_int=1;
  881. pUSBCtrlAddr->EIR.ep4_int=1;
  882. pUSBCtrlAddr->UIR.reset_int = 1;
  883. pUSBCtrlAddr->UIR.sus_int = 1;
  884. pUSBCtrlAddr->UIR.resume_int = 1;
  885. //Clear all usbd pending bits
  886. //EP0,1,3 & reset interrupt are enabled
  887. pUSBCtrlAddr->EIER.ep0_int_en = 1;
  888. pUSBCtrlAddr->EIER.ep1_int_en = 1;
  889. pUSBCtrlAddr->EIER.ep3_int_en = 1;
  890. pUSBCtrlAddr->UIER.reset_int_en = 1;
  891. ep0State=EP0_STATE_INIT;
  892. }
  893. void Isr_Init(void)
  894. {
  895. volatile INTreg *s2440INT = (INTreg *)INT_BASE;
  896. s2440INT->rINTMOD=0x0;   // All=IRQ mode
  897. s2440INT->rINTMSK=BIT_ALLMSK;   // All interrupt is masked.
  898. // EdbgOutputDebugString("INFO: (unsigned)IsrUsbd : 0x%xrn", (unsigned)IsrUsbd);
  899. // EdbgOutputDebugString("INFO: (unsigned)IsrHandler : 0x%xrn", (unsigned)IsrHandler);
  900. // make value to assemble code "b IsrHandler"
  901. pISR =(unsigned)(0xEA000000)+(((unsigned)IsrHandler - (0x8c000000 + 0x18 + 0x8) )>>2);
  902. s2440INT->rSRCPND  = BIT_USBD;
  903. if (s2440INT->rINTPND & BIT_USBD) s2440INT->rINTPND = BIT_USBD;
  904. s2440INT->rINTMSK &= ~BIT_USBD; // USB Interrupt enable.
  905. s2440INT->rSRCPND  = BIT_DMA2;
  906. if (s2440INT->rINTPND & BIT_DMA2) s2440INT->rINTPND = BIT_DMA2;
  907. s2440INT->rINTMSK &= ~BIT_DMA2; // DMA Interrupt enable.
  908. }
  909. void IsrUsbd(unsigned int val)
  910. {
  911.     U8 saveIndexReg=pUSBCtrlAddr->INDEX.index;
  912. volatile INTreg *s2440INT = (INTreg *)INT_BASE;
  913. // EdbgOutputDebugString("INFO : IsrUsbd : Interrupt occurred rn");
  914. if (s2440INT->rINTPND & BIT_DMA2)
  915. {
  916. DMA2Handler();
  917. goto Exit;
  918. }
  919.     if(pUSBCtrlAddr->UIR.sus_int)
  920.     {
  921. pUSBCtrlAddr->UIR.sus_int = 1;
  922. EdbgOutputDebugString("<SUS]rn");
  923.     }
  924.     if(pUSBCtrlAddr->UIR.resume_int)
  925.     {
  926. pUSBCtrlAddr->UIR.resume_int = 1;
  927. EdbgOutputDebugString("<RSM]rn");
  928.     }
  929.     if(pUSBCtrlAddr->UIR.reset_int)
  930.     {
  931. EdbgOutputDebugString("<RSET]rn");
  932.      ReconfigUsbd();
  933. pUSBCtrlAddr->UIR.reset_int = 1;
  934.         PrepareEp1Fifo(); 
  935.     }
  936.     if(pUSBCtrlAddr->EIR.ep0_int)
  937.     {
  938. EdbgOutputDebugString("EP0 Interruptrn");
  939. pUSBCtrlAddr->EIR.ep0_int=1;
  940.      Ep0Handler();
  941.     }
  942.     if(pUSBCtrlAddr->EIR.ep1_int)
  943.     {
  944. EdbgOutputDebugString("<1:TBD]rn");
  945. pUSBCtrlAddr->EIR.ep1_int=1;
  946.      Ep1Handler();
  947.     }
  948.     if(pUSBCtrlAddr->EIR.ep2_int)
  949.     {
  950. pUSBCtrlAddr->EIR.ep2_int=1;
  951. EdbgOutputDebugString("<2:TBD]rn");
  952.     }
  953.     if(pUSBCtrlAddr->EIR.ep3_int)
  954.     {
  955.      Ep3Handler();
  956. pUSBCtrlAddr->EIR.ep3_int=1;
  957.     }
  958.     if(pUSBCtrlAddr->EIR.ep4_int)
  959.     {
  960. pUSBCtrlAddr->EIR.ep4_int=1;
  961. EdbgOutputDebugString("<4:TBD]rn");
  962.     }
  963. if (s2440INT->rINTPND & BIT_USBD)
  964. {
  965. s2440INT->rSRCPND  = BIT_USBD;
  966. if (s2440INT->rINTPND & BIT_USBD) s2440INT->rINTPND = BIT_USBD;
  967. }
  968. Exit:
  969.     pUSBCtrlAddr->INDEX.index=saveIndexReg;
  970. }
  971. void Ep3Handler(void)
  972. {
  973.     int fifoCnt;
  974. volatile INTreg *s2440INT = (INTreg *)INT_BASE;
  975.     pUSBCtrlAddr->INDEX.index=3;
  976.     if(pUSBCtrlAddr->OCSR1.out_pkt_rdy)
  977.     {
  978. fifoCnt=pUSBCtrlAddr->OFCR1.out_cnt_low;
  979. downPt = (LPBYTE)(downPtIndex);
  980. RdPktEp3((U8 *)downPt,fifoCnt);
  981. downPtIndex += 64;
  982. s2440INT->rINTMSK |= BIT_USBD; // USB Interrupt disable.
  983. return;
  984.     }
  985.     //I think that EPO_SENT_STALL will not be set to 1.
  986.     if(pUSBCtrlAddr->OCSR1.sent_stall)
  987.     {
  988.     CLR_EP3_SENT_STALL();
  989.     return;
  990.     }
  991. }
  992. void DMA2Handler(void)
  993. {
  994. volatile INTreg *s2440INT = (INTreg *)INT_BASE;
  995. volatile DMAreg *v_pDMAregs = (DMAreg *)DMA_BASE;
  996. s2440INT->rSRCPND  = BIT_DMA2;
  997. if (s2440INT->rINTPND & BIT_DMA2) s2440INT->rINTPND = BIT_DMA2;
  998. downPtIndex += 0x80000;
  999. v_pDMAregs->rDIDST2=((U32)downPtIndex+0x80000);
  1000. v_pDMAregs->rDIDSTC2=(1<<2)|(0<<1)|(0<<0);  
  1001. v_pDMAregs->rDCON2=v_pDMAregs->rDCON2&~(0xfffff)|(0x80000); 
  1002. while(rEP3_DMA_TTC<0xfffff)
  1003. {
  1004. pUSBCtrlAddr->EP3DTL.ep3_ttl_l = 0xff;
  1005. pUSBCtrlAddr->EP3DTM.ep3_ttl_m = 0xff;
  1006. pUSBCtrlAddr->EP3DTH.ep3_ttl_h = 0x0f;
  1007. }
  1008. }
  1009. void ConfigEp3DmaMode(U32 bufAddr,U32 count)
  1010. {
  1011. volatile INTreg *s2440INT = (INTreg *)INT_BASE;
  1012. volatile DMAreg *v_pDMAregs = (DMAreg *)DMA_BASE;
  1013. int i;
  1014. pUSBCtrlAddr->INDEX.index=3;
  1015. count=count&0xfffff; //transfer size should be <1MB
  1016. v_pDMAregs->rDISRCC2=(1<<1)|(1<<0);
  1017. v_pDMAregs->rDISRC2=REAL_PHYSICAL_ADDR_EP3_FIFO; //src=APB,fixed,src=EP3_FIFO
  1018. v_pDMAregs->rDIDSTC2=(0<<2)|(0<<1)|(0<<0);
  1019. v_pDMAregs->rDIDST2=bufAddr;       //dst=AHB,increase,dst=bufAddr
  1020. v_pDMAregs->rDCON2=(count)|(1<<31)|(0<<30)|(1<<29)|(0<<28)|(0<<27)|(4<<24)|(1<<23)|(0<<22)|(0<<20); 
  1021. //handshake,requestor=APB,CURR_TC int enable,unit transfer,
  1022. //single service,src=USBD,H/W request,autoreload,byte,CURR_TC
  1023. v_pDMAregs->rDMASKTRIG2 = (1<<1);
  1024. //DMA 2 on
  1025. pUSBCtrlAddr->EP3DTL.ep3_ttl_l = 0xff;
  1026. pUSBCtrlAddr->EP3DTM.ep3_ttl_m = 0xff;
  1027. pUSBCtrlAddr->EP3DTH.ep3_ttl_h = 0x0f;
  1028. pUSBCtrlAddr->OCSR2.auto_clr = 1;
  1029. pUSBCtrlAddr->OCSR2.out_dma_int_en = 1;
  1030. //AUTO_CLR(OUT_PKT_READY is cleared automatically), interrupt_masking.
  1031. pUSBCtrlAddr->EP3DU.ep3_unit_cnt = 1;
  1032. *(volatile BYTE *)&pUSBCtrlAddr->EP3DC=UDMA_OUT_DMA_RUN|UDMA_DMA_MODE_EN;
  1033. // deamnd disable,out_dma_run=run,in_dma_run=stop,DMA mode enable
  1034.     //wait until DMA_CON is effective.
  1035. *(volatile BYTE *)&pUSBCtrlAddr->EP3DC;
  1036. for(i=0;i<10;i++);
  1037. }
  1038. void ConfigEp3IntMode(void)
  1039. {
  1040. volatile DMAreg *v_pDMAregs = (DMAreg *)DMA_BASE;
  1041.     pUSBCtrlAddr->INDEX.index=3;
  1042.     
  1043.     v_pDMAregs->rDMASKTRIG2= (0<<1);  // EP3=DMA ch 2
  1044.         //DMA channel off
  1045. pUSBCtrlAddr->OCSR2.auto_clr = 0;
  1046. //AUTOCLEAR off,interrupt_enabled (???)
  1047. pUSBCtrlAddr->EP3DU.ep3_unit_cnt = 1;
  1048. *(volatile BYTE *)&pUSBCtrlAddr->EP3DC=0x0;
  1049.     //wait until DMA_CON is effective.
  1050.     *(volatile BYTE *)&pUSBCtrlAddr->EP3DC;
  1051. }
  1052. #pragma optimize ("",off)
  1053. BOOL UbootReadData(DWORD cbData, LPBYTE pbData)
  1054. {
  1055. volatile USBD_GLOBALS *usbdShMem = (USBD_GLOBALS *)(DRIVER_GLOBALS_PHYSICAL_MEMORY_START);
  1056. volatile INTreg *s2440INT = (INTreg *)INT_BASE;
  1057. volatile DMAreg *v_pDMAregs = (DMAreg *)DMA_BASE;
  1058. unsigned int temp;
  1059. int i;
  1060. Loop:
  1061. if ( downPtIndex > readPtIndex + cbData )
  1062. {
  1063. memcpy(pbData, readPtIndex, cbData);
  1064. readPtIndex += cbData;
  1065. }
  1066. else if (downPtIndex == DMABUFFER)
  1067. {
  1068. while (downPtIndex == DMABUFFER) {}; // first 64 bytes, get interrupt mode.
  1069. if ( readPtIndex == DMABUFFER )
  1070. {
  1071. memcpy(pbData, readPtIndex, cbData);
  1072. readPtIndex += cbData;
  1073. }
  1074. s2440INT->rSRCPND  = BIT_USBD;
  1075. if (s2440INT->rINTPND & BIT_USBD) s2440INT->rINTPND = BIT_USBD;
  1076. s2440INT->rINTMSK |= BIT_USBD; // USB Interrupt disable.
  1077. // read data with DMA operation.
  1078. s2440INT->rSRCPND  = BIT_DMA2;
  1079. if (s2440INT->rINTPND & BIT_DMA2) s2440INT->rINTPND = BIT_DMA2;
  1080. s2440INT->rINTMSK &= ~BIT_DMA2; // DMA Interrupt enable.
  1081. pUSBCtrlAddr->INDEX.index=3;
  1082. CLR_EP3_OUT_PKT_READY();
  1083. ConfigEp3DmaMode(downPtIndex,0x80000);
  1084. v_pDMAregs->rDIDST2=(downPtIndex+0x80000); //for 1st autoreload.
  1085. v_pDMAregs->rDIDSTC2=(1<<2)|(0<<1)|(0<<0);
  1086. v_pDMAregs->rDCON2=v_pDMAregs->rDCON2&~(0xfffff)|(0x80000);
  1087. while(rEP3_DMA_TTC<0xfffff)
  1088. {
  1089. pUSBCtrlAddr->EP3DTL.ep3_ttl_l = 0xff;
  1090. pUSBCtrlAddr->EP3DTM.ep3_ttl_m = 0xff;
  1091. pUSBCtrlAddr->EP3DTH.ep3_ttl_h = 0x0f;
  1092. }
  1093. }
  1094. else
  1095. {
  1096. temp = rEP3_DMA_TTC;
  1097. for (i = 0; i < 60000; i++ )
  1098. {
  1099. }
  1100. if ( temp == rEP3_DMA_TTC )
  1101. {
  1102. EdbgOutputDebugString("INFO : UbootReadData : downPtIndex(0x%x) - readPtIndex(0x%x) = 0x%xrn", downPtIndex, readPtIndex, downPtIndex - readPtIndex);
  1103. downPtIndex += ((unsigned int)0xfffff - (unsigned int)rEP3_DMA_TTC);
  1104. EdbgOutputDebugString("INFO : UbootReadData : rEP3_DMA_TTC = 0x%xrn", rEP3_DMA_TTC);
  1105. }
  1106. goto Loop;
  1107. }
  1108. return TRUE;
  1109. }
  1110. #pragma optimize ("",on)