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