1 // SPDX-License-Identifier: GPL-2.0
2 #include "wilc_wfi_cfgoperations.h"
3 #include "wilc_wlan_if.h"
6 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/delay.h>
9 #include <linux/workqueue.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/gpio.h>
14 #include <linux/kthread.h>
15 #include <linux/firmware.h>
17 #include <linux/init.h>
18 #include <linux/netdevice.h>
19 #include <linux/inetdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/skbuff.h>
24 #include <linux/mutex.h>
25 #include <linux/completion.h>
27 static int dev_state_ev_handler(struct notifier_block *this,
28 unsigned long event, void *ptr);
30 static struct notifier_block g_dev_notifier = {
31 .notifier_call = dev_state_ev_handler
34 static int wlan_deinit_locks(struct net_device *dev);
35 static void wlan_deinitialize_threads(struct net_device *dev);
37 static void linux_wlan_tx_complete(void *priv, int status);
38 static int mac_init_fn(struct net_device *ndev);
39 static struct net_device_stats *mac_stats(struct net_device *dev);
40 static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd);
41 static int wilc_mac_open(struct net_device *ndev);
42 static int wilc_mac_close(struct net_device *ndev);
43 static void wilc_set_multicast_list(struct net_device *dev);
45 bool wilc_enable_ps = true;
47 static const struct net_device_ops wilc_netdev_ops = {
48 .ndo_init = mac_init_fn,
49 .ndo_open = wilc_mac_open,
50 .ndo_stop = wilc_mac_close,
51 .ndo_start_xmit = wilc_mac_xmit,
52 .ndo_do_ioctl = mac_ioctl,
53 .ndo_get_stats = mac_stats,
54 .ndo_set_rx_mode = wilc_set_multicast_list,
58 static int dev_state_ev_handler(struct notifier_block *this,
59 unsigned long event, void *ptr)
61 struct in_ifaddr *dev_iface = ptr;
62 struct wilc_priv *priv;
63 struct host_if_drv *hif_drv;
64 struct net_device *dev;
68 char wlan_dev_name[5] = "wlan0";
70 if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev)
73 if (memcmp(dev_iface->ifa_label, "wlan0", 5) &&
74 memcmp(dev_iface->ifa_label, "p2p0", 4))
77 dev = (struct net_device *)dev_iface->ifa_dev->dev;
78 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
81 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
85 hif_drv = (struct host_if_drv *)priv->hif_drv;
86 vif = netdev_priv(dev);
92 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
94 wilc_optaining_ip = false;
95 del_timer(&wilc_during_ip_timer);
99 wilc_set_power_mgmt(vif, 1, 0);
101 netdev_dbg(dev, "[%s] Up IP\n", dev_iface->ifa_label);
103 ip_addr_buf = (char *)&dev_iface->ifa_address;
104 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
105 ip_addr_buf[0], ip_addr_buf[1],
106 ip_addr_buf[2], ip_addr_buf[3]);
107 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
112 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
114 wilc_optaining_ip = false;
117 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
118 wilc_set_power_mgmt(vif, 0, 0);
120 wilc_resolve_disconnect_aberration(vif);
122 netdev_dbg(dev, "[%s] Down IP\n", dev_iface->ifa_label);
124 ip_addr_buf = null_ip;
125 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
126 ip_addr_buf[0], ip_addr_buf[1],
127 ip_addr_buf[2], ip_addr_buf[3]);
129 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
140 static irqreturn_t isr_uh_routine(int irq, void *user_data)
142 struct wilc_vif *vif;
144 struct net_device *dev = user_data;
146 vif = netdev_priv(dev);
150 netdev_err(dev, "Can't handle UH interrupt\n");
153 return IRQ_WAKE_THREAD;
156 static irqreturn_t isr_bh_routine(int irq, void *userdata)
158 struct wilc_vif *vif;
160 struct net_device *dev = userdata;
162 vif = netdev_priv(userdata);
166 netdev_err(dev, "Can't handle BH interrupt\n");
170 wilc_handle_isr(wilc);
175 static int init_irq(struct net_device *dev)
178 struct wilc_vif *vif;
181 vif = netdev_priv(dev);
184 if ((gpio_request(wl->gpio, "WILC_INTR") == 0) &&
185 (gpio_direction_input(wl->gpio) == 0)) {
186 wl->dev_irq_num = gpio_to_irq(wl->gpio);
189 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
192 if (ret != -1 && request_threaded_irq(wl->dev_irq_num,
195 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
196 "WILC_IRQ", dev) < 0) {
197 netdev_err(dev, "Failed to request IRQ GPIO: %d\n", wl->gpio);
202 "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
203 wl->dev_irq_num, wl->gpio);
209 static void deinit_irq(struct net_device *dev)
211 struct wilc_vif *vif;
214 vif = netdev_priv(dev);
217 /* Deinitialize IRQ */
218 if (wilc->dev_irq_num) {
219 free_irq(wilc->dev_irq_num, wilc);
220 gpio_free(wilc->gpio);
224 void wilc_mac_indicate(struct wilc *wilc, int flag)
228 if (flag == WILC_MAC_INDICATE_STATUS) {
229 wilc_wlan_cfg_get_val(WID_STATUS,
230 (unsigned char *)&status, 4);
231 if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
232 wilc->mac_status = status;
233 complete(&wilc->sync_event);
235 wilc->mac_status = status;
240 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
245 bssid = mac_header + 10;
246 bssid1 = mac_header + 4;
248 for (i = 0; i < wilc->vif_num; i++) {
249 if (wilc->vif[i]->mode == STATION_MODE)
250 if (ether_addr_equal_unaligned(bssid,
251 wilc->vif[i]->bssid))
252 return wilc->vif[i]->ndev;
253 if (wilc->vif[i]->mode == AP_MODE)
254 if (ether_addr_equal_unaligned(bssid1,
255 wilc->vif[i]->bssid))
256 return wilc->vif[i]->ndev;
262 int wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
264 struct wilc_vif *vif = netdev_priv(wilc_netdev);
266 memcpy(vif->bssid, bssid, 6);
272 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
275 u8 null_bssid[6] = {0};
278 for (i = 0; i < wilc->vif_num; i++)
279 if (memcmp(wilc->vif[i]->bssid, null_bssid, 6))
285 static int linux_wlan_txq_task(void *vp)
289 struct wilc_vif *vif;
291 struct net_device *dev = vp;
293 vif = netdev_priv(dev);
296 complete(&wl->txq_thread_started);
298 wait_for_completion(&wl->txq_event);
301 complete(&wl->txq_thread_started);
303 while (!kthread_should_stop())
308 ret = wilc_wlan_handle_txq(dev, &txq_count);
309 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
310 if (netif_queue_stopped(wl->vif[0]->ndev))
311 netif_wake_queue(wl->vif[0]->ndev);
312 if (netif_queue_stopped(wl->vif[1]->ndev))
313 netif_wake_queue(wl->vif[1]->ndev);
315 } while (ret == WILC_TX_ERR_NO_BUF && !wl->close);
320 int wilc_wlan_get_firmware(struct net_device *dev)
322 struct wilc_vif *vif;
324 int chip_id, ret = 0;
325 const struct firmware *wilc_firmware;
328 vif = netdev_priv(dev);
331 chip_id = wilc_get_chipid(wilc, false);
333 if (chip_id < 0x1003a0)
334 firmware = FIRMWARE_1002;
336 firmware = FIRMWARE_1003;
338 netdev_info(dev, "loading firmware %s\n", firmware);
340 if (!(&vif->ndev->dev))
343 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
344 netdev_err(dev, "%s - firmware not available\n", firmware);
348 wilc->firmware = wilc_firmware;
355 static int linux_wlan_start_firmware(struct net_device *dev)
357 struct wilc_vif *vif;
361 vif = netdev_priv(dev);
364 ret = wilc_wlan_start(wilc);
368 if (!wait_for_completion_timeout(&wilc->sync_event,
369 msecs_to_jiffies(5000)))
375 static int wilc1000_firmware_download(struct net_device *dev)
377 struct wilc_vif *vif;
381 vif = netdev_priv(dev);
384 if (!wilc->firmware) {
385 netdev_err(dev, "Firmware buffer is NULL\n");
389 ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
390 wilc->firmware->size);
394 release_firmware(wilc->firmware);
395 wilc->firmware = NULL;
397 netdev_dbg(dev, "Download Succeeded\n");
402 static int linux_wlan_init_test_config(struct net_device *dev,
403 struct wilc_vif *vif)
405 unsigned char c_val[64];
406 struct wilc *wilc = vif->wilc;
407 struct wilc_priv *priv;
408 struct host_if_drv *hif_drv;
410 netdev_dbg(dev, "Start configuring Firmware\n");
411 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
412 hif_drv = (struct host_if_drv *)priv->hif_drv;
413 netdev_dbg(dev, "Host = %p\n", hif_drv);
414 wilc_get_chipid(wilc, false);
418 if (!wilc_wlan_cfg_set(vif, 1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
422 if (!wilc_wlan_cfg_set(vif, 0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
425 c_val[0] = INFRASTRUCTURE;
426 if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, c_val, 1, 0, 0))
429 c_val[0] = RATE_AUTO;
430 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
433 c_val[0] = G_MIXED_11B_2_MODE;
434 if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, c_val, 1, 0,
439 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
442 c_val[0] = G_SHORT_PREAMBLE;
443 if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, c_val, 1, 0, 0))
446 c_val[0] = AUTO_PROT;
447 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
450 c_val[0] = ACTIVE_SCAN;
451 if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, c_val, 1, 0, 0))
454 c_val[0] = SITE_SURVEY_OFF;
455 if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, c_val, 1, 0, 0))
458 *((int *)c_val) = 0xffff;
459 if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
462 *((int *)c_val) = 2346;
463 if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
467 if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, c_val, 1, 0, 0))
471 if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, c_val, 1, 0, 0))
474 c_val[0] = NO_POWERSAVE;
475 if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
478 c_val[0] = NO_SECURITY; /* NO_ENCRYPT, 0x79 */
479 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, c_val, 1, 0, 0))
482 c_val[0] = OPEN_SYSTEM;
483 if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, c_val, 1, 0, 0))
486 strcpy(c_val, "123456790abcdef1234567890");
487 if (!wilc_wlan_cfg_set(vif, 0, WID_WEP_KEY_VALUE, c_val,
488 (strlen(c_val) + 1), 0, 0))
491 strcpy(c_val, "12345678");
492 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_PSK, c_val, (strlen(c_val)), 0,
496 strcpy(c_val, "password");
497 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_KEY, c_val, (strlen(c_val) + 1),
505 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
509 if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
513 if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
516 c_val[0] = NORMAL_ACK;
517 if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, c_val, 1, 0, 0))
521 if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1,
526 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0,
531 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0,
535 *((int *)c_val) = 100;
536 if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
539 c_val[0] = REKEY_DISABLE;
540 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, c_val, 1, 0, 0))
543 *((int *)c_val) = 84600;
544 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
547 *((int *)c_val) = 500;
548 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, c_val, 4, 0,
553 if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0,
557 c_val[0] = G_SELF_CTS_PROT;
558 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
562 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, c_val, 1, 0, 0))
565 c_val[0] = HT_MIXED_MODE;
566 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, c_val, 1, 0,
571 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0,
575 c_val[0] = DETECT_PROTECT_REPORT;
576 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1,
580 c_val[0] = RTS_CTS_NONHT_PROT;
581 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
585 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0,
589 c_val[0] = MIMO_MODE;
590 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
594 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0,
599 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1,
609 void wilc1000_wlan_deinit(struct net_device *dev)
611 struct wilc_vif *vif;
614 vif = netdev_priv(dev);
618 netdev_err(dev, "wl is NULL\n");
622 if (wl->initialized) {
623 netdev_info(dev, "Deinitializing wilc1000...\n");
625 if (!wl->dev_irq_num &&
626 wl->hif_func->disable_interrupt) {
627 mutex_lock(&wl->hif_cs);
628 wl->hif_func->disable_interrupt(wl);
629 mutex_unlock(&wl->hif_cs);
632 complete(&wl->txq_event);
634 wlan_deinitialize_threads(dev);
638 wilc_wlan_cleanup(dev);
639 wlan_deinit_locks(dev);
641 wl->initialized = false;
643 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
645 netdev_dbg(dev, "wilc1000 is not initialized\n");
649 static int wlan_init_locks(struct net_device *dev)
651 struct wilc_vif *vif;
654 vif = netdev_priv(dev);
657 mutex_init(&wl->hif_cs);
658 mutex_init(&wl->rxq_cs);
660 spin_lock_init(&wl->txq_spinlock);
661 mutex_init(&wl->txq_add_to_head_cs);
663 init_completion(&wl->txq_event);
665 init_completion(&wl->cfg_event);
666 init_completion(&wl->sync_event);
667 init_completion(&wl->txq_thread_started);
672 static int wlan_deinit_locks(struct net_device *dev)
674 struct wilc_vif *vif;
677 vif = netdev_priv(dev);
681 mutex_destroy(&wilc->hif_cs);
684 mutex_destroy(&wilc->rxq_cs);
689 static int wlan_initialize_threads(struct net_device *dev)
691 struct wilc_vif *vif;
694 vif = netdev_priv(dev);
697 wilc->txq_thread = kthread_run(linux_wlan_txq_task, (void *)dev,
699 if (IS_ERR(wilc->txq_thread)) {
700 netdev_err(dev, "couldn't create TXQ thread\n");
702 return PTR_ERR(wilc->txq_thread);
704 wait_for_completion(&wilc->txq_thread_started);
709 static void wlan_deinitialize_threads(struct net_device *dev)
711 struct wilc_vif *vif;
714 vif = netdev_priv(dev);
720 complete(&wl->txq_event);
722 if (wl->txq_thread) {
723 kthread_stop(wl->txq_thread);
724 wl->txq_thread = NULL;
728 int wilc1000_wlan_init(struct net_device *dev, struct wilc_vif *vif)
731 struct wilc *wl = vif->wilc;
733 if (!wl->initialized) {
734 wl->mac_status = WILC_MAC_STATUS_INIT;
737 wlan_init_locks(dev);
739 ret = wilc_wlan_init(dev);
745 if (wl->gpio >= 0 && init_irq(dev)) {
750 ret = wlan_initialize_threads(dev);
753 goto _fail_wilc_wlan_;
756 if (!wl->dev_irq_num &&
757 wl->hif_func->enable_interrupt &&
758 wl->hif_func->enable_interrupt(wl)) {
760 goto _fail_irq_init_;
763 if (wilc_wlan_get_firmware(dev)) {
765 goto _fail_irq_enable_;
768 ret = wilc1000_firmware_download(dev);
771 goto _fail_irq_enable_;
774 ret = linux_wlan_start_firmware(dev);
777 goto _fail_irq_enable_;
780 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
782 char firmware_ver[20];
784 size = wilc_wlan_cfg_get_val(WID_FIRMWARE_VERSION,
786 sizeof(firmware_ver));
787 firmware_ver[size] = '\0';
788 netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
790 ret = linux_wlan_init_test_config(dev, vif);
793 netdev_err(dev, "Failed to configure firmware\n");
795 goto _fail_fw_start_;
798 wl->initialized = true;
805 if (!wl->dev_irq_num &&
806 wl->hif_func->disable_interrupt)
807 wl->hif_func->disable_interrupt(wl);
812 wlan_deinitialize_threads(dev);
814 wilc_wlan_cleanup(dev);
816 wlan_deinit_locks(dev);
817 netdev_err(dev, "WLAN initialization FAILED\n");
819 netdev_dbg(dev, "wilc1000 already initialized\n");
824 static int mac_init_fn(struct net_device *ndev)
826 netif_start_queue(ndev);
827 netif_stop_queue(ndev);
832 static int wilc_mac_open(struct net_device *ndev)
834 struct wilc_vif *vif;
836 unsigned char mac_add[ETH_ALEN] = {0};
841 vif = netdev_priv(ndev);
844 if (!wl || !wl->dev) {
845 netdev_err(ndev, "device not ready\n");
849 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
851 ret = wilc_init_host_int(ndev);
855 ret = wilc1000_wlan_init(ndev, vif);
857 wilc_deinit_host_int(ndev);
861 for (i = 0; i < wl->vif_num; i++) {
862 if (ndev == wl->vif[i]->ndev) {
863 wilc_set_wfi_drv_handler(vif, wilc_get_vif_idx(vif),
864 vif->iftype, vif->ifc_id);
865 wilc_set_operation_mode(vif, vif->iftype);
869 wilc_get_mac_address(vif, mac_add);
870 netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
871 memcpy(wl->vif[i]->src_addr, mac_add, ETH_ALEN);
873 memcpy(ndev->dev_addr, wl->vif[i]->src_addr, ETH_ALEN);
875 if (!is_valid_ether_addr(ndev->dev_addr)) {
876 netdev_err(ndev, "Wrong MAC address\n");
877 wilc_deinit_host_int(ndev);
878 wilc1000_wlan_deinit(ndev);
882 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
883 vif->ndev->ieee80211_ptr,
884 vif->frame_reg[0].type,
885 vif->frame_reg[0].reg);
886 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
887 vif->ndev->ieee80211_ptr,
888 vif->frame_reg[1].type,
889 vif->frame_reg[1].reg);
890 netif_wake_queue(ndev);
896 static struct net_device_stats *mac_stats(struct net_device *dev)
898 struct wilc_vif *vif = netdev_priv(dev);
900 return &vif->netstats;
903 static void wilc_set_multicast_list(struct net_device *dev)
905 struct netdev_hw_addr *ha;
906 struct wilc_vif *vif;
909 vif = netdev_priv(dev);
911 if (dev->flags & IFF_PROMISC)
914 if (dev->flags & IFF_ALLMULTI ||
915 dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
916 wilc_setup_multicast_filter(vif, false, 0);
920 if (dev->mc.count == 0) {
921 wilc_setup_multicast_filter(vif, true, 0);
925 netdev_for_each_mc_addr(ha, dev) {
926 memcpy(wilc_multicast_mac_addr_list[i], ha->addr, ETH_ALEN);
927 netdev_dbg(dev, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
928 wilc_multicast_mac_addr_list[i][0],
929 wilc_multicast_mac_addr_list[i][1],
930 wilc_multicast_mac_addr_list[i][2],
931 wilc_multicast_mac_addr_list[i][3],
932 wilc_multicast_mac_addr_list[i][4],
933 wilc_multicast_mac_addr_list[i][5]);
937 wilc_setup_multicast_filter(vif, true, (dev->mc.count));
940 static void linux_wlan_tx_complete(void *priv, int status)
942 struct tx_complete_data *pv_data = priv;
944 dev_kfree_skb(pv_data->skb);
948 int wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
950 struct wilc_vif *vif;
951 struct tx_complete_data *tx_data = NULL;
955 struct ethhdr *eth_h;
958 vif = netdev_priv(ndev);
961 if (skb->dev != ndev) {
962 netdev_err(ndev, "Packet not destined to this device\n");
966 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
969 netif_wake_queue(ndev);
973 tx_data->buff = skb->data;
974 tx_data->size = skb->len;
977 eth_h = (struct ethhdr *)(skb->data);
978 if (eth_h->h_proto == cpu_to_be16(0x8e88))
979 netdev_dbg(ndev, "EAPOL transmitted\n");
981 ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
983 udp_buf = (char *)ih + sizeof(struct iphdr);
984 if ((udp_buf[1] == 68 && udp_buf[3] == 67) ||
985 (udp_buf[1] == 67 && udp_buf[3] == 68))
986 netdev_dbg(ndev, "DHCP Message transmitted, type:%x %x %x\n",
987 udp_buf[248], udp_buf[249], udp_buf[250]);
989 vif->netstats.tx_packets++;
990 vif->netstats.tx_bytes += tx_data->size;
991 tx_data->bssid = wilc->vif[vif->idx]->bssid;
992 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
993 tx_data->buff, tx_data->size,
994 linux_wlan_tx_complete);
996 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
997 netif_stop_queue(wilc->vif[0]->ndev);
998 netif_stop_queue(wilc->vif[1]->ndev);
1004 static int wilc_mac_close(struct net_device *ndev)
1006 struct wilc_priv *priv;
1007 struct wilc_vif *vif;
1008 struct host_if_drv *hif_drv;
1011 vif = netdev_priv(ndev);
1013 if (!vif || !vif->ndev || !vif->ndev->ieee80211_ptr ||
1014 !vif->ndev->ieee80211_ptr->wiphy)
1017 priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
1023 hif_drv = (struct host_if_drv *)priv->hif_drv;
1025 netdev_dbg(ndev, "Mac close\n");
1033 if (wl->open_ifcs > 0)
1039 netif_stop_queue(vif->ndev);
1041 wilc_deinit_host_int(vif->ndev);
1044 if (wl->open_ifcs == 0) {
1045 netdev_dbg(ndev, "Deinitializing wilc1000\n");
1047 wilc1000_wlan_deinit(ndev);
1048 WILC_WFI_deinit_mon_interface();
1051 vif->mac_opened = 0;
1056 static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1061 struct wilc_vif *vif;
1065 vif = netdev_priv(ndev);
1068 if (!wilc->initialized)
1074 struct iwreq *wrq = (struct iwreq *)req;
1076 size = wrq->u.data.length;
1078 if (size && wrq->u.data.pointer) {
1079 buff = memdup_user(wrq->u.data.pointer,
1080 wrq->u.data.length);
1082 return PTR_ERR(buff);
1084 if (strncasecmp(buff, "RSSI", size) == 0) {
1085 ret = wilc_get_rssi(vif, &rssi);
1086 netdev_info(ndev, "RSSI :%d\n", rssi);
1090 snprintf(buff, size, "rssi %d", rssi);
1092 if (copy_to_user(wrq->u.data.pointer, buff, size)) {
1093 netdev_err(ndev, "failed to copy\n");
1104 netdev_info(ndev, "Command - %d - has been received\n", cmd);
1117 void wilc_frmw_to_linux(struct wilc *wilc, u8 *buff, u32 size, u32 pkt_offset)
1119 unsigned int frame_len = 0;
1121 unsigned char *buff_to_send = NULL;
1122 struct sk_buff *skb;
1123 struct net_device *wilc_netdev;
1124 struct wilc_vif *vif;
1129 wilc_netdev = get_if_handler(wilc, buff);
1134 vif = netdev_priv(wilc_netdev);
1138 buff_to_send = buff;
1140 skb = dev_alloc_skb(frame_len);
1144 skb->dev = wilc_netdev;
1146 skb_put_data(skb, buff_to_send, frame_len);
1148 skb->protocol = eth_type_trans(skb, wilc_netdev);
1149 vif->netstats.rx_packets++;
1150 vif->netstats.rx_bytes += frame_len;
1151 skb->ip_summed = CHECKSUM_UNNECESSARY;
1152 stats = netif_rx(skb);
1153 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
1157 void WILC_WFI_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
1160 struct wilc_vif *vif;
1162 for (i = 0; i < wilc->vif_num; i++) {
1163 vif = netdev_priv(wilc->vif[i]->ndev);
1164 if (vif->monitor_flag) {
1165 WILC_WFI_monitor_rx(buff, size);
1170 vif = netdev_priv(wilc->vif[1]->ndev);
1171 if ((buff[0] == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
1172 (buff[0] == vif->frame_reg[1].type && vif->frame_reg[1].reg))
1173 WILC_WFI_p2p_rx(wilc->vif[1]->ndev, buff, size);
1176 void wilc_netdev_cleanup(struct wilc *wilc)
1180 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev))
1181 unregister_inetaddr_notifier(&g_dev_notifier);
1183 if (wilc && wilc->firmware) {
1184 release_firmware(wilc->firmware);
1185 wilc->firmware = NULL;
1188 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1189 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1190 if (wilc->vif[i]->ndev)
1191 if (wilc->vif[i]->mac_opened)
1192 wilc_mac_close(wilc->vif[i]->ndev);
1194 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1195 unregister_netdev(wilc->vif[i]->ndev);
1196 wilc_free_wiphy(wilc->vif[i]->ndev);
1197 free_netdev(wilc->vif[i]->ndev);
1203 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
1205 int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
1206 int gpio, const struct wilc_hif_func *ops)
1209 struct wilc_vif *vif;
1210 struct net_device *ndev;
1213 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
1218 wl->io_type = io_type;
1222 register_inetaddr_notifier(&g_dev_notifier);
1224 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1225 ndev = alloc_etherdev(sizeof(struct wilc_vif));
1229 vif = netdev_priv(ndev);
1230 memset(vif, 0, sizeof(struct wilc_vif));
1233 strcpy(ndev->name, "wlan%d");
1236 strcpy(ndev->name, "p2p%d");
1243 vif->idx = wl->vif_num;
1245 ndev->netdev_ops = &wilc_netdev_ops;
1248 struct wireless_dev *wdev;
1250 wdev = wilc_create_wiphy(ndev, dev);
1253 SET_NETDEV_DEV(ndev, dev);
1256 netdev_err(ndev, "Can't register WILC Wiphy\n");
1260 vif->ndev->ieee80211_ptr = wdev;
1261 vif->ndev->ml_priv = vif;
1262 wdev->netdev = vif->ndev;
1263 vif->netstats.rx_packets = 0;
1264 vif->netstats.tx_packets = 0;
1265 vif->netstats.rx_bytes = 0;
1266 vif->netstats.tx_bytes = 0;
1269 ret = register_netdev(ndev);
1273 vif->iftype = STATION_MODE;
1274 vif->mac_opened = 0;
1279 EXPORT_SYMBOL_GPL(wilc_netdev_init);
1281 MODULE_LICENSE("GPL");