]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/wilc1000/wilc_wlan.c
Merge tag 'armsoc-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[karo-tx-linux.git] / drivers / staging / wilc1000 / wilc_wlan.c
1 #include "wilc_wlan_if.h"
2 #include "wilc_wlan.h"
3 #include "wilc_wfi_netdevice.h"
4 #include "wilc_wlan_cfg.h"
5
6 static CHIP_PS_STATE_T chip_ps_state = CHIP_WAKEDUP;
7
8 static inline void acquire_bus(struct wilc *wilc, BUS_ACQUIRE_T acquire)
9 {
10         mutex_lock(&wilc->hif_cs);
11         if (acquire == ACQUIRE_AND_WAKEUP)
12                 chip_wakeup(wilc);
13 }
14
15 static inline void release_bus(struct wilc *wilc, BUS_RELEASE_T release)
16 {
17         if (release == RELEASE_ALLOW_SLEEP)
18                 chip_allow_sleep(wilc);
19         mutex_unlock(&wilc->hif_cs);
20 }
21
22 static void wilc_wlan_txq_remove(struct wilc *wilc, struct txq_entry_t *tqe)
23 {
24         if (tqe == wilc->txq_head) {
25                 wilc->txq_head = tqe->next;
26                 if (wilc->txq_head)
27                         wilc->txq_head->prev = NULL;
28         } else if (tqe == wilc->txq_tail) {
29                 wilc->txq_tail = (tqe->prev);
30                 if (wilc->txq_tail)
31                         wilc->txq_tail->next = NULL;
32         } else {
33                 tqe->prev->next = tqe->next;
34                 tqe->next->prev = tqe->prev;
35         }
36         wilc->txq_entries -= 1;
37 }
38
39 static struct txq_entry_t *
40 wilc_wlan_txq_remove_from_head(struct net_device *dev)
41 {
42         struct txq_entry_t *tqe;
43         unsigned long flags;
44         struct wilc_vif *vif;
45         struct wilc *wilc;
46
47         vif = netdev_priv(dev);
48         wilc = vif->wilc;
49
50         spin_lock_irqsave(&wilc->txq_spinlock, flags);
51         if (wilc->txq_head) {
52                 tqe = wilc->txq_head;
53                 wilc->txq_head = tqe->next;
54                 if (wilc->txq_head)
55                         wilc->txq_head->prev = NULL;
56
57                 wilc->txq_entries -= 1;
58         } else {
59                 tqe = NULL;
60         }
61         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
62         return tqe;
63 }
64
65 static void wilc_wlan_txq_add_to_tail(struct net_device *dev,
66                                       struct txq_entry_t *tqe)
67 {
68         unsigned long flags;
69         struct wilc_vif *vif;
70         struct wilc *wilc;
71
72         vif = netdev_priv(dev);
73         wilc = vif->wilc;
74
75         spin_lock_irqsave(&wilc->txq_spinlock, flags);
76
77         if (!wilc->txq_head) {
78                 tqe->next = NULL;
79                 tqe->prev = NULL;
80                 wilc->txq_head = tqe;
81                 wilc->txq_tail = tqe;
82         } else {
83                 tqe->next = NULL;
84                 tqe->prev = wilc->txq_tail;
85                 wilc->txq_tail->next = tqe;
86                 wilc->txq_tail = tqe;
87         }
88         wilc->txq_entries += 1;
89
90         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
91
92         up(&wilc->txq_event);
93 }
94
95 static int wilc_wlan_txq_add_to_head(struct wilc_vif *vif,
96                                      struct txq_entry_t *tqe)
97 {
98         unsigned long flags;
99         struct wilc *wilc = vif->wilc;
100
101         if (wilc_lock_timeout(wilc, &wilc->txq_add_to_head_cs,
102                                     CFG_PKTS_TIMEOUT))
103                 return -1;
104
105         spin_lock_irqsave(&wilc->txq_spinlock, flags);
106
107         if (!wilc->txq_head) {
108                 tqe->next = NULL;
109                 tqe->prev = NULL;
110                 wilc->txq_head = tqe;
111                 wilc->txq_tail = tqe;
112         } else {
113                 tqe->next = wilc->txq_head;
114                 tqe->prev = NULL;
115                 wilc->txq_head->prev = tqe;
116                 wilc->txq_head = tqe;
117         }
118         wilc->txq_entries += 1;
119
120         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
121         up(&wilc->txq_add_to_head_cs);
122         up(&wilc->txq_event);
123
124         return 0;
125 }
126
127 struct ack_session_info;
128 struct ack_session_info {
129         u32 seq_num;
130         u32 bigger_ack_num;
131         u16 src_port;
132         u16 dst_port;
133         u16 status;
134 };
135
136 struct pending_acks_info {
137         u32 ack_num;
138         u32 session_index;
139         struct txq_entry_t  *txqe;
140 };
141
142 #define NOT_TCP_ACK                     (-1)
143
144 #define MAX_TCP_SESSION         25
145 #define MAX_PENDING_ACKS                256
146 static struct ack_session_info ack_session_info[2 * MAX_TCP_SESSION];
147 static struct pending_acks_info pending_acks_info[MAX_PENDING_ACKS];
148
149 static u32 pending_base;
150 static u32 tcp_session;
151 static u32 pending_acks;
152
153 static inline int init_tcp_tracking(void)
154 {
155         return 0;
156 }
157
158 static inline int add_tcp_session(u32 src_prt, u32 dst_prt, u32 seq)
159 {
160         if (tcp_session < 2 * MAX_TCP_SESSION) {
161                 ack_session_info[tcp_session].seq_num = seq;
162                 ack_session_info[tcp_session].bigger_ack_num = 0;
163                 ack_session_info[tcp_session].src_port = src_prt;
164                 ack_session_info[tcp_session].dst_port = dst_prt;
165                 tcp_session++;
166         }
167         return 0;
168 }
169
170 static inline int update_tcp_session(u32 index, u32 ack)
171 {
172         if (index < 2 * MAX_TCP_SESSION &&
173             ack > ack_session_info[index].bigger_ack_num)
174                 ack_session_info[index].bigger_ack_num = ack;
175         return 0;
176 }
177
178 static inline int add_tcp_pending_ack(u32 ack, u32 session_index,
179                                       struct txq_entry_t *txqe)
180 {
181         if (pending_base + pending_acks < MAX_PENDING_ACKS) {
182                 pending_acks_info[pending_base + pending_acks].ack_num = ack;
183                 pending_acks_info[pending_base + pending_acks].txqe = txqe;
184                 pending_acks_info[pending_base + pending_acks].session_index = session_index;
185                 txqe->tcp_pending_ack_idx = pending_base + pending_acks;
186                 pending_acks++;
187         }
188         return 0;
189 }
190
191 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
192 {
193         u8 *eth_hdr_ptr;
194         u8 *buffer = tqe->buffer;
195         unsigned short h_proto;
196         int i;
197         unsigned long flags;
198         struct wilc_vif *vif;
199         struct wilc *wilc;
200
201         vif = netdev_priv(dev);
202         wilc = vif->wilc;
203
204         spin_lock_irqsave(&wilc->txq_spinlock, flags);
205
206         eth_hdr_ptr = &buffer[0];
207         h_proto = ntohs(*((unsigned short *)&eth_hdr_ptr[12]));
208         if (h_proto == ETH_P_IP) {
209                 u8 *ip_hdr_ptr;
210                 u8 protocol;
211
212                 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
213                 protocol = ip_hdr_ptr[9];
214
215                 if (protocol == 0x06) {
216                         u8 *tcp_hdr_ptr;
217                         u32 IHL, total_length, data_offset;
218
219                         tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
220                         IHL = (ip_hdr_ptr[0] & 0xf) << 2;
221                         total_length = ((u32)ip_hdr_ptr[2] << 8) +
222                                         (u32)ip_hdr_ptr[3];
223                         data_offset = ((u32)tcp_hdr_ptr[12] & 0xf0) >> 2;
224                         if (total_length == (IHL + data_offset)) {
225                                 u32 seq_no, ack_no;
226
227                                 seq_no = ((u32)tcp_hdr_ptr[4] << 24) +
228                                          ((u32)tcp_hdr_ptr[5] << 16) +
229                                          ((u32)tcp_hdr_ptr[6] << 8) +
230                                          (u32)tcp_hdr_ptr[7];
231
232                                 ack_no = ((u32)tcp_hdr_ptr[8] << 24) +
233                                          ((u32)tcp_hdr_ptr[9] << 16) +
234                                          ((u32)tcp_hdr_ptr[10] << 8) +
235                                          (u32)tcp_hdr_ptr[11];
236
237                                 for (i = 0; i < tcp_session; i++) {
238                                         if (i < 2 * MAX_TCP_SESSION &&
239                                             ack_session_info[i].seq_num == seq_no) {
240                                                 update_tcp_session(i, ack_no);
241                                                 break;
242                                         }
243                                 }
244                                 if (i == tcp_session)
245                                         add_tcp_session(0, 0, seq_no);
246
247                                 add_tcp_pending_ack(ack_no, i, tqe);
248                         }
249                 }
250         }
251         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
252 }
253
254 static int wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
255 {
256         struct wilc_vif *vif;
257         struct wilc *wilc;
258         u32 i = 0;
259         u32 dropped = 0;
260
261         vif = netdev_priv(dev);
262         wilc = vif->wilc;
263
264         spin_lock_irqsave(&wilc->txq_spinlock, wilc->txq_spinlock_flags);
265         for (i = pending_base; i < (pending_base + pending_acks); i++) {
266                 if (i >= MAX_PENDING_ACKS ||
267                     pending_acks_info[i].session_index >= 2 * MAX_TCP_SESSION)
268                         break;
269                 if (pending_acks_info[i].ack_num < ack_session_info[pending_acks_info[i].session_index].bigger_ack_num) {
270                         struct txq_entry_t *tqe;
271
272                         tqe = pending_acks_info[i].txqe;
273                         if (tqe) {
274                                 wilc_wlan_txq_remove(wilc, tqe);
275                                 tqe->status = 1;
276                                 if (tqe->tx_complete_func)
277                                         tqe->tx_complete_func(tqe->priv,
278                                                               tqe->status);
279                                 kfree(tqe);
280                                 dropped++;
281                         }
282                 }
283         }
284         pending_acks = 0;
285         tcp_session = 0;
286
287         if (pending_base == 0)
288                 pending_base = MAX_TCP_SESSION;
289         else
290                 pending_base = 0;
291
292         spin_unlock_irqrestore(&wilc->txq_spinlock, wilc->txq_spinlock_flags);
293
294         while (dropped > 0) {
295                 wilc_lock_timeout(wilc, &wilc->txq_event, 1);
296                 dropped--;
297         }
298
299         return 1;
300 }
301
302 static bool enabled;
303
304 void wilc_enable_tcp_ack_filter(bool value)
305 {
306         enabled = value;
307 }
308
309 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
310                                      u32 buffer_size)
311 {
312         struct txq_entry_t *tqe;
313         struct wilc *wilc = vif->wilc;
314
315         netdev_dbg(vif->ndev, "Adding config packet ...\n");
316         if (wilc->quit) {
317                 netdev_dbg(vif->ndev, "Return due to clear function\n");
318                 up(&wilc->cfg_event);
319                 return 0;
320         }
321
322         tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
323         if (!tqe)
324                 return 0;
325
326         tqe->type = WILC_CFG_PKT;
327         tqe->buffer = buffer;
328         tqe->buffer_size = buffer_size;
329         tqe->tx_complete_func = NULL;
330         tqe->priv = NULL;
331         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
332
333         if (wilc_wlan_txq_add_to_head(vif, tqe))
334                 return 0;
335         return 1;
336 }
337
338 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
339                               u32 buffer_size, wilc_tx_complete_func_t func)
340 {
341         struct txq_entry_t *tqe;
342         struct wilc_vif *vif = netdev_priv(dev);
343         struct wilc *wilc;
344
345         wilc = vif->wilc;
346
347         if (wilc->quit)
348                 return 0;
349
350         tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
351
352         if (!tqe)
353                 return 0;
354         tqe->type = WILC_NET_PKT;
355         tqe->buffer = buffer;
356         tqe->buffer_size = buffer_size;
357         tqe->tx_complete_func = func;
358         tqe->priv = priv;
359
360         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
361         if (enabled)
362                 tcp_process(dev, tqe);
363         wilc_wlan_txq_add_to_tail(dev, tqe);
364         return wilc->txq_entries;
365 }
366
367 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
368                                u32 buffer_size, wilc_tx_complete_func_t func)
369 {
370         struct txq_entry_t *tqe;
371         struct wilc_vif *vif = netdev_priv(dev);
372         struct wilc *wilc;
373
374         wilc = vif->wilc;
375
376         if (wilc->quit)
377                 return 0;
378
379         tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
380
381         if (!tqe)
382                 return 0;
383         tqe->type = WILC_MGMT_PKT;
384         tqe->buffer = buffer;
385         tqe->buffer_size = buffer_size;
386         tqe->tx_complete_func = func;
387         tqe->priv = priv;
388         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
389         wilc_wlan_txq_add_to_tail(dev, tqe);
390         return 1;
391 }
392
393 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
394 {
395         struct txq_entry_t *tqe;
396         unsigned long flags;
397
398         spin_lock_irqsave(&wilc->txq_spinlock, flags);
399
400         tqe = wilc->txq_head;
401
402         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
403
404         return tqe;
405 }
406
407 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
408                                                   struct txq_entry_t *tqe)
409 {
410         unsigned long flags;
411
412         spin_lock_irqsave(&wilc->txq_spinlock, flags);
413
414         tqe = tqe->next;
415         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
416
417         return tqe;
418 }
419
420 static int wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
421 {
422         if (wilc->quit)
423                 return 0;
424
425         mutex_lock(&wilc->rxq_cs);
426         if (!wilc->rxq_head) {
427                 rqe->next = NULL;
428                 wilc->rxq_head = rqe;
429                 wilc->rxq_tail = rqe;
430         } else {
431                 wilc->rxq_tail->next = rqe;
432                 rqe->next = NULL;
433                 wilc->rxq_tail = rqe;
434         }
435         wilc->rxq_entries += 1;
436         mutex_unlock(&wilc->rxq_cs);
437         return wilc->rxq_entries;
438 }
439
440 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
441 {
442         if (wilc->rxq_head) {
443                 struct rxq_entry_t *rqe;
444
445                 mutex_lock(&wilc->rxq_cs);
446                 rqe = wilc->rxq_head;
447                 wilc->rxq_head = wilc->rxq_head->next;
448                 wilc->rxq_entries -= 1;
449                 mutex_unlock(&wilc->rxq_cs);
450                 return rqe;
451         }
452         return NULL;
453 }
454
455 void chip_allow_sleep(struct wilc *wilc)
456 {
457         u32 reg = 0;
458
459         wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
460
461         wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
462         wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
463 }
464 EXPORT_SYMBOL_GPL(chip_allow_sleep);
465
466 void chip_wakeup(struct wilc *wilc)
467 {
468         u32 reg, clk_status_reg;
469
470         if ((wilc->io_type & 0x1) == HIF_SPI) {
471                 do {
472                         wilc->hif_func->hif_read_reg(wilc, 1, &reg);
473                         wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
474                         wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
475
476                         do {
477                                 usleep_range(2 * 1000, 2 * 1000);
478                                 wilc_get_chipid(wilc, true);
479                         } while (wilc_get_chipid(wilc, true) == 0);
480                 } while (wilc_get_chipid(wilc, true) == 0);
481         } else if ((wilc->io_type & 0x1) == HIF_SDIO)    {
482                 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
483                 udelay(200);
484                 wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
485                 do {
486                         wilc->hif_func->hif_write_reg(wilc, 0xf0,
487                                                       reg | BIT(0));
488                         wilc->hif_func->hif_read_reg(wilc, 0xf1,
489                                                      &clk_status_reg);
490
491                         while ((clk_status_reg & 0x1) == 0) {
492                                 usleep_range(2 * 1000, 2 * 1000);
493
494                                 wilc->hif_func->hif_read_reg(wilc, 0xf1,
495                                                              &clk_status_reg);
496                         }
497                         if ((clk_status_reg & 0x1) == 0) {
498                                 wilc->hif_func->hif_write_reg(wilc, 0xf0,
499                                                               reg & (~BIT(0)));
500                         }
501                 } while ((clk_status_reg & 0x1) == 0);
502         }
503
504         if (chip_ps_state == CHIP_SLEEPING_MANUAL) {
505                 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
506                         u32 val32;
507
508                         wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
509                         val32 |= BIT(6);
510                         wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
511
512                         wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
513                         val32 |= BIT(6);
514                         wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
515                 }
516         }
517         chip_ps_state = CHIP_WAKEDUP;
518 }
519 EXPORT_SYMBOL_GPL(chip_wakeup);
520
521 void wilc_chip_sleep_manually(struct wilc *wilc)
522 {
523         if (chip_ps_state != CHIP_WAKEDUP)
524                 return;
525         acquire_bus(wilc, ACQUIRE_ONLY);
526
527         chip_allow_sleep(wilc);
528         wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
529
530         chip_ps_state = CHIP_SLEEPING_MANUAL;
531         release_bus(wilc, RELEASE_ONLY);
532 }
533 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
534
535 void host_wakeup_notify(struct wilc *wilc)
536 {
537         acquire_bus(wilc, ACQUIRE_ONLY);
538         wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
539         release_bus(wilc, RELEASE_ONLY);
540 }
541 EXPORT_SYMBOL_GPL(host_wakeup_notify);
542
543 void host_sleep_notify(struct wilc *wilc)
544 {
545         acquire_bus(wilc, ACQUIRE_ONLY);
546         wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
547         release_bus(wilc, RELEASE_ONLY);
548 }
549 EXPORT_SYMBOL_GPL(host_sleep_notify);
550
551 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
552 {
553         int i, entries = 0;
554         u32 sum;
555         u32 reg;
556         u8 *txb;
557         u32 offset = 0;
558         int vmm_sz = 0;
559         struct txq_entry_t *tqe;
560         int ret = 0;
561         int counter;
562         int timeout;
563         u32 vmm_table[WILC_VMM_TBL_SIZE];
564         struct wilc_vif *vif;
565         struct wilc *wilc;
566
567         vif = netdev_priv(dev);
568         wilc = vif->wilc;
569
570         txb = wilc->tx_buffer;
571         wilc->txq_exit = 0;
572         do {
573                 if (wilc->quit)
574                         break;
575
576                 wilc_lock_timeout(wilc, &wilc->txq_add_to_head_cs,
577                                         CFG_PKTS_TIMEOUT);
578                 wilc_wlan_txq_filter_dup_tcp_ack(dev);
579                 tqe = wilc_wlan_txq_get_first(wilc);
580                 i = 0;
581                 sum = 0;
582                 do {
583                         if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
584                                 if (tqe->type == WILC_CFG_PKT)
585                                         vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
586
587                                 else if (tqe->type == WILC_NET_PKT)
588                                         vmm_sz = ETH_ETHERNET_HDR_OFFSET;
589
590                                 else
591                                         vmm_sz = HOST_HDR_OFFSET;
592
593                                 vmm_sz += tqe->buffer_size;
594
595                                 if (vmm_sz & 0x3)
596                                         vmm_sz = (vmm_sz + 4) & ~0x3;
597
598                                 if ((sum + vmm_sz) > LINUX_TX_SIZE)
599                                         break;
600
601                                 vmm_table[i] = vmm_sz / 4;
602                                 if (tqe->type == WILC_CFG_PKT)
603                                         vmm_table[i] |= BIT(10);
604                                 vmm_table[i] = cpu_to_le32(vmm_table[i]);
605
606                                 i++;
607                                 sum += vmm_sz;
608                                 tqe = wilc_wlan_txq_get_next(wilc, tqe);
609                         } else {
610                                 break;
611                         }
612                 } while (1);
613
614                 if (i == 0)
615                         break;
616                 vmm_table[i] = 0x0;
617
618                 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
619                 counter = 0;
620                 do {
621                         ret = wilc->hif_func->hif_read_reg(wilc,
622                                                            WILC_HOST_TX_CTRL,
623                                                            &reg);
624                         if (!ret)
625                                 break;
626
627                         if ((reg & 0x1) == 0) {
628                                 break;
629                         } else {
630                                 counter++;
631                                 if (counter > 200) {
632                                         counter = 0;
633                                         ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
634                                         break;
635                                 }
636                         }
637                 } while (!wilc->quit);
638
639                 if (!ret)
640                         goto _end_;
641
642                 timeout = 200;
643                 do {
644                         ret = wilc->hif_func->hif_block_tx(wilc, WILC_VMM_TBL_RX_SHADOW_BASE, (u8 *)vmm_table, ((i + 1) * 4));
645                         if (!ret)
646                                 break;
647
648                         ret = wilc->hif_func->hif_write_reg(wilc,
649                                                             WILC_HOST_VMM_CTL,
650                                                             0x2);
651                         if (!ret)
652                                 break;
653
654                         do {
655                                 ret = wilc->hif_func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, &reg);
656                                 if (!ret)
657                                         break;
658                                 if ((reg >> 2) & 0x1) {
659                                         entries = ((reg >> 3) & 0x3f);
660                                         break;
661                                 } else {
662                                         release_bus(wilc, RELEASE_ALLOW_SLEEP);
663                                 }
664                         } while (--timeout);
665                         if (timeout <= 0) {
666                                 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
667                                 break;
668                         }
669
670                         if (!ret)
671                                 break;
672
673                         if (entries == 0) {
674                                 ret = wilc->hif_func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
675                                 if (!ret)
676                                         break;
677                                 reg &= ~BIT(0);
678                                 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
679                                 if (!ret)
680                                         break;
681                                 break;
682                         } else {
683                                 break;
684                         }
685                 } while (1);
686
687                 if (!ret)
688                         goto _end_;
689
690                 if (entries == 0) {
691                         ret = WILC_TX_ERR_NO_BUF;
692                         goto _end_;
693                 }
694
695                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
696
697                 offset = 0;
698                 i = 0;
699                 do {
700                         tqe = wilc_wlan_txq_remove_from_head(dev);
701                         if (tqe && (vmm_table[i] != 0)) {
702                                 u32 header, buffer_offset;
703
704                                 vmm_table[i] = cpu_to_le32(vmm_table[i]);
705                                 vmm_sz = (vmm_table[i] & 0x3ff);
706                                 vmm_sz *= 4;
707                                 header = (tqe->type << 31) |
708                                          (tqe->buffer_size << 15) |
709                                          vmm_sz;
710                                 if (tqe->type == WILC_MGMT_PKT)
711                                         header |= BIT(30);
712                                 else
713                                         header &= ~BIT(30);
714
715                                 header = cpu_to_le32(header);
716                                 memcpy(&txb[offset], &header, 4);
717                                 if (tqe->type == WILC_CFG_PKT) {
718                                         buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
719                                 } else if (tqe->type == WILC_NET_PKT) {
720                                         char *bssid = ((struct tx_complete_data *)(tqe->priv))->bssid;
721
722                                         buffer_offset = ETH_ETHERNET_HDR_OFFSET;
723                                         memcpy(&txb[offset + 4], bssid, 6);
724                                 } else {
725                                         buffer_offset = HOST_HDR_OFFSET;
726                                 }
727
728                                 memcpy(&txb[offset + buffer_offset],
729                                        tqe->buffer, tqe->buffer_size);
730                                 offset += vmm_sz;
731                                 i++;
732                                 tqe->status = 1;
733                                 if (tqe->tx_complete_func)
734                                         tqe->tx_complete_func(tqe->priv,
735                                                               tqe->status);
736                                 if (tqe->tcp_pending_ack_idx != NOT_TCP_ACK &&
737                                     tqe->tcp_pending_ack_idx < MAX_PENDING_ACKS)
738                                         pending_acks_info[tqe->tcp_pending_ack_idx].txqe = NULL;
739                                 kfree(tqe);
740                         } else {
741                                 break;
742                         }
743                 } while (--entries);
744
745                 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
746
747                 ret = wilc->hif_func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
748                 if (!ret)
749                         goto _end_;
750
751                 ret = wilc->hif_func->hif_block_tx_ext(wilc, 0, txb, offset);
752                 if (!ret)
753                         goto _end_;
754
755 _end_:
756
757                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
758                 if (ret != 1)
759                         break;
760         } while (0);
761         up(&wilc->txq_add_to_head_cs);
762
763         wilc->txq_exit = 1;
764         *txq_count = wilc->txq_entries;
765         return ret;
766 }
767
768 static void wilc_wlan_handle_rxq(struct wilc *wilc)
769 {
770         int offset = 0, size;
771         u8 *buffer;
772         struct rxq_entry_t *rqe;
773
774         wilc->rxq_exit = 0;
775
776         do {
777                 if (wilc->quit) {
778                         up(&wilc->cfg_event);
779                         break;
780                 }
781                 rqe = wilc_wlan_rxq_remove(wilc);
782                 if (!rqe)
783                         break;
784
785                 buffer = rqe->buffer;
786                 size = rqe->buffer_size;
787                 offset = 0;
788
789                 do {
790                         u32 header;
791                         u32 pkt_len, pkt_offset, tp_len;
792                         int is_cfg_packet;
793
794                         memcpy(&header, &buffer[offset], 4);
795                         header = cpu_to_le32(header);
796
797                         is_cfg_packet = (header >> 31) & 0x1;
798                         pkt_offset = (header >> 22) & 0x1ff;
799                         tp_len = (header >> 11) & 0x7ff;
800                         pkt_len = header & 0x7ff;
801
802                         if (pkt_len == 0 || tp_len == 0)
803                                 break;
804
805                         #define IS_MANAGMEMENT                          0x100
806                         #define IS_MANAGMEMENT_CALLBACK                 0x080
807                         #define IS_MGMT_STATUS_SUCCES                   0x040
808
809                         if (pkt_offset & IS_MANAGMEMENT) {
810                                 pkt_offset &= ~(IS_MANAGMEMENT |
811                                                 IS_MANAGMEMENT_CALLBACK |
812                                                 IS_MGMT_STATUS_SUCCES);
813
814                                 WILC_WFI_mgmt_rx(wilc, &buffer[offset + HOST_HDR_OFFSET], pkt_len);
815                         } else {
816                                 if (!is_cfg_packet) {
817                                         if (pkt_len > 0) {
818                                                 wilc_frmw_to_linux(wilc,
819                                                               &buffer[offset],
820                                                               pkt_len,
821                                                               pkt_offset);
822                                         }
823                                 } else {
824                                         struct wilc_cfg_rsp rsp;
825
826                                         wilc_wlan_cfg_indicate_rx(wilc, &buffer[pkt_offset + offset], pkt_len, &rsp);
827                                         if (rsp.type == WILC_CFG_RSP) {
828                                                 if (wilc->cfg_seq_no == rsp.seq_no)
829                                                         up(&wilc->cfg_event);
830                                         } else if (rsp.type == WILC_CFG_RSP_STATUS) {
831                                                 wilc_mac_indicate(wilc, WILC_MAC_INDICATE_STATUS);
832
833                                         } else if (rsp.type == WILC_CFG_RSP_SCAN) {
834                                                 wilc_mac_indicate(wilc, WILC_MAC_INDICATE_SCAN);
835                                         }
836                                 }
837                         }
838                         offset += tp_len;
839                         if (offset >= size)
840                                 break;
841                 } while (1);
842                 kfree(rqe);
843         } while (1);
844
845         wilc->rxq_exit = 1;
846 }
847
848 static void wilc_unknown_isr_ext(struct wilc *wilc)
849 {
850         wilc->hif_func->hif_clear_int_ext(wilc, 0);
851 }
852
853 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
854 {
855         int trials = 10;
856
857         wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
858
859         if (wilc->io_type == HIF_SDIO)
860                 mdelay(WILC_PLL_TO_SDIO);
861         else
862                 mdelay(WILC_PLL_TO_SPI);
863
864         while (!(ISWILC1000(wilc_get_chipid(wilc, true)) && --trials))
865                 mdelay(1);
866 }
867
868 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
869 {
870         wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
871 }
872
873 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
874 {
875         u32 offset = wilc->rx_buffer_offset;
876         u8 *buffer = NULL;
877         u32 size;
878         u32 retries = 0;
879         int ret = 0;
880         struct rxq_entry_t *rqe;
881
882         size = (int_status & 0x7fff) << 2;
883
884         while (!size && retries < 10) {
885                 wilc->hif_func->hif_read_size(wilc, &size);
886                 size = (size & 0x7fff) << 2;
887                 retries++;
888         }
889
890         if (size > 0) {
891                 if (LINUX_RX_SIZE - offset < size)
892                         offset = 0;
893
894                 if (wilc->rx_buffer)
895                         buffer = &wilc->rx_buffer[offset];
896                 else
897                         goto _end_;
898
899                 wilc->hif_func->hif_clear_int_ext(wilc,
900                                               DATA_INT_CLR | ENABLE_RX_VMM);
901                 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
902
903                 if (!ret)
904                         goto _end_;
905 _end_:
906                 if (ret) {
907                         offset += size;
908                         wilc->rx_buffer_offset = offset;
909                         rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
910                         if (rqe) {
911                                 rqe->buffer = buffer;
912                                 rqe->buffer_size = size;
913                                 wilc_wlan_rxq_add(wilc, rqe);
914                         }
915                 }
916         }
917         wilc_wlan_handle_rxq(wilc);
918 }
919
920 void wilc_handle_isr(struct wilc *wilc)
921 {
922         u32 int_status;
923
924         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
925         wilc->hif_func->hif_read_int(wilc, &int_status);
926
927         if (int_status & PLL_INT_EXT)
928                 wilc_pllupdate_isr_ext(wilc, int_status);
929
930         if (int_status & DATA_INT_EXT)
931                 wilc_wlan_handle_isr_ext(wilc, int_status);
932
933         if (int_status & SLEEP_INT_EXT)
934                 wilc_sleeptimer_isr_ext(wilc, int_status);
935
936         if (!(int_status & (ALL_INT_EXT)))
937                 wilc_unknown_isr_ext(wilc);
938
939         release_bus(wilc, RELEASE_ALLOW_SLEEP);
940 }
941 EXPORT_SYMBOL_GPL(wilc_handle_isr);
942
943 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
944                                 u32 buffer_size)
945 {
946         u32 offset;
947         u32 addr, size, size2, blksz;
948         u8 *dma_buffer;
949         int ret = 0;
950
951         blksz = BIT(12);
952
953         dma_buffer = kmalloc(blksz, GFP_KERNEL);
954         if (!dma_buffer) {
955                 ret = -EIO;
956                 goto _fail_1;
957         }
958
959         offset = 0;
960         do {
961                 memcpy(&addr, &buffer[offset], 4);
962                 memcpy(&size, &buffer[offset + 4], 4);
963                 addr = cpu_to_le32(addr);
964                 size = cpu_to_le32(size);
965                 acquire_bus(wilc, ACQUIRE_ONLY);
966                 offset += 8;
967                 while (((int)size) && (offset < buffer_size)) {
968                         if (size <= blksz)
969                                 size2 = size;
970                         else
971                                 size2 = blksz;
972
973                         memcpy(dma_buffer, &buffer[offset], size2);
974                         ret = wilc->hif_func->hif_block_tx(wilc, addr,
975                                                            dma_buffer, size2);
976                         if (!ret)
977                                 break;
978
979                         addr += size2;
980                         offset += size2;
981                         size -= size2;
982                 }
983                 release_bus(wilc, RELEASE_ONLY);
984
985                 if (!ret) {
986                         ret = -EIO;
987                         goto _fail_;
988                 }
989         } while (offset < buffer_size);
990
991 _fail_:
992
993         kfree(dma_buffer);
994
995 _fail_1:
996
997         return (ret < 0) ? ret : 0;
998 }
999
1000 int wilc_wlan_start(struct wilc *wilc)
1001 {
1002         u32 reg = 0;
1003         int ret;
1004         u32 chipid;
1005
1006         if (wilc->io_type == HIF_SDIO) {
1007                 reg = 0;
1008                 reg |= BIT(3);
1009         } else if (wilc->io_type == HIF_SPI) {
1010                 reg = 1;
1011         }
1012         acquire_bus(wilc, ACQUIRE_ONLY);
1013         ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
1014         if (!ret) {
1015                 release_bus(wilc, RELEASE_ONLY);
1016                 ret = -EIO;
1017                 return ret;
1018         }
1019         reg = 0;
1020         if (wilc->io_type == HIF_SDIO && wilc->dev_irq_num)
1021                 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1022
1023 #ifdef WILC_DISABLE_PMU
1024 #else
1025         reg |= WILC_HAVE_USE_PMU;
1026 #endif
1027
1028 #ifdef WILC_SLEEP_CLK_SRC_XO
1029         reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1030 #elif defined WILC_SLEEP_CLK_SRC_RTC
1031         reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1032 #endif
1033
1034 #ifdef WILC_EXT_PA_INV_TX_RX
1035         reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1036 #endif
1037         reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
1038         reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1039 #ifdef XTAL_24
1040         reg |= WILC_HAVE_XTAL_24;
1041 #endif
1042 #ifdef DISABLE_WILC_UART
1043         reg |= WILC_HAVE_DISABLE_WILC_UART;
1044 #endif
1045
1046         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1047         if (!ret) {
1048                 release_bus(wilc, RELEASE_ONLY);
1049                 ret = -EIO;
1050                 return ret;
1051         }
1052
1053         wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1054
1055         ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
1056         if (!ret) {
1057                 release_bus(wilc, RELEASE_ONLY);
1058                 ret = -EIO;
1059                 return ret;
1060         }
1061
1062         wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1063         if ((reg & BIT(10)) == BIT(10)) {
1064                 reg &= ~BIT(10);
1065                 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1066                 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1067         }
1068
1069         reg |= BIT(10);
1070         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1071         wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1072         release_bus(wilc, RELEASE_ONLY);
1073
1074         return (ret < 0) ? ret : 0;
1075 }
1076
1077 int wilc_wlan_stop(struct wilc *wilc)
1078 {
1079         u32 reg = 0;
1080         int ret;
1081         u8 timeout = 10;
1082
1083         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1084
1085         ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1086         if (!ret) {
1087                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1088                 return ret;
1089         }
1090
1091         reg &= ~BIT(10);
1092         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1093         if (!ret) {
1094                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1095                 return ret;
1096         }
1097
1098         do {
1099                 ret = wilc->hif_func->hif_read_reg(wilc,
1100                                                    WILC_GLB_RESET_0, &reg);
1101                 if (!ret) {
1102                         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1103                         return ret;
1104                 }
1105
1106                 if ((reg & BIT(10))) {
1107                         reg &= ~BIT(10);
1108                         ret = wilc->hif_func->hif_write_reg(wilc,
1109                                                             WILC_GLB_RESET_0,
1110                                                             reg);
1111                         timeout--;
1112                 } else {
1113                         ret = wilc->hif_func->hif_read_reg(wilc,
1114                                                            WILC_GLB_RESET_0,
1115                                                            &reg);
1116                         if (!ret) {
1117                                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1118                                 return ret;
1119                         }
1120                         break;
1121                 }
1122
1123         } while (timeout);
1124         reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1125                BIT(29) | BIT(30) | BIT(31));
1126
1127         wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1128         reg = (u32)~BIT(10);
1129
1130         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1131
1132         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1133
1134         return ret;
1135 }
1136
1137 void wilc_wlan_cleanup(struct net_device *dev)
1138 {
1139         struct txq_entry_t *tqe;
1140         struct rxq_entry_t *rqe;
1141         u32 reg = 0;
1142         int ret;
1143         struct wilc_vif *vif;
1144         struct wilc *wilc;
1145
1146         vif = netdev_priv(dev);
1147         wilc = vif->wilc;
1148
1149         wilc->quit = 1;
1150         do {
1151                 tqe = wilc_wlan_txq_remove_from_head(dev);
1152                 if (!tqe)
1153                         break;
1154                 if (tqe->tx_complete_func)
1155                         tqe->tx_complete_func(tqe->priv, 0);
1156                 kfree(tqe);
1157         } while (1);
1158
1159         do {
1160                 rqe = wilc_wlan_rxq_remove(wilc);
1161                 if (!rqe)
1162                         break;
1163                 kfree(rqe);
1164         } while (1);
1165
1166         kfree(wilc->rx_buffer);
1167         wilc->rx_buffer = NULL;
1168         kfree(wilc->tx_buffer);
1169         wilc->tx_buffer = NULL;
1170
1171         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1172
1173         ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, &reg);
1174         if (!ret)
1175                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1176
1177         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1178                                         (reg | ABORT_INT));
1179         if (!ret)
1180                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1181
1182         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1183         wilc->hif_func->hif_deinit(NULL);
1184 }
1185
1186 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1187                                 u32 drv_handler)
1188 {
1189         struct wilc *wilc = vif->wilc;
1190         struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1191         int total_len = wilc->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1192         int seq_no = wilc->cfg_seq_no % 256;
1193         int driver_handler = (u32)drv_handler;
1194
1195         if (type == WILC_CFG_SET)
1196                 cfg->wid_header[0] = 'W';
1197         else
1198                 cfg->wid_header[0] = 'Q';
1199         cfg->wid_header[1] = seq_no;
1200         cfg->wid_header[2] = (u8)total_len;
1201         cfg->wid_header[3] = (u8)(total_len >> 8);
1202         cfg->wid_header[4] = (u8)driver_handler;
1203         cfg->wid_header[5] = (u8)(driver_handler >> 8);
1204         cfg->wid_header[6] = (u8)(driver_handler >> 16);
1205         cfg->wid_header[7] = (u8)(driver_handler >> 24);
1206         wilc->cfg_seq_no = seq_no;
1207
1208         if (!wilc_wlan_txq_add_cfg_pkt(vif, &cfg->wid_header[0], total_len))
1209                 return -1;
1210
1211         return 0;
1212 }
1213
1214 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u32 wid, u8 *buffer,
1215                       u32 buffer_size, int commit, u32 drv_handler)
1216 {
1217         u32 offset;
1218         int ret_size;
1219         struct wilc *wilc = vif->wilc;
1220
1221         if (wilc->cfg_frame_in_use)
1222                 return 0;
1223
1224         if (start)
1225                 wilc->cfg_frame_offset = 0;
1226
1227         offset = wilc->cfg_frame_offset;
1228         ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1229                                          (u16)wid, buffer, buffer_size);
1230         offset += ret_size;
1231         wilc->cfg_frame_offset = offset;
1232
1233         if (commit) {
1234                 netdev_dbg(vif->ndev,
1235                            "[WILC]PACKET Commit with sequence number %d\n",
1236                            wilc->cfg_seq_no);
1237                 netdev_dbg(vif->ndev, "Processing cfg_set()\n");
1238                 wilc->cfg_frame_in_use = 1;
1239
1240                 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1241                         ret_size = 0;
1242
1243                 if (wilc_lock_timeout(wilc, &wilc->cfg_event,
1244                                             CFG_PKTS_TIMEOUT)) {
1245                         netdev_dbg(vif->ndev, "Set Timed Out\n");
1246                         ret_size = 0;
1247                 }
1248                 wilc->cfg_frame_in_use = 0;
1249                 wilc->cfg_frame_offset = 0;
1250                 wilc->cfg_seq_no += 1;
1251         }
1252
1253         return ret_size;
1254 }
1255
1256 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u32 wid, int commit,
1257                       u32 drv_handler)
1258 {
1259         u32 offset;
1260         int ret_size;
1261         struct wilc *wilc = vif->wilc;
1262
1263         if (wilc->cfg_frame_in_use)
1264                 return 0;
1265
1266         if (start)
1267                 wilc->cfg_frame_offset = 0;
1268
1269         offset = wilc->cfg_frame_offset;
1270         ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset,
1271                                          (u16)wid);
1272         offset += ret_size;
1273         wilc->cfg_frame_offset = offset;
1274
1275         if (commit) {
1276                 wilc->cfg_frame_in_use = 1;
1277
1278                 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1279                         ret_size = 0;
1280
1281                 if (wilc_lock_timeout(wilc, &wilc->cfg_event,
1282                                             CFG_PKTS_TIMEOUT)) {
1283                         netdev_dbg(vif->ndev, "Get Timed Out\n");
1284                         ret_size = 0;
1285                 }
1286                 wilc->cfg_frame_in_use = 0;
1287                 wilc->cfg_frame_offset = 0;
1288                 wilc->cfg_seq_no += 1;
1289         }
1290
1291         return ret_size;
1292 }
1293
1294 int wilc_wlan_cfg_get_val(u32 wid, u8 *buffer, u32 buffer_size)
1295 {
1296         return wilc_wlan_cfg_get_wid_value((u16)wid, buffer, buffer_size);
1297 }
1298
1299 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1300                          u32 count, u32 drv)
1301 {
1302         int i;
1303         int ret = 0;
1304
1305         if (mode == GET_CFG) {
1306                 for (i = 0; i < count; i++) {
1307                         if (!wilc_wlan_cfg_get(vif, !i,
1308                                                wids[i].id,
1309                                                (i == count - 1),
1310                                                drv)) {
1311                                 ret = -ETIMEDOUT;
1312                                 break;
1313                         }
1314                 }
1315                 for (i = 0; i < count; i++) {
1316                         wids[i].size = wilc_wlan_cfg_get_val(wids[i].id,
1317                                                              wids[i].val,
1318                                                              wids[i].size);
1319                 }
1320         } else if (mode == SET_CFG) {
1321                 for (i = 0; i < count; i++) {
1322                         if (!wilc_wlan_cfg_set(vif, !i,
1323                                                wids[i].id,
1324                                                wids[i].val,
1325                                                wids[i].size,
1326                                                (i == count - 1),
1327                                                drv)) {
1328                                 ret = -ETIMEDOUT;
1329                                 break;
1330                         }
1331                 }
1332         }
1333
1334         return ret;
1335 }
1336
1337 static u32 init_chip(struct net_device *dev)
1338 {
1339         u32 chipid;
1340         u32 reg, ret = 0;
1341         struct wilc_vif *vif;
1342         struct wilc *wilc;
1343
1344         vif = netdev_priv(dev);
1345         wilc = vif->wilc;
1346
1347         acquire_bus(wilc, ACQUIRE_ONLY);
1348
1349         chipid = wilc_get_chipid(wilc, true);
1350
1351         if ((chipid & 0xfff) != 0xa0) {
1352                 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, &reg);
1353                 if (!ret) {
1354                         netdev_err(dev, "fail read reg 0x1118\n");
1355                         return ret;
1356                 }
1357                 reg |= BIT(0);
1358                 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1359                 if (!ret) {
1360                         netdev_err(dev, "fail write reg 0x1118\n");
1361                         return ret;
1362                 }
1363                 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1364                 if (!ret) {
1365                         netdev_err(dev, "fail write reg 0xc0000\n");
1366                         return ret;
1367                 }
1368         }
1369
1370         release_bus(wilc, RELEASE_ONLY);
1371
1372         return ret;
1373 }
1374
1375 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1376 {
1377         static u32 chipid;
1378         u32 tempchipid = 0;
1379         u32 rfrevid = 0;
1380
1381         if (chipid == 0 || update) {
1382                 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1383                 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1384                 if (!ISWILC1000(tempchipid)) {
1385                         chipid = 0;
1386                         return chipid;
1387                 }
1388                 if (tempchipid == 0x1002a0) {
1389                         if (rfrevid != 0x1)
1390                                 tempchipid = 0x1002a1;
1391                 } else if (tempchipid == 0x1002b0) {
1392                         if (rfrevid == 0x4)
1393                                 tempchipid = 0x1002b1;
1394                         else if (rfrevid != 0x3)
1395                                 tempchipid = 0x1002b2;
1396                 }
1397
1398                 chipid = tempchipid;
1399         }
1400         return chipid;
1401 }
1402
1403 int wilc_wlan_init(struct net_device *dev)
1404 {
1405         int ret = 0;
1406         struct wilc_vif *vif = netdev_priv(dev);
1407         struct wilc *wilc;
1408
1409         wilc = vif->wilc;
1410
1411         wilc->quit = 0;
1412
1413         if (!wilc->hif_func->hif_init(wilc, false)) {
1414                 ret = -EIO;
1415                 goto _fail_;
1416         }
1417
1418         if (!wilc_wlan_cfg_init()) {
1419                 ret = -ENOBUFS;
1420                 goto _fail_;
1421         }
1422
1423         if (!wilc->tx_buffer)
1424                 wilc->tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
1425
1426         if (!wilc->tx_buffer) {
1427                 ret = -ENOBUFS;
1428                 goto _fail_;
1429         }
1430
1431         if (!wilc->rx_buffer)
1432                 wilc->rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
1433
1434         if (!wilc->rx_buffer) {
1435                 ret = -ENOBUFS;
1436                 goto _fail_;
1437         }
1438
1439         if (!init_chip(dev)) {
1440                 ret = -EIO;
1441                 goto _fail_;
1442         }
1443         init_tcp_tracking();
1444
1445         return 1;
1446
1447 _fail_:
1448
1449         kfree(wilc->rx_buffer);
1450         wilc->rx_buffer = NULL;
1451         kfree(wilc->tx_buffer);
1452         wilc->tx_buffer = NULL;
1453
1454         return ret;
1455 }