hif/usb_api: remove dup code - usb_reg_in_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 ep3
31  */
32 void usb_status_in_patch(void)
33 {
34         uint16_t count;
35         uint16_t remainder;
36         uint16_t reg_buf_len;
37         static uint16_t buf_len;
38         static VBUF *evntbuf = NULL;
39         static volatile uint32_t *regaddr;
40         static BOOLEAN cmd_is_new = TRUE;
41         BOOLEAN cmd_end = FALSE;
42
43         if (cmd_is_new) {
44                 evntbuf = usbFifoConf.get_event_buf();
45                 if (evntbuf != NULL) {
46                         regaddr = (uint32_t *)VBUF_GET_DATA_ADDR(evntbuf);
47                         buf_len = evntbuf->buf_length;
48                 } else {
49                         mUSB_STATUS_IN_INT_DISABLE();
50                         return;
51                 }
52
53                 cmd_is_new = FALSE;
54         }
55
56         if (buf_len > USB_EP3_MAX_PKT_SIZE) {
57                 reg_buf_len = USB_EP3_MAX_PKT_SIZE;
58                 buf_len -= USB_EP3_MAX_PKT_SIZE;
59         }
60         /* TODO: 64 bytes...
61          * controller supposed will take care of zero-length? */
62         else {
63                 reg_buf_len = buf_len;
64                 cmd_end = TRUE;
65         }
66
67         /* INT use EP3 */
68         for (count = 0; count < (reg_buf_len / 4); count++)
69         {
70                 USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, *regaddr);
71                 regaddr++;
72         }
73
74         remainder = reg_buf_len % 4;
75
76         if (remainder) {
77                 switch(remainder) {
78                 case 3:
79                         USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x7);
80                         break;
81                 case 2:
82                         USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x3);
83                         break;
84                 case 1:
85                         USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x1);
86                         break;
87                 }
88
89                 USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, *regaddr);
90
91                 /* Restore CBus FIFO size to word size */
92                 USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0xF);
93         }
94
95         mUSB_EP3_XFER_DONE();
96
97         if (evntbuf != NULL && cmd_end) {
98                 usbFifoConf.send_event_done(evntbuf);
99                 cmd_is_new = TRUE;
100         }
101 }
102
103 /*
104  * support more than 64 bytes command on ep4 
105  */
106 void usb_reg_out_patch(void)
107 {
108         uint16_t usbfifolen;
109         uint16_t ii;
110         uint32_t ep4_data;
111         static volatile uint32_t *regaddr;
112         static uint16_t cmd_len;
113         static VBUF *buf;
114         BOOLEAN cmd_is_last = FALSE;
115         static BOOLEAN cmd_is_new = TRUE;
116
117         /* get the size of this transcation */
118         usbfifolen = USB_BYTE_REG_READ(ZM_EP4_BYTE_COUNT_LOW_OFFSET);
119
120         if (usbfifolen > USB_EP4_MAX_PKT_SIZE) {
121                 A_PRINTF("EP4 FIFO Bug? Buffer is too big: %x\n", usbfifolen);
122                 cold_reboot();
123         }
124
125         /* check is command is new */
126         if(cmd_is_new) {
127
128                 buf = usbFifoConf.get_command_buf();
129                 cmd_len = 0;
130
131                 if(!buf) {
132                         A_PRINTF("%s: Filed to get new buffer.\n", __func__);
133                         goto err;
134                 }
135
136                 /* copy free, assignment buffer of the address */
137                 regaddr = (uint32_t *)buf->desc_list->buf_addr;
138
139                 cmd_is_new = FALSE;
140         }
141
142         /* just in case, suppose should not happen */
143         if(!buf)
144                 goto err;
145
146         /* if size is smaller, this is the last command!
147          * zero-length supposed should be set through 0x27/bit7->0x19/bit4, not here
148          */
149         if(usbfifolen < USB_EP4_MAX_PKT_SIZE)
150                 cmd_is_last = TRUE;
151
152         /* accumulate the size */
153         cmd_len += usbfifolen;
154
155         if (cmd_len > buf->desc_list->buf_size) {
156                 A_PRINTF("%s: Data length on EP4 FIFO is bigger as "
157                          "allocated buffer data! Drop it!\n", __func__);
158                 goto err;
159         }
160
161         /* round it to alignment */
162         if(usbfifolen % 4)
163                 usbfifolen = (usbfifolen >> 2) + 1;
164         else
165                 usbfifolen = usbfifolen >> 2;
166
167         /* retrieve the data from fifo */
168         for(ii = 0; ii < usbfifolen; ii++) {
169                 /* read fifo data out */
170                 ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);
171                 *regaddr = ep4_data;
172                 regaddr++;
173         }
174
175         /* if this is the last command, callback to HTC */
176         if (cmd_is_last) {
177                 buf->desc_list->next_desc = NULL;
178                 buf->desc_list->data_offset = 0;
179                 buf->desc_list->data_size = cmd_len;
180                 buf->desc_list->control = 0;
181                 buf->next_buf = NULL;
182                 buf->buf_length = cmd_len;
183
184                 usbFifoConf.recv_command(buf);
185
186                 cmd_is_new = TRUE;
187         }
188
189         goto done;
190 err:
191         /* we might get no command buffer here?
192          * but if we return here, the ep4 fifo will be lock out,
193          * so that we still read them out but just drop it? */
194         for(ii = 0; ii < usbfifolen; ii++)
195                 ep4_data = USB_WORD_REG_READ(ZM_EP4_DATA_OFFSET);
196
197 done:
198         /* mUSB_STATUS_IN_INT_ENABLE(); */
199         ;
200 }
201