1 #include "wilc_wfi_cfgoperations.h"
2 #include "wilc_wlan_if.h"
5 #include <linux/slab.h>
6 #include <linux/sched.h>
7 #include <linux/delay.h>
8 #include <linux/workqueue.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/gpio.h>
13 #include <linux/kthread.h>
14 #include <linux/firmware.h>
16 #include <linux/init.h>
17 #include <linux/netdevice.h>
18 #include <linux/inetdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/skbuff.h>
23 #include <linux/mutex.h>
24 #include <linux/completion.h>
26 static int dev_state_ev_handler(struct notifier_block *this,
27 unsigned long event, void *ptr);
29 static struct notifier_block g_dev_notifier = {
30 .notifier_call = dev_state_ev_handler
33 static int wlan_deinit_locks(struct net_device *dev);
34 static void wlan_deinitialize_threads(struct net_device *dev);
36 static void linux_wlan_tx_complete(void *priv, int status);
37 static int mac_init_fn(struct net_device *ndev);
38 static struct net_device_stats *mac_stats(struct net_device *dev);
39 static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd);
40 static void wilc_set_multicast_list(struct net_device *dev);
42 bool wilc_enable_ps = true;
44 static const struct net_device_ops wilc_netdev_ops = {
45 .ndo_init = mac_init_fn,
46 .ndo_open = wilc_mac_open,
47 .ndo_stop = wilc_mac_close,
48 .ndo_start_xmit = wilc_mac_xmit,
49 .ndo_do_ioctl = mac_ioctl,
50 .ndo_get_stats = mac_stats,
51 .ndo_set_rx_mode = wilc_set_multicast_list,
55 static int dev_state_ev_handler(struct notifier_block *this,
56 unsigned long event, void *ptr)
58 struct in_ifaddr *dev_iface = ptr;
59 struct wilc_priv *priv;
60 struct host_if_drv *hif_drv;
61 struct net_device *dev;
65 char wlan_dev_name[5] = "wlan0";
67 if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev)
70 if (memcmp(dev_iface->ifa_label, "wlan0", 5) &&
71 memcmp(dev_iface->ifa_label, "p2p0", 4))
74 dev = (struct net_device *)dev_iface->ifa_dev->dev;
75 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
78 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
82 hif_drv = (struct host_if_drv *)priv->hif_drv;
83 vif = netdev_priv(dev);
89 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
91 wilc_optaining_ip = false;
92 del_timer(&wilc_during_ip_timer);
96 wilc_set_power_mgmt(vif, 1, 0);
98 netdev_dbg(dev, "[%s] Up IP\n", dev_iface->ifa_label);
100 ip_addr_buf = (char *)&dev_iface->ifa_address;
101 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
102 ip_addr_buf[0], ip_addr_buf[1],
103 ip_addr_buf[2], ip_addr_buf[3]);
104 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
109 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
111 wilc_optaining_ip = false;
114 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
115 wilc_set_power_mgmt(vif, 0, 0);
117 wilc_resolve_disconnect_aberration(vif);
119 netdev_dbg(dev, "[%s] Down IP\n", dev_iface->ifa_label);
121 ip_addr_buf = null_ip;
122 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
123 ip_addr_buf[0], ip_addr_buf[1],
124 ip_addr_buf[2], ip_addr_buf[3]);
126 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
137 static irqreturn_t isr_uh_routine(int irq, void *user_data)
139 struct wilc_vif *vif;
141 struct net_device *dev = user_data;
143 vif = netdev_priv(dev);
147 netdev_err(dev, "Can't handle UH interrupt\n");
150 return IRQ_WAKE_THREAD;
153 static irqreturn_t isr_bh_routine(int irq, void *userdata)
155 struct wilc_vif *vif;
157 struct net_device *dev = userdata;
159 vif = netdev_priv(userdata);
163 netdev_err(dev, "Can't handle BH interrupt\n");
167 wilc_handle_isr(wilc);
172 static int init_irq(struct net_device *dev)
175 struct wilc_vif *vif;
178 vif = netdev_priv(dev);
181 if ((gpio_request(wl->gpio, "WILC_INTR") == 0) &&
182 (gpio_direction_input(wl->gpio) == 0)) {
183 wl->dev_irq_num = gpio_to_irq(wl->gpio);
186 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
189 if (ret != -1 && request_threaded_irq(wl->dev_irq_num,
192 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
193 "WILC_IRQ", dev) < 0) {
194 netdev_err(dev, "Failed to request IRQ GPIO: %d\n", wl->gpio);
199 "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
200 wl->dev_irq_num, wl->gpio);
206 static void deinit_irq(struct net_device *dev)
208 struct wilc_vif *vif;
211 vif = netdev_priv(dev);
214 /* Deintialize IRQ */
215 if (wilc->dev_irq_num) {
216 free_irq(wilc->dev_irq_num, wilc);
217 gpio_free(wilc->gpio);
221 int wilc_lock_timeout(struct wilc *nic, void *vp, u32 timeout)
223 /* FIXME: replace with mutex_lock or wait_for_completion */
227 error = down_timeout(vp,
228 msecs_to_jiffies(timeout));
232 void wilc_mac_indicate(struct wilc *wilc, int flag)
236 if (flag == WILC_MAC_INDICATE_STATUS) {
237 wilc_wlan_cfg_get_val(WID_STATUS,
238 (unsigned char *)&status, 4);
239 if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
240 wilc->mac_status = status;
241 complete(&wilc->sync_event);
243 wilc->mac_status = status;
248 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
253 bssid = mac_header + 10;
254 bssid1 = mac_header + 4;
256 for (i = 0; i < wilc->vif_num; i++) {
257 if (wilc->vif[i]->mode == STATION_MODE)
258 if (ether_addr_equal_unaligned(bssid,
259 wilc->vif[i]->bssid))
260 return wilc->vif[i]->ndev;
261 if (wilc->vif[i]->mode == AP_MODE)
262 if (ether_addr_equal_unaligned(bssid1,
263 wilc->vif[i]->bssid))
264 return wilc->vif[i]->ndev;
270 int wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
274 struct wilc_vif *vif;
277 vif = netdev_priv(wilc_netdev);
280 for (i = 0; i < wilc->vif_num; i++)
281 if (wilc->vif[i]->ndev == wilc_netdev) {
282 memcpy(wilc->vif[i]->bssid, bssid, 6);
283 wilc->vif[i]->mode = mode;
291 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
294 u8 null_bssid[6] = {0};
297 for (i = 0; i < wilc->vif_num; i++)
298 if (memcmp(wilc->vif[i]->bssid, null_bssid, 6))
304 static int linux_wlan_txq_task(void *vp)
307 struct wilc_vif *vif;
309 struct net_device *dev = vp;
311 vif = netdev_priv(dev);
314 complete(&wl->txq_thread_started);
316 wait_for_completion(&wl->txq_event);
319 complete(&wl->txq_thread_started);
321 while (!kthread_should_stop())
326 ret = wilc_wlan_handle_txq(dev, &txq_count);
327 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
328 if (netif_queue_stopped(wl->vif[0]->ndev))
329 netif_wake_queue(wl->vif[0]->ndev);
330 if (netif_queue_stopped(wl->vif[1]->ndev))
331 netif_wake_queue(wl->vif[1]->ndev);
333 } while (ret == WILC_TX_ERR_NO_BUF && !wl->close);
338 int wilc_wlan_get_firmware(struct net_device *dev)
340 struct wilc_vif *vif;
342 int chip_id, ret = 0;
343 const struct firmware *wilc_firmware;
346 vif = netdev_priv(dev);
349 chip_id = wilc_get_chipid(wilc, false);
351 if (chip_id < 0x1003a0)
352 firmware = FIRMWARE_1002;
354 firmware = FIRMWARE_1003;
356 netdev_info(dev, "loading firmware %s\n", firmware);
358 if (!(&vif->ndev->dev))
361 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
362 netdev_err(dev, "%s - firmware not available\n", firmware);
366 wilc->firmware = wilc_firmware;
373 static int linux_wlan_start_firmware(struct net_device *dev)
375 struct wilc_vif *vif;
379 vif = netdev_priv(dev);
382 ret = wilc_wlan_start(wilc);
386 if (!wait_for_completion_timeout(&wilc->sync_event,
387 msecs_to_jiffies(5000)))
393 static int wilc1000_firmware_download(struct net_device *dev)
395 struct wilc_vif *vif;
399 vif = netdev_priv(dev);
402 if (!wilc->firmware) {
403 netdev_err(dev, "Firmware buffer is NULL\n");
407 ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
408 wilc->firmware->size);
412 release_firmware(wilc->firmware);
413 wilc->firmware = NULL;
415 netdev_dbg(dev, "Download Succeeded\n");
420 static int linux_wlan_init_test_config(struct net_device *dev,
421 struct wilc_vif *vif)
423 unsigned char c_val[64];
424 struct wilc *wilc = vif->wilc;
425 struct wilc_priv *priv;
426 struct host_if_drv *hif_drv;
428 netdev_dbg(dev, "Start configuring Firmware\n");
429 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
430 hif_drv = (struct host_if_drv *)priv->hif_drv;
431 netdev_dbg(dev, "Host = %p\n", hif_drv);
432 wilc_get_chipid(wilc, false);
436 if (!wilc_wlan_cfg_set(vif, 1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
440 if (!wilc_wlan_cfg_set(vif, 0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
443 c_val[0] = INFRASTRUCTURE;
444 if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, c_val, 1, 0, 0))
447 c_val[0] = RATE_AUTO;
448 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
451 c_val[0] = G_MIXED_11B_2_MODE;
452 if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, c_val, 1, 0,
457 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
460 c_val[0] = G_SHORT_PREAMBLE;
461 if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, c_val, 1, 0, 0))
464 c_val[0] = AUTO_PROT;
465 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
468 c_val[0] = ACTIVE_SCAN;
469 if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, c_val, 1, 0, 0))
472 c_val[0] = SITE_SURVEY_OFF;
473 if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, c_val, 1, 0, 0))
476 *((int *)c_val) = 0xffff;
477 if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
480 *((int *)c_val) = 2346;
481 if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
485 if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, c_val, 1, 0, 0))
489 if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, c_val, 1, 0, 0))
492 c_val[0] = NO_POWERSAVE;
493 if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
496 c_val[0] = NO_SECURITY; /* NO_ENCRYPT, 0x79 */
497 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, c_val, 1, 0, 0))
500 c_val[0] = OPEN_SYSTEM;
501 if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, c_val, 1, 0, 0))
504 strcpy(c_val, "123456790abcdef1234567890");
505 if (!wilc_wlan_cfg_set(vif, 0, WID_WEP_KEY_VALUE, c_val,
506 (strlen(c_val) + 1), 0, 0))
509 strcpy(c_val, "12345678");
510 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_PSK, c_val, (strlen(c_val)), 0,
514 strcpy(c_val, "password");
515 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_KEY, c_val, (strlen(c_val) + 1),
523 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
527 if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
531 if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
534 c_val[0] = NORMAL_ACK;
535 if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, c_val, 1, 0, 0))
539 if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1,
544 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0,
549 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0,
553 *((int *)c_val) = 100;
554 if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
557 c_val[0] = REKEY_DISABLE;
558 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, c_val, 1, 0, 0))
561 *((int *)c_val) = 84600;
562 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
565 *((int *)c_val) = 500;
566 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, c_val, 4, 0,
571 if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0,
575 c_val[0] = G_SELF_CTS_PROT;
576 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
580 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, c_val, 1, 0, 0))
583 c_val[0] = HT_MIXED_MODE;
584 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, c_val, 1, 0,
589 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0,
593 c_val[0] = DETECT_PROTECT_REPORT;
594 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1,
598 c_val[0] = RTS_CTS_NONHT_PROT;
599 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
603 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0,
607 c_val[0] = MIMO_MODE;
608 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
612 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0,
617 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1,
627 void wilc1000_wlan_deinit(struct net_device *dev)
629 struct wilc_vif *vif;
632 vif = netdev_priv(dev);
636 netdev_err(dev, "wl is NULL\n");
640 if (wl->initialized) {
641 netdev_info(dev, "Deinitializing wilc1000...\n");
643 if (!wl->dev_irq_num &&
644 wl->hif_func->disable_interrupt) {
645 mutex_lock(&wl->hif_cs);
646 wl->hif_func->disable_interrupt(wl);
647 mutex_unlock(&wl->hif_cs);
650 complete(&wl->txq_event);
652 wlan_deinitialize_threads(dev);
656 wilc_wlan_cleanup(dev);
657 wlan_deinit_locks(dev);
659 wl->initialized = false;
661 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
663 netdev_dbg(dev, "wilc1000 is not initialized\n");
667 static int wlan_init_locks(struct net_device *dev)
669 struct wilc_vif *vif;
672 vif = netdev_priv(dev);
675 mutex_init(&wl->hif_cs);
676 mutex_init(&wl->rxq_cs);
678 spin_lock_init(&wl->txq_spinlock);
679 mutex_init(&wl->txq_add_to_head_cs);
681 init_completion(&wl->txq_event);
683 init_completion(&wl->cfg_event);
684 init_completion(&wl->sync_event);
685 init_completion(&wl->txq_thread_started);
690 static int wlan_deinit_locks(struct net_device *dev)
692 struct wilc_vif *vif;
695 vif = netdev_priv(dev);
699 mutex_destroy(&wilc->hif_cs);
702 mutex_destroy(&wilc->rxq_cs);
707 static int wlan_initialize_threads(struct net_device *dev)
709 struct wilc_vif *vif;
712 vif = netdev_priv(dev);
715 wilc->txq_thread = kthread_run(linux_wlan_txq_task, (void *)dev,
717 if (IS_ERR(wilc->txq_thread)) {
718 netdev_err(dev, "couldn't create TXQ thread\n");
720 return PTR_ERR(wilc->txq_thread);
722 wait_for_completion(&wilc->txq_thread_started);
727 static void wlan_deinitialize_threads(struct net_device *dev)
729 struct wilc_vif *vif;
732 vif = netdev_priv(dev);
738 complete(&wl->txq_event);
740 if (wl->txq_thread) {
741 kthread_stop(wl->txq_thread);
742 wl->txq_thread = NULL;
746 int wilc1000_wlan_init(struct net_device *dev, struct wilc_vif *vif)
749 struct wilc *wl = vif->wilc;
751 if (!wl->initialized) {
752 wl->mac_status = WILC_MAC_STATUS_INIT;
755 wlan_init_locks(dev);
757 ret = wilc_wlan_init(dev);
763 if (wl->gpio >= 0 && init_irq(dev)) {
768 ret = wlan_initialize_threads(dev);
771 goto _fail_wilc_wlan_;
774 if (!wl->dev_irq_num &&
775 wl->hif_func->enable_interrupt &&
776 wl->hif_func->enable_interrupt(wl)) {
778 goto _fail_irq_init_;
781 if (wilc_wlan_get_firmware(dev)) {
783 goto _fail_irq_enable_;
786 ret = wilc1000_firmware_download(dev);
789 goto _fail_irq_enable_;
792 ret = linux_wlan_start_firmware(dev);
795 goto _fail_irq_enable_;
798 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
800 char firmware_ver[20];
802 size = wilc_wlan_cfg_get_val(WID_FIRMWARE_VERSION,
804 sizeof(firmware_ver));
805 firmware_ver[size] = '\0';
806 netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
808 ret = linux_wlan_init_test_config(dev, vif);
811 netdev_err(dev, "Failed to configure firmware\n");
813 goto _fail_fw_start_;
816 wl->initialized = true;
823 if (!wl->dev_irq_num &&
824 wl->hif_func->disable_interrupt)
825 wl->hif_func->disable_interrupt(wl);
830 wlan_deinitialize_threads(dev);
832 wilc_wlan_cleanup(dev);
834 wlan_deinit_locks(dev);
835 netdev_err(dev, "WLAN Iinitialization FAILED\n");
837 netdev_dbg(dev, "wilc1000 already initialized\n");
842 static int mac_init_fn(struct net_device *ndev)
844 netif_start_queue(ndev);
845 netif_stop_queue(ndev);
850 int wilc_mac_open(struct net_device *ndev)
852 struct wilc_vif *vif;
854 unsigned char mac_add[ETH_ALEN] = {0};
859 vif = netdev_priv(ndev);
862 if (!wl || !wl->dev) {
863 netdev_err(ndev, "device not ready\n");
867 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
869 ret = wilc_init_host_int(ndev);
873 ret = wilc1000_wlan_init(ndev, vif);
875 wilc_deinit_host_int(ndev);
879 for (i = 0; i < wl->vif_num; i++) {
880 if (ndev == wl->vif[i]->ndev) {
881 if (vif->iftype == AP_MODE) {
882 wilc_set_wfi_drv_handler(vif,
883 wilc_get_vif_idx(vif),
885 } else if (!wilc_wlan_get_num_conn_ifcs(wl)) {
886 wilc_set_wfi_drv_handler(vif,
887 wilc_get_vif_idx(vif),
890 if (memcmp(wl->vif[i ^ 1]->bssid,
891 wl->vif[i ^ 1]->src_addr, 6))
892 wilc_set_wfi_drv_handler(vif,
893 wilc_get_vif_idx(vif),
896 wilc_set_wfi_drv_handler(vif,
897 wilc_get_vif_idx(vif),
900 wilc_set_operation_mode(vif, vif->iftype);
902 wilc_get_mac_address(vif, mac_add);
903 netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
904 memcpy(wl->vif[i]->src_addr, mac_add, ETH_ALEN);
910 memcpy(ndev->dev_addr, wl->vif[i]->src_addr, ETH_ALEN);
912 if (!is_valid_ether_addr(ndev->dev_addr)) {
913 netdev_err(ndev, "Wrong MAC address\n");
914 wilc_deinit_host_int(ndev);
915 wilc1000_wlan_deinit(ndev);
919 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
920 vif->ndev->ieee80211_ptr,
921 vif->frame_reg[0].type,
922 vif->frame_reg[0].reg);
923 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
924 vif->ndev->ieee80211_ptr,
925 vif->frame_reg[1].type,
926 vif->frame_reg[1].reg);
927 netif_wake_queue(ndev);
933 static struct net_device_stats *mac_stats(struct net_device *dev)
935 struct wilc_vif *vif = netdev_priv(dev);
937 return &vif->netstats;
940 static void wilc_set_multicast_list(struct net_device *dev)
942 struct netdev_hw_addr *ha;
943 struct wilc_vif *vif;
946 vif = netdev_priv(dev);
948 if (dev->flags & IFF_PROMISC)
951 if ((dev->flags & IFF_ALLMULTI) ||
952 (dev->mc.count) > WILC_MULTICAST_TABLE_SIZE) {
953 wilc_setup_multicast_filter(vif, false, 0);
957 if ((dev->mc.count) == 0) {
958 wilc_setup_multicast_filter(vif, true, 0);
962 netdev_for_each_mc_addr(ha, dev) {
963 memcpy(wilc_multicast_mac_addr_list[i], ha->addr, ETH_ALEN);
964 netdev_dbg(dev, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
965 wilc_multicast_mac_addr_list[i][0],
966 wilc_multicast_mac_addr_list[i][1],
967 wilc_multicast_mac_addr_list[i][2],
968 wilc_multicast_mac_addr_list[i][3],
969 wilc_multicast_mac_addr_list[i][4],
970 wilc_multicast_mac_addr_list[i][5]);
974 wilc_setup_multicast_filter(vif, true, (dev->mc.count));
977 static void linux_wlan_tx_complete(void *priv, int status)
979 struct tx_complete_data *pv_data = priv;
981 dev_kfree_skb(pv_data->skb);
985 int wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
987 struct wilc_vif *vif;
988 struct tx_complete_data *tx_data = NULL;
992 struct ethhdr *eth_h;
995 vif = netdev_priv(ndev);
998 if (skb->dev != ndev) {
999 netdev_err(ndev, "Packet not destined to this device\n");
1003 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
1006 netif_wake_queue(ndev);
1010 tx_data->buff = skb->data;
1011 tx_data->size = skb->len;
1014 eth_h = (struct ethhdr *)(skb->data);
1015 if (eth_h->h_proto == 0x8e88)
1016 netdev_dbg(ndev, "EAPOL transmitted\n");
1018 ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
1020 udp_buf = (char *)ih + sizeof(struct iphdr);
1021 if ((udp_buf[1] == 68 && udp_buf[3] == 67) ||
1022 (udp_buf[1] == 67 && udp_buf[3] == 68))
1023 netdev_dbg(ndev, "DHCP Message transmitted, type:%x %x %x\n",
1024 udp_buf[248], udp_buf[249], udp_buf[250]);
1026 vif->netstats.tx_packets++;
1027 vif->netstats.tx_bytes += tx_data->size;
1028 tx_data->bssid = wilc->vif[vif->idx]->bssid;
1029 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
1030 tx_data->buff, tx_data->size,
1031 linux_wlan_tx_complete);
1033 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
1034 netif_stop_queue(wilc->vif[0]->ndev);
1035 netif_stop_queue(wilc->vif[1]->ndev);
1041 int wilc_mac_close(struct net_device *ndev)
1043 struct wilc_priv *priv;
1044 struct wilc_vif *vif;
1045 struct host_if_drv *hif_drv;
1048 vif = netdev_priv(ndev);
1050 if (!vif || !vif->ndev || !vif->ndev->ieee80211_ptr ||
1051 !vif->ndev->ieee80211_ptr->wiphy)
1054 priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
1060 hif_drv = (struct host_if_drv *)priv->hif_drv;
1062 netdev_dbg(ndev, "Mac close\n");
1070 if ((wl->open_ifcs) > 0)
1076 netif_stop_queue(vif->ndev);
1078 wilc_deinit_host_int(vif->ndev);
1081 if (wl->open_ifcs == 0) {
1082 netdev_dbg(ndev, "Deinitializing wilc1000\n");
1084 wilc1000_wlan_deinit(ndev);
1085 WILC_WFI_deinit_mon_interface();
1088 vif->mac_opened = 0;
1093 static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1097 u32 size = 0, length = 0;
1098 struct wilc_vif *vif;
1102 vif = netdev_priv(ndev);
1105 if (!wilc->initialized)
1111 struct iwreq *wrq = (struct iwreq *)req;
1113 size = wrq->u.data.length;
1115 if (size && wrq->u.data.pointer) {
1116 buff = memdup_user(wrq->u.data.pointer,
1117 wrq->u.data.length);
1119 return PTR_ERR(buff);
1121 if (strncasecmp(buff, "RSSI", length) == 0) {
1122 ret = wilc_get_rssi(vif, &rssi);
1123 netdev_info(ndev, "RSSI :%d\n", rssi);
1127 snprintf(buff, size, "rssi %d", rssi);
1129 if (copy_to_user(wrq->u.data.pointer, buff, size)) {
1130 netdev_err(ndev, "failed to copy\n");
1141 netdev_info(ndev, "Command - %d - has been received\n", cmd);
1154 void wilc_frmw_to_linux(struct wilc *wilc, u8 *buff, u32 size, u32 pkt_offset)
1156 unsigned int frame_len = 0;
1158 unsigned char *buff_to_send = NULL;
1159 struct sk_buff *skb;
1160 struct net_device *wilc_netdev;
1161 struct wilc_vif *vif;
1166 wilc_netdev = get_if_handler(wilc, buff);
1171 vif = netdev_priv(wilc_netdev);
1175 buff_to_send = buff;
1177 skb = dev_alloc_skb(frame_len);
1181 skb->dev = wilc_netdev;
1183 memcpy(skb_put(skb, frame_len), buff_to_send, frame_len);
1185 skb->protocol = eth_type_trans(skb, wilc_netdev);
1186 vif->netstats.rx_packets++;
1187 vif->netstats.rx_bytes += frame_len;
1188 skb->ip_summed = CHECKSUM_UNNECESSARY;
1189 stats = netif_rx(skb);
1190 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
1194 void WILC_WFI_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
1197 struct wilc_vif *vif;
1199 for (i = 0; i < wilc->vif_num; i++) {
1200 vif = netdev_priv(wilc->vif[i]->ndev);
1201 if (vif->monitor_flag) {
1202 WILC_WFI_monitor_rx(buff, size);
1207 vif = netdev_priv(wilc->vif[1]->ndev);
1208 if ((buff[0] == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
1209 (buff[0] == vif->frame_reg[1].type && vif->frame_reg[1].reg))
1210 WILC_WFI_p2p_rx(wilc->vif[1]->ndev, buff, size);
1213 void wilc_netdev_cleanup(struct wilc *wilc)
1216 struct wilc_vif *vif[NUM_CONCURRENT_IFC];
1218 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1219 unregister_inetaddr_notifier(&g_dev_notifier);
1221 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1222 vif[i] = netdev_priv(wilc->vif[i]->ndev);
1225 if (wilc && wilc->firmware) {
1226 release_firmware(wilc->firmware);
1227 wilc->firmware = NULL;
1230 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1231 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1232 if (wilc->vif[i]->ndev)
1233 if (vif[i]->mac_opened)
1234 wilc_mac_close(wilc->vif[i]->ndev);
1236 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1237 unregister_netdev(wilc->vif[i]->ndev);
1238 wilc_free_wiphy(wilc->vif[i]->ndev);
1239 free_netdev(wilc->vif[i]->ndev);
1245 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
1247 int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
1248 int gpio, const struct wilc_hif_func *ops)
1251 struct wilc_vif *vif;
1252 struct net_device *ndev;
1255 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
1260 wl->io_type = io_type;
1264 register_inetaddr_notifier(&g_dev_notifier);
1266 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1267 ndev = alloc_etherdev(sizeof(struct wilc_vif));
1271 vif = netdev_priv(ndev);
1272 memset(vif, 0, sizeof(struct wilc_vif));
1275 strcpy(ndev->name, "wlan%d");
1277 strcpy(ndev->name, "p2p%d");
1279 vif->idx = wl->vif_num;
1282 wl->vif[wl->vif_num]->ndev = ndev;
1284 ndev->netdev_ops = &wilc_netdev_ops;
1287 struct wireless_dev *wdev;
1289 wdev = wilc_create_wiphy(ndev, dev);
1292 SET_NETDEV_DEV(ndev, dev);
1295 netdev_err(ndev, "Can't register WILC Wiphy\n");
1299 vif->ndev->ieee80211_ptr = wdev;
1300 vif->ndev->ml_priv = vif;
1301 wdev->netdev = vif->ndev;
1302 vif->netstats.rx_packets = 0;
1303 vif->netstats.tx_packets = 0;
1304 vif->netstats.rx_bytes = 0;
1305 vif->netstats.tx_bytes = 0;
1308 ret = register_netdev(ndev);
1312 vif->iftype = STATION_MODE;
1313 vif->mac_opened = 0;
1318 EXPORT_SYMBOL_GPL(wilc_netdev_init);
1320 MODULE_LICENSE("GPL");