k2_fw_usb_api: prevent buffer overflow.
[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(0x1000404c, 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         A_DELAY_USECS(1000);
201     }
202
203     //A_PRINTF("reg(0x10020)=(%x)\n", HAL_WORD_REG_READ(0x10020));
204     // disable ep3 int enable, so that resume back won't send wdt magic pattern out!!!
205     mUSB_STATUS_IN_INT_DISABLE();            
206
207     MAGPIE_REG_USB_RX0_SWAP_DATA = 0x1;
208     MAGPIE_REG_USB_TX0_SWAP_DATA = 0x1;
209     MAGPIE_REG_USB_RX1_SWAP_DATA = 0x1;
210     MAGPIE_REG_USB_RX2_SWAP_DATA = 0x1;
211
212     if (((DEBUG_SYSTEM_STATE&~(0x0000ffff))>>16 == 0x5342)) {
213         /* UART_SEL and SPI_SEL */
214         HAL_WORD_REG_WRITE(0x50040, (0x300|0|(1>>1)<<12));
215     }
216
217     /* Jump to boot code */
218     A_USB_JUMP_BOOT();
219
220 }
221
222 /*
223  * -- patch usb_fw_task --
224  * . usb zero length interrupt should not clear by s/w, h/w will handle that
225  * . complete suspend handle, configure gpio, turn off related function, 
226  *   slow down the pll for stable issue
227  */
228 void _fw_usb_fw_task(void)
229 {
230     register uint8_t usb_interrupt_level1;
231     register uint8_t usb_interrupt_level2;
232
233     usb_interrupt_level1 = USB_BYTE_REG_READ(ZM_INTR_GROUP_OFFSET);
234 #if 0 // these endpoints are handled by DMA
235     if (usb_interrupt_level1 & BIT5)            //Group Byte 5
236     {
237         vUsb_Data_In();
238     }
239 #endif
240     if (usb_interrupt_level1 & BIT4)
241     {
242         usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_4_OFFSET);
243         if( usb_interrupt_level2 & BIT6)
244             A_USB_REG_OUT();//vUsb_Reg_Out();
245     }
246
247     if (usb_interrupt_level1 & BIT6)
248     {
249         //zfGenWatchDogEvent();
250     usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_6_OFFSET);
251         if( usb_interrupt_level2 & BIT6)
252              A_USB_STATUS_IN();//vUsb_Status_In();
253     }
254
255     if (usb_interrupt_level1 & BIT0)            //Group Byte 0
256     {
257         //usb_interrupt_level2 = ZM_INTR_SOURCE_0_REG;
258         usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_0_OFFSET);
259
260         // refer to FUSB200, p 48, offset:21H, bit7 description, should clear the command abort interrupt first!?
261         if (usb_interrupt_level2 & BIT7)
262         {
263             //ZM_INTR_SOURCE_0_REG &= 0x7f;       // Handle command abort
264             USB_BYTE_REG_WRITE(ZM_INTR_SOURCE_0_OFFSET, (USB_BYTE_REG_READ(ZM_INTR_SOURCE_0_OFFSET)& ~BIT7));
265             A_PRINTF("![SOURCE_0] bit7 on\n\r");
266         }
267
268         if (usb_interrupt_level2 & BIT1)
269         {
270             //A_PRINTF("![USB] ep0 IN in \n\r");
271             A_USB_EP0_TX();                       // USB EP0 tx interrupt
272         }
273         if (usb_interrupt_level2 & BIT2)
274         {
275             //A_PRINTF("![USB] ep0 OUT in\n\r");
276             A_USB_EP0_RX();                       // USB EP0 rx interrupt
277         }
278         if (usb_interrupt_level2 & BIT0)
279         {
280             //A_PRINTF("![USB] ep0 SETUP in\n\r");
281             A_USB_EP0_SETUP();
282             //vWriteUSBFakeData();
283         }
284 //        else if (usb_interrupt_level2 & BIT3)
285         if (usb_interrupt_level2 & BIT3)
286         {
287             vUsb_ep0end();
288 //            A_PRINTF("![SOURCE_0] ep0 CMD_END\n\r");
289         }
290         if (usb_interrupt_level2 & BIT4)
291         {
292             vUsb_ep0fail();
293 //            A_PRINTF("![SOURCE_0] ep0 CMD_FAIL\n\r");
294         }
295         if (eUsbCxFinishAction == ACT_STALL)
296         {
297             // set CX_STL to stall Endpoint0 & will also clear FIFO0
298             USB_BYTE_REG_WRITE(ZM_CX_CONFIG_STATUS_OFFSET, 0x04);
299 //            A_PRINTF("![USB] ZM_CX_CONFIG_STATUS_REG = 0x04\n\r");
300         }
301         else if (eUsbCxFinishAction == ACT_DONE)
302         {
303             // set CX_DONE to indicate the transmistion of control frame
304             USB_BYTE_REG_WRITE(ZM_CX_CONFIG_STATUS_OFFSET, 0x01);
305         }
306         eUsbCxFinishAction = ACT_IDLE;
307     }
308
309     if (usb_interrupt_level1 & BIT7)            //Group Byte 7
310     {
311         //usb_interrupt_level2 = ZM_INTR_SOURCE_7_REG;
312         usb_interrupt_level2 = USB_BYTE_REG_READ(ZM_INTR_SOURCE_7_OFFSET);
313
314 #if 0
315         if (usb_interrupt_level2 & BIT7)
316         {
317             vUsb_Data_Out0Byte();
318 //            A_PRINTF("![SOURCE_7] bit7 on, clear it\n\r");
319         }
320         if (usb_interrupt_level2 & BIT6)
321         {
322             vUsb_Data_In0Byte();
323 //            A_PRINTF("![SOURCE_7] bit6 on, clear it\n\r");
324         }
325 #endif
326         
327         if (usb_interrupt_level2 & BIT1)
328         {
329             vUsb_rst();
330             //USB_BYTE_REG_WRITE(ZM_INTR_SOURCE_7_REG, (USB_BYTE_REG_READ(ZM_INTR_SOURCE_7_OFFSET)&~0x2));
331             A_PRINTF("!USB reset\n\r");
332 //            A_PRINTF("![0x1012c]: %\n\r", USB_WORD_REG_READ(0x12c));
333         }
334         if (usb_interrupt_level2 & BIT2)
335         {
336            // TBD: the suspend resume code should put here, Ryan, 07/18
337            //
338            //  issue, jump back to rom code and what peripherals should we reset here?
339            //
340            _fw_usb_suspend_reboot();            
341         }
342         if (usb_interrupt_level2 & BIT3)
343         {
344             vUsb_resm();
345             A_PRINTF("!USB resume\n\r");
346         }
347     }
348
349 }
350
351
352 void _fw_usb_reset_fifo(void)
353 {
354     volatile uint32_t   *reg_data;
355
356     HAL_BYTE_REG_WRITE(0x100ae, (HAL_BYTE_REG_READ(0x100ae)|0x10));
357     HAL_BYTE_REG_WRITE(0x100af, (HAL_BYTE_REG_READ(0x100af)|0x10));
358
359     // disable ep3 int enable, so that resume back won't send wdt magic pattern out!!!
360     mUSB_STATUS_IN_INT_DISABLE();
361
362     // update magic pattern to indicate this is a suspend
363     // k2: MAGPIE_REG_RST_WDT_TIMER_CTRL_ADDR
364     // magpie: MAGPIE_REG_RST_STATUS_ADDR
365     HAL_WORD_REG_WRITE(MAGPIE_REG_RST_STATUS_ADDR, SUS_MAGIC_PATTERN);
366
367     /*
368      * Before USB suspend, USB DMA must be reset(refer to Otus)
369      * Otus runs the following statements only
370      * HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, BIT0|BIT2 );
371      * HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, 0x0 );
372      * K2 must run the following statements additionally
373      * reg_data = (A_UINT32 *)(USB_CTRL_BASE_ADDRESS + 0x118);
374      * *reg_data = 0x00000000;
375      * *reg_data = 0x00000001;
376      * because of Hardware bug in K2
377      */
378     reg_data = (uint32_t *)(USB_CTRL_BASE_ADDRESS + 0x118);
379     *reg_data = 0x00000000;
380
381     // reset both usb(bit2)/wlan(bit1) dma
382     HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, (BIT2) );
383     HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, (HAL_WORD_REG_READ(MAGPIE_REG_RST_PWDN_CTRL_ADDR)|BIT0));
384     HAL_WORD_REG_WRITE( MAGPIE_REG_RST_PWDN_CTRL_ADDR, 0x0 );
385
386     *reg_data = 0x00000001;
387
388     /* MAC warem reset */
389     //reg_data = (uint32_t *)(K2_REG_MAC_BASE_ADDR + 0x7000);
390     //*reg_data = 0x00000001;
391
392     //A_DELAY_USECS(1);
393
394     //*reg_data = 0x00000000;
395
396     //while (*reg_data)   ;
397
398     A_PRINTF("\n change clock to 22 and go to suspend now!");
399     
400     /* UART_SEL */
401     HAL_WORD_REG_WRITE(0x50040, (0x200|0|(1>>1)<<12));
402     A_UART_HWINIT((22*1000*1000), 19200);
403 }
404
405 /*
406  * -- support more than 64 bytes command on ep4 -- 
407  */
408 void vUsb_Reg_Out_patch(void)
409 {   
410     uint16_t usbfifolen;
411     uint16_t ii;
412     uint32_t ep4_data;
413     static volatile uint32_t *regaddr;    // = (volatile uint32_t *) ZM_CMD_BUFFER;
414     static uint16_t cmdLen;
415     static VBUF *buf;
416     BOOLEAN cmd_is_last = FALSE;
417     static BOOLEAN cmd_is_new = TRUE;
418
419     // get the size of this transcation
420     usbfifolen = USB_BYTE_REG_READ(ZM_EP4_BYTE_COUNT_LOW_OFFSET);
421     if (usbfifolen > 0x40) {
422         A_PRINTF("EP4 FIFO Bug? Buffer is too big: %x\n", usbfifolen);
423         goto ERR;
424     }
425
426     // check is command is new
427     if( cmd_is_new ){
428
429         buf = usbFifoConf.get_command_buf();
430         cmdLen = 0;
431     
432         if( !buf )
433             goto ERR;
434     
435         // copy free, assignment buffer of the address
436         regaddr = (uint32_t *)buf->desc_list->buf_addr;
437
438         cmd_is_new = FALSE;
439     }
440     
441     // just in case, suppose should not happen
442     if( !buf )
443         goto ERR;
444     
445     // if size is smaller, this is the last command!
446     //
447     // zero-length supposed should be set through 0x27/bit7->0x19/bit4, not here
448     //
449     if( usbfifolen<bUSB_EP_MAX_PKT_SIZE_64 ) {
450         cmd_is_last = TRUE;
451     }
452     
453     // accumulate the size
454     cmdLen += usbfifolen;
455     if (cmdLen > buf->desc_list->buf_size) {
456         A_PRINTF("Data length on EP4 FIFO is bigger as allocated buffer data!"
457                  " Drop it!\n");
458         goto ERR;
459     }
460     
461     // round it to alignment
462     if(usbfifolen % 4)
463         usbfifolen = (usbfifolen >> 2) + 1;
464     else
465         usbfifolen = usbfifolen >> 2;
466     
467 //    A_PRINTF("copy data out from fifo to - %p\n\r", regaddr);
468     // retrieve the data from fifo
469     for(ii = 0; ii < usbfifolen; ii++)
470     {
471         ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);   // read fifo data out
472         *regaddr = ep4_data;
473         regaddr++;
474     }
475     
476     // if this is the last command, callback to HTC
477     if (  cmd_is_last  )
478     {
479         buf->desc_list->next_desc = NULL;
480         buf->desc_list->data_offset = 0;
481         buf->desc_list->data_size = cmdLen;
482         buf->desc_list->control = 0;
483         buf->next_buf = NULL;
484         buf->buf_length = cmdLen;
485     
486         usbFifoConf.recv_command(buf);
487
488         cmd_is_new = TRUE;
489     }
490
491     goto DONE;
492 ERR:
493 //    we might get no command buffer here?
494 //    but if we return here, the ep4 fifo will be lock out,
495 //    so that we still read them out but just drop it ?
496     for(ii = 0; ii < usbfifolen; ii++)
497     {
498         ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);   // read fifo data out
499     }
500
501 DONE:
502     //mUSB_STATUS_IN_INT_ENABLE();
503     ;
504 }
505
506
507
508 /* 
509  *  -- usb1.1 ep6 fix --
510  */
511 extern uint16_t       u8UsbConfigValue;
512 extern uint16_t       u8UsbInterfaceValue;
513 extern uint16_t       u8UsbInterfaceAlternateSetting;
514 extern SetupPacket    ControlCmd;
515 extern void vUsbClrEPx(void);
516
517 void vUSBFIFO_EP6Cfg_FS_patch(void)
518 {
519 #if (FS_C1_I0_A0_EP_NUMBER >= 6)
520     int i;
521
522     //EP0X06
523     mUsbEPMap(EP6, FS_C1_I0_A0_EP6_MAP);
524     mUsbFIFOMap(FS_C1_I0_A0_EP6_FIFO_START, FS_C1_I0_A0_EP6_FIFO_MAP);
525     mUsbFIFOConfig(FS_C1_I0_A0_EP6_FIFO_START, FS_C1_I0_A0_EP6_FIFO_CONFIG);
526
527     for(i = FS_C1_I0_A0_EP6_FIFO_START + 1 ;
528         i < FS_C1_I0_A0_EP6_FIFO_START + FS_C1_I0_A0_EP6_FIFO_NO ; i ++)
529     {
530         mUsbFIFOConfig(i, (FS_C1_I0_A0_EP6_FIFO_CONFIG & (~BIT7)) );
531     }
532                             
533     mUsbEPMxPtSzHigh(EP6, FS_C1_I0_A0_EP6_DIRECTION, (FS_C1_I0_A0_EP6_MAX_PACKET & 0x7ff));
534     mUsbEPMxPtSzLow(EP6, FS_C1_I0_A0_EP6_DIRECTION, (FS_C1_I0_A0_EP6_MAX_PACKET & 0x7ff));
535     mUsbEPinHighBandSet(EP6 , FS_C1_I0_A0_EP6_DIRECTION, FS_C1_I0_A0_EP6_MAX_PACKET);
536 #endif
537 }
538
539 void vUsbFIFO_EPxCfg_FS_patch(void)
540 {
541     switch (u8UsbConfigValue)
542     {
543         #if (FS_CONFIGURATION_NUMBER >= 1)
544         // Configuration 0X01
545         case 0X01:
546             switch (u8UsbInterfaceValue)
547             {
548                 #if (FS_C1_INTERFACE_NUMBER >= 1)
549                 // Interface 0
550                 case 0:
551                     switch (u8UsbInterfaceAlternateSetting)
552                     {
553
554                         #if (FS_C1_I0_ALT_NUMBER >= 1)
555                         // AlternateSetting 0
556                         case 0:
557
558                                                         // snapped....
559
560                                                         // patch up this ep6_fs config
561                             vUSBFIFO_EP6Cfg_FS_patch();
562
563                             break;
564
565                         #endif
566                         default:
567                             break;
568                     }
569                     break;
570                 #endif
571                 default:
572                     break;
573             }
574             break;
575         #endif
576         default:
577             break;
578     }
579     //mCHECK_STACK();
580 }
581
582
583 BOOLEAN bSet_configuration_patch(void)
584 {
585     //A_PRINTF("bSet_configuration...\n\r");
586
587         bSet_configuration();
588
589     if (mLOW_BYTE(mDEV_REQ_VALUE()) == 0)
590     {
591                 // snapped....
592                 ;
593     }
594     else
595     {
596         if (mUsbHighSpeedST())                  // First judge HS or FS??
597         {
598                         // snapped....
599                         ;
600         }
601         else
602         {
603                         // snapped....
604                         vUsbFIFO_EPxCfg_FS_patch();
605         }
606             
607                 // snapped....
608     }
609
610     eUsbCxFinishAction = ACT_DONE;
611     return TRUE;
612 }
613
614
615 /*
616  * -- support more than 64 bytes command on ep3 -- 
617  */
618 void vUsb_Status_In_patch(void)
619 {
620     uint16_t count;
621     uint16_t remainder;
622     u16_t RegBufLen;
623     BOOLEAN cmdEnd = FALSE;
624
625     static u16_t mBufLen;
626     static VBUF *evntbuf = NULL;
627     static volatile u32_t *regaddr;
628     static BOOLEAN cmd_is_new = TRUE;
629
630     if( cmd_is_new )
631     {
632         evntbuf = usbFifoConf.get_event_buf();
633         if ( evntbuf != NULL )
634         {
635             regaddr = (u32_t *)VBUF_GET_DATA_ADDR(evntbuf);
636             mBufLen = evntbuf->buf_length;
637         }
638         else
639         {
640             mUSB_STATUS_IN_INT_DISABLE();
641             goto ERR_DONE;
642         }
643     
644     }
645
646 //    if( mBufLen>bUSB_EP_MAX_PKT_SIZE_64 )
647 //        A_PRINTF("EP3 send %d bytes to host \n", mBufLen);
648
649 //    while(1)
650     {
651         if( mBufLen > bUSB_EP_MAX_PKT_SIZE_64 ) {
652             RegBufLen = bUSB_EP_MAX_PKT_SIZE_64;
653             mBufLen -= bUSB_EP_MAX_PKT_SIZE_64;
654         }
655         // TODO: 64 byes... controller supposed will take care of zero-length?
656         else {
657             RegBufLen = mBufLen;
658             cmdEnd = TRUE;
659         }
660
661         /* INT use EP3 */
662         for(count = 0; count < (RegBufLen / 4); count++)
663         {
664             USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, *regaddr);
665             regaddr++;
666         }
667         
668         remainder = RegBufLen % 4;
669         
670         if (remainder)
671         {
672             switch(remainder)
673             {
674             case 3:
675                 USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x7);
676                 break;
677             case 2:
678                 USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x3);
679                 break;
680             case 1:
681                 USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x1);
682                 break;
683             }
684         
685             USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, *regaddr);
686
687             // Restore CBus FIFO size to word size            
688             USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0xF);
689         }
690
691         mUSB_EP3_XFER_DONE();
692         
693 //        if( mBufLen<=bUSB_EP_MAX_PKT_SIZE_64 )
694 //            break;   
695     }
696
697     if ( evntbuf != NULL && cmdEnd )
698     {
699         usbFifoConf.send_event_done(evntbuf);
700         cmd_is_new = TRUE;
701     }
702     
703 ERR_DONE:
704     ;
705 }
706
707 extern uint16_t *u8ConfigDescriptorEX;
708 extern uint16_t *pu8DescriptorEX;
709 extern uint16_t u16TxRxCounter;
710 extern BOOLEAN bGet_descriptor(void);
711
712 uint16_t ConfigDescriptorPatch[30];
713
714 #define EP3_TRANSFER_TYPE_OFFSET    17
715 #define EP3_INT_INTERVAL            19
716 #define EP4_TRANSFER_TYPE_OFFSET    21
717 #define EP4_INT_INTERVAL            22
718
719 BOOLEAN bGet_descriptor_patch(void)
720 {
721     if (mDEV_REQ_VALUE_HIGH() == 2) {
722         uint8_t *p = (uint8_t *)u8ConfigDescriptorEX;
723
724         /* Copy ConfigDescriptor */
725         ath_hal_memcpy(ConfigDescriptorPatch, p, sizeof(ConfigDescriptorPatch));
726
727         p = (uint8_t *)ConfigDescriptorPatch;
728
729         /* Patch the transfer type of EP3 and EP4 */
730         ConfigDescriptorPatch[EP3_TRANSFER_TYPE_OFFSET] = 0x0283;
731         ConfigDescriptorPatch[EP3_INT_INTERVAL] = 0x0700;
732         ConfigDescriptorPatch[EP4_TRANSFER_TYPE_OFFSET] = 0x4002;
733         ConfigDescriptorPatch[EP4_INT_INTERVAL] = 0x00;
734
735         switch (mDEV_REQ_VALUE_LOW())
736         {
737         case 0x00:      // configuration no: 0
738             pu8DescriptorEX = ConfigDescriptorPatch;
739             u16TxRxCounter = ConfigDescriptorPatch[1];
740             //u16TxRxCounter = 46;
741             break;
742         default:
743             return FALSE;
744         }
745
746         if (u16TxRxCounter > mDEV_REQ_LENGTH())
747             u16TxRxCounter = mDEV_REQ_LENGTH();
748
749         A_USB_EP0_TX_DATA();
750         return TRUE;
751     }
752     else {
753         return bGet_descriptor();
754     }
755 }
756
757 extern BOOLEAN bStandardCommand(void);
758
759 BOOLEAN bStandardCommand_patch(void)
760 {
761     if (mDEV_REQ_REQ() == USB_SET_CONFIGURATION) {
762         A_USB_SET_CONFIG();
763
764 #if ENABLE_SWAP_DATA_MODE
765         // SWAP FUNCTION should be enabled while DMA engine is not working,
766         // the best place to enable it is before we trigger the DMA
767         MAGPIE_REG_USB_RX0_SWAP_DATA = 0x1;
768         MAGPIE_REG_USB_TX0_SWAP_DATA = 0x1;
769
770         #if SYSTEM_MODULE_HP_EP5
771             MAGPIE_REG_USB_RX1_SWAP_DATA = 0x1;
772         #endif
773
774         #if SYSTEM_MODULE_HP_EP6
775             MAGPIE_REG_USB_RX2_SWAP_DATA = 0x1;
776         #endif
777
778 #endif //ENABLE_SWAP_DATA_MODE
779         return TRUE;
780     }
781     else {
782         return bStandardCommand();
783     }
784 }
785
786 #endif
787
788