ACPI / button: remove pointer to old lid_sysfs on unbind
[cascardo/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 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                 wilc_lock_timeout(wilc, &wilc->txq_event, 1);
291                 dropped--;
292         }
293
294         return 1;
295 }
296
297 static bool enabled;
298
299 void wilc_enable_tcp_ack_filter(bool value)
300 {
301         enabled = value;
302 }
303
304 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
305                                      u32 buffer_size)
306 {
307         struct txq_entry_t *tqe;
308         struct wilc *wilc = vif->wilc;
309
310         netdev_dbg(vif->ndev, "Adding config packet ...\n");
311         if (wilc->quit) {
312                 netdev_dbg(vif->ndev, "Return due to clear function\n");
313                 up(&wilc->cfg_event);
314                 return 0;
315         }
316
317         tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
318         if (!tqe)
319                 return 0;
320
321         tqe->type = WILC_CFG_PKT;
322         tqe->buffer = buffer;
323         tqe->buffer_size = buffer_size;
324         tqe->tx_complete_func = NULL;
325         tqe->priv = NULL;
326         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
327
328         if (wilc_wlan_txq_add_to_head(vif, tqe)) {
329                 kfree(tqe);
330                 return 0;
331         }
332
333         return 1;
334 }
335
336 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
337                               u32 buffer_size, wilc_tx_complete_func_t func)
338 {
339         struct txq_entry_t *tqe;
340         struct wilc_vif *vif = netdev_priv(dev);
341         struct wilc *wilc;
342
343         wilc = vif->wilc;
344
345         if (wilc->quit)
346                 return 0;
347
348         tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
349
350         if (!tqe)
351                 return 0;
352         tqe->type = WILC_NET_PKT;
353         tqe->buffer = buffer;
354         tqe->buffer_size = buffer_size;
355         tqe->tx_complete_func = func;
356         tqe->priv = priv;
357
358         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
359         if (enabled)
360                 tcp_process(dev, tqe);
361         wilc_wlan_txq_add_to_tail(dev, tqe);
362         return wilc->txq_entries;
363 }
364
365 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
366                                u32 buffer_size, wilc_tx_complete_func_t func)
367 {
368         struct txq_entry_t *tqe;
369         struct wilc_vif *vif = netdev_priv(dev);
370         struct wilc *wilc;
371
372         wilc = vif->wilc;
373
374         if (wilc->quit)
375                 return 0;
376
377         tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
378
379         if (!tqe)
380                 return 0;
381         tqe->type = WILC_MGMT_PKT;
382         tqe->buffer = buffer;
383         tqe->buffer_size = buffer_size;
384         tqe->tx_complete_func = func;
385         tqe->priv = priv;
386         tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
387         wilc_wlan_txq_add_to_tail(dev, tqe);
388         return 1;
389 }
390
391 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
392 {
393         struct txq_entry_t *tqe;
394         unsigned long flags;
395
396         spin_lock_irqsave(&wilc->txq_spinlock, flags);
397
398         tqe = wilc->txq_head;
399
400         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
401
402         return tqe;
403 }
404
405 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
406                                                   struct txq_entry_t *tqe)
407 {
408         unsigned long flags;
409
410         spin_lock_irqsave(&wilc->txq_spinlock, flags);
411
412         tqe = tqe->next;
413         spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
414
415         return tqe;
416 }
417
418 static int wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
419 {
420         if (wilc->quit)
421                 return 0;
422
423         mutex_lock(&wilc->rxq_cs);
424         if (!wilc->rxq_head) {
425                 rqe->next = NULL;
426                 wilc->rxq_head = rqe;
427                 wilc->rxq_tail = rqe;
428         } else {
429                 wilc->rxq_tail->next = rqe;
430                 rqe->next = NULL;
431                 wilc->rxq_tail = rqe;
432         }
433         wilc->rxq_entries += 1;
434         mutex_unlock(&wilc->rxq_cs);
435         return wilc->rxq_entries;
436 }
437
438 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
439 {
440         if (wilc->rxq_head) {
441                 struct rxq_entry_t *rqe;
442
443                 mutex_lock(&wilc->rxq_cs);
444                 rqe = wilc->rxq_head;
445                 wilc->rxq_head = wilc->rxq_head->next;
446                 wilc->rxq_entries -= 1;
447                 mutex_unlock(&wilc->rxq_cs);
448                 return rqe;
449         }
450         return NULL;
451 }
452
453 void chip_allow_sleep(struct wilc *wilc)
454 {
455         u32 reg = 0;
456
457         wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
458
459         wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
460         wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
461 }
462 EXPORT_SYMBOL_GPL(chip_allow_sleep);
463
464 void chip_wakeup(struct wilc *wilc)
465 {
466         u32 reg, clk_status_reg;
467
468         if ((wilc->io_type & 0x1) == HIF_SPI) {
469                 do {
470                         wilc->hif_func->hif_read_reg(wilc, 1, &reg);
471                         wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
472                         wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
473
474                         do {
475                                 usleep_range(2 * 1000, 2 * 1000);
476                                 wilc_get_chipid(wilc, true);
477                         } while (wilc_get_chipid(wilc, true) == 0);
478                 } while (wilc_get_chipid(wilc, true) == 0);
479         } else if ((wilc->io_type & 0x1) == HIF_SDIO)    {
480                 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
481                 udelay(200);
482                 wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
483                 do {
484                         wilc->hif_func->hif_write_reg(wilc, 0xf0,
485                                                       reg | BIT(0));
486                         wilc->hif_func->hif_read_reg(wilc, 0xf1,
487                                                      &clk_status_reg);
488
489                         while ((clk_status_reg & 0x1) == 0) {
490                                 usleep_range(2 * 1000, 2 * 1000);
491
492                                 wilc->hif_func->hif_read_reg(wilc, 0xf1,
493                                                              &clk_status_reg);
494                         }
495                         if ((clk_status_reg & 0x1) == 0) {
496                                 wilc->hif_func->hif_write_reg(wilc, 0xf0,
497                                                               reg & (~BIT(0)));
498                         }
499                 } while ((clk_status_reg & 0x1) == 0);
500         }
501
502         if (chip_ps_state == CHIP_SLEEPING_MANUAL) {
503                 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
504                         u32 val32;
505
506                         wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
507                         val32 |= BIT(6);
508                         wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
509
510                         wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
511                         val32 |= BIT(6);
512                         wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
513                 }
514         }
515         chip_ps_state = CHIP_WAKEDUP;
516 }
517 EXPORT_SYMBOL_GPL(chip_wakeup);
518
519 void wilc_chip_sleep_manually(struct wilc *wilc)
520 {
521         if (chip_ps_state != CHIP_WAKEDUP)
522                 return;
523         acquire_bus(wilc, ACQUIRE_ONLY);
524
525         chip_allow_sleep(wilc);
526         wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
527
528         chip_ps_state = CHIP_SLEEPING_MANUAL;
529         release_bus(wilc, RELEASE_ONLY);
530 }
531 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
532
533 void host_wakeup_notify(struct wilc *wilc)
534 {
535         acquire_bus(wilc, ACQUIRE_ONLY);
536         wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
537         release_bus(wilc, RELEASE_ONLY);
538 }
539 EXPORT_SYMBOL_GPL(host_wakeup_notify);
540
541 void host_sleep_notify(struct wilc *wilc)
542 {
543         acquire_bus(wilc, ACQUIRE_ONLY);
544         wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
545         release_bus(wilc, RELEASE_ONLY);
546 }
547 EXPORT_SYMBOL_GPL(host_sleep_notify);
548
549 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
550 {
551         int i, entries = 0;
552         u32 sum;
553         u32 reg;
554         u8 *txb;
555         u32 offset = 0;
556         int vmm_sz = 0;
557         struct txq_entry_t *tqe;
558         int ret = 0;
559         int counter;
560         int timeout;
561         u32 vmm_table[WILC_VMM_TBL_SIZE];
562         struct wilc_vif *vif;
563         struct wilc *wilc;
564
565         vif = netdev_priv(dev);
566         wilc = vif->wilc;
567
568         txb = wilc->tx_buffer;
569         wilc->txq_exit = 0;
570         do {
571                 if (wilc->quit)
572                         break;
573
574                 wilc_lock_timeout(wilc, &wilc->txq_add_to_head_cs,
575                                         CFG_PKTS_TIMEOUT);
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 + 4], 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         up(&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                         up(&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                                                         up(&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                 ret = -EIO;
1006                 return ret;
1007         }
1008         reg = 0;
1009         if (wilc->io_type == HIF_SDIO && wilc->dev_irq_num)
1010                 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1011
1012 #ifdef WILC_DISABLE_PMU
1013 #else
1014         reg |= WILC_HAVE_USE_PMU;
1015 #endif
1016
1017 #ifdef WILC_SLEEP_CLK_SRC_XO
1018         reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1019 #elif defined WILC_SLEEP_CLK_SRC_RTC
1020         reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1021 #endif
1022
1023 #ifdef WILC_EXT_PA_INV_TX_RX
1024         reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1025 #endif
1026         reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
1027         reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1028 #ifdef XTAL_24
1029         reg |= WILC_HAVE_XTAL_24;
1030 #endif
1031 #ifdef DISABLE_WILC_UART
1032         reg |= WILC_HAVE_DISABLE_WILC_UART;
1033 #endif
1034
1035         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1036         if (!ret) {
1037                 release_bus(wilc, RELEASE_ONLY);
1038                 ret = -EIO;
1039                 return ret;
1040         }
1041
1042         wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1043
1044         ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
1045         if (!ret) {
1046                 release_bus(wilc, RELEASE_ONLY);
1047                 ret = -EIO;
1048                 return ret;
1049         }
1050
1051         wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1052         if ((reg & BIT(10)) == BIT(10)) {
1053                 reg &= ~BIT(10);
1054                 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1055                 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1056         }
1057
1058         reg |= BIT(10);
1059         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1060         wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1061         release_bus(wilc, RELEASE_ONLY);
1062
1063         return (ret < 0) ? ret : 0;
1064 }
1065
1066 int wilc_wlan_stop(struct wilc *wilc)
1067 {
1068         u32 reg = 0;
1069         int ret;
1070         u8 timeout = 10;
1071
1072         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1073
1074         ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1075         if (!ret) {
1076                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1077                 return ret;
1078         }
1079
1080         reg &= ~BIT(10);
1081         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1082         if (!ret) {
1083                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1084                 return ret;
1085         }
1086
1087         do {
1088                 ret = wilc->hif_func->hif_read_reg(wilc,
1089                                                    WILC_GLB_RESET_0, &reg);
1090                 if (!ret) {
1091                         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1092                         return ret;
1093                 }
1094
1095                 if ((reg & BIT(10))) {
1096                         reg &= ~BIT(10);
1097                         ret = wilc->hif_func->hif_write_reg(wilc,
1098                                                             WILC_GLB_RESET_0,
1099                                                             reg);
1100                         timeout--;
1101                 } else {
1102                         ret = wilc->hif_func->hif_read_reg(wilc,
1103                                                            WILC_GLB_RESET_0,
1104                                                            &reg);
1105                         if (!ret) {
1106                                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1107                                 return ret;
1108                         }
1109                         break;
1110                 }
1111
1112         } while (timeout);
1113         reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1114                BIT(29) | BIT(30) | BIT(31));
1115
1116         wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1117         reg = (u32)~BIT(10);
1118
1119         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1120
1121         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1122
1123         return ret;
1124 }
1125
1126 void wilc_wlan_cleanup(struct net_device *dev)
1127 {
1128         struct txq_entry_t *tqe;
1129         struct rxq_entry_t *rqe;
1130         u32 reg = 0;
1131         int ret;
1132         struct wilc_vif *vif;
1133         struct wilc *wilc;
1134
1135         vif = netdev_priv(dev);
1136         wilc = vif->wilc;
1137
1138         wilc->quit = 1;
1139         do {
1140                 tqe = wilc_wlan_txq_remove_from_head(dev);
1141                 if (!tqe)
1142                         break;
1143                 if (tqe->tx_complete_func)
1144                         tqe->tx_complete_func(tqe->priv, 0);
1145                 kfree(tqe);
1146         } while (1);
1147
1148         do {
1149                 rqe = wilc_wlan_rxq_remove(wilc);
1150                 if (!rqe)
1151                         break;
1152                 kfree(rqe);
1153         } while (1);
1154
1155         kfree(wilc->rx_buffer);
1156         wilc->rx_buffer = NULL;
1157         kfree(wilc->tx_buffer);
1158         wilc->tx_buffer = NULL;
1159
1160         acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1161
1162         ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, &reg);
1163         if (!ret)
1164                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1165
1166         ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1167                                         (reg | ABORT_INT));
1168         if (!ret)
1169                 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1170
1171         release_bus(wilc, RELEASE_ALLOW_SLEEP);
1172         wilc->hif_func->hif_deinit(NULL);
1173 }
1174
1175 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1176                                 u32 drv_handler)
1177 {
1178         struct wilc *wilc = vif->wilc;
1179         struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1180         int total_len = wilc->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1181         int seq_no = wilc->cfg_seq_no % 256;
1182         int driver_handler = (u32)drv_handler;
1183
1184         if (type == WILC_CFG_SET)
1185                 cfg->wid_header[0] = 'W';
1186         else
1187                 cfg->wid_header[0] = 'Q';
1188         cfg->wid_header[1] = seq_no;
1189         cfg->wid_header[2] = (u8)total_len;
1190         cfg->wid_header[3] = (u8)(total_len >> 8);
1191         cfg->wid_header[4] = (u8)driver_handler;
1192         cfg->wid_header[5] = (u8)(driver_handler >> 8);
1193         cfg->wid_header[6] = (u8)(driver_handler >> 16);
1194         cfg->wid_header[7] = (u8)(driver_handler >> 24);
1195         wilc->cfg_seq_no = seq_no;
1196
1197         if (!wilc_wlan_txq_add_cfg_pkt(vif, &cfg->wid_header[0], total_len))
1198                 return -1;
1199
1200         return 0;
1201 }
1202
1203 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1204                       u32 buffer_size, int commit, u32 drv_handler)
1205 {
1206         u32 offset;
1207         int ret_size;
1208         struct wilc *wilc = vif->wilc;
1209
1210         if (wilc->cfg_frame_in_use)
1211                 return 0;
1212
1213         if (start)
1214                 wilc->cfg_frame_offset = 0;
1215
1216         offset = wilc->cfg_frame_offset;
1217         ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1218                                          wid, buffer, buffer_size);
1219         offset += ret_size;
1220         wilc->cfg_frame_offset = offset;
1221
1222         if (commit) {
1223                 netdev_dbg(vif->ndev,
1224                            "[WILC]PACKET Commit with sequence number %d\n",
1225                            wilc->cfg_seq_no);
1226                 netdev_dbg(vif->ndev, "Processing cfg_set()\n");
1227                 wilc->cfg_frame_in_use = 1;
1228
1229                 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1230                         ret_size = 0;
1231
1232                 if (wilc_lock_timeout(wilc, &wilc->cfg_event,
1233                                             CFG_PKTS_TIMEOUT)) {
1234                         netdev_dbg(vif->ndev, "Set Timed Out\n");
1235                         ret_size = 0;
1236                 }
1237                 wilc->cfg_frame_in_use = 0;
1238                 wilc->cfg_frame_offset = 0;
1239                 wilc->cfg_seq_no += 1;
1240         }
1241
1242         return ret_size;
1243 }
1244
1245 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1246                       u32 drv_handler)
1247 {
1248         u32 offset;
1249         int ret_size;
1250         struct wilc *wilc = vif->wilc;
1251
1252         if (wilc->cfg_frame_in_use)
1253                 return 0;
1254
1255         if (start)
1256                 wilc->cfg_frame_offset = 0;
1257
1258         offset = wilc->cfg_frame_offset;
1259         ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1260         offset += ret_size;
1261         wilc->cfg_frame_offset = offset;
1262
1263         if (commit) {
1264                 wilc->cfg_frame_in_use = 1;
1265
1266                 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1267                         ret_size = 0;
1268
1269                 if (wilc_lock_timeout(wilc, &wilc->cfg_event,
1270                                             CFG_PKTS_TIMEOUT)) {
1271                         netdev_dbg(vif->ndev, "Get Timed Out\n");
1272                         ret_size = 0;
1273                 }
1274                 wilc->cfg_frame_in_use = 0;
1275                 wilc->cfg_frame_offset = 0;
1276                 wilc->cfg_seq_no += 1;
1277         }
1278
1279         return ret_size;
1280 }
1281
1282 int wilc_wlan_cfg_get_val(u16 wid, u8 *buffer, u32 buffer_size)
1283 {
1284         return wilc_wlan_cfg_get_wid_value(wid, buffer, buffer_size);
1285 }
1286
1287 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1288                          u32 count, u32 drv)
1289 {
1290         int i;
1291         int ret = 0;
1292
1293         if (mode == GET_CFG) {
1294                 for (i = 0; i < count; i++) {
1295                         if (!wilc_wlan_cfg_get(vif, !i,
1296                                                wids[i].id,
1297                                                (i == count - 1),
1298                                                drv)) {
1299                                 ret = -ETIMEDOUT;
1300                                 break;
1301                         }
1302                 }
1303                 for (i = 0; i < count; i++) {
1304                         wids[i].size = wilc_wlan_cfg_get_val(wids[i].id,
1305                                                              wids[i].val,
1306                                                              wids[i].size);
1307                 }
1308         } else if (mode == SET_CFG) {
1309                 for (i = 0; i < count; i++) {
1310                         if (!wilc_wlan_cfg_set(vif, !i,
1311                                                wids[i].id,
1312                                                wids[i].val,
1313                                                wids[i].size,
1314                                                (i == count - 1),
1315                                                drv)) {
1316                                 ret = -ETIMEDOUT;
1317                                 break;
1318                         }
1319                 }
1320         }
1321
1322         return ret;
1323 }
1324
1325 static u32 init_chip(struct net_device *dev)
1326 {
1327         u32 chipid;
1328         u32 reg, ret = 0;
1329         struct wilc_vif *vif;
1330         struct wilc *wilc;
1331
1332         vif = netdev_priv(dev);
1333         wilc = vif->wilc;
1334
1335         acquire_bus(wilc, ACQUIRE_ONLY);
1336
1337         chipid = wilc_get_chipid(wilc, true);
1338
1339         if ((chipid & 0xfff) != 0xa0) {
1340                 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, &reg);
1341                 if (!ret) {
1342                         netdev_err(dev, "fail read reg 0x1118\n");
1343                         return ret;
1344                 }
1345                 reg |= BIT(0);
1346                 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1347                 if (!ret) {
1348                         netdev_err(dev, "fail write reg 0x1118\n");
1349                         return ret;
1350                 }
1351                 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1352                 if (!ret) {
1353                         netdev_err(dev, "fail write reg 0xc0000\n");
1354                         return ret;
1355                 }
1356         }
1357
1358         release_bus(wilc, RELEASE_ONLY);
1359
1360         return ret;
1361 }
1362
1363 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1364 {
1365         static u32 chipid;
1366         u32 tempchipid = 0;
1367         u32 rfrevid = 0;
1368
1369         if (chipid == 0 || update) {
1370                 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1371                 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1372                 if (!ISWILC1000(tempchipid)) {
1373                         chipid = 0;
1374                         return chipid;
1375                 }
1376                 if (tempchipid == 0x1002a0) {
1377                         if (rfrevid != 0x1)
1378                                 tempchipid = 0x1002a1;
1379                 } else if (tempchipid == 0x1002b0) {
1380                         if (rfrevid == 0x4)
1381                                 tempchipid = 0x1002b1;
1382                         else if (rfrevid != 0x3)
1383                                 tempchipid = 0x1002b2;
1384                 }
1385
1386                 chipid = tempchipid;
1387         }
1388         return chipid;
1389 }
1390
1391 int wilc_wlan_init(struct net_device *dev)
1392 {
1393         int ret = 0;
1394         struct wilc_vif *vif = netdev_priv(dev);
1395         struct wilc *wilc;
1396
1397         wilc = vif->wilc;
1398
1399         wilc->quit = 0;
1400
1401         if (!wilc->hif_func->hif_init(wilc, false)) {
1402                 ret = -EIO;
1403                 goto _fail_;
1404         }
1405
1406         if (!wilc_wlan_cfg_init()) {
1407                 ret = -ENOBUFS;
1408                 goto _fail_;
1409         }
1410
1411         if (!wilc->tx_buffer)
1412                 wilc->tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
1413
1414         if (!wilc->tx_buffer) {
1415                 ret = -ENOBUFS;
1416                 goto _fail_;
1417         }
1418
1419         if (!wilc->rx_buffer)
1420                 wilc->rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
1421
1422         if (!wilc->rx_buffer) {
1423                 ret = -ENOBUFS;
1424                 goto _fail_;
1425         }
1426
1427         if (!init_chip(dev)) {
1428                 ret = -EIO;
1429                 goto _fail_;
1430         }
1431
1432         return 1;
1433
1434 _fail_:
1435
1436         kfree(wilc->rx_buffer);
1437         wilc->rx_buffer = NULL;
1438         kfree(wilc->tx_buffer);
1439         wilc->tx_buffer = NULL;
1440
1441         return ret;
1442 }