]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/wilc1000/wilc_wlan.c
staging: wilc1000: remove pointless kmalloc wrapper
[karo-tx-linux.git] / drivers / staging / wilc1000 / wilc_wlan.c
1 /* ////////////////////////////////////////////////////////////////////////// */
2 /*  */
3 /* Copyright (c) Atmel Corporation.  All rights reserved. */
4 /*  */
5 /* Module Name:  wilc_wlan.c */
6 /*  */
7 /*  */
8 /* //////////////////////////////////////////////////////////////////////////// */
9
10 #include "wilc_wlan_if.h"
11 #include "wilc_wlan.h"
12 #define INLINE static __inline
13
14 /********************************************
15  *
16  *      Global
17  *
18  ********************************************/
19 extern wilc_hif_func_t hif_sdio;
20 extern wilc_hif_func_t hif_spi;
21 extern wilc_cfg_func_t mac_cfg;
22 #if defined(PLAT_RK3026_TCHIP)
23 extern u8 g_wilc_initialized; /* AMR : 0422 RK3026 Crash issue */
24 #endif
25 extern void WILC_WFI_mgmt_rx(uint8_t *buff, uint32_t size);
26 uint32_t wilc_get_chipid(uint8_t update);
27 u16 Set_machw_change_vir_if(bool bValue);
28
29
30
31 typedef struct {
32         int quit;
33
34         /**
35          *      input interface functions
36          **/
37         wilc_wlan_os_func_t os_func;
38         wilc_wlan_io_func_t io_func;
39         wilc_wlan_net_func_t net_func;
40         wilc_wlan_indicate_func_t indicate_func;
41
42         /**
43          *      host interface functions
44          **/
45         wilc_hif_func_t hif_func;
46         void *hif_lock;
47
48         /**
49          *      configuration interface functions
50          **/
51         wilc_cfg_func_t cif_func;
52         int cfg_frame_in_use;
53         wilc_cfg_frame_t cfg_frame;
54         uint32_t cfg_frame_offset;
55         int cfg_seq_no;
56         void *cfg_wait;
57
58         /**
59          *      RX buffer
60          **/
61         #ifdef MEMORY_STATIC
62         uint32_t rx_buffer_size;
63         uint8_t *rx_buffer;
64         uint32_t rx_buffer_offset;
65         #endif
66         /**
67          *      TX buffer
68          **/
69         uint32_t tx_buffer_size;
70         uint8_t *tx_buffer;
71         uint32_t tx_buffer_offset;
72
73         /**
74          *      TX queue
75          **/
76         void *txq_lock;
77
78         /*Added by Amr - BugID_4720*/
79         void *txq_add_to_head_lock;
80         void *txq_spinlock;
81         unsigned long txq_spinlock_flags;
82
83         struct txq_entry_t *txq_head;
84         struct txq_entry_t *txq_tail;
85         int txq_entries;
86         void *txq_wait;
87         int txq_exit;
88
89         /**
90          *      RX queue
91          **/
92         void *rxq_lock;
93         struct rxq_entry_t *rxq_head;
94         struct rxq_entry_t *rxq_tail;
95         int rxq_entries;
96         void *rxq_wait;
97         int rxq_exit;
98
99
100 } wilc_wlan_dev_t;
101
102 static wilc_wlan_dev_t g_wlan;
103
104 INLINE void chip_allow_sleep(void);
105 INLINE void chip_wakeup(void);
106 /********************************************
107  *
108  *      Debug
109  *
110  ********************************************/
111
112 static uint32_t dbgflag = N_INIT | N_ERR | N_INTR | N_TXQ | N_RXQ;
113
114 static void wilc_debug(uint32_t flag, char *fmt, ...)
115 {
116         char buf[256];
117         va_list args;
118
119         if (flag & dbgflag) {
120                 va_start(args, fmt);
121                 vsprintf(buf, fmt, args);
122                 va_end(args);
123
124                 if (g_wlan.os_func.os_debug)
125                         g_wlan.os_func.os_debug(buf);
126         }
127 }
128
129 static CHIP_PS_STATE_T genuChipPSstate = CHIP_WAKEDUP;
130
131 /*BugID_5213*/
132 /*acquire_bus() and release_bus() are made INLINE functions*/
133 /*as a temporary workaround to fix a problem of receiving*/
134 /*unknown interrupt from FW*/
135 INLINE void acquire_bus(BUS_ACQUIRE_T acquire)
136 {
137
138         g_wlan.os_func.os_enter_cs(g_wlan.hif_lock);
139         #ifndef WILC_OPTIMIZE_SLEEP_INT
140         if (genuChipPSstate != CHIP_WAKEDUP)
141         #endif
142         {
143                 if (acquire == ACQUIRE_AND_WAKEUP)
144                         chip_wakeup();
145         }
146
147 }
148 INLINE void release_bus(BUS_RELEASE_T release)
149 {
150         #ifdef WILC_OPTIMIZE_SLEEP_INT
151         if (release == RELEASE_ALLOW_SLEEP)
152                 chip_allow_sleep();
153         #endif
154         g_wlan.os_func.os_leave_cs(g_wlan.hif_lock);
155 }
156 /********************************************
157  *
158  *      Queue
159  *
160  ********************************************/
161
162 static void wilc_wlan_txq_remove(struct txq_entry_t *tqe)
163 {
164
165         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
166         /* unsigned long flags; */
167         if (tqe == p->txq_head) {
168
169                 p->txq_head = tqe->next;
170                 if (p->txq_head)
171                         p->txq_head->prev = NULL;
172
173
174         } else if (tqe == p->txq_tail)      {
175                 p->txq_tail = (tqe->prev);
176                 if (p->txq_tail)
177                         p->txq_tail->next = NULL;
178         } else {
179                 tqe->prev->next = tqe->next;
180                 tqe->next->prev = tqe->prev;
181         }
182         p->txq_entries -= 1;
183
184 }
185
186 static struct txq_entry_t *wilc_wlan_txq_remove_from_head(void)
187 {
188         struct txq_entry_t *tqe;
189         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
190         unsigned long flags;
191
192         p->os_func.os_spin_lock(p->txq_spinlock, &flags);
193         if (p->txq_head) {
194                 tqe = p->txq_head;
195                 p->txq_head = tqe->next;
196                 if (p->txq_head) {
197                         p->txq_head->prev = NULL;
198                 }
199                 p->txq_entries -= 1;
200
201                 /*Added by Amr - BugID_4720*/
202
203
204
205         } else {
206                 tqe = NULL;
207         }
208         p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
209         return tqe;
210 }
211
212 static void wilc_wlan_txq_add_to_tail(struct txq_entry_t *tqe)
213 {
214         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
215         unsigned long flags;
216         /*Added by Amr - BugID_4720*/
217         p->os_func.os_spin_lock(p->txq_spinlock, &flags);
218
219         if (p->txq_head == NULL) {
220                 tqe->next = NULL;
221                 tqe->prev = NULL;
222                 p->txq_head = tqe;
223                 p->txq_tail = tqe;
224         } else {
225                 tqe->next = NULL;
226                 tqe->prev = p->txq_tail;
227                 p->txq_tail->next = tqe;
228                 p->txq_tail = tqe;
229         }
230         p->txq_entries += 1;
231         PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
232
233         /*Added by Amr - BugID_4720*/
234         p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
235
236         /**
237          *      wake up TX queue
238          **/
239         PRINT_D(TX_DBG, "Wake the txq_handling\n");
240
241         p->os_func.os_signal(p->txq_wait);
242
243
244 }
245
246 static int wilc_wlan_txq_add_to_head(struct txq_entry_t *tqe)
247 {
248         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
249         unsigned long flags;
250         /*Added by Amr - BugID_4720*/
251         if (p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT))
252                 return -1;
253
254         p->os_func.os_spin_lock(p->txq_spinlock, &flags);
255
256         if (p->txq_head == NULL) {
257                 tqe->next = NULL;
258                 tqe->prev = NULL;
259                 p->txq_head = tqe;
260                 p->txq_tail = tqe;
261         } else {
262                 tqe->next = p->txq_head;
263                 tqe->prev = NULL;
264                 p->txq_head->prev = tqe;
265                 p->txq_head = tqe;
266         }
267         p->txq_entries += 1;
268         PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
269
270         /*Added by Amr - BugID_4720*/
271         p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
272         p->os_func.os_signal(p->txq_add_to_head_lock);
273
274
275         /**
276          *      wake up TX queue
277          **/
278         p->os_func.os_signal(p->txq_wait);
279         PRINT_D(TX_DBG, "Wake up the txq_handler\n");
280
281         /*Added by Amr - BugID_4720*/
282         return 0;
283
284 }
285
286 uint32_t Statisitcs_totalAcks = 0, Statisitcs_DroppedAcks = 0;
287
288 #ifdef  TCP_ACK_FILTER
289 struct Ack_session_info;
290 struct Ack_session_info {
291         uint32_t Ack_seq_num;
292         uint32_t Bigger_Ack_num;
293         uint16_t src_port;
294         uint16_t dst_port;
295         uint16_t status;
296 };
297
298 typedef struct {
299         uint32_t ack_num;
300         uint32_t Session_index;
301         struct txq_entry_t  *txqe;
302 } Pending_Acks_info_t /*Ack_info_t*/;
303
304
305
306
307 struct Ack_session_info *Free_head;
308 struct Ack_session_info *Alloc_head;
309
310 #define TCP_FIN_MASK            (1 << 0)
311 #define TCP_SYN_MASK            (1 << 1)
312 #define TCP_Ack_MASK            (1 << 4)
313 #define NOT_TCP_ACK                     (-1)
314
315 #define MAX_TCP_SESSION         25
316 #define MAX_PENDING_ACKS                256
317 struct Ack_session_info Acks_keep_track_info[2 * MAX_TCP_SESSION];
318 Pending_Acks_info_t Pending_Acks_info[MAX_PENDING_ACKS];
319
320 uint32_t PendingAcks_arrBase;
321 uint32_t Opened_TCP_session;
322 uint32_t Pending_Acks;
323
324
325
326 static __inline int Init_TCP_tracking(void)
327 {
328
329         return 0;
330
331 }
332 static __inline int add_TCP_track_session(uint32_t src_prt, uint32_t dst_prt, uint32_t seq)
333 {
334         Acks_keep_track_info[Opened_TCP_session].Ack_seq_num = seq;
335         Acks_keep_track_info[Opened_TCP_session].Bigger_Ack_num = 0;
336         Acks_keep_track_info[Opened_TCP_session].src_port = src_prt;
337         Acks_keep_track_info[Opened_TCP_session].dst_port = dst_prt;
338         Opened_TCP_session++;
339
340         PRINT_D(TCP_ENH, "TCP Session %d to Ack %d\n", Opened_TCP_session, seq);
341         return 0;
342 }
343
344 static __inline int Update_TCP_track_session(uint32_t index, uint32_t Ack)
345 {
346
347         if (Ack > Acks_keep_track_info[index].Bigger_Ack_num) {
348                 Acks_keep_track_info[index].Bigger_Ack_num = Ack;
349         }
350         return 0;
351
352 }
353 static __inline int add_TCP_Pending_Ack(uint32_t Ack, uint32_t Session_index, struct txq_entry_t  *txqe)
354 {
355         Statisitcs_totalAcks++;
356         if (Pending_Acks < MAX_PENDING_ACKS) {
357                 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].ack_num = Ack;
358                 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].txqe = txqe;
359                 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].Session_index = Session_index;
360                 txqe->tcp_PendingAck_index = PendingAcks_arrBase + Pending_Acks;
361                 Pending_Acks++;
362
363         } else {
364
365         }
366         return 0;
367 }
368 static __inline int remove_TCP_related(void)
369 {
370         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
371         unsigned long flags;
372
373         p->os_func.os_spin_lock(p->txq_spinlock, &flags);
374
375         p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
376         return 0;
377 }
378
379 static __inline int tcp_process(struct txq_entry_t *tqe)
380 {
381         int ret;
382         uint8_t *eth_hdr_ptr;
383         uint8_t *buffer = tqe->buffer;
384         unsigned short h_proto;
385         int i;
386         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
387         unsigned long flags;
388
389         p->os_func.os_spin_lock(p->txq_spinlock, &flags);
390
391         eth_hdr_ptr = &buffer[0];
392         h_proto = ntohs(*((unsigned short *)&eth_hdr_ptr[12]));
393         if (h_proto == 0x0800) { /* IP */
394                 uint8_t *ip_hdr_ptr;
395                 uint8_t protocol;
396
397                 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
398                 protocol = ip_hdr_ptr[9];
399
400
401                 if (protocol == 0x06) {
402                         uint8_t *tcp_hdr_ptr;
403                         uint32_t IHL, Total_Length, Data_offset;
404
405                         tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
406                         IHL = (ip_hdr_ptr[0] & 0xf) << 2;
407                         Total_Length = (((uint32_t)ip_hdr_ptr[2]) << 8) + ((uint32_t)ip_hdr_ptr[3]);
408                         Data_offset = (((uint32_t)tcp_hdr_ptr[12] & 0xf0) >> 2);
409                         if (Total_Length == (IHL + Data_offset)) { /*we want to recognize the clear Acks(packet only carry Ack infos not with data) so data size must be equal zero*/
410                                 uint32_t seq_no, Ack_no;
411
412                                 seq_no  = (((uint32_t)tcp_hdr_ptr[4]) << 24) + (((uint32_t)tcp_hdr_ptr[5]) << 16) + (((uint32_t)tcp_hdr_ptr[6]) << 8) + ((uint32_t)tcp_hdr_ptr[7]);
413
414                                 Ack_no  = (((uint32_t)tcp_hdr_ptr[8]) << 24) + (((uint32_t)tcp_hdr_ptr[9]) << 16) + (((uint32_t)tcp_hdr_ptr[10]) << 8) + ((uint32_t)tcp_hdr_ptr[11]);
415
416
417                                 for (i = 0; i < Opened_TCP_session; i++) {
418                                         if (Acks_keep_track_info[i].Ack_seq_num == seq_no) {
419                                                 Update_TCP_track_session(i, Ack_no);
420                                                 break;
421                                         }
422                                 }
423                                 if (i == Opened_TCP_session) {
424                                         add_TCP_track_session(0, 0, seq_no);
425                                 }
426                                 add_TCP_Pending_Ack(Ack_no, i, tqe);
427
428
429                         }
430
431                 } else {
432                         ret = 0;
433                 }
434         } else {
435                 ret = 0;
436         }
437         p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
438         return ret;
439 }
440
441
442 static int wilc_wlan_txq_filter_dup_tcp_ack(void)
443 {
444
445         uint32_t i = 0;
446         uint32_t Dropped = 0;
447         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
448
449         p->os_func.os_spin_lock(p->txq_spinlock, &p->txq_spinlock_flags);
450         for (i = PendingAcks_arrBase; i < (PendingAcks_arrBase + Pending_Acks); i++) {
451                 if (Pending_Acks_info[i].ack_num < Acks_keep_track_info[Pending_Acks_info[i].Session_index].Bigger_Ack_num) {
452                         struct txq_entry_t *tqe;
453
454                         PRINT_D(TCP_ENH, "DROP ACK: %u\n", Pending_Acks_info[i].ack_num);
455                         tqe = Pending_Acks_info[i].txqe;
456                         if (tqe) {
457                                 wilc_wlan_txq_remove(tqe);
458                                 Statisitcs_DroppedAcks++;
459                                 tqe->status = 1;                                /* mark the packet send */
460                                 if (tqe->tx_complete_func)
461                                         tqe->tx_complete_func(tqe->priv, tqe->status);
462                                 p->os_func.os_free(tqe);
463                                 Dropped++;
464                         }
465                 }
466         }
467         Pending_Acks = 0;
468         Opened_TCP_session = 0;
469
470         if (PendingAcks_arrBase == 0)
471                 PendingAcks_arrBase = MAX_TCP_SESSION;
472         else
473                 PendingAcks_arrBase = 0;
474
475
476         p->os_func.os_spin_unlock(p->txq_spinlock, &p->txq_spinlock_flags);
477
478         while (Dropped > 0) {
479                 /*consume the semaphore count of the removed packet*/
480                 p->os_func.os_wait(p->txq_wait, 1);
481                 Dropped--;
482         }
483
484         return 1;
485 }
486 #endif
487
488 #ifdef TCP_ENHANCEMENTS
489 bool EnableTCPAckFilter = false;
490
491 void Enable_TCP_ACK_Filter(bool value)
492 {
493         EnableTCPAckFilter = value;
494 }
495
496 bool is_TCP_ACK_Filter_Enabled(void)
497 {
498         return EnableTCPAckFilter;
499 }
500 #endif
501
502 static int wilc_wlan_txq_add_cfg_pkt(uint8_t *buffer, uint32_t buffer_size)
503 {
504         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
505         struct txq_entry_t *tqe;
506
507         PRINT_D(TX_DBG, "Adding config packet ...\n");
508         if (p->quit) {
509                 PRINT_D(TX_DBG, "Return due to clear function\n");
510                 p->os_func.os_signal(p->cfg_wait);
511                 return 0;
512         }
513
514         tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
515         if (tqe == NULL) {
516                 PRINT_ER("Failed to allocate memory\n");
517                 return 0;
518         }
519
520         tqe->type = WILC_CFG_PKT;
521         tqe->buffer = buffer;
522         tqe->buffer_size = buffer_size;
523         tqe->tx_complete_func = NULL;
524         tqe->priv = NULL;
525 #ifdef TCP_ACK_FILTER
526         tqe->tcp_PendingAck_index = NOT_TCP_ACK;
527 #endif
528         /**
529          *      Configuration packet always at the front
530          **/
531         PRINT_D(TX_DBG, "Adding the config packet at the Queue tail\n");
532
533         /*Edited by Amr - BugID_4720*/
534         if (wilc_wlan_txq_add_to_head(tqe))
535                 return 0;
536         return 1;
537 }
538
539 static int wilc_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
540 {
541         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
542         struct txq_entry_t *tqe;
543
544         if (p->quit)
545                 return 0;
546
547         tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
548
549         if (tqe == NULL)
550                 return 0;
551         tqe->type = WILC_NET_PKT;
552         tqe->buffer = buffer;
553         tqe->buffer_size = buffer_size;
554         tqe->tx_complete_func = func;
555         tqe->priv = priv;
556
557         PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
558 #ifdef TCP_ACK_FILTER
559         tqe->tcp_PendingAck_index = NOT_TCP_ACK;
560 #ifdef TCP_ENHANCEMENTS
561         if (is_TCP_ACK_Filter_Enabled())
562 #endif
563         tcp_process(tqe);
564 #endif
565         wilc_wlan_txq_add_to_tail(tqe);
566         /*return number of itemes in the queue*/
567         return p->txq_entries;
568 }
569 /*Bug3959: transmitting mgmt frames received from host*/
570 #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
571 int wilc_wlan_txq_add_mgmt_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
572 {
573
574         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
575         struct txq_entry_t *tqe;
576
577         if (p->quit)
578                 return 0;
579
580         tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
581
582         if (tqe == NULL)
583                 return 0;
584         tqe->type = WILC_MGMT_PKT;
585         tqe->buffer = buffer;
586         tqe->buffer_size = buffer_size;
587         tqe->tx_complete_func = func;
588         tqe->priv = priv;
589 #ifdef TCP_ACK_FILTER
590         tqe->tcp_PendingAck_index = NOT_TCP_ACK;
591 #endif
592         PRINT_D(TX_DBG, "Adding Network packet at the Queue tail\n");
593         wilc_wlan_txq_add_to_tail(tqe);
594         return 1;
595 }
596
597 #ifdef WILC_FULLY_HOSTING_AP
598 int wilc_FH_wlan_txq_add_net_pkt(void *priv, uint8_t *buffer, uint32_t buffer_size, wilc_tx_complete_func_t func)
599 {
600         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
601         struct txq_entry_t *tqe;
602
603         if (p->quit)
604                 return 0;
605
606         tqe = (struct txq_entry_t *)p->os_func.os_malloc_atomic(sizeof(struct txq_entry_t));
607
608         if (tqe == NULL)
609                 return 0;
610         tqe->type = WILC_FH_DATA_PKT;
611         tqe->buffer = buffer;
612         tqe->buffer_size = buffer_size;
613         tqe->tx_complete_func = func;
614         tqe->priv = priv;
615         PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
616         wilc_wlan_txq_add_to_tail(tqe);
617         /*return number of itemes in the queue*/
618         return p->txq_entries;
619 }
620 #endif  /* WILC_FULLY_HOSTING_AP*/
621 #endif /*WILC_AP_EXTERNAL_MLME*/
622 static struct txq_entry_t *wilc_wlan_txq_get_first(void)
623 {
624         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
625         struct txq_entry_t *tqe;
626         unsigned long flags;
627
628         /*Added by Amr - BugID_4720*/
629         p->os_func.os_spin_lock(p->txq_spinlock, &flags);
630
631         tqe = p->txq_head;
632
633         /*Added by Amr - BugID_4720*/
634         p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
635
636
637         return tqe;
638 }
639
640 static struct txq_entry_t *wilc_wlan_txq_get_next(struct txq_entry_t *tqe)
641 {
642         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
643         unsigned long flags;
644         /*Added by Amr - BugID_4720*/
645         p->os_func.os_spin_lock(p->txq_spinlock, &flags);
646
647         tqe = tqe->next;
648         /*Added by Amr - BugID_4720*/
649         p->os_func.os_spin_unlock(p->txq_spinlock, &flags);
650
651
652         return tqe;
653 }
654
655 static int wilc_wlan_rxq_add(struct rxq_entry_t *rqe)
656 {
657         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
658
659         if (p->quit)
660                 return 0;
661
662         p->os_func.os_enter_cs(p->rxq_lock);
663         if (p->rxq_head == NULL) {
664                 PRINT_D(RX_DBG, "Add to Queue head\n");
665                 rqe->next = NULL;
666                 p->rxq_head = rqe;
667                 p->rxq_tail = rqe;
668         } else {
669                 PRINT_D(RX_DBG, "Add to Queue tail\n");
670                 p->rxq_tail->next = rqe;
671                 rqe->next = NULL;
672                 p->rxq_tail = rqe;
673         }
674         p->rxq_entries += 1;
675         PRINT_D(RX_DBG, "Number of queue entries: %d\n", p->rxq_entries);
676         p->os_func.os_leave_cs(p->rxq_lock);
677         return p->rxq_entries;
678 }
679
680 static struct rxq_entry_t *wilc_wlan_rxq_remove(void)
681 {
682         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
683
684         PRINT_D(RX_DBG, "Getting rxQ element\n");
685         if (p->rxq_head) {
686                 struct rxq_entry_t *rqe;
687
688                 p->os_func.os_enter_cs(p->rxq_lock);
689                 rqe = p->rxq_head;
690                 p->rxq_head = p->rxq_head->next;
691                 p->rxq_entries -= 1;
692                 PRINT_D(RX_DBG, "RXQ entries decreased\n");
693                 p->os_func.os_leave_cs(p->rxq_lock);
694                 return rqe;
695         }
696         PRINT_D(RX_DBG, "Nothing to get from Q\n");
697         return NULL;
698 }
699
700
701 /********************************************
702  *
703  *      Power Save handle functions
704  *
705  ********************************************/
706
707
708
709 #ifdef WILC_OPTIMIZE_SLEEP_INT
710
711 INLINE void chip_allow_sleep(void)
712 {
713         uint32_t reg = 0;
714
715         /* Clear bit 1 */
716         g_wlan.hif_func.hif_read_reg(0xf0, &reg);
717
718         g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
719 }
720
721 INLINE void chip_wakeup(void)
722 {
723         uint32_t reg, clk_status_reg, trials = 0;
724         uint32_t sleep_time;
725
726         if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
727                 do {
728                         g_wlan.hif_func.hif_read_reg(1, &reg);
729                         /* Set bit 1 */
730                         g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
731
732                         /* Clear bit 1*/
733                         g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
734
735                         do {
736                                 /* Wait for the chip to stabilize*/
737                                 usleep_range(2 * 1000, 2 * 1000);
738                                 /* Make sure chip is awake. This is an extra step that can be removed */
739                                 /* later to avoid the bus access overhead */
740                                 if ((wilc_get_chipid(true) == 0)) {
741                                         wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
742                                 }
743                         } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
744
745                 } while (wilc_get_chipid(true) == 0);
746         } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO)   {
747                 g_wlan.hif_func.hif_read_reg(0xf0, &reg);
748                 do {
749                         /* Set bit 1 */
750                         g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
751
752                         /* Check the clock status */
753                         g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
754
755                         /* in case of clocks off, wait 2ms, and check it again. */
756                         /* if still off, wait for another 2ms, for a total wait of 6ms. */
757                         /* If still off, redo the wake up sequence */
758                         while (((clk_status_reg & 0x1) == 0) && (((++trials) % 3) == 0)) {
759                                 /* Wait for the chip to stabilize*/
760                                 usleep_range(2 * 1000, 2 * 1000);
761
762                                 /* Make sure chip is awake. This is an extra step that can be removed */
763                                 /* later to avoid the bus access overhead */
764                                 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
765
766                                 if ((clk_status_reg & 0x1) == 0) {
767                                         wilc_debug(N_ERR, "clocks still OFF. Wake up failed\n");
768                                 }
769                         }
770                         /* in case of failure, Reset the wakeup bit to introduce a new edge on the next loop */
771                         if ((clk_status_reg & 0x1) == 0) {
772                                 /* Reset bit 0 */
773                                 g_wlan.hif_func.hif_write_reg(0xf0, reg & (~(1 << 0)));
774                         }
775                 } while ((clk_status_reg & 0x1) == 0);
776         }
777
778
779         if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
780                 g_wlan.hif_func.hif_read_reg(0x1C0C, &reg);
781                 reg &= ~(1 << 0);
782                 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
783
784                 if (wilc_get_chipid(false) >= 0x1002b0) {
785                         /* Enable PALDO back right after wakeup */
786                         uint32_t val32;
787
788                         g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
789                         val32 |= (1 << 6);
790                         g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
791
792                         g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
793                         val32 |= (1 << 6);
794                         g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
795                 }
796         }
797         genuChipPSstate = CHIP_WAKEDUP;
798 }
799 #else
800 INLINE void chip_wakeup(void)
801 {
802         uint32_t reg, trials = 0;
803
804         do {
805                 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
806                         g_wlan.hif_func.hif_read_reg(1, &reg);
807                         /* Make sure bit 1 is 0 before we start. */
808                         g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
809                         /* Set bit 1 */
810                         g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
811                         /* Clear bit 1*/
812                         g_wlan.hif_func.hif_write_reg(1, reg  & ~(1 << 1));
813                 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO)   {
814                         /* Make sure bit 0 is 0 before we start. */
815                         g_wlan.hif_func.hif_read_reg(0xf0, &reg);
816                         g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
817                         /* Set bit 1 */
818                         g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
819                         /* Clear bit 1 */
820                         g_wlan.hif_func.hif_write_reg(0xf0, reg  & ~(1 << 0));
821                 }
822
823                 do {
824                         /* Wait for the chip to stabilize*/
825                         mdelay(3);
826
827                         /* Make sure chip is awake. This is an extra step that can be removed */
828                         /* later to avoid the bus access overhead */
829                         if ((wilc_get_chipid(true) == 0)) {
830                                 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
831                         }
832                 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
833
834         } while (wilc_get_chipid(true) == 0);
835
836         if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
837                 g_wlan.hif_func.hif_read_reg(0x1C0C, &reg);
838                 reg &= ~(1 << 0);
839                 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
840
841                 if (wilc_get_chipid(false) >= 0x1002b0) {
842                         /* Enable PALDO back right after wakeup */
843                         uint32_t val32;
844
845                         g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
846                         val32 |= (1 << 6);
847                         g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
848
849                         g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
850                         val32 |= (1 << 6);
851                         g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
852                 }
853         }
854         genuChipPSstate = CHIP_WAKEDUP;
855 }
856 #endif
857 void chip_sleep_manually(u32 u32SleepTime)
858 {
859         if (genuChipPSstate != CHIP_WAKEDUP) {
860                 /* chip is already sleeping. Do nothing */
861                 return;
862         }
863         acquire_bus(ACQUIRE_ONLY);
864
865 #ifdef WILC_OPTIMIZE_SLEEP_INT
866         chip_allow_sleep();
867 #endif
868
869         /* Trigger the manual sleep interrupt */
870         g_wlan.hif_func.hif_write_reg(0x10a8, 1);
871
872         genuChipPSstate = CHIP_SLEEPING_MANUAL;
873         release_bus(RELEASE_ONLY);
874
875 }
876
877
878 /********************************************
879  *
880  *      Tx, Rx queue handle functions
881  *
882  ********************************************/
883 static int wilc_wlan_handle_txq(uint32_t *pu32TxqCount)
884 {
885         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
886         int i, entries = 0;
887         uint32_t sum;
888         uint32_t reg;
889         uint8_t *txb = p->tx_buffer;
890         uint32_t offset = 0;
891         int vmm_sz = 0;
892         struct txq_entry_t *tqe;
893         int ret = 0;
894         int counter;
895         int timeout;
896         uint32_t vmm_table[WILC_VMM_TBL_SIZE];
897
898         p->txq_exit = 0;
899         do {
900                 if (p->quit)
901                         break;
902
903                 /*Added by Amr - BugID_4720*/
904                 p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT);
905 #ifdef  TCP_ACK_FILTER
906                 wilc_wlan_txq_filter_dup_tcp_ack();
907 #endif
908                 /**
909                  *      build the vmm list
910                  **/
911                 PRINT_D(TX_DBG, "Getting the head of the TxQ\n");
912                 tqe = wilc_wlan_txq_get_first();
913                 i = 0;
914                 sum = 0;
915                 do {
916                         /* if ((tqe != NULL) && (i < (8)) && */
917                         /* if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE-1)) && */
918                         if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE - 1)) /* reserve last entry to 0 */) {
919
920                                 if (tqe->type == WILC_CFG_PKT) {
921                                         vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
922                                 }
923                                 /*Bug3959: transmitting mgmt frames received from host*/
924                                 /*vmm_sz will only be equal to tqe->buffer_size + 4 bytes (HOST_HDR_OFFSET)*/
925                                 /* in other cases WILC_MGMT_PKT and WILC_DATA_PKT_MAC_HDR*/
926                                 else if (tqe->type == WILC_NET_PKT) {
927                                         vmm_sz = ETH_ETHERNET_HDR_OFFSET;
928                                 }
929 #ifdef WILC_FULLY_HOSTING_AP
930                                 else if (tqe->type == WILC_FH_DATA_PKT) {
931                                         vmm_sz = FH_TX_HOST_HDR_OFFSET;
932                                 }
933 #endif
934 #ifdef WILC_AP_EXTERNAL_MLME
935                                 else {
936                                         vmm_sz = HOST_HDR_OFFSET;
937                                 }
938 #endif
939                                 vmm_sz += tqe->buffer_size;
940                                 PRINT_D(TX_DBG, "VMM Size before alignment = %d\n", vmm_sz);
941                                 if (vmm_sz & 0x3) {                                                                                                     /* has to be word aligned */
942                                         vmm_sz = (vmm_sz + 4) & ~0x3;
943                                 }
944                                 if ((sum + vmm_sz) > p->tx_buffer_size) {
945                                         break;
946                                 }
947                                 PRINT_D(TX_DBG, "VMM Size AFTER alignment = %d\n", vmm_sz);
948                                 vmm_table[i] = vmm_sz / 4;                                                                                /* table take the word size */
949                                 PRINT_D(TX_DBG, "VMMTable entry size = %d\n", vmm_table[i]);
950
951                                 if (tqe->type == WILC_CFG_PKT) {
952                                         vmm_table[i] |= (1 << 10);
953                                         PRINT_D(TX_DBG, "VMMTable entry changed for CFG packet = %d\n", vmm_table[i]);
954                                 }
955 #ifdef BIG_ENDIAN
956                                 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
957 #endif
958
959                                 i++;
960                                 sum += vmm_sz;
961                                 PRINT_D(TX_DBG, "sum = %d\n", sum);
962                                 tqe = wilc_wlan_txq_get_next(tqe);
963                         } else {
964                                 break;
965                         }
966                 } while (1);
967
968                 if (i == 0) {           /* nothing in the queue */
969                         PRINT_D(TX_DBG, "Nothing in TX-Q\n");
970                         break;
971                 } else {
972                         PRINT_D(TX_DBG, "Mark the last entry in VMM table - number of previous entries = %d\n", i);
973                         vmm_table[i] = 0x0;     /* mark the last element to 0 */
974                 }
975                 acquire_bus(ACQUIRE_AND_WAKEUP);
976                 counter = 0;
977                 do {
978
979                         ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, &reg);
980                         if (!ret) {
981                                 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
982                                 break;
983                         }
984
985                         if ((reg & 0x1) == 0) {
986                                 /**
987                                  *      write to vmm table
988                                  **/
989                                 PRINT_D(TX_DBG, "Writing VMM table ... with Size = %d\n", ((i + 1) * 4));
990                                 break;
991                         } else {
992                                 counter++;
993                                 if (counter > 200) {
994                                         counter = 0;
995                                         PRINT_D(TX_DBG, "Looping in tx ctrl , forcce quit\n");
996                                         ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, 0);
997                                         break;
998                                 }
999                                 /**
1000                                  *      wait for vmm table is ready
1001                                  **/
1002                                 PRINT_WRN(GENERIC_DBG, "[wilc txq]: warn, vmm table not clear yet, wait...\n");
1003                                 release_bus(RELEASE_ALLOW_SLEEP);
1004                                 p->os_func.os_sleep(3); /* wait 3 ms */
1005                                 acquire_bus(ACQUIRE_AND_WAKEUP);
1006                         }
1007                 } while (!p->quit);
1008
1009                 if (!ret) {
1010                         goto _end_;
1011                 }
1012
1013                 timeout = 200;
1014                 do {
1015
1016                         /**
1017                          * write to vmm table
1018                          **/
1019                         ret = p->hif_func.hif_block_tx(WILC_VMM_TBL_RX_SHADOW_BASE, (uint8_t *)vmm_table, ((i + 1) * 4)); /* Bug 4477 fix */
1020                         if (!ret) {
1021                                 wilc_debug(N_ERR, "ERR block TX of VMM table.\n");
1022                                 break;
1023                         }
1024
1025
1026                         /**
1027                          * interrupt firmware
1028                          **/
1029                         ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x2);
1030                         if (!ret) {
1031                                 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
1032                                 break;
1033                         }
1034
1035                         /**
1036                          *      wait for confirm...
1037                          **/
1038
1039                         do {
1040                                 ret = p->hif_func.hif_read_reg(WILC_HOST_VMM_CTL, &reg);
1041                                 if (!ret) {
1042                                         wilc_debug(N_ERR, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
1043                                         break;
1044                                 }
1045                                 if ((reg >> 2) & 0x1) {
1046                                         /**
1047                                          *      Get the entries
1048                                          **/
1049                                         entries = ((reg >> 3) & 0x3f);
1050                                         /* entries = ((reg>>3)&0x2f); */
1051                                         break;
1052                                 } else {
1053                                         release_bus(RELEASE_ALLOW_SLEEP);
1054                                         p->os_func.os_sleep(3); /* wait 3 ms */
1055                                         acquire_bus(ACQUIRE_AND_WAKEUP);
1056                                         PRINT_WRN(GENERIC_DBG, "Can't get VMM entery - reg = %2x\n", reg);
1057                                 }
1058                         } while (--timeout);
1059                         if (timeout <= 0) {
1060                                 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x0);
1061                                 break;
1062                         }
1063
1064                         if (!ret) {
1065                                 break;
1066                         }
1067
1068                         if (entries == 0) {
1069                                 PRINT_WRN(GENERIC_DBG, "[wilc txq]: no more buffer in the chip (reg: %08x), retry later [[ %d, %x ]]\n", reg, i, vmm_table[i - 1]);
1070
1071                                 /* undo the transaction. */
1072                                 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, &reg);
1073                                 if (!ret) {
1074                                         wilc_debug(N_ERR, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
1075                                         break;
1076                                 }
1077                                 reg &= ~(1ul << 0);
1078                                 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, reg);
1079                                 if (!ret) {
1080                                         wilc_debug(N_ERR, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
1081                                         break;
1082                                 }
1083                                 break;
1084                         } else {
1085                                 break;
1086                         }
1087                 } while (1);
1088
1089                 if (!ret) {
1090                         goto _end_;
1091                 }
1092                 if (entries == 0) {
1093                         ret = WILC_TX_ERR_NO_BUF;
1094                         goto _end_;
1095                 }
1096
1097                 /* since copying data into txb takes some time, then
1098                  * allow the bus lock to be released let the RX task go. */
1099                 release_bus(RELEASE_ALLOW_SLEEP);
1100
1101                 /**
1102                  *      Copy data to the TX buffer
1103                  **/
1104                 offset = 0;
1105                 i = 0;
1106                 do {
1107                         tqe = wilc_wlan_txq_remove_from_head();
1108                         if (tqe != NULL && (vmm_table[i] != 0)) {
1109                                 uint32_t header, buffer_offset;
1110
1111 #ifdef BIG_ENDIAN
1112                                 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
1113 #endif
1114                                 vmm_sz = (vmm_table[i] & 0x3ff);        /* in word unit */
1115                                 vmm_sz *= 4;
1116                                 header = (tqe->type << 31) | (tqe->buffer_size << 15) | vmm_sz;
1117                                 /*Bug3959: transmitting mgmt frames received from host*/
1118                                 /*setting bit 30 in the host header to indicate mgmt frame*/
1119 #ifdef WILC_AP_EXTERNAL_MLME
1120                                 if (tqe->type == WILC_MGMT_PKT)
1121                                         header |= (1 << 30);
1122                                 else
1123                                         header &= ~(1 << 30);
1124 #endif
1125
1126 #ifdef BIG_ENDIAN
1127                                 header = BYTE_SWAP(header);
1128 #endif
1129                                 memcpy(&txb[offset], &header, 4);
1130                                 if (tqe->type == WILC_CFG_PKT) {
1131                                         buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
1132                                 }
1133                                 /*Bug3959: transmitting mgmt frames received from host*/
1134                                 /*buffer offset = HOST_HDR_OFFSET in other cases: WILC_MGMT_PKT*/
1135                                 /* and WILC_DATA_PKT_MAC_HDR*/
1136                                 else if (tqe->type == WILC_NET_PKT) {
1137                                         char *pBSSID = ((struct tx_complete_data *)(tqe->priv))->pBssid;
1138
1139                                         buffer_offset = ETH_ETHERNET_HDR_OFFSET;
1140                                         /* copy the bssid at the sart of the buffer */
1141                                         memcpy(&txb[offset + 4], pBSSID, 6);
1142                                 }
1143 #ifdef WILC_FULLY_HOSTING_AP
1144                                 else if (tqe->type == WILC_FH_DATA_PKT) {
1145                                         buffer_offset = FH_TX_HOST_HDR_OFFSET;
1146                                 }
1147 #endif
1148                                 else {
1149                                         buffer_offset = HOST_HDR_OFFSET;
1150                                 }
1151
1152                                 memcpy(&txb[offset + buffer_offset], tqe->buffer, tqe->buffer_size);
1153                                 offset += vmm_sz;
1154                                 i++;
1155                                 tqe->status = 1;                                /* mark the packet send */
1156                                 if (tqe->tx_complete_func)
1157                                         tqe->tx_complete_func(tqe->priv, tqe->status);
1158                                 #ifdef TCP_ACK_FILTER
1159                                 if (tqe->tcp_PendingAck_index != NOT_TCP_ACK) {
1160                                         Pending_Acks_info[tqe->tcp_PendingAck_index].txqe = NULL;
1161                                 }
1162                                 #endif
1163                                 p->os_func.os_free(tqe);
1164                         } else {
1165                                 break;
1166                         }
1167                 } while (--entries);
1168
1169                 /**
1170                  *      lock the bus
1171                  **/
1172                 acquire_bus(ACQUIRE_AND_WAKEUP);
1173
1174                 ret = p->hif_func.hif_clear_int_ext(ENABLE_TX_VMM);
1175                 if (!ret) {
1176                         wilc_debug(N_ERR, "[wilc txq]: fail can't start tx VMM ...\n");
1177                         goto _end_;
1178                 }
1179
1180                 /**
1181                  *      transfer
1182                  **/
1183                 ret = p->hif_func.hif_block_tx_ext(0, txb, offset);
1184                 if (!ret) {
1185                         wilc_debug(N_ERR, "[wilc txq]: fail can't block tx ext...\n");
1186                         goto _end_;
1187                 }
1188
1189 _end_:
1190
1191                 release_bus(RELEASE_ALLOW_SLEEP);
1192                 if (ret != 1)
1193                         break;
1194         } while (0);
1195         /*Added by Amr - BugID_4720*/
1196         p->os_func.os_signal(p->txq_add_to_head_lock);
1197
1198         p->txq_exit = 1;
1199         PRINT_D(TX_DBG, "THREAD: Exiting txq\n");
1200         /* return tx[]q count */
1201         *pu32TxqCount = p->txq_entries;
1202         return ret;
1203 }
1204
1205 static void wilc_wlan_handle_rxq(void)
1206 {
1207         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1208         int offset = 0, size, has_packet = 0;
1209         uint8_t *buffer;
1210         struct rxq_entry_t *rqe;
1211
1212         p->rxq_exit = 0;
1213
1214
1215
1216
1217         do {
1218                 if (p->quit) {
1219                         PRINT_D(RX_DBG, "exit 1st do-while due to Clean_UP function\n");
1220                         p->os_func.os_signal(p->cfg_wait);
1221                         break;
1222                 }
1223                 rqe = wilc_wlan_rxq_remove();
1224                 if (rqe == NULL) {
1225                         PRINT_D(RX_DBG, "nothing in the queue - exit 1st do-while\n");
1226                         break;
1227                 }
1228                 buffer = rqe->buffer;
1229                 size = rqe->buffer_size;
1230                 PRINT_D(RX_DBG, "rxQ entery Size = %d - Address = %p\n", size, buffer);
1231                 offset = 0;
1232
1233
1234
1235                 do {
1236                         uint32_t header;
1237                         uint32_t pkt_len, pkt_offset, tp_len;
1238                         int is_cfg_packet;
1239
1240                         PRINT_D(RX_DBG, "In the 2nd do-while\n");
1241                         memcpy(&header, &buffer[offset], 4);
1242 #ifdef BIG_ENDIAN
1243                         header = BYTE_SWAP(header);
1244 #endif
1245                         PRINT_D(RX_DBG, "Header = %04x - Offset = %d\n", header, offset);
1246
1247
1248
1249                         is_cfg_packet = (header >> 31) & 0x1;
1250                         pkt_offset = (header >> 22) & 0x1ff;
1251                         tp_len = (header >> 11) & 0x7ff;
1252                         pkt_len = header & 0x7ff;
1253
1254                         if (pkt_len == 0 || tp_len == 0) {
1255                                 wilc_debug(N_RXQ, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len, tp_len);
1256                                 break;
1257                         }
1258
1259 /*bug 3887: [AP] Allow Management frames to be passed to the host*/
1260                         #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
1261                         #define IS_MANAGMEMENT                          0x100
1262                         #define IS_MANAGMEMENT_CALLBACK                 0x080
1263                         #define IS_MGMT_STATUS_SUCCES                   0x040
1264
1265
1266                         if (pkt_offset & IS_MANAGMEMENT) {
1267                                 /* reset mgmt indicator bit, to use pkt_offeset in furthur calculations */
1268                                 pkt_offset &= ~(IS_MANAGMEMENT | IS_MANAGMEMENT_CALLBACK | IS_MGMT_STATUS_SUCCES);
1269
1270 #ifdef USE_WIRELESS
1271                                 WILC_WFI_mgmt_rx(&buffer[offset + HOST_HDR_OFFSET], pkt_len);
1272
1273 #endif
1274
1275                         }
1276                         /* BUG4530 fix */
1277                         else
1278                         #endif
1279                         {
1280
1281                                 if (!is_cfg_packet) {
1282
1283                                         if (p->net_func.rx_indicate) {
1284                                                 if (pkt_len > 0) {
1285                                                         p->net_func.rx_indicate(&buffer[offset], pkt_len, pkt_offset);
1286                                                         has_packet = 1;
1287                                                 }
1288                                         }
1289                                 } else {
1290                                         wilc_cfg_rsp_t rsp;
1291
1292
1293
1294                                         p->cif_func.rx_indicate(&buffer[pkt_offset + offset], pkt_len, &rsp);
1295                                         if (rsp.type == WILC_CFG_RSP) {
1296                                                 /**
1297                                                  *      wake up the waiting task...
1298                                                  **/
1299                                                 PRINT_D(RX_DBG, "p->cfg_seq_no = %d - rsp.seq_no = %d\n", p->cfg_seq_no, rsp.seq_no);
1300                                                 if (p->cfg_seq_no == rsp.seq_no) {
1301                                                         p->os_func.os_signal(p->cfg_wait);
1302                                                 }
1303                                         } else if (rsp.type == WILC_CFG_RSP_STATUS) {
1304                                                 /**
1305                                                  *      Call back to indicate status...
1306                                                  **/
1307                                                 if (p->indicate_func.mac_indicate) {
1308                                                         p->indicate_func.mac_indicate(WILC_MAC_INDICATE_STATUS);
1309                                                 }
1310
1311                                         } else if (rsp.type == WILC_CFG_RSP_SCAN) {
1312                                                 if (p->indicate_func.mac_indicate)
1313                                                         p->indicate_func.mac_indicate(WILC_MAC_INDICATE_SCAN);
1314                                         }
1315                                 }
1316                         }
1317                         offset += tp_len;
1318                         if (offset >= size)
1319                                 break;
1320                 } while (1);
1321
1322
1323 #ifndef MEMORY_STATIC
1324                 if (buffer != NULL)
1325                         p->os_func.os_free((void *)buffer);
1326 #endif
1327                 if (rqe != NULL)
1328                         p->os_func.os_free((void *)rqe);
1329
1330                 if (has_packet) {
1331                         if (p->net_func.rx_complete)
1332                                 p->net_func.rx_complete();
1333                 }
1334         } while (1);
1335
1336         p->rxq_exit = 1;
1337         PRINT_D(RX_DBG, "THREAD: Exiting RX thread\n");
1338 }
1339
1340 /********************************************
1341  *
1342  *      Fast DMA Isr
1343  *
1344  ********************************************/
1345 static void wilc_unknown_isr_ext(void)
1346 {
1347         g_wlan.hif_func.hif_clear_int_ext(0);
1348 }
1349 static void wilc_pllupdate_isr_ext(uint32_t int_stats)
1350 {
1351
1352         int trials = 10;
1353
1354         g_wlan.hif_func.hif_clear_int_ext(PLL_INT_CLR);
1355
1356         /* Waiting for PLL */
1357         g_wlan.os_func.os_atomic_sleep(WILC_PLL_TO);
1358
1359         /* poll till read a valid data */
1360         while (!(ISWILC1000(wilc_get_chipid(true)) && --trials)) {
1361                 PRINT_D(TX_DBG, "PLL update retrying\n");
1362                 g_wlan.os_func.os_atomic_sleep(1);
1363         }
1364 }
1365
1366 static void wilc_sleeptimer_isr_ext(uint32_t int_stats1)
1367 {
1368         g_wlan.hif_func.hif_clear_int_ext(SLEEP_INT_CLR);
1369 #ifndef WILC_OPTIMIZE_SLEEP_INT
1370         genuChipPSstate = CHIP_SLEEPING_AUTO;
1371 #endif
1372 }
1373
1374 static void wilc_wlan_handle_isr_ext(uint32_t int_status)
1375 {
1376         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1377 #ifdef MEMORY_STATIC
1378         uint32_t offset = p->rx_buffer_offset;
1379 #endif
1380         uint8_t *buffer = NULL;
1381         uint32_t size;
1382         uint32_t retries = 0;
1383         int ret = 0;
1384         struct rxq_entry_t *rqe;
1385
1386
1387         /**
1388          *      Get the rx size
1389          **/
1390
1391         size = ((int_status & 0x7fff) << 2);
1392
1393         while (!size && retries < 10) {
1394                 uint32_t time = 0;
1395                 /*looping more secure*/
1396                 /*zero size make a crashe because the dma will not happen and that will block the firmware*/
1397                 wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
1398                 p->hif_func.hif_read_size(&size);
1399                 size = ((size & 0x7fff) << 2);
1400                 retries++;
1401
1402         }
1403
1404         if (size > 0) {
1405 #ifdef MEMORY_STATIC
1406                 if (p->rx_buffer_size - offset < size)
1407                         offset = 0;
1408
1409                 if (p->rx_buffer)
1410                         buffer = &p->rx_buffer[offset];
1411                 else {
1412                         wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
1413                         goto _end_;
1414                 }
1415
1416 #else
1417                 buffer = kmalloc(size, GFP_KERNEL);
1418                 if (buffer == NULL) {
1419                         wilc_debug(N_ERR, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size);
1420                         usleep_range(100 * 1000, 100 * 1000);
1421                         goto _end_;
1422                 }
1423 #endif
1424
1425                 /**
1426                  *      clear the chip's interrupt       after getting size some register getting corrupted after clear the interrupt
1427                  **/
1428                 p->hif_func.hif_clear_int_ext(DATA_INT_CLR | ENABLE_RX_VMM);
1429
1430
1431                 /**
1432                  * start transfer
1433                  **/
1434                 ret = p->hif_func.hif_block_rx_ext(0, buffer, size);
1435
1436                 if (!ret) {
1437                         wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
1438                         goto _end_;
1439                 }
1440 _end_:
1441
1442
1443                 if (ret) {
1444 #ifdef MEMORY_STATIC
1445                         offset += size;
1446                         p->rx_buffer_offset = offset;
1447 #endif
1448                         /**
1449                          *      add to rx queue
1450                          **/
1451                         rqe = kmalloc(sizeof(struct rxq_entry_t), GFP_KERNEL);
1452                         if (rqe != NULL) {
1453                                 rqe->buffer = buffer;
1454                                 rqe->buffer_size = size;
1455                                 PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
1456                                 wilc_wlan_rxq_add(rqe);
1457                                 p->os_func.os_signal(p->rxq_wait);
1458                         }
1459                 } else {
1460 #ifndef MEMORY_STATIC
1461                         if (buffer != NULL)
1462                                 p->os_func.os_free(buffer);
1463 #endif
1464                 }
1465         }
1466 #ifdef TCP_ENHANCEMENTS
1467         wilc_wlan_handle_rxq();
1468 #endif
1469 }
1470
1471 void wilc_handle_isr(void)
1472 {
1473         uint32_t int_status;
1474
1475         acquire_bus(ACQUIRE_AND_WAKEUP);
1476         g_wlan.hif_func.hif_read_int(&int_status);
1477
1478         if (int_status & PLL_INT_EXT) {
1479                 wilc_pllupdate_isr_ext(int_status);
1480         }
1481         if (int_status & DATA_INT_EXT) {
1482                 wilc_wlan_handle_isr_ext(int_status);
1483         #ifndef WILC_OPTIMIZE_SLEEP_INT
1484                 /* Chip is up and talking*/
1485                 genuChipPSstate = CHIP_WAKEDUP;
1486         #endif
1487         }
1488         if (int_status & SLEEP_INT_EXT) {
1489                 wilc_sleeptimer_isr_ext(int_status);
1490         }
1491
1492         if (!(int_status & (ALL_INT_EXT))) {
1493 #ifdef WILC_SDIO
1494                 PRINT_D(TX_DBG, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status);
1495 #endif
1496                 wilc_unknown_isr_ext();
1497         }
1498 #if ((!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO))
1499         linux_wlan_enable_irq();
1500 #endif
1501         release_bus(RELEASE_ALLOW_SLEEP);
1502 }
1503
1504 /********************************************
1505  *
1506  *      Firmware download
1507  *
1508  ********************************************/
1509 static int wilc_wlan_firmware_download(const uint8_t *buffer, uint32_t buffer_size)
1510 {
1511         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1512         uint32_t offset;
1513         uint32_t addr, size, size2, blksz;
1514         uint8_t *dma_buffer;
1515         int ret = 0;
1516
1517         blksz = (1ul << 12); /* Bug 4703: 4KB Good enough size for most platforms = PAGE_SIZE. */
1518         /* Allocate a DMA coherent  buffer. */
1519
1520         dma_buffer = kmalloc(blksz, GFP_KERNEL);
1521         if (dma_buffer == NULL) {
1522                 /*EIO   5*/
1523                 ret = -5;
1524                 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1525                 goto _fail_1;
1526         }
1527
1528         PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
1529         /**
1530          *      load the firmware
1531          **/
1532         offset = 0;
1533         do {
1534                 memcpy(&addr, &buffer[offset], 4);
1535                 memcpy(&size, &buffer[offset + 4], 4);
1536 #ifdef BIG_ENDIAN
1537                 addr = BYTE_SWAP(addr);
1538                 size = BYTE_SWAP(size);
1539 #endif
1540                 acquire_bus(ACQUIRE_ONLY);
1541                 offset += 8;
1542                 while (((int)size) && (offset < buffer_size)) {
1543                         if (size <= blksz)
1544                                 size2 = size;
1545                         else
1546                                 size2 = blksz;
1547                         /* Copy firmware into a DMA coherent buffer */
1548                         memcpy(dma_buffer, &buffer[offset], size2);
1549                         ret = p->hif_func.hif_block_tx(addr, dma_buffer, size2);
1550                         if (!ret)
1551                                 break;
1552
1553                         addr += size2;
1554                         offset += size2;
1555                         size -= size2;
1556                 }
1557                 release_bus(RELEASE_ONLY);
1558
1559                 if (!ret) {
1560                         /*EIO   5*/
1561                         ret = -5;
1562                         PRINT_ER("Can't download firmware IO error\n ");
1563                         goto _fail_;
1564                 }
1565                 PRINT_D(INIT_DBG, "Offset = %d\n", offset);
1566         } while (offset < buffer_size);
1567
1568 _fail_:
1569
1570         if (dma_buffer)
1571                 g_wlan.os_func.os_free(dma_buffer);
1572
1573 _fail_1:
1574
1575         return (ret < 0) ? ret : 0;
1576 }
1577
1578 /********************************************
1579  *
1580  *      Common
1581  *
1582  ********************************************/
1583 static int wilc_wlan_start(void)
1584 {
1585         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1586         uint32_t reg = 0;
1587         int ret;
1588         uint32_t chipid;
1589
1590         /**
1591          *      Set the host interface
1592          **/
1593 #ifdef OLD_FPGA_BITFILE
1594         acquire_bus(ACQUIRE_ONLY);
1595         ret = p->hif_func.hif_read_reg(WILC_VMM_CORE_CTL, &reg);
1596         if (!ret) {
1597                 wilc_debug(N_ERR, "[wilc start]: fail read reg vmm_core_ctl...\n");
1598                 release_bus(RELEASE_ALLOW_SLEEP);
1599                 return ret;
1600         }
1601         reg |= (p->io_func.io_type << 2);
1602         ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CTL, reg);
1603         if (!ret) {
1604                 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_ctl...\n");
1605                 release_bus(RELEASE_ONLY);
1606                 return ret;
1607         }
1608 #else
1609         if (p->io_func.io_type == HIF_SDIO) {
1610                 reg = 0;
1611                 reg |= (1 << 3); /* bug 4456 and 4557 */
1612         } else if (p->io_func.io_type == HIF_SPI) {
1613                 reg = 1;
1614         }
1615         acquire_bus(ACQUIRE_ONLY);
1616         ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CFG, reg);
1617         if (!ret) {
1618                 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_cfg...\n");
1619                 release_bus(RELEASE_ONLY);
1620                 /* EIO  5*/
1621                 ret = -5;
1622                 return ret;
1623         }
1624         reg = 0;
1625 #ifdef WILC_SDIO_IRQ_GPIO
1626         reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1627 #endif
1628
1629 #ifdef WILC_DISABLE_PMU
1630 #else
1631         reg |= WILC_HAVE_USE_PMU;
1632 #endif
1633
1634 #ifdef WILC_SLEEP_CLK_SRC_XO
1635         reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1636 #elif defined WILC_SLEEP_CLK_SRC_RTC
1637         reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1638 #endif
1639
1640 #ifdef WILC_EXT_PA_INV_TX_RX
1641         reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1642 #endif
1643
1644         reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1645
1646
1647 /*BugID_5257*/
1648 /*Set oscillator frequency*/
1649 #ifdef XTAL_24
1650         reg |= WILC_HAVE_XTAL_24;
1651 #endif
1652
1653 /*BugID_5271*/
1654 /*Enable/Disable GPIO configuration for FW logs*/
1655 #ifdef DISABLE_WILC_UART
1656         reg |= WILC_HAVE_DISABLE_WILC_UART;
1657 #endif
1658
1659         ret = p->hif_func.hif_write_reg(WILC_GP_REG_1, reg);
1660         if (!ret) {
1661                 wilc_debug(N_ERR, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
1662                 release_bus(RELEASE_ONLY);
1663                 /* EIO  5*/
1664                 ret = -5;
1665                 return ret;
1666         }
1667 #endif
1668
1669
1670         /**
1671          *      Bus related
1672          **/
1673         p->hif_func.hif_sync_ext(NUM_INT_EXT);
1674
1675         ret = p->hif_func.hif_read_reg(0x1000, &chipid);
1676         if (!ret) {
1677                 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1000 ...\n");
1678                 release_bus(RELEASE_ONLY);
1679                 /* EIO  5*/
1680                 ret = -5;
1681                 return ret;
1682         }
1683
1684         /**
1685          *      Go...
1686          **/
1687
1688
1689         p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1690         if ((reg & (1ul << 10)) == (1ul << 10)) {
1691                 reg &= ~(1ul << 10);
1692                 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1693                 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1694         }
1695
1696         reg |= (1ul << 10);
1697         ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1698         p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1699         release_bus(RELEASE_ONLY);
1700
1701         return (ret < 0) ? ret : 0;
1702 }
1703
1704 void wilc_wlan_global_reset(void)
1705 {
1706
1707         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1708
1709         acquire_bus(ACQUIRE_AND_WAKEUP);
1710         p->hif_func.hif_write_reg(WILC_GLB_RESET_0, 0x0);
1711         release_bus(RELEASE_ONLY);
1712 }
1713 static int wilc_wlan_stop(void)
1714 {
1715         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1716         uint32_t reg = 0;
1717         int ret;
1718         uint8_t timeout = 10;
1719         /**
1720          *      TODO: stop the firmware, need a re-download
1721          **/
1722         acquire_bus(ACQUIRE_AND_WAKEUP);
1723
1724         ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1725         if (!ret) {
1726                 PRINT_ER("Error while reading reg\n");
1727                 release_bus(RELEASE_ALLOW_SLEEP);
1728                 return ret;
1729         }
1730
1731         reg &= ~(1 << 10);
1732
1733
1734         ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1735         if (!ret) {
1736                 PRINT_ER("Error while writing reg\n");
1737                 release_bus(RELEASE_ALLOW_SLEEP);
1738                 return ret;
1739         }
1740
1741
1742
1743         do {
1744                 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1745                 if (!ret) {
1746                         PRINT_ER("Error while reading reg\n");
1747                         release_bus(RELEASE_ALLOW_SLEEP);
1748                         return ret;
1749                 }
1750                 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1751                 /*Workaround to ensure that the chip is actually reset*/
1752                 if ((reg & (1 << 10))) {
1753                         PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n", timeout);
1754                         reg &= ~(1 << 10);
1755                         ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1756                         timeout--;
1757                 } else {
1758                         PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n", timeout);
1759                         ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1760                         if (!ret) {
1761                                 PRINT_ER("Error while reading reg\n");
1762                                 release_bus(RELEASE_ALLOW_SLEEP);
1763                                 return ret;
1764                         }
1765                         PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1766                         break;
1767                 }
1768
1769         } while (timeout);
1770 #if 1
1771 /******************************************************************************/
1772 /* This was add at Bug 4595 to reset the chip while maintaining the bus state */
1773 /******************************************************************************/
1774         reg = ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 8) | (1 << 9) | (1 << 26) | (1 << 29) | (1 << 30) | (1 << 31)); /**/
1775         /**/
1776         p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);                                 /**/
1777         reg = ~(1 << 10);                                                                                               /**/
1778         /**/
1779         ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);                                 /**/
1780 /******************************************************************************/
1781 #endif
1782
1783         release_bus(RELEASE_ALLOW_SLEEP);
1784
1785         return ret;
1786 }
1787
1788 static void wilc_wlan_cleanup(void)
1789 {
1790         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1791         struct txq_entry_t *tqe;
1792         struct rxq_entry_t *rqe;
1793         uint32_t reg = 0;
1794         int ret;
1795
1796         p->quit = 1;
1797         do {
1798                 tqe = wilc_wlan_txq_remove_from_head();
1799                 if (tqe == NULL)
1800                         break;
1801                 if (tqe->tx_complete_func)
1802                         tqe->tx_complete_func(tqe->priv, 0);
1803                 p->os_func.os_free((void *)tqe);
1804         } while (1);
1805
1806         do {
1807                 rqe = wilc_wlan_rxq_remove();
1808                 if (rqe == NULL)
1809                         break;
1810 #ifdef MEMORY_DYNAMIC
1811                 p->os_func.os_free((void *)tqe->buffer);
1812 #endif
1813                 p->os_func.os_free((void *)rqe);
1814         } while (1);
1815
1816         /**
1817          *      clean up buffer
1818          **/
1819
1820         #ifdef MEMORY_STATIC
1821         if (p->rx_buffer) {
1822                 p->os_func.os_free(p->rx_buffer);
1823                 p->rx_buffer = NULL;
1824         }
1825         #endif
1826         if (p->tx_buffer) {
1827                 p->os_func.os_free(p->tx_buffer);
1828                 p->tx_buffer = NULL;
1829         }
1830
1831         acquire_bus(ACQUIRE_AND_WAKEUP);
1832
1833
1834         ret = p->hif_func.hif_read_reg(WILC_GP_REG_0, &reg);
1835         if (!ret) {
1836                 PRINT_ER("Error while reading reg\n");
1837                 release_bus(RELEASE_ALLOW_SLEEP);
1838         }
1839         PRINT_ER("Writing ABORT reg\n");
1840         ret = p->hif_func.hif_write_reg(WILC_GP_REG_0, (reg | ABORT_INT));
1841         if (!ret) {
1842                 PRINT_ER("Error while writing reg\n");
1843                 release_bus(RELEASE_ALLOW_SLEEP);
1844         }
1845         release_bus(RELEASE_ALLOW_SLEEP);
1846         /**
1847          *      io clean up
1848          **/
1849         p->hif_func.hif_deinit(NULL);
1850
1851 }
1852
1853 static int wilc_wlan_cfg_commit(int type, uint32_t drvHandler)
1854 {
1855         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1856         wilc_cfg_frame_t *cfg = &p->cfg_frame;
1857         int total_len = p->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1858         int seq_no = p->cfg_seq_no % 256;
1859         int driver_handler = (u32)drvHandler;
1860
1861
1862         /**
1863          *      Set up header
1864          **/
1865         if (type == WILC_CFG_SET) {             /* Set */
1866                 cfg->wid_header[0] = 'W';
1867         } else {                                        /* Query */
1868                 cfg->wid_header[0] = 'Q';
1869         }
1870         cfg->wid_header[1] = seq_no;    /* sequence number */
1871         cfg->wid_header[2] = (uint8_t)total_len;
1872         cfg->wid_header[3] = (uint8_t)(total_len >> 8);
1873         cfg->wid_header[4] = (uint8_t)driver_handler;
1874         cfg->wid_header[5] = (uint8_t)(driver_handler >> 8);
1875         cfg->wid_header[6] = (uint8_t)(driver_handler >> 16);
1876         cfg->wid_header[7] = (uint8_t)(driver_handler >> 24);
1877         p->cfg_seq_no = seq_no;
1878
1879         /**
1880          *      Add to TX queue
1881          **/
1882
1883         /*Edited by Amr - BugID_4720*/
1884         if (!wilc_wlan_txq_add_cfg_pkt(&cfg->wid_header[0], total_len))
1885                 return -1;
1886
1887         return 0;
1888 }
1889
1890 static int wilc_wlan_cfg_set(int start, uint32_t wid, uint8_t *buffer, uint32_t buffer_size, int commit, uint32_t drvHandler)
1891 {
1892         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1893         uint32_t offset;
1894         int ret_size;
1895
1896
1897         if (p->cfg_frame_in_use)
1898                 return 0;
1899
1900         if (start)
1901                 p->cfg_frame_offset = 0;
1902
1903         offset = p->cfg_frame_offset;
1904         ret_size = p->cif_func.cfg_wid_set(p->cfg_frame.frame, offset, (uint16_t)wid, buffer, buffer_size);
1905         offset += ret_size;
1906         p->cfg_frame_offset = offset;
1907
1908         if (commit) {
1909                 PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n", p->cfg_seq_no);
1910                 PRINT_D(RX_DBG, "Processing cfg_set()\n");
1911                 p->cfg_frame_in_use = 1;
1912
1913                 /*Edited by Amr - BugID_4720*/
1914                 if (wilc_wlan_cfg_commit(WILC_CFG_SET, drvHandler))
1915                         ret_size = 0;   /* BugID_5213 */
1916
1917                 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1918                         PRINT_D(TX_DBG, "Set Timed Out\n");
1919                         ret_size = 0;
1920                 }
1921                 p->cfg_frame_in_use = 0;
1922                 p->cfg_frame_offset = 0;
1923                 p->cfg_seq_no += 1;
1924
1925         }
1926
1927         return ret_size;
1928 }
1929 static int wilc_wlan_cfg_get(int start, uint32_t wid, int commit, uint32_t drvHandler)
1930 {
1931         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1932         uint32_t offset;
1933         int ret_size;
1934
1935
1936         if (p->cfg_frame_in_use)
1937                 return 0;
1938
1939         if (start)
1940                 p->cfg_frame_offset = 0;
1941
1942         offset = p->cfg_frame_offset;
1943         ret_size = p->cif_func.cfg_wid_get(p->cfg_frame.frame, offset, (uint16_t)wid);
1944         offset += ret_size;
1945         p->cfg_frame_offset = offset;
1946
1947         if (commit) {
1948                 p->cfg_frame_in_use = 1;
1949
1950                 /*Edited by Amr - BugID_4720*/
1951                 if (wilc_wlan_cfg_commit(WILC_CFG_QUERY, drvHandler))
1952                         ret_size = 0;   /* BugID_5213 */
1953
1954
1955                 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1956                         PRINT_D(TX_DBG, "Get Timed Out\n");
1957                         ret_size = 0;
1958                 }
1959                 PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
1960                 p->cfg_frame_in_use = 0;
1961                 p->cfg_frame_offset = 0;
1962                 p->cfg_seq_no += 1;
1963         }
1964
1965         return ret_size;
1966 }
1967
1968 static int wilc_wlan_cfg_get_val(uint32_t wid, uint8_t *buffer, uint32_t buffer_size)
1969 {
1970         wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1971         int ret;
1972
1973         ret = p->cif_func.cfg_wid_get_val((uint16_t)wid, buffer, buffer_size);
1974
1975         return ret;
1976 }
1977
1978 void wilc_bus_set_max_speed(void)
1979 {
1980
1981         /* Increase bus speed to max possible.  */
1982         g_wlan.hif_func.hif_set_max_bus_speed();
1983 }
1984
1985 void wilc_bus_set_default_speed(void)
1986 {
1987
1988         /* Restore bus speed to default.  */
1989         g_wlan.hif_func.hif_set_default_bus_speed();
1990 }
1991 uint32_t init_chip(void)
1992 {
1993         uint32_t chipid;
1994         uint32_t reg, ret = 0;
1995
1996 #if defined(PLAT_RK3026_TCHIP)
1997         acquire_bus(ACQUIRE_AND_WAKEUP); /* AMR : 0422 RK3026 Crash issue */
1998 #else
1999         acquire_bus(ACQUIRE_ONLY);
2000 #endif
2001
2002         chipid = wilc_get_chipid(true);
2003
2004
2005
2006         if ((chipid & 0xfff) != 0xa0) {
2007                 /**
2008                  * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
2009                  * or SD_DAT3 [SPI platform] to ?1?
2010                  **/
2011                 /* Set cortus reset register to register control. */
2012                 ret = g_wlan.hif_func.hif_read_reg(0x1118, &reg);
2013                 if (!ret) {
2014                         wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
2015                         return ret;
2016                 }
2017                 reg |= (1 << 0);
2018                 ret = g_wlan.hif_func.hif_write_reg(0x1118, reg);
2019                 if (!ret) {
2020                         wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
2021                         return ret;
2022                 }
2023                 /**
2024                  * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
2025                  * (Cortus map) or C0000 (AHB map).
2026                  **/
2027                 ret = g_wlan.hif_func.hif_write_reg(0xc0000, 0x71);
2028                 if (!ret) {
2029                         wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
2030                         return ret;
2031                 }
2032         }
2033
2034         release_bus(RELEASE_ONLY);
2035
2036         return ret;
2037
2038 }
2039
2040 uint32_t wilc_get_chipid(uint8_t update)
2041 {
2042         static uint32_t chipid;
2043         /* SDIO can't read into global variables */
2044         /* Use this variable as a temp, then copy to the global */
2045         uint32_t tempchipid = 0;
2046         uint32_t rfrevid;
2047
2048         if (chipid == 0 || update != 0) {
2049                 g_wlan.hif_func.hif_read_reg(0x1000, &tempchipid);
2050                 g_wlan.hif_func.hif_read_reg(0x13f4, &rfrevid);
2051                 if (!ISWILC1000(tempchipid)) {
2052                         chipid = 0;
2053                         goto _fail_;
2054                 }
2055                 if (tempchipid == 0x1002a0) {
2056                         if (rfrevid == 0x1) { /* 1002A0 */
2057                         } else { /* if (rfrevid == 0x2) */   /* 1002A1 */
2058                                 tempchipid = 0x1002a1;
2059                         }
2060                 } else if (tempchipid == 0x1002b0) {
2061                         if (rfrevid == 3) { /* 1002B0 */
2062                         } else if (rfrevid == 4) { /* 1002B1 */
2063                                 tempchipid = 0x1002b1;
2064                         } else { /* if(rfrevid == 5) */   /* 1002B2 */
2065                                 tempchipid = 0x1002b2;
2066                         }
2067                 } else {
2068                 }
2069
2070                 chipid = tempchipid;
2071         }
2072 _fail_:
2073         return chipid;
2074 }
2075
2076 #ifdef COMPLEMENT_BOOT
2077 uint8_t core_11b_ready(void)
2078 {
2079         uint32_t reg_val;
2080
2081         acquire_bus(ACQUIRE_ONLY);
2082         g_wlan.hif_func.hif_write_reg(0x16082c, 1);
2083         g_wlan.hif_func.hif_write_reg(0x161600, 0x90);
2084         g_wlan.hif_func.hif_read_reg(0x161600, &reg_val);
2085         release_bus(RELEASE_ONLY);
2086
2087         if (reg_val == 0x90)
2088                 return 0;
2089         else
2090                 return 1;
2091 }
2092 #endif
2093
2094 int wilc_wlan_init(wilc_wlan_inp_t *inp, wilc_wlan_oup_t *oup)
2095 {
2096
2097         int ret = 0;
2098
2099         PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
2100
2101         memset((void *)&g_wlan, 0, sizeof(wilc_wlan_dev_t));
2102
2103         /**
2104          *      store the input
2105          **/
2106         memcpy((void *)&g_wlan.os_func, (void *)&inp->os_func, sizeof(wilc_wlan_os_func_t));
2107         memcpy((void *)&g_wlan.io_func, (void *)&inp->io_func, sizeof(wilc_wlan_io_func_t));
2108         memcpy((void *)&g_wlan.net_func, (void *)&inp->net_func, sizeof(wilc_wlan_net_func_t));
2109         memcpy((void *)&g_wlan.indicate_func, (void *)&inp->indicate_func, sizeof(wilc_wlan_net_func_t));
2110         g_wlan.hif_lock = inp->os_context.hif_critical_section;
2111         g_wlan.txq_lock = inp->os_context.txq_critical_section;
2112
2113         /*Added by Amr - BugID_4720*/
2114         g_wlan.txq_add_to_head_lock = inp->os_context.txq_add_to_head_critical_section;
2115
2116         /*Added by Amr - BugID_4720*/
2117         g_wlan.txq_spinlock = inp->os_context.txq_spin_lock;
2118
2119         g_wlan.rxq_lock = inp->os_context.rxq_critical_section;
2120         g_wlan.txq_wait = inp->os_context.txq_wait_event;
2121         g_wlan.rxq_wait = inp->os_context.rxq_wait_event;
2122         g_wlan.cfg_wait = inp->os_context.cfg_wait_event;
2123         g_wlan.tx_buffer_size = inp->os_context.tx_buffer_size;
2124 #if defined (MEMORY_STATIC)
2125         g_wlan.rx_buffer_size = inp->os_context.rx_buffer_size;
2126 #endif
2127         /***
2128          *      host interface init
2129          **/
2130 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2131         if (!g_wilc_initialized) {
2132                 custom_lock_bus(g_mac_open);
2133                 custom_wakeup(g_mac_open);
2134         }
2135 #endif
2136
2137         if ((inp->io_func.io_type & 0x1) == HIF_SDIO) {
2138                 if (!hif_sdio.hif_init(inp, wilc_debug)) {
2139                         /* EIO  5 */
2140                         ret = -5;
2141                         goto _fail_;
2142                 }
2143                 memcpy((void *)&g_wlan.hif_func, &hif_sdio, sizeof(wilc_hif_func_t));
2144         } else {
2145                 if ((inp->io_func.io_type & 0x1) == HIF_SPI) {
2146                         /**
2147                          *      TODO:
2148                          **/
2149                         if (!hif_spi.hif_init(inp, wilc_debug)) {
2150                                 /* EIO  5 */
2151                                 ret = -5;
2152                                 goto _fail_;
2153                         }
2154                         memcpy((void *)&g_wlan.hif_func, &hif_spi, sizeof(wilc_hif_func_t));
2155                 } else {
2156                         /* EIO  5 */
2157                         ret = -5;
2158                         goto _fail_;
2159                 }
2160         }
2161
2162         /***
2163          *      mac interface init
2164          **/
2165         if (!mac_cfg.cfg_init(wilc_debug)) {
2166                 /* ENOBUFS      105 */
2167                 ret = -105;
2168                 goto _fail_;
2169         }
2170         memcpy((void *)&g_wlan.cif_func, &mac_cfg, sizeof(wilc_cfg_func_t));
2171
2172
2173         /**
2174          *      alloc tx, rx buffer
2175          **/
2176         if (g_wlan.tx_buffer == NULL)
2177                 g_wlan.tx_buffer = kmalloc(g_wlan.tx_buffer_size, GFP_KERNEL);
2178         PRINT_D(TX_DBG, "g_wlan.tx_buffer = %p\n", g_wlan.tx_buffer);
2179
2180         if (g_wlan.tx_buffer == NULL) {
2181                 /* ENOBUFS      105 */
2182                 ret = -105;
2183                 PRINT_ER("Can't allocate Tx Buffer");
2184                 goto _fail_;
2185         }
2186
2187 /* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
2188 #if defined (MEMORY_STATIC)
2189         if (g_wlan.rx_buffer == NULL)
2190                 g_wlan.rx_buffer = kmalloc(g_wlan.rx_buffer_size, GFP_KERNEL);
2191         PRINT_D(TX_DBG, "g_wlan.rx_buffer =%p\n", g_wlan.rx_buffer);
2192         if (g_wlan.rx_buffer == NULL) {
2193                 /* ENOBUFS      105 */
2194                 ret = -105;
2195                 PRINT_ER("Can't allocate Rx Buffer");
2196                 goto _fail_;
2197         }
2198 #endif
2199
2200         /**
2201          *      export functions
2202          **/
2203         oup->wlan_firmware_download = wilc_wlan_firmware_download;
2204         oup->wlan_start = wilc_wlan_start;
2205         oup->wlan_stop = wilc_wlan_stop;
2206         oup->wlan_add_to_tx_que = wilc_wlan_txq_add_net_pkt;
2207         oup->wlan_handle_tx_que = wilc_wlan_handle_txq;
2208         oup->wlan_handle_rx_que = wilc_wlan_handle_rxq;
2209         oup->wlan_handle_rx_isr = wilc_handle_isr;
2210         oup->wlan_cleanup = wilc_wlan_cleanup;
2211         oup->wlan_cfg_set = wilc_wlan_cfg_set;
2212         oup->wlan_cfg_get = wilc_wlan_cfg_get;
2213         oup->wlan_cfg_get_value = wilc_wlan_cfg_get_val;
2214
2215         /*Bug3959: transmitting mgmt frames received from host*/
2216         #if defined(WILC_AP_EXTERNAL_MLME) || defined(WILC_P2P)
2217         oup->wlan_add_mgmt_to_tx_que = wilc_wlan_txq_add_mgmt_pkt;
2218
2219         #ifdef WILC_FULLY_HOSTING_AP
2220         oup->wlan_add_data_to_tx_que = wilc_FH_wlan_txq_add_net_pkt;
2221         #endif
2222         #endif
2223
2224         if (!init_chip()) {
2225                 /* EIO  5 */
2226                 ret = -5;
2227                 goto _fail_;
2228         }
2229 #ifdef  TCP_ACK_FILTER
2230         Init_TCP_tracking();
2231 #endif
2232
2233 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2234         if (!g_wilc_initialized)
2235                 custom_unlock_bus(g_mac_open);
2236 #endif
2237
2238         return 1;
2239
2240 _fail_:
2241
2242   #ifdef MEMORY_STATIC
2243         if (g_wlan.rx_buffer) {
2244                 g_wlan.os_func.os_free(g_wlan.rx_buffer);
2245                 g_wlan.rx_buffer = NULL;
2246         }
2247   #endif
2248         if (g_wlan.tx_buffer) {
2249                 g_wlan.os_func.os_free(g_wlan.tx_buffer);
2250                 g_wlan.tx_buffer = NULL;
2251         }
2252
2253 #if defined(PLAT_RK3026_TCHIP) /* AMR : 0422 RK3026 Crash issue */
2254         if (!g_wilc_initialized)
2255                 custom_unlock_bus(g_mac_open);
2256 #endif
2257
2258         return ret;
2259
2260 }
2261
2262 #define BIT31 (1 << 31)
2263 u16 Set_machw_change_vir_if(bool bValue)
2264 {
2265         u16 ret;
2266         u32 reg;
2267
2268         /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
2269         (&g_wlan)->os_func.os_enter_cs((&g_wlan)->hif_lock);
2270         ret = (&g_wlan)->hif_func.hif_read_reg(WILC_CHANGING_VIR_IF, &reg);
2271         if (!ret) {
2272                 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
2273         }
2274
2275         if (bValue)
2276                 reg |= (BIT31);
2277         else
2278                 reg &= ~(BIT31);
2279
2280         ret = (&g_wlan)->hif_func.hif_write_reg(WILC_CHANGING_VIR_IF, reg);
2281
2282         if (!ret) {
2283                 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
2284         }
2285         (&g_wlan)->os_func.os_leave_cs((&g_wlan)->hif_lock);
2286
2287         return ret;
2288 }
2289
2290 #ifdef WILC_FULLY_HOSTING_AP
2291 wilc_wlan_dev_t *Get_wlan_context(u16 *pu16size)
2292 {
2293         *pu16size = sizeof(wilc_wlan_dev_t);
2294         return &g_wlan;
2295 }
2296 #endif
2297