47aa3804c87df0c5134f96f3cadabe2e5c41cd7e
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / target / hif / k2_fw_usb_api.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include "usb_defs.h"
36 #include "usb_type.h"
37 #include "usb_pre.h"
38 #include "usb_extr.h"
39 #include "usb_std.h"
40 #include "reg_defs.h"
41 #include "athos_api.h"
42 #include "usbfifo_api.h"
43
44
45 #include "sys_cfg.h"
46
47 typedef void (* USBFIFO_recv_command)(VBUF *cmd);
48 void _fw_usb_suspend_reboot();
49
50 extern Action      eUsbCxFinishAction;
51 extern CommandType eUsbCxCommand;
52 extern BOOLEAN     UsbChirpFinish;
53 extern USB_FIFO_CONFIG usbFifoConf;
54
55 USBFIFO_recv_command m_origUsbfifoRecvCmd = NULL;
56
57 #if SYSTEM_MODULE_USB
58 #define vUsb_ep0end(void)                                   \
59 {                                                           \
60     eUsbCxCommand = CMD_VOID;                               \
61     USB_BYTE_REG_WRITE(ZM_CX_CONFIG_STATUS_OFFSET, 0x01);   \
62 }
63
64 #define vUsb_ep0fail(void)  USB_BYTE_REG_WRITE(ZM_CX_CONFIG_STATUS_OFFSET, 0x04)
65
66 #define vUsb_rst()                                              \
67 {                                                               \
68     USB_BYTE_REG_WRITE(ZM_INTR_SOURCE_7_OFFSET,                 \
69         (USB_BYTE_REG_READ(ZM_INTR_SOURCE_7_OFFSET)&~BIT1));    \
70     UsbChirpFinish = FALSE;                                     \
71 }
72
73 #define vUsb_suspend()  USB_BYTE_REG_WRITE(ZM_INTR_SOURCE_7_OFFSET, \
74                             (USB_BYTE_REG_READ(ZM_INTR_SOURCE_7_OFFSET)&~BIT2))
75
76 #define vUsb_resm() USB_BYTE_REG_WRITE(ZM_INTR_SOURCE_7_OFFSET,     \
77                         (USB_BYTE_REG_READ(ZM_INTR_SOURCE_7_OFFSET)&~BIT3))
78
79 void _fw_usbfifo_recv_command(VBUF *buf)
80 {
81     A_UINT8 *cmd_data;
82     A_UINT32 tmp;
83
84     cmd_data = (A_UINT8 *)(buf->desc_list->buf_addr + buf->desc_list->data_offset);
85     tmp = *((A_UINT32 *)cmd_data);
86     if ( tmp == 0xFFFFFFFF ) {
87         _fw_usb_suspend_reboot();
88     } else {
89         m_origUsbfifoRecvCmd(buf);
90     }
91 }
92
93 void _fw_usbfifo_init(USB_FIFO_CONFIG *pConfig)
94 {
95     m_origUsbfifoRecvCmd = pConfig->recv_command;
96
97     usbFifoConf.get_command_buf = pConfig->get_command_buf;
98     usbFifoConf.recv_command    = _fw_usbfifo_recv_command;
99     usbFifoConf.get_event_buf   = pConfig->get_event_buf;
100     usbFifoConf.send_event_done = pConfig->send_event_done;
101 }
102
103 #define CHECK_SOF_LOOP_CNT    50
104
105 void _fw_usb_suspend_reboot()
106 {
107     volatile uint32_t gpio_in = 0;
108     volatile uint32_t pupd = 0;
109     volatile uint32_t t = 0;
110     volatile uint32_t sof_no=0,sof_no_new=0;
111     /* Set GO_TO_SUSPEND bit to USB main control register */
112     vUsb_suspend();
113     A_PRINTF("!USB suspend\n\r");
114
115     // keep the record of suspend
116 #if defined(PROJECT_MAGPIE)
117     *((volatile uint32_t*)WATCH_DOG_MAGIC_PATTERN_ADDR) = SUS_MAGIC_PATTERN;
118 #elif defined(PROJECT_K2)
119     HAL_WORD_REG_WRITE(MAGPIE_REG_RST_STATUS_ADDR, SUS_MAGIC_PATTERN);
120 #endif /* #if defined(PROJECT_MAGPIE) */
121
122     /* Reset USB FIFO */
123     A_USB_RESET_FIFO();
124
125     /* Turn off power */
126     A_USB_POWER_OFF();
127
128     DEBUG_SYSTEM_STATE = (DEBUG_SYSTEM_STATE&(~0xffff)) | 0x1000;
129
130     // reset ep3/ep4 fifo in case there is data which might affect resuming
131 //  HAL_BYTE_REG_WRITE(0x100ae, (HAL_BYTE_REG_READ(0x100ae)|0x10));
132 //  HAL_BYTE_REG_WRITE(0x100ae, (HAL_BYTE_REG_READ(0x100af)|0x10));
133
134     {
135         // config gpio to input before goto suspend
136
137         //disable JTAG/ICE
138         //jtag = HAL_WORD_REG_READ(0x10004054);
139         //HAL_WORD_REG_WRITE(0x10004054, (jtag|BIT17));
140                 
141                 //disable SPI
142         //spi = HAL_WORD_REG_READ(0x50040);
143         //HAL_WORD_REG_WRITE(0x50040, (spi&~(BIT8)));
144                 
145                 //set all GPIO to input
146         gpio_in = HAL_WORD_REG_READ(0x1000404c);
147         HAL_WORD_REG_WRITE(0x100404c, 0x0);
148                 
149                 //set PU/PD for all GPIO except two UART pins
150         pupd = HAL_WORD_REG_READ(0x10004088);
151         HAL_WORD_REG_WRITE(0x10004088, 0xA982AA6A);
152     }
153
154     sof_no= HAL_WORD_REG_READ(0x10004); 
155     for (t = 0; t < CHECK_SOF_LOOP_CNT; t++)
156     {
157         A_DELAY_USECS(1000);    //delay 1ms     
158         sof_no_new = HAL_WORD_REG_READ(0x10004);
159
160         if(sof_no_new == sof_no)
161             break; 
162
163         sof_no = sof_no_new;      
164     } 
165     
166     /* 
167      * Reset "printf" module patch point(RAM to ROM) when K2 warm start or suspend,  
168      * which fixed the error issue cause by redownload another different firmware. 
169      */
170     _indir_tbl.cmnos.printf._printf = save_cmnos_printf;
171     
172     ///////////////////////////////////////////////////////////////
173     // setting the go suspend here, power down right away...
174     if (t != CHECK_SOF_LOOP_CNT)   // not time out
175         HAL_WORD_REG_WRITE(0x10000, HAL_WORD_REG_READ(0x10000)|(0x8));
176     ///////////////////////////////////////////////////////////////
177
178     DEBUG_SYSTEM_STATE = (DEBUG_SYSTEM_STATE&(~0xffff)) | 0x1100;
179
180 #if 0 // pll unstable, h/w bug?
181     HAL_WORD_REG_WRITE(0x50040, (0x300|6|(1>>1)<<12));
182     A_UART_HWINIT((40*1000*1000)/1, 19200);
183 #endif
184     {
185         // restore gpio setting
186         //HAL_WORD_REG_WRITE(0x10004054, jtag);
187         //HAL_WORD_REG_WRITE(0x50040, spi);
188         HAL_WORD_REG_WRITE(0x1000404c, gpio_in);
189         HAL_WORD_REG_WRITE(0x10004088, pupd);
190     }
191     DEBUG_SYSTEM_STATE = (DEBUG_SYSTEM_STATE&(~0xffff)) | 0x1200;
192
193     {
194         // since we still need to touch mac_base address after resuming back, so that
195         // reset mac can't be done in ResetFifo function, move to here... 
196         // whole mac control reset.... (bit1)
197         HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, (BIT1) );
198         HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, (HAL_WORD_REG_READ(MAGPIE_REG_RST_PWDN_CTRL_ADDR)|BIT0));
199         HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, 0x0 );
200     }
201
202     //A_PRINTF("reg(0x10020)=(%x)\n", HAL_WORD_REG_READ(0x10020));
203     // disable ep3 int enable, so that resume back won't send wdt magic pattern out!!!
204     mUSB_STATUS_IN_INT_DISABLE();            
205
206     MAGPIE_REG_USB_RX0_SWAP_DATA = 0x1;
207     MAGPIE_REG_USB_TX0_SWAP_DATA = 0x1;
208     MAGPIE_REG_USB_RX1_SWAP_DATA = 0x1;
209     MAGPIE_REG_USB_RX2_SWAP_DATA = 0x1;
210
211     if (((DEBUG_SYSTEM_STATE&~(0x0000ffff))>>16 == 0x5342)) {
212         /* UART_SEL and SPI_SEL */
213         HAL_WORD_REG_WRITE(0x50040, (0x300|0|(1>>1)<<12));
214     }
215
216     /* Jump to boot code */
217     A_USB_JUMP_BOOT();
218
219 }
220
221 /*
222  * -- patch usb_fw_task --
223  * . usb zero length interrupt should not clear by s/w, h/w will handle that
224  * . complete suspend handle, configure gpio, turn off related function, 
225  *   slow down the pll for stable issue
226  */
227 void _fw_usb_fw_task(void)
228 {
229     register uint8_t usb_interrupt_level1;
230     register uint8_t usb_interrupt_level2;
231
232     usb_interrupt_level1 = USB_BYTE_REG_READ(ZM_INTR_GROUP_OFFSET);
233 #if 0 // these endpoints are handled by DMA
234     if (usb_interrupt_level1 & BIT5)            //Group Byte 5
235     {
236         vUsb_Data_In();
237     }
238 #endif
239     if (usb_interrupt_level1 & BIT4)
240     {
241         usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_4_OFFSET);
242         if( usb_interrupt_level2 & BIT6)
243             A_USB_REG_OUT();//vUsb_Reg_Out();
244     }
245
246     if (usb_interrupt_level1 & BIT6)
247     {
248         //zfGenWatchDogEvent();
249     usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_6_OFFSET);
250         if( usb_interrupt_level2 & BIT6)
251              A_USB_STATUS_IN();//vUsb_Status_In();
252     }
253
254     if (usb_interrupt_level1 & BIT0)            //Group Byte 0
255     {
256         //usb_interrupt_level2 = ZM_INTR_SOURCE_0_REG;
257         usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_0_OFFSET);
258
259         // refer to FUSB200, p 48, offset:21H, bit7 description, should clear the command abort interrupt first!?
260         if (usb_interrupt_level2 & BIT7)
261         {
262             //ZM_INTR_SOURCE_0_REG &= 0x7f;       // Handle command abort
263             USB_BYTE_REG_WRITE(ZM_INTR_SOURCE_0_OFFSET, (USB_BYTE_REG_READ(ZM_INTR_SOURCE_0_OFFSET)& ~BIT7));
264             A_PRINTF("![SOURCE_0] bit7 on\n\r");
265         }
266
267         if (usb_interrupt_level2 & BIT1)
268         {
269             //A_PRINTF("![USB] ep0 IN in \n\r");
270             A_USB_EP0_TX();                       // USB EP0 tx interrupt
271         }
272         if (usb_interrupt_level2 & BIT2)
273         {
274             //A_PRINTF("![USB] ep0 OUT in\n\r");
275             A_USB_EP0_RX();                       // USB EP0 rx interrupt
276         }
277         if (usb_interrupt_level2 & BIT0)
278         {
279             //A_PRINTF("![USB] ep0 SETUP in\n\r");
280             A_USB_EP0_SETUP();
281             //vWriteUSBFakeData();
282         }
283 //        else if (usb_interrupt_level2 & BIT3)
284         if (usb_interrupt_level2 & BIT3)
285         {
286             vUsb_ep0end();
287 //            A_PRINTF("![SOURCE_0] ep0 CMD_END\n\r");
288         }
289         if (usb_interrupt_level2 & BIT4)
290         {
291             vUsb_ep0fail();
292 //            A_PRINTF("![SOURCE_0] ep0 CMD_FAIL\n\r");
293         }
294         if (eUsbCxFinishAction == ACT_STALL)
295         {
296             // set CX_STL to stall Endpoint0 & will also clear FIFO0
297             USB_BYTE_REG_WRITE(ZM_CX_CONFIG_STATUS_OFFSET, 0x04);
298 //            A_PRINTF("![USB] ZM_CX_CONFIG_STATUS_REG = 0x04\n\r");
299         }
300         else if (eUsbCxFinishAction == ACT_DONE)
301         {
302             // set CX_DONE to indicate the transmistion of control frame
303             USB_BYTE_REG_WRITE(ZM_CX_CONFIG_STATUS_OFFSET, 0x01);
304         }
305         eUsbCxFinishAction = ACT_IDLE;
306     }
307
308     if (usb_interrupt_level1 & BIT7)            //Group Byte 7
309     {
310         //usb_interrupt_level2 = ZM_INTR_SOURCE_7_REG;
311         usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_7_OFFSET);
312
313 #if 0
314         if (usb_interrupt_level2 & BIT7)
315         {
316             vUsb_Data_Out0Byte();
317 //            A_PRINTF("![SOURCE_7] bit7 on, clear it\n\r");
318         }
319         if (usb_interrupt_level2 & BIT6)
320         {
321             vUsb_Data_In0Byte();
322 //            A_PRINTF("![SOURCE_7] bit6 on, clear it\n\r");
323         }
324 #endif
325         
326         if (usb_interrupt_level2 & BIT1)
327         {
328             vUsb_rst();
329             //USB_BYTE_REG_WRITE(ZM_INTR_SOURCE_7_REG, (USB_BYTE_REG_READ(ZM_INTR_SOURCE_7_OFFSET)&~0x2));
330             A_PRINTF("!USB reset\n\r");
331 //            A_PRINTF("![0x1012c]: %\n\r", USB_WORD_REG_READ(0x12c));
332         }
333         if (usb_interrupt_level2 & BIT2)
334         {
335            // TBD: the suspend resume code should put here, Ryan, 07/18
336            //
337            //  issue, jump back to rom code and what peripherals should we reset here?
338            //
339            _fw_usb_suspend_reboot();            
340         }
341         if (usb_interrupt_level2 & BIT3)
342         {
343             vUsb_resm();
344             A_PRINTF("!USB resume\n\r");
345         }
346     }
347
348 }
349
350
351 void _fw_usb_reset_fifo(void)
352 {
353     volatile uint32_t   *reg_data;
354
355     HAL_BYTE_REG_WRITE(0x100ae, (HAL_BYTE_REG_READ(0x100ae)|0x10));
356     HAL_BYTE_REG_WRITE(0x100ae, (HAL_BYTE_REG_READ(0x100af)|0x10));
357
358     // disable ep3 int enable, so that resume back won't send wdt magic pattern out!!!
359     mUSB_STATUS_IN_INT_DISABLE();
360
361     // update magic pattern to indicate this is a suspend
362     // k2: MAGPIE_REG_RST_WDT_TIMER_CTRL_ADDR
363     // magpie: MAGPIE_REG_RST_STATUS_ADDR
364     HAL_WORD_REG_WRITE(MAGPIE_REG_RST_STATUS_ADDR, SUS_MAGIC_PATTERN);
365
366     /*
367      * Before USB suspend, USB DMA must be reset(refer to Otus)
368      * Otus runs the following statements only
369      * HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, BIT0|BIT2 );
370      * HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, 0x0 );
371      * K2 must run the following statements additionally
372      * reg_data = (A_UINT32 *)(USB_CTRL_BASE_ADDRESS + 0x118);
373      * *reg_data = 0x00000000;
374      * *reg_data = 0x00000001;
375      * because of Hardware bug in K2
376      */
377     reg_data = (uint32_t *)(USB_CTRL_BASE_ADDRESS + 0x118);
378     *reg_data = 0x00000000;
379
380     // reset both usb(bit2)/wlan(bit1) dma
381     HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, (BIT2) );
382     HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, (HAL_WORD_REG_READ(MAGPIE_REG_RST_PWDN_CTRL_ADDR)|BIT0));
383     HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, 0x0 );
384
385     *reg_data = 0x00000001;
386
387     /* MAC warem reset */
388     //reg_data = (uint32_t *)(K2_REG_MAC_BASE_ADDR + 0x7000);
389     //*reg_data = 0x00000001;
390
391     //A_DELAY_USECS(1);
392
393     //*reg_data = 0x00000000;
394
395     //while (*reg_data)   ;
396
397     A_PRINTF("\n change clock to 22 and go to suspend now!");
398     
399     /* UART_SEL */
400     HAL_WORD_REG_WRITE(0x50040, (0x200|0|(1>>1)<<12));
401     A_UART_HWINIT((22*1000*1000), 19200);
402 }
403
404 /*
405  * -- support more than 64 bytes command on ep4 -- 
406  */
407 void vUsb_Reg_Out_patch(void)
408 {   
409     uint16_t usbfifolen;
410     uint16_t ii;
411     uint32_t ep4_data;
412     static volatile uint32_t *regaddr;    // = (volatile uint32_t *) ZM_CMD_BUFFER;
413     static uint16_t cmdLen;
414     static VBUF *buf;
415     BOOLEAN cmd_is_last = FALSE;
416     static BOOLEAN cmd_is_new = TRUE;
417
418     // get the size of this transcation
419     usbfifolen = USB_BYTE_REG_READ(ZM_EP4_BYTE_COUNT_LOW_OFFSET);
420
421     // check is command is new
422     if( cmd_is_new ){
423
424         buf = usbFifoConf.get_command_buf();
425         cmdLen = 0;
426     
427         if( !buf )
428             goto ERR;
429     
430         // copy free, assignment buffer of the address
431         regaddr = (uint32_t *)buf->desc_list->buf_addr;
432
433         cmd_is_new = FALSE;
434     }
435     
436     // just in case, suppose should not happen
437     if( !buf )
438         goto ERR;
439     
440     // if size is smaller, this is the last command!
441     //
442     // zero-length supposed should be set through 0x27/bit7->0x19/bit4, not here
443     //
444     if( usbfifolen<bUSB_EP_MAX_PKT_SIZE_64 ) {
445         cmd_is_last = TRUE;
446     }
447     
448     // accumulate the size
449     cmdLen += usbfifolen;
450     
451     // round it to alignment
452     if(usbfifolen % 4)
453         usbfifolen = (usbfifolen >> 2) + 1;
454     else
455         usbfifolen = usbfifolen >> 2;
456     
457 //    A_PRINTF("copy data out from fifo to - %p\n\r", regaddr);
458     // retrieve the data from fifo
459     for(ii = 0; ii < usbfifolen; ii++)
460     {
461         ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);   // read fifo data out
462         *regaddr = ep4_data;
463         regaddr++;
464     }
465     
466     // if this is the last command, callback to HTC
467     if (  cmd_is_last  )
468     {
469         buf->desc_list->next_desc = NULL;
470         buf->desc_list->data_offset = 0;
471         buf->desc_list->data_size = cmdLen;
472         buf->desc_list->control = 0;
473         buf->next_buf = NULL;
474         buf->buf_length = cmdLen;
475     
476         usbFifoConf.recv_command(buf);
477
478         cmd_is_new = TRUE;
479     }
480
481     goto DONE;
482 ERR:
483 //    we might get no command buffer here?
484 //    but if we return here, the ep4 fifo will be lock out,
485 //    so that we still read them out but just drop it ?
486     for(ii = 0; ii < usbfifolen; ii++)
487     {
488         ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);   // read fifo data out
489     }
490
491 DONE:
492     //mUSB_STATUS_IN_INT_ENABLE();
493     ;
494 }
495
496
497
498 /* 
499  *  -- usb1.1 ep6 fix --
500  */
501 extern uint16_t       u8UsbConfigValue;
502 extern uint16_t       u8UsbInterfaceValue;
503 extern uint16_t       u8UsbInterfaceAlternateSetting;
504 extern SetupPacket    ControlCmd;
505 extern void vUsbClrEPx(void);
506
507 void vUSBFIFO_EP6Cfg_FS_patch(void)
508 {
509 #if (FS_C1_I0_A0_EP_NUMBER >= 6)
510     int i;
511
512     //EP0X06
513     mUsbEPMap(EP6, FS_C1_I0_A0_EP6_MAP);
514     mUsbFIFOMap(FS_C1_I0_A0_EP6_FIFO_START, FS_C1_I0_A0_EP6_FIFO_MAP);
515     mUsbFIFOConfig(FS_C1_I0_A0_EP6_FIFO_START, FS_C1_I0_A0_EP6_FIFO_CONFIG);
516
517     for(i = FS_C1_I0_A0_EP6_FIFO_START + 1 ;
518         i < FS_C1_I0_A0_EP6_FIFO_START + FS_C1_I0_A0_EP6_FIFO_NO ; i ++)
519     {
520         mUsbFIFOConfig(i, (FS_C1_I0_A0_EP6_FIFO_CONFIG & (~BIT7)) );
521     }
522                             
523     mUsbEPMxPtSzHigh(EP6, FS_C1_I0_A0_EP6_DIRECTION, (FS_C1_I0_A0_EP6_MAX_PACKET & 0x7ff));
524     mUsbEPMxPtSzLow(EP6, FS_C1_I0_A0_EP6_DIRECTION, (FS_C1_I0_A0_EP6_MAX_PACKET & 0x7ff));
525     mUsbEPinHighBandSet(EP6 , FS_C1_I0_A0_EP6_DIRECTION, FS_C1_I0_A0_EP6_MAX_PACKET);
526 #endif
527 }
528
529 void vUsbFIFO_EPxCfg_FS_patch(void)
530 {
531     switch (u8UsbConfigValue)
532     {
533         #if (FS_CONFIGURATION_NUMBER >= 1)
534         // Configuration 0X01
535         case 0X01:
536             switch (u8UsbInterfaceValue)
537             {
538                 #if (FS_C1_INTERFACE_NUMBER >= 1)
539                 // Interface 0
540                 case 0:
541                     switch (u8UsbInterfaceAlternateSetting)
542                     {
543
544                         #if (FS_C1_I0_ALT_NUMBER >= 1)
545                         // AlternateSetting 0
546                         case 0:
547
548                                                         // snapped....
549
550                                                         // patch up this ep6_fs config
551                             vUSBFIFO_EP6Cfg_FS_patch();
552
553                             break;
554
555                         #endif
556                         default:
557                             break;
558                     }
559                     break;
560                 #endif
561                 default:
562                     break;
563             }
564             break;
565         #endif
566         default:
567             break;
568     }
569     //mCHECK_STACK();
570 }
571
572
573 BOOLEAN bSet_configuration_patch(void)
574 {
575     //A_PRINTF("bSet_configuration...\n\r");
576
577         bSet_configuration();
578
579     if (mLOW_BYTE(mDEV_REQ_VALUE()) == 0)
580     {
581                 // snapped....
582                 ;
583     }
584     else
585     {
586         if (mUsbHighSpeedST())                  // First judge HS or FS??
587         {
588                         // snapped....
589                         ;
590         }
591         else
592         {
593                         // snapped....
594                         vUsbFIFO_EPxCfg_FS_patch();
595         }
596             
597                 // snapped....
598     }
599
600     eUsbCxFinishAction = ACT_DONE;
601     return TRUE;
602 }
603
604
605 /*
606  * -- support more than 64 bytes command on ep3 -- 
607  */
608 void vUsb_Status_In_patch(void)
609 {
610     uint16_t count;
611     uint16_t remainder;
612     u16_t RegBufLen;
613     BOOLEAN cmdEnd = FALSE;
614
615     static u16_t mBufLen;
616     static VBUF *evntbuf = NULL;
617     static volatile u32_t *regaddr;
618     static BOOLEAN cmd_is_new = TRUE;
619
620     if( cmd_is_new )
621     {
622         evntbuf = usbFifoConf.get_event_buf();
623         if ( evntbuf != NULL )
624         {
625             regaddr = (u32_t *)VBUF_GET_DATA_ADDR(evntbuf);
626             mBufLen = evntbuf->buf_length;
627         }
628         else
629         {
630             mUSB_STATUS_IN_INT_DISABLE();
631             goto ERR_DONE;
632         }
633     
634     }
635
636 //    if( mBufLen>bUSB_EP_MAX_PKT_SIZE_64 )
637 //        A_PRINTF("EP3 send %d bytes to host \n", mBufLen);
638
639 //    while(1)
640     {
641         if( mBufLen > bUSB_EP_MAX_PKT_SIZE_64 ) {
642             RegBufLen = bUSB_EP_MAX_PKT_SIZE_64;
643             mBufLen -= bUSB_EP_MAX_PKT_SIZE_64;
644         }
645         // TODO: 64 byes... controller supposed will take care of zero-length?
646         else {
647             RegBufLen = mBufLen;
648             cmdEnd = TRUE;
649         }
650
651         /* INT use EP3 */
652         for(count = 0; count < (RegBufLen / 4); count++)
653         {
654             USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, *regaddr);
655             regaddr++;
656         }
657         
658         remainder = RegBufLen % 4;
659         
660         if (remainder)
661         {
662             switch(remainder)
663             {
664             case 3:
665                 USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x7);
666                 break;
667             case 2:
668                 USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x3);
669                 break;
670             case 1:
671                 USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x1);
672                 break;
673             }
674         
675             USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, *regaddr);
676
677             // Restore CBus FIFO size to word size            
678             USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0xF);
679         }
680
681         mUSB_EP3_XFER_DONE();
682         
683 //        if( mBufLen<=bUSB_EP_MAX_PKT_SIZE_64 )
684 //            break;   
685     }
686
687     if ( evntbuf != NULL && cmdEnd )
688     {
689         usbFifoConf.send_event_done(evntbuf);
690         cmd_is_new = TRUE;
691     }
692     
693 ERR_DONE:
694     ;
695 }
696
697 extern uint16_t *u8ConfigDescriptorEX;
698 extern uint16_t *pu8DescriptorEX;
699 extern uint16_t u16TxRxCounter;
700 extern BOOLEAN bGet_descriptor(void);
701
702 uint16_t ConfigDescriptorPatch[30];
703
704 #define EP3_TRANSFER_TYPE_OFFSET    17
705 #define EP3_INT_INTERVAL            19
706 #define EP4_TRANSFER_TYPE_OFFSET    21
707 #define EP4_INT_INTERVAL            22
708
709 BOOLEAN bGet_descriptor_patch(void)
710 {
711     if (mDEV_REQ_VALUE_HIGH() == 2) {
712         uint8_t *p = (uint8_t *)u8ConfigDescriptorEX;
713
714         /* Copy ConfigDescriptor */
715         ath_hal_memcpy(ConfigDescriptorPatch, p, sizeof(ConfigDescriptorPatch));
716
717         p = (uint8_t *)ConfigDescriptorPatch;
718
719         /* Patch the transfer type of EP3 and EP4 */
720         ConfigDescriptorPatch[EP3_TRANSFER_TYPE_OFFSET] = 0x0283;
721         ConfigDescriptorPatch[EP3_INT_INTERVAL] = 0x0700;
722         ConfigDescriptorPatch[EP4_TRANSFER_TYPE_OFFSET] = 0x4002;
723         ConfigDescriptorPatch[EP4_INT_INTERVAL] = 0x00;
724
725         switch (mDEV_REQ_VALUE_LOW())
726         {
727         case 0x00:      // configuration no: 0
728             pu8DescriptorEX = ConfigDescriptorPatch;
729             u16TxRxCounter = ConfigDescriptorPatch[1];
730             //u16TxRxCounter = 46;
731             break;
732         default:
733             return FALSE;
734         }
735
736         if (u16TxRxCounter > mDEV_REQ_LENGTH())
737             u16TxRxCounter = mDEV_REQ_LENGTH();
738
739         A_USB_EP0_TX_DATA();
740         return TRUE;
741     }
742     else {
743         return bGet_descriptor();
744     }
745 }
746
747 extern BOOLEAN bStandardCommand(void);
748
749 BOOLEAN bStandardCommand_patch(void)
750 {
751     if (mDEV_REQ_REQ() == USB_SET_CONFIGURATION) {
752         A_USB_SET_CONFIG();
753
754 #if ENABLE_SWAP_DATA_MODE
755         // SWAP FUNCTION should be enabled while DMA engine is not working,
756         // the best place to enable it is before we trigger the DMA
757         MAGPIE_REG_USB_RX0_SWAP_DATA = 0x1;
758         MAGPIE_REG_USB_TX0_SWAP_DATA = 0x1;
759
760         #if SYSTEM_MODULE_HP_EP5
761             MAGPIE_REG_USB_RX1_SWAP_DATA = 0x1;
762         #endif
763
764         #if SYSTEM_MODULE_HP_EP6
765             MAGPIE_REG_USB_RX2_SWAP_DATA = 0x1;
766         #endif
767
768 #endif //ENABLE_SWAP_DATA_MODE
769         return TRUE;
770     }
771     else {
772         return bStandardCommand();
773     }
774 }
775
776 #endif
777
778