1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/completion.h>
3 #include "wilc_wlan_if.h"
5 #include "wilc_wfi_netdevice.h"
6 #include "wilc_wlan_cfg.h"
8 static enum chip_ps_states chip_ps_state = CHIP_WAKEDUP;
10 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
12 mutex_lock(&wilc->hif_cs);
13 if (acquire == ACQUIRE_AND_WAKEUP)
17 static inline void release_bus(struct wilc *wilc, enum bus_release release)
19 if (release == RELEASE_ALLOW_SLEEP)
20 chip_allow_sleep(wilc);
21 mutex_unlock(&wilc->hif_cs);
24 static void wilc_wlan_txq_remove(struct wilc *wilc, struct txq_entry_t *tqe)
26 if (tqe == wilc->txq_head) {
27 wilc->txq_head = tqe->next;
29 wilc->txq_head->prev = NULL;
30 } else if (tqe == wilc->txq_tail) {
31 wilc->txq_tail = (tqe->prev);
33 wilc->txq_tail->next = NULL;
35 tqe->prev->next = tqe->next;
36 tqe->next->prev = tqe->prev;
38 wilc->txq_entries -= 1;
41 static struct txq_entry_t *
42 wilc_wlan_txq_remove_from_head(struct net_device *dev)
44 struct txq_entry_t *tqe;
49 vif = netdev_priv(dev);
52 spin_lock_irqsave(&wilc->txq_spinlock, flags);
55 wilc->txq_head = tqe->next;
57 wilc->txq_head->prev = NULL;
59 wilc->txq_entries -= 1;
63 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
67 static void wilc_wlan_txq_add_to_tail(struct net_device *dev,
68 struct txq_entry_t *tqe)
74 vif = netdev_priv(dev);
77 spin_lock_irqsave(&wilc->txq_spinlock, flags);
79 if (!wilc->txq_head) {
86 tqe->prev = wilc->txq_tail;
87 wilc->txq_tail->next = tqe;
90 wilc->txq_entries += 1;
92 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
94 complete(&wilc->txq_event);
97 static int wilc_wlan_txq_add_to_head(struct wilc_vif *vif,
98 struct txq_entry_t *tqe)
101 struct wilc *wilc = vif->wilc;
103 mutex_lock(&wilc->txq_add_to_head_cs);
105 spin_lock_irqsave(&wilc->txq_spinlock, flags);
107 if (!wilc->txq_head) {
110 wilc->txq_head = tqe;
111 wilc->txq_tail = tqe;
113 tqe->next = wilc->txq_head;
115 wilc->txq_head->prev = tqe;
116 wilc->txq_head = tqe;
118 wilc->txq_entries += 1;
120 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
121 mutex_unlock(&wilc->txq_add_to_head_cs);
122 complete(&wilc->txq_event);
127 struct ack_session_info;
128 struct ack_session_info {
136 struct pending_acks_info {
139 struct txq_entry_t *txqe;
142 #define NOT_TCP_ACK (-1)
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];
149 static u32 pending_base;
150 static u32 tcp_session;
151 static u32 pending_acks;
153 static inline int add_tcp_session(u32 src_prt, u32 dst_prt, u32 seq)
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;
165 static inline int update_tcp_session(u32 index, u32 ack)
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;
173 static inline int add_tcp_pending_ack(u32 ack, u32 session_index,
174 struct txq_entry_t *txqe)
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;
186 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
189 u8 *buffer = tqe->buffer;
190 unsigned short h_proto;
193 struct wilc_vif *vif;
196 vif = netdev_priv(dev);
199 spin_lock_irqsave(&wilc->txq_spinlock, flags);
201 eth_hdr_ptr = &buffer[0];
202 h_proto = ntohs(*((unsigned short *)ð_hdr_ptr[12]));
203 if (h_proto == ETH_P_IP) {
207 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
208 protocol = ip_hdr_ptr[9];
210 if (protocol == 0x06) {
212 u32 IHL, total_length, data_offset;
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) +
218 data_offset = ((u32)tcp_hdr_ptr[12] & 0xf0) >> 2;
219 if (total_length == (IHL + data_offset)) {
222 seq_no = ((u32)tcp_hdr_ptr[4] << 24) +
223 ((u32)tcp_hdr_ptr[5] << 16) +
224 ((u32)tcp_hdr_ptr[6] << 8) +
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];
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);
239 if (i == tcp_session)
240 add_tcp_session(0, 0, seq_no);
242 add_tcp_pending_ack(ack_no, i, tqe);
246 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
249 static int wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
251 struct wilc_vif *vif;
256 vif = netdev_priv(dev);
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)
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;
267 tqe = pending_acks_info[i].txqe;
269 wilc_wlan_txq_remove(wilc, tqe);
271 if (tqe->tx_complete_func)
272 tqe->tx_complete_func(tqe->priv,
282 if (pending_base == 0)
283 pending_base = MAX_TCP_SESSION;
287 spin_unlock_irqrestore(&wilc->txq_spinlock, wilc->txq_spinlock_flags);
289 while (dropped > 0) {
290 wait_for_completion_timeout(&wilc->txq_event,
291 msecs_to_jiffies(1));
300 void wilc_enable_tcp_ack_filter(bool value)
305 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
308 struct txq_entry_t *tqe;
309 struct wilc *wilc = vif->wilc;
311 netdev_dbg(vif->ndev, "Adding config packet ...\n");
313 netdev_dbg(vif->ndev, "Return due to clear function\n");
314 complete(&wilc->cfg_event);
318 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
322 tqe->type = WILC_CFG_PKT;
323 tqe->buffer = buffer;
324 tqe->buffer_size = buffer_size;
325 tqe->tx_complete_func = NULL;
327 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
329 if (wilc_wlan_txq_add_to_head(vif, tqe)) {
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)
340 struct txq_entry_t *tqe;
341 struct wilc_vif *vif = netdev_priv(dev);
349 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
353 tqe->type = WILC_NET_PKT;
354 tqe->buffer = buffer;
355 tqe->buffer_size = buffer_size;
356 tqe->tx_complete_func = func;
359 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
361 tcp_process(dev, tqe);
362 wilc_wlan_txq_add_to_tail(dev, tqe);
363 return wilc->txq_entries;
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)
369 struct txq_entry_t *tqe;
370 struct wilc_vif *vif = netdev_priv(dev);
378 tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
382 tqe->type = WILC_MGMT_PKT;
383 tqe->buffer = buffer;
384 tqe->buffer_size = buffer_size;
385 tqe->tx_complete_func = func;
387 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
388 wilc_wlan_txq_add_to_tail(dev, tqe);
392 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
394 struct txq_entry_t *tqe;
397 spin_lock_irqsave(&wilc->txq_spinlock, flags);
399 tqe = wilc->txq_head;
401 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
406 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
407 struct txq_entry_t *tqe)
411 spin_lock_irqsave(&wilc->txq_spinlock, flags);
414 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
419 static int wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
424 mutex_lock(&wilc->rxq_cs);
425 if (!wilc->rxq_head) {
427 wilc->rxq_head = rqe;
428 wilc->rxq_tail = rqe;
430 wilc->rxq_tail->next = rqe;
432 wilc->rxq_tail = rqe;
434 wilc->rxq_entries += 1;
435 mutex_unlock(&wilc->rxq_cs);
436 return wilc->rxq_entries;
439 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
441 if (wilc->rxq_head) {
442 struct rxq_entry_t *rqe;
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);
454 void chip_allow_sleep(struct wilc *wilc)
458 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
460 wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
461 wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
463 EXPORT_SYMBOL_GPL(chip_allow_sleep);
465 void chip_wakeup(struct wilc *wilc)
467 u32 reg, clk_status_reg;
469 if ((wilc->io_type & 0x1) == HIF_SPI) {
471 wilc->hif_func->hif_read_reg(wilc, 1, ®);
472 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
473 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
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);
483 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
485 wilc->hif_func->hif_write_reg(wilc, 0xf0,
487 wilc->hif_func->hif_read_reg(wilc, 0xf1,
490 while ((clk_status_reg & 0x1) == 0) {
491 usleep_range(2 * 1000, 2 * 1000);
493 wilc->hif_func->hif_read_reg(wilc, 0xf1,
496 if ((clk_status_reg & 0x1) == 0) {
497 wilc->hif_func->hif_write_reg(wilc, 0xf0,
500 } while ((clk_status_reg & 0x1) == 0);
503 if (chip_ps_state == CHIP_SLEEPING_MANUAL) {
504 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
507 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
509 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
511 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
513 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
516 chip_ps_state = CHIP_WAKEDUP;
518 EXPORT_SYMBOL_GPL(chip_wakeup);
520 void wilc_chip_sleep_manually(struct wilc *wilc)
522 if (chip_ps_state != CHIP_WAKEDUP)
524 acquire_bus(wilc, ACQUIRE_ONLY);
526 chip_allow_sleep(wilc);
527 wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
529 chip_ps_state = CHIP_SLEEPING_MANUAL;
530 release_bus(wilc, RELEASE_ONLY);
532 EXPORT_SYMBOL_GPL(wilc_chip_sleep_manually);
534 void host_wakeup_notify(struct wilc *wilc)
536 acquire_bus(wilc, ACQUIRE_ONLY);
537 wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
538 release_bus(wilc, RELEASE_ONLY);
540 EXPORT_SYMBOL_GPL(host_wakeup_notify);
542 void host_sleep_notify(struct wilc *wilc)
544 acquire_bus(wilc, ACQUIRE_ONLY);
545 wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
546 release_bus(wilc, RELEASE_ONLY);
548 EXPORT_SYMBOL_GPL(host_sleep_notify);
550 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
558 struct txq_entry_t *tqe;
562 u32 vmm_table[WILC_VMM_TBL_SIZE];
563 struct wilc_vif *vif;
566 vif = netdev_priv(dev);
569 txb = wilc->tx_buffer;
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);
581 if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
582 if (tqe->type == WILC_CFG_PKT)
583 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
585 else if (tqe->type == WILC_NET_PKT)
586 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
589 vmm_sz = HOST_HDR_OFFSET;
591 vmm_sz += tqe->buffer_size;
594 vmm_sz = (vmm_sz + 4) & ~0x3;
596 if ((sum + vmm_sz) > LINUX_TX_SIZE)
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]);
606 tqe = wilc_wlan_txq_get_next(wilc, tqe);
616 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
619 ret = wilc->hif_func->hif_read_reg(wilc,
625 if ((reg & 0x1) == 0)
631 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
634 } while (!wilc->quit);
641 ret = wilc->hif_func->hif_block_tx(wilc, WILC_VMM_TBL_RX_SHADOW_BASE, (u8 *)vmm_table, ((i + 1) * 4));
645 ret = wilc->hif_func->hif_write_reg(wilc,
652 ret = wilc->hif_func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
655 if ((reg >> 2) & 0x1) {
656 entries = ((reg >> 3) & 0x3f);
659 release_bus(wilc, RELEASE_ALLOW_SLEEP);
662 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
670 ret = wilc->hif_func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
674 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
686 ret = WILC_TX_ERR_NO_BUF;
690 release_bus(wilc, RELEASE_ALLOW_SLEEP);
695 tqe = wilc_wlan_txq_remove_from_head(dev);
696 if (tqe && vmm_table[i] != 0) {
697 u32 header, buffer_offset;
699 vmm_table[i] = cpu_to_le32(vmm_table[i]);
700 vmm_sz = (vmm_table[i] & 0x3ff);
702 header = (tqe->type << 31) |
703 (tqe->buffer_size << 15) |
705 if (tqe->type == WILC_MGMT_PKT)
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;
717 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
718 memcpy(&txb[offset + 8], bssid, 6);
720 buffer_offset = HOST_HDR_OFFSET;
723 memcpy(&txb[offset + buffer_offset],
724 tqe->buffer, tqe->buffer_size);
728 if (tqe->tx_complete_func)
729 tqe->tx_complete_func(tqe->priv,
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;
740 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
742 ret = wilc->hif_func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
746 ret = wilc->hif_func->hif_block_tx_ext(wilc, 0, txb, offset);
752 release_bus(wilc, RELEASE_ALLOW_SLEEP);
756 mutex_unlock(&wilc->txq_add_to_head_cs);
759 *txq_count = wilc->txq_entries;
763 static void wilc_wlan_handle_rxq(struct wilc *wilc)
765 int offset = 0, size;
767 struct rxq_entry_t *rqe;
773 complete(&wilc->cfg_event);
776 rqe = wilc_wlan_rxq_remove(wilc);
780 buffer = rqe->buffer;
781 size = rqe->buffer_size;
786 u32 pkt_len, pkt_offset, tp_len;
789 memcpy(&header, &buffer[offset], 4);
790 header = cpu_to_le32(header);
792 is_cfg_packet = (header >> 31) & 0x1;
793 pkt_offset = (header >> 22) & 0x1ff;
794 tp_len = (header >> 11) & 0x7ff;
795 pkt_len = header & 0x7ff;
797 if (pkt_len == 0 || tp_len == 0)
800 #define IS_MANAGMEMENT 0x100
801 #define IS_MANAGMEMENT_CALLBACK 0x080
802 #define IS_MGMT_STATUS_SUCCES 0x040
804 if (pkt_offset & IS_MANAGMEMENT) {
805 pkt_offset &= ~(IS_MANAGMEMENT |
806 IS_MANAGMEMENT_CALLBACK |
807 IS_MGMT_STATUS_SUCCES);
809 WILC_WFI_mgmt_rx(wilc, &buffer[offset + HOST_HDR_OFFSET], pkt_len);
811 if (!is_cfg_packet) {
813 wilc_frmw_to_linux(wilc,
819 struct wilc_cfg_rsp rsp;
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);
828 } else if (rsp.type == WILC_CFG_RSP_SCAN) {
829 wilc_mac_indicate(wilc, WILC_MAC_INDICATE_SCAN);
843 static void wilc_unknown_isr_ext(struct wilc *wilc)
845 wilc->hif_func->hif_clear_int_ext(wilc, 0);
848 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
852 wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
854 if (wilc->io_type == HIF_SDIO)
855 mdelay(WILC_PLL_TO_SDIO);
857 mdelay(WILC_PLL_TO_SPI);
859 while (!(ISWILC1000(wilc_get_chipid(wilc, true)) && --trials))
863 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
865 wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
868 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
870 u32 offset = wilc->rx_buffer_offset;
875 struct rxq_entry_t *rqe;
877 size = (int_status & 0x7fff) << 2;
879 while (!size && retries < 10) {
880 wilc->hif_func->hif_read_size(wilc, &size);
881 size = (size & 0x7fff) << 2;
886 if (LINUX_RX_SIZE - offset < size)
890 buffer = &wilc->rx_buffer[offset];
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);
901 wilc->rx_buffer_offset = offset;
902 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
904 rqe->buffer = buffer;
905 rqe->buffer_size = size;
906 wilc_wlan_rxq_add(wilc, rqe);
910 wilc_wlan_handle_rxq(wilc);
913 void wilc_handle_isr(struct wilc *wilc)
917 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
918 wilc->hif_func->hif_read_int(wilc, &int_status);
920 if (int_status & PLL_INT_EXT)
921 wilc_pllupdate_isr_ext(wilc, int_status);
923 if (int_status & DATA_INT_EXT)
924 wilc_wlan_handle_isr_ext(wilc, int_status);
926 if (int_status & SLEEP_INT_EXT)
927 wilc_sleeptimer_isr_ext(wilc, int_status);
929 if (!(int_status & (ALL_INT_EXT)))
930 wilc_unknown_isr_ext(wilc);
932 release_bus(wilc, RELEASE_ALLOW_SLEEP);
934 EXPORT_SYMBOL_GPL(wilc_handle_isr);
936 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
940 u32 addr, size, size2, blksz;
946 dma_buffer = kmalloc(blksz, GFP_KERNEL);
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);
958 while (((int)size) && (offset < buffer_size)) {
964 memcpy(dma_buffer, &buffer[offset], size2);
965 ret = wilc->hif_func->hif_block_tx(wilc, addr,
974 release_bus(wilc, RELEASE_ONLY);
980 } while (offset < buffer_size);
986 return (ret < 0) ? ret : 0;
989 int wilc_wlan_start(struct wilc *wilc)
995 if (wilc->io_type == HIF_SDIO) {
998 } else if (wilc->io_type == HIF_SPI) {
1001 acquire_bus(wilc, ACQUIRE_ONLY);
1002 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
1004 release_bus(wilc, RELEASE_ONLY);
1008 if (wilc->io_type == HIF_SDIO && wilc->dev_irq_num)
1009 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1011 #ifdef WILC_DISABLE_PMU
1013 reg |= WILC_HAVE_USE_PMU;
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;
1022 #ifdef WILC_EXT_PA_INV_TX_RX
1023 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1025 reg |= WILC_HAVE_USE_IRQ_AS_HOST_WAKE;
1026 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1028 reg |= WILC_HAVE_XTAL_24;
1030 #ifdef DISABLE_WILC_UART
1031 reg |= WILC_HAVE_DISABLE_WILC_UART;
1034 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1036 release_bus(wilc, RELEASE_ONLY);
1040 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1042 ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
1044 release_bus(wilc, RELEASE_ONLY);
1048 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1049 if ((reg & BIT(10)) == 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, ®);
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, ®);
1058 release_bus(wilc, RELEASE_ONLY);
1060 return (ret < 0) ? ret : 0;
1063 int wilc_wlan_stop(struct wilc *wilc)
1069 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1071 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1073 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1078 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1080 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1085 ret = wilc->hif_func->hif_read_reg(wilc,
1086 WILC_GLB_RESET_0, ®);
1088 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1092 if ((reg & BIT(10))) {
1094 ret = wilc->hif_func->hif_write_reg(wilc,
1099 ret = wilc->hif_func->hif_read_reg(wilc,
1103 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1110 reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1111 BIT(29) | BIT(30) | BIT(31));
1113 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1114 reg = (u32)~BIT(10);
1116 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1118 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1123 void wilc_wlan_cleanup(struct net_device *dev)
1125 struct txq_entry_t *tqe;
1126 struct rxq_entry_t *rqe;
1129 struct wilc_vif *vif;
1132 vif = netdev_priv(dev);
1137 tqe = wilc_wlan_txq_remove_from_head(dev);
1140 if (tqe->tx_complete_func)
1141 tqe->tx_complete_func(tqe->priv, 0);
1146 rqe = wilc_wlan_rxq_remove(wilc);
1152 kfree(wilc->rx_buffer);
1153 wilc->rx_buffer = NULL;
1154 kfree(wilc->tx_buffer);
1155 wilc->tx_buffer = NULL;
1157 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1159 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1161 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1163 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1166 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1168 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1169 wilc->hif_func->hif_deinit(NULL);
1172 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
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;
1181 if (type == WILC_CFG_SET)
1182 cfg->wid_header[0] = 'W';
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;
1194 if (!wilc_wlan_txq_add_cfg_pkt(vif, &cfg->wid_header[0], total_len))
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)
1205 struct wilc *wilc = vif->wilc;
1207 if (wilc->cfg_frame_in_use)
1211 wilc->cfg_frame_offset = 0;
1213 offset = wilc->cfg_frame_offset;
1214 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1215 wid, buffer, buffer_size);
1217 wilc->cfg_frame_offset = offset;
1220 netdev_dbg(vif->ndev,
1221 "[WILC]PACKET Commit with sequence number %d\n",
1223 netdev_dbg(vif->ndev, "Processing cfg_set()\n");
1224 wilc->cfg_frame_in_use = 1;
1226 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
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");
1235 wilc->cfg_frame_in_use = 0;
1236 wilc->cfg_frame_offset = 0;
1237 wilc->cfg_seq_no += 1;
1243 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1248 struct wilc *wilc = vif->wilc;
1250 if (wilc->cfg_frame_in_use)
1254 wilc->cfg_frame_offset = 0;
1256 offset = wilc->cfg_frame_offset;
1257 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1259 wilc->cfg_frame_offset = offset;
1262 wilc->cfg_frame_in_use = 1;
1264 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
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");
1272 wilc->cfg_frame_in_use = 0;
1273 wilc->cfg_frame_offset = 0;
1274 wilc->cfg_seq_no += 1;
1280 int wilc_wlan_cfg_get_val(u16 wid, u8 *buffer, u32 buffer_size)
1282 return wilc_wlan_cfg_get_wid_value(wid, buffer, buffer_size);
1285 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1291 if (mode == GET_CFG) {
1292 for (i = 0; i < count; i++) {
1293 if (!wilc_wlan_cfg_get(vif, !i,
1301 for (i = 0; i < count; i++) {
1302 wids[i].size = wilc_wlan_cfg_get_val(wids[i].id,
1306 } else if (mode == SET_CFG) {
1307 for (i = 0; i < count; i++) {
1308 if (!wilc_wlan_cfg_set(vif, !i,
1323 static u32 init_chip(struct net_device *dev)
1327 struct wilc_vif *vif;
1330 vif = netdev_priv(dev);
1333 acquire_bus(wilc, ACQUIRE_ONLY);
1335 chipid = wilc_get_chipid(wilc, true);
1337 if ((chipid & 0xfff) != 0xa0) {
1338 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, ®);
1340 netdev_err(dev, "fail read reg 0x1118\n");
1344 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1346 netdev_err(dev, "fail write reg 0x1118\n");
1349 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1351 netdev_err(dev, "fail write reg 0xc0000\n");
1356 release_bus(wilc, RELEASE_ONLY);
1361 u32 wilc_get_chipid(struct wilc *wilc, bool update)
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)) {
1374 if (tempchipid == 0x1002a0) {
1376 tempchipid = 0x1002a1;
1377 } else if (tempchipid == 0x1002b0) {
1379 tempchipid = 0x1002b1;
1380 else if (rfrevid != 0x3)
1381 tempchipid = 0x1002b2;
1384 chipid = tempchipid;
1389 int wilc_wlan_init(struct net_device *dev)
1392 struct wilc_vif *vif = netdev_priv(dev);
1399 if (!wilc->hif_func->hif_init(wilc, false)) {
1404 if (!wilc_wlan_cfg_init()) {
1409 if (!wilc->tx_buffer)
1410 wilc->tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
1412 if (!wilc->tx_buffer) {
1417 if (!wilc->rx_buffer)
1418 wilc->rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
1420 if (!wilc->rx_buffer) {
1425 if (!init_chip(dev)) {
1434 kfree(wilc->rx_buffer);
1435 wilc->rx_buffer = NULL;
1436 kfree(wilc->tx_buffer);
1437 wilc->tx_buffer = NULL;