hif/usb_api: remove dup code - usb_reg_out_patch
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / target / hif / usb_api_main_patch.c
1
2 #include "usb_defs.h"
3 #include "usb_type.h"
4 #include "usb_pre.h"
5 #include "usb_extr.h"
6 #include "usb_std.h"
7 #include "reg_defs.h"
8 #include "athos_api.h"
9 #include "usbfifo_api.h"
10
11 #include "sys_cfg.h"
12
13 #define USB_EP4_MAX_PKT_SIZE bUSB_EP_MAX_PKT_SIZE_64
14 #define USB_EP3_MAX_PKT_SIZE bUSB_EP_MAX_PKT_SIZE_64
15
16 extern USB_FIFO_CONFIG usbFifoConf;
17
18 void cold_reboot(void)
19 {
20         A_PRINTF("Cold reboot initiated.");
21 #if defined(PROJECT_MAGPIE)
22         HAL_WORD_REG_WRITE(WATCH_DOG_MAGIC_PATTERN_ADDR, 0);
23 #elif defined(PROJECT_K2)
24         HAL_WORD_REG_WRITE(MAGPIE_REG_RST_STATUS_ADDR, 0);
25 #endif /* #if defined(PROJECT_MAGPIE) */
26         A_USB_JUMP_BOOT();
27 }
28
29 /*
30  * support more than 64 bytes command on ep4 
31  */
32 void usb_reg_out_patch(void)
33 {
34         uint16_t usbfifolen;
35         uint16_t ii;
36         uint32_t ep4_data;
37         static volatile uint32_t *regaddr;
38         static uint16_t cmd_len;
39         static VBUF *buf;
40         BOOLEAN cmd_is_last = FALSE;
41         static BOOLEAN cmd_is_new = TRUE;
42
43         /* get the size of this transcation */
44         usbfifolen = USB_BYTE_REG_READ(ZM_EP4_BYTE_COUNT_LOW_OFFSET);
45
46         if (usbfifolen > USB_EP4_MAX_PKT_SIZE) {
47                 A_PRINTF("EP4 FIFO Bug? Buffer is too big: %x\n", usbfifolen);
48                 cold_reboot();
49         }
50
51         /* check is command is new */
52         if(cmd_is_new) {
53
54                 buf = usbFifoConf.get_command_buf();
55                 cmd_len = 0;
56
57                 if(!buf) {
58                         A_PRINTF("%s: Filed to get new buffer.\n", __func__);
59                         goto err;
60                 }
61
62                 /* copy free, assignment buffer of the address */
63                 regaddr = (uint32_t *)buf->desc_list->buf_addr;
64
65                 cmd_is_new = FALSE;
66         }
67
68         /* just in case, suppose should not happen */
69         if(!buf)
70                 goto err;
71
72         /* if size is smaller, this is the last command!
73          * zero-length supposed should be set through 0x27/bit7->0x19/bit4, not here
74          */
75         if(usbfifolen < USB_EP4_MAX_PKT_SIZE)
76                 cmd_is_last = TRUE;
77
78         /* accumulate the size */
79         cmd_len += usbfifolen;
80
81         if (cmd_len > buf->desc_list->buf_size) {
82                 A_PRINTF("%s: Data length on EP4 FIFO is bigger as "
83                          "allocated buffer data! Drop it!\n", __func__);
84                 goto err;
85         }
86
87         /* round it to alignment */
88         if(usbfifolen % 4)
89                 usbfifolen = (usbfifolen >> 2) + 1;
90         else
91                 usbfifolen = usbfifolen >> 2;
92
93         /* retrieve the data from fifo */
94         for(ii = 0; ii < usbfifolen; ii++) {
95                 /* read fifo data out */
96                 ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);
97                 *regaddr = ep4_data;
98                 regaddr++;
99         }
100
101         /* if this is the last command, callback to HTC */
102         if (cmd_is_last) {
103                 buf->desc_list->next_desc = NULL;
104                 buf->desc_list->data_offset = 0;
105                 buf->desc_list->data_size = cmd_len;
106                 buf->desc_list->control = 0;
107                 buf->next_buf = NULL;
108                 buf->buf_length = cmd_len;
109
110                 usbFifoConf.recv_command(buf);
111
112                 cmd_is_new = TRUE;
113         }
114
115         goto done;
116 err:
117         /* we might get no command buffer here?
118          * but if we return here, the ep4 fifo will be lock out,
119          * so that we still read them out but just drop it? */
120         for(ii = 0; ii < usbfifolen; ii++)
121                 ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);
122
123 done:
124         /* mUSB_STATUS_IN_INT_ENABLE(); */
125         ;
126 }
127