1 #include "wilc_wfi_cfgoperations.h"
2 #include "linux_wlan_common.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>
16 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/netdevice.h>
20 #include <linux/inetdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/skbuff.h>
26 #include <linux/semaphore.h>
28 static int dev_state_ev_handler(struct notifier_block *this,
29 unsigned long event, void *ptr);
31 static struct notifier_block g_dev_notifier = {
32 .notifier_call = dev_state_ev_handler
37 static struct semaphore close_exit_sync;
39 static int wlan_deinit_locks(struct net_device *dev);
40 static void wlan_deinitialize_threads(struct net_device *dev);
42 static void linux_wlan_tx_complete(void *priv, int status);
43 static int mac_init_fn(struct net_device *ndev);
44 static struct net_device_stats *mac_stats(struct net_device *dev);
45 static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd);
46 static void wilc_set_multicast_list(struct net_device *dev);
48 bool wilc_enable_ps = true;
50 static const struct net_device_ops wilc_netdev_ops = {
51 .ndo_init = mac_init_fn,
52 .ndo_open = wilc_mac_open,
53 .ndo_stop = wilc_mac_close,
54 .ndo_start_xmit = wilc_mac_xmit,
55 .ndo_do_ioctl = mac_ioctl,
56 .ndo_get_stats = mac_stats,
57 .ndo_set_rx_mode = wilc_set_multicast_list,
61 static int dev_state_ev_handler(struct notifier_block *this,
62 unsigned long event, void *ptr)
64 struct in_ifaddr *dev_iface = ptr;
65 struct wilc_priv *priv;
66 struct host_if_drv *hif_drv;
67 struct net_device *dev;
71 char wlan_dev_name[5] = "wlan0";
73 if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev)
76 if (memcmp(dev_iface->ifa_label, "wlan0", 5) &&
77 memcmp(dev_iface->ifa_label, "p2p0", 4))
80 dev = (struct net_device *)dev_iface->ifa_dev->dev;
81 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
84 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
88 hif_drv = (struct host_if_drv *)priv->hif_drv;
89 vif = netdev_priv(dev);
95 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
97 wilc_optaining_ip = false;
98 del_timer(&wilc_during_ip_timer);
102 wilc_set_power_mgmt(vif, 1, 0);
104 netdev_dbg(dev, "[%s] Up IP\n", dev_iface->ifa_label);
106 ip_addr_buf = (char *)&dev_iface->ifa_address;
107 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
108 ip_addr_buf[0], ip_addr_buf[1],
109 ip_addr_buf[2], ip_addr_buf[3]);
110 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
115 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
117 wilc_optaining_ip = false;
120 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
121 wilc_set_power_mgmt(vif, 0, 0);
123 wilc_resolve_disconnect_aberration(vif);
125 netdev_dbg(dev, "[%s] Down IP\n", dev_iface->ifa_label);
127 ip_addr_buf = null_ip;
128 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
129 ip_addr_buf[0], ip_addr_buf[1],
130 ip_addr_buf[2], ip_addr_buf[3]);
132 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
143 static irqreturn_t isr_uh_routine(int irq, void *user_data)
145 struct wilc_vif *vif;
147 struct net_device *dev = user_data;
149 vif = netdev_priv(dev);
153 netdev_err(dev, "Can't handle UH interrupt\n");
156 return IRQ_WAKE_THREAD;
159 static irqreturn_t isr_bh_routine(int irq, void *userdata)
161 struct wilc_vif *vif;
163 struct net_device *dev = userdata;
165 vif = netdev_priv(userdata);
169 netdev_err(dev, "Can't handle BH interrupt\n");
173 wilc_handle_isr(wilc);
178 static int init_irq(struct net_device *dev)
181 struct wilc_vif *vif;
184 vif = netdev_priv(dev);
187 if ((gpio_request(wl->gpio, "WILC_INTR") == 0) &&
188 (gpio_direction_input(wl->gpio) == 0)) {
189 wl->dev_irq_num = gpio_to_irq(wl->gpio);
192 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
195 if (ret != -1 && request_threaded_irq(wl->dev_irq_num,
198 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
199 "WILC_IRQ", dev) < 0) {
200 netdev_err(dev, "Failed to request IRQ GPIO: %d\n", wl->gpio);
205 "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
206 wl->dev_irq_num, wl->gpio);
212 static void deinit_irq(struct net_device *dev)
214 struct wilc_vif *vif;
217 vif = netdev_priv(dev);
220 /* Deintialize IRQ */
221 if (wilc->dev_irq_num) {
222 free_irq(wilc->dev_irq_num, wilc);
223 gpio_free(wilc->gpio);
227 int wilc_lock_timeout(struct wilc *nic, void *vp, u32 timeout)
229 /* FIXME: replace with mutex_lock or wait_for_completion */
233 error = down_timeout(vp,
234 msecs_to_jiffies(timeout));
238 void wilc_mac_indicate(struct wilc *wilc, int flag)
242 if (flag == WILC_MAC_INDICATE_STATUS) {
243 wilc_wlan_cfg_get_val(WID_STATUS,
244 (unsigned char *)&status, 4);
245 if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
246 wilc->mac_status = status;
247 up(&wilc->sync_event);
249 wilc->mac_status = status;
254 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
259 bssid = mac_header + 10;
260 bssid1 = mac_header + 4;
262 for (i = 0; i < wilc->vif_num; i++) {
263 if (wilc->vif[i]->mode == STATION_MODE)
264 if (!memcmp(bssid, wilc->vif[i]->bssid, ETH_ALEN))
265 return wilc->vif[i]->ndev;
266 if (wilc->vif[i]->mode == AP_MODE)
267 if (!memcmp(bssid1, wilc->vif[i]->bssid, ETH_ALEN))
268 return wilc->vif[i]->ndev;
274 int wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
278 struct wilc_vif *vif;
281 vif = netdev_priv(wilc_netdev);
284 for (i = 0; i < wilc->vif_num; i++)
285 if (wilc->vif[i]->ndev == wilc_netdev) {
286 memcpy(wilc->vif[i]->bssid, bssid, 6);
287 wilc->vif[i]->mode = mode;
295 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
298 u8 null_bssid[6] = {0};
301 for (i = 0; i < wilc->vif_num; i++)
302 if (memcmp(wilc->vif[i]->bssid, null_bssid, 6))
308 #define USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
310 static int linux_wlan_txq_task(void *vp)
313 struct wilc_vif *vif;
315 struct net_device *dev = vp;
316 #if defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
317 #define TX_BACKOFF_WEIGHT_INCR_STEP (1)
318 #define TX_BACKOFF_WEIGHT_DECR_STEP (1)
319 #define TX_BACKOFF_WEIGHT_MAX (7)
320 #define TX_BACKOFF_WEIGHT_MIN (0)
321 #define TX_BACKOFF_WEIGHT_UNIT_MS (10)
322 int backoff_weight = TX_BACKOFF_WEIGHT_MIN;
325 vif = netdev_priv(dev);
328 up(&wl->txq_thread_started);
330 down(&wl->txq_event);
333 up(&wl->txq_thread_started);
335 while (!kthread_should_stop())
339 #if !defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
340 ret = wilc_wlan_handle_txq(dev, &txq_count);
343 ret = wilc_wlan_handle_txq(dev, &txq_count);
344 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
345 if (netif_queue_stopped(wl->vif[0]->ndev))
346 netif_wake_queue(wl->vif[0]->ndev);
347 if (netif_queue_stopped(wl->vif[1]->ndev))
348 netif_wake_queue(wl->vif[1]->ndev);
351 if (ret == WILC_TX_ERR_NO_BUF) {
352 backoff_weight += TX_BACKOFF_WEIGHT_INCR_STEP;
353 if (backoff_weight > TX_BACKOFF_WEIGHT_MAX)
354 backoff_weight = TX_BACKOFF_WEIGHT_MAX;
356 if (backoff_weight > TX_BACKOFF_WEIGHT_MIN) {
357 backoff_weight -= TX_BACKOFF_WEIGHT_DECR_STEP;
358 if (backoff_weight < TX_BACKOFF_WEIGHT_MIN)
359 backoff_weight = TX_BACKOFF_WEIGHT_MIN;
362 } while (ret == WILC_TX_ERR_NO_BUF && !wl->close);
368 int wilc_wlan_get_firmware(struct net_device *dev)
370 struct wilc_vif *vif;
372 int chip_id, ret = 0;
373 const struct firmware *wilc_firmware;
376 vif = netdev_priv(dev);
379 chip_id = wilc_get_chipid(wilc, false);
381 if (chip_id < 0x1003a0)
382 firmware = FIRMWARE_1002;
384 firmware = FIRMWARE_1003;
386 netdev_info(dev, "loading firmware %s\n", firmware);
388 if (!(&vif->ndev->dev))
391 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
392 netdev_err(dev, "%s - firmare not available\n", firmware);
396 wilc->firmware = wilc_firmware;
403 static int linux_wlan_start_firmware(struct net_device *dev)
405 struct wilc_vif *vif;
409 vif = netdev_priv(dev);
412 ret = wilc_wlan_start(wilc);
416 ret = wilc_lock_timeout(wilc, &wilc->sync_event, 5000);
423 static int wilc1000_firmware_download(struct net_device *dev)
425 struct wilc_vif *vif;
429 vif = netdev_priv(dev);
432 if (!wilc->firmware) {
433 netdev_err(dev, "Firmware buffer is NULL\n");
437 ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
438 wilc->firmware->size);
442 release_firmware(wilc->firmware);
443 wilc->firmware = NULL;
445 netdev_dbg(dev, "Download Succeeded\n");
450 static int linux_wlan_init_test_config(struct net_device *dev,
451 struct wilc_vif *vif)
453 unsigned char c_val[64];
454 unsigned char mac_add[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xff};
455 struct wilc *wilc = vif->wilc;
456 struct wilc_priv *priv;
457 struct host_if_drv *hif_drv;
459 netdev_dbg(dev, "Start configuring Firmware\n");
460 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
461 hif_drv = (struct host_if_drv *)priv->hif_drv;
462 netdev_dbg(dev, "Host = %p\n", hif_drv);
463 wilc_get_mac_address(vif, mac_add);
465 netdev_dbg(dev, "MAC address is : %pM\n", mac_add);
466 wilc_get_chipid(wilc, false);
470 if (!wilc_wlan_cfg_set(vif, 1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
474 if (!wilc_wlan_cfg_set(vif, 0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
477 c_val[0] = INFRASTRUCTURE;
478 if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, c_val, 1, 0, 0))
481 c_val[0] = RATE_AUTO;
482 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
485 c_val[0] = G_MIXED_11B_2_MODE;
486 if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, c_val, 1, 0,
491 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
494 c_val[0] = G_SHORT_PREAMBLE;
495 if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, c_val, 1, 0, 0))
498 c_val[0] = AUTO_PROT;
499 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
502 c_val[0] = ACTIVE_SCAN;
503 if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, c_val, 1, 0, 0))
506 c_val[0] = SITE_SURVEY_OFF;
507 if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, c_val, 1, 0, 0))
510 *((int *)c_val) = 0xffff;
511 if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
514 *((int *)c_val) = 2346;
515 if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
519 if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, c_val, 1, 0, 0))
523 if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, c_val, 1, 0, 0))
526 c_val[0] = NO_POWERSAVE;
527 if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
530 c_val[0] = NO_SECURITY; /* NO_ENCRYPT, 0x79 */
531 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, c_val, 1, 0, 0))
534 c_val[0] = OPEN_SYSTEM;
535 if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, c_val, 1, 0, 0))
538 strcpy(c_val, "123456790abcdef1234567890");
539 if (!wilc_wlan_cfg_set(vif, 0, WID_WEP_KEY_VALUE, c_val,
540 (strlen(c_val) + 1), 0, 0))
543 strcpy(c_val, "12345678");
544 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_PSK, c_val, (strlen(c_val)), 0,
548 strcpy(c_val, "password");
549 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_KEY, c_val, (strlen(c_val) + 1),
557 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
561 if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
565 if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
568 c_val[0] = NORMAL_ACK;
569 if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, c_val, 1, 0, 0))
573 if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1,
578 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0,
583 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0,
587 *((int *)c_val) = 100;
588 if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
591 c_val[0] = REKEY_DISABLE;
592 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, c_val, 1, 0, 0))
595 *((int *)c_val) = 84600;
596 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
599 *((int *)c_val) = 500;
600 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, c_val, 4, 0,
605 if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0,
609 c_val[0] = G_SELF_CTS_PROT;
610 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
614 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, c_val, 1, 0, 0))
617 c_val[0] = HT_MIXED_MODE;
618 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, c_val, 1, 0,
623 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0,
627 memcpy(c_val, mac_add, 6);
629 if (!wilc_wlan_cfg_set(vif, 0, WID_MAC_ADDR, c_val, 6, 0, 0))
632 c_val[0] = DETECT_PROTECT_REPORT;
633 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1,
637 c_val[0] = RTS_CTS_NONHT_PROT;
638 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
642 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0,
646 c_val[0] = MIMO_MODE;
647 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
651 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0,
656 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1,
666 void wilc1000_wlan_deinit(struct net_device *dev)
668 struct wilc_vif *vif;
671 vif = netdev_priv(dev);
675 netdev_err(dev, "wl is NULL\n");
679 if (wl->initialized) {
680 netdev_info(dev, "Deinitializing wilc1000...\n");
682 if (!wl->dev_irq_num &&
683 wl->hif_func->disable_interrupt) {
684 mutex_lock(&wl->hif_cs);
685 wl->hif_func->disable_interrupt(wl);
686 mutex_unlock(&wl->hif_cs);
691 wlan_deinitialize_threads(dev);
695 wilc_wlan_cleanup(dev);
696 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
697 if (!wl->dev_irq_num &&
698 wl->hif_func->disable_interrupt) {
699 mutex_lock(&wl->hif_cs);
700 wl->hif_func->disable_interrupt(wl);
701 mutex_unlock(&wl->hif_cs);
704 wlan_deinit_locks(dev);
706 wl->initialized = false;
708 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
710 netdev_dbg(dev, "wilc1000 is not initialized\n");
714 static int wlan_init_locks(struct net_device *dev)
716 struct wilc_vif *vif;
719 vif = netdev_priv(dev);
722 mutex_init(&wl->hif_cs);
723 mutex_init(&wl->rxq_cs);
725 spin_lock_init(&wl->txq_spinlock);
726 sema_init(&wl->txq_add_to_head_cs, 1);
728 sema_init(&wl->txq_event, 0);
730 sema_init(&wl->cfg_event, 0);
731 sema_init(&wl->sync_event, 0);
733 sema_init(&wl->txq_thread_started, 0);
738 static int wlan_deinit_locks(struct net_device *dev)
740 struct wilc_vif *vif;
743 vif = netdev_priv(dev);
747 mutex_destroy(&wilc->hif_cs);
750 mutex_destroy(&wilc->rxq_cs);
755 static int wlan_initialize_threads(struct net_device *dev)
757 struct wilc_vif *vif;
760 vif = netdev_priv(dev);
763 wilc->txq_thread = kthread_run(linux_wlan_txq_task, (void *)dev,
765 if (!wilc->txq_thread) {
766 netdev_err(dev, "couldn't create TXQ thread\n");
770 down(&wilc->txq_thread_started);
775 static void wlan_deinitialize_threads(struct net_device *dev)
777 struct wilc_vif *vif;
780 vif = netdev_priv(dev);
788 if (wl->txq_thread) {
789 kthread_stop(wl->txq_thread);
790 wl->txq_thread = NULL;
794 int wilc1000_wlan_init(struct net_device *dev, struct wilc_vif *vif)
797 struct wilc *wl = vif->wilc;
799 if (!wl->initialized) {
800 wl->mac_status = WILC_MAC_STATUS_INIT;
803 wlan_init_locks(dev);
805 ret = wilc_wlan_init(dev);
811 if (wl->gpio >= 0 && init_irq(dev)) {
816 ret = wlan_initialize_threads(dev);
819 goto _fail_wilc_wlan_;
822 if (!wl->dev_irq_num &&
823 wl->hif_func->enable_interrupt &&
824 wl->hif_func->enable_interrupt(wl)) {
826 goto _fail_irq_init_;
829 if (wilc_wlan_get_firmware(dev)) {
831 goto _fail_irq_enable_;
834 ret = wilc1000_firmware_download(dev);
837 goto _fail_irq_enable_;
840 ret = linux_wlan_start_firmware(dev);
843 goto _fail_irq_enable_;
846 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
848 char firmware_ver[20];
850 size = wilc_wlan_cfg_get_val(WID_FIRMWARE_VERSION,
852 sizeof(firmware_ver));
853 firmware_ver[size] = '\0';
854 netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
856 ret = linux_wlan_init_test_config(dev, vif);
859 netdev_err(dev, "Failed to configure firmware\n");
861 goto _fail_fw_start_;
864 wl->initialized = true;
871 if (!wl->dev_irq_num &&
872 wl->hif_func->disable_interrupt)
873 wl->hif_func->disable_interrupt(wl);
878 wlan_deinitialize_threads(dev);
880 wilc_wlan_cleanup(dev);
882 wlan_deinit_locks(dev);
883 netdev_err(dev, "WLAN Iinitialization FAILED\n");
885 netdev_dbg(dev, "wilc1000 already initialized\n");
890 static int mac_init_fn(struct net_device *ndev)
892 netif_start_queue(ndev);
893 netif_stop_queue(ndev);
898 int wilc_mac_open(struct net_device *ndev)
900 struct wilc_vif *vif;
903 unsigned char mac_add[ETH_ALEN] = {0};
906 struct wilc_priv *priv;
909 vif = netdev_priv(ndev);
912 if (!wl || !wl->dev) {
913 netdev_err(ndev, "wilc1000: SPI device not ready\n");
917 vif = netdev_priv(ndev);
919 priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
920 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
922 ret = wilc_init_host_int(ndev);
926 ret = wilc1000_wlan_init(ndev, vif);
928 wilc_deinit_host_int(ndev);
932 for (i = 0; i < wl->vif_num; i++) {
933 if (ndev == wl->vif[i]->ndev) {
934 if (vif->iftype == AP_MODE) {
935 wilc_set_wfi_drv_handler(vif,
936 wilc_get_vif_idx(vif),
938 } else if (!wilc_wlan_get_num_conn_ifcs(wilc)) {
939 wilc_set_wfi_drv_handler(vif,
940 wilc_get_vif_idx(vif),
943 if (memcmp(wilc->vif[i ^ 1]->bssid,
944 wilc->vif[i ^ 1]->src_addr, 6))
945 wilc_set_wfi_drv_handler(vif,
946 wilc_get_vif_idx(vif),
949 wilc_set_wfi_drv_handler(vif,
950 wilc_get_vif_idx(vif),
953 wilc_set_operation_mode(vif, vif->iftype);
955 wilc_get_mac_address(vif, mac_add);
956 netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
957 memcpy(wl->vif[i]->src_addr, mac_add, ETH_ALEN);
963 memcpy(ndev->dev_addr, wl->vif[i]->src_addr, ETH_ALEN);
965 if (!is_valid_ether_addr(ndev->dev_addr)) {
966 netdev_err(ndev, "Wrong MAC address\n");
967 wilc_deinit_host_int(ndev);
968 wilc1000_wlan_deinit(ndev);
972 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
973 vif->ndev->ieee80211_ptr,
974 vif->g_struct_frame_reg[0].frame_type,
975 vif->g_struct_frame_reg[0].reg);
976 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
977 vif->ndev->ieee80211_ptr,
978 vif->g_struct_frame_reg[1].frame_type,
979 vif->g_struct_frame_reg[1].reg);
980 netif_wake_queue(ndev);
986 static struct net_device_stats *mac_stats(struct net_device *dev)
988 struct wilc_vif *vif = netdev_priv(dev);
990 return &vif->netstats;
993 static void wilc_set_multicast_list(struct net_device *dev)
995 struct netdev_hw_addr *ha;
996 struct wilc_priv *priv;
997 struct host_if_drv *hif_drv;
998 struct wilc_vif *vif;
1001 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
1002 vif = netdev_priv(dev);
1003 hif_drv = (struct host_if_drv *)priv->hif_drv;
1005 if (dev->flags & IFF_PROMISC)
1008 if ((dev->flags & IFF_ALLMULTI) ||
1009 (dev->mc.count) > WILC_MULTICAST_TABLE_SIZE) {
1010 wilc_setup_multicast_filter(vif, false, 0);
1014 if ((dev->mc.count) == 0) {
1015 wilc_setup_multicast_filter(vif, true, 0);
1019 netdev_for_each_mc_addr(ha, dev) {
1020 memcpy(wilc_multicast_mac_addr_list[i], ha->addr, ETH_ALEN);
1021 netdev_dbg(dev, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
1022 wilc_multicast_mac_addr_list[i][0],
1023 wilc_multicast_mac_addr_list[i][1],
1024 wilc_multicast_mac_addr_list[i][2],
1025 wilc_multicast_mac_addr_list[i][3],
1026 wilc_multicast_mac_addr_list[i][4],
1027 wilc_multicast_mac_addr_list[i][5]);
1031 wilc_setup_multicast_filter(vif, true, (dev->mc.count));
1034 static void linux_wlan_tx_complete(void *priv, int status)
1036 struct tx_complete_data *pv_data = priv;
1038 dev_kfree_skb(pv_data->skb);
1042 int wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
1044 struct wilc_vif *vif;
1045 struct tx_complete_data *tx_data = NULL;
1049 struct ethhdr *eth_h;
1052 vif = netdev_priv(ndev);
1055 if (skb->dev != ndev) {
1056 netdev_err(ndev, "Packet not destined to this device\n");
1060 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
1063 netif_wake_queue(ndev);
1067 tx_data->buff = skb->data;
1068 tx_data->size = skb->len;
1071 eth_h = (struct ethhdr *)(skb->data);
1072 if (eth_h->h_proto == 0x8e88)
1073 netdev_dbg(ndev, "EAPOL transmitted\n");
1075 ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
1077 udp_buf = (char *)ih + sizeof(struct iphdr);
1078 if ((udp_buf[1] == 68 && udp_buf[3] == 67) ||
1079 (udp_buf[1] == 67 && udp_buf[3] == 68))
1080 netdev_dbg(ndev, "DHCP Message transmitted, type:%x %x %x\n",
1081 udp_buf[248], udp_buf[249], udp_buf[250]);
1083 vif->netstats.tx_packets++;
1084 vif->netstats.tx_bytes += tx_data->size;
1085 tx_data->bssid = wilc->vif[vif->idx]->bssid;
1086 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
1087 tx_data->buff, tx_data->size,
1088 linux_wlan_tx_complete);
1090 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
1091 netif_stop_queue(wilc->vif[0]->ndev);
1092 netif_stop_queue(wilc->vif[1]->ndev);
1098 int wilc_mac_close(struct net_device *ndev)
1100 struct wilc_priv *priv;
1101 struct wilc_vif *vif;
1102 struct host_if_drv *hif_drv;
1105 vif = netdev_priv(ndev);
1107 if (!vif || !vif->ndev || !vif->ndev->ieee80211_ptr ||
1108 !vif->ndev->ieee80211_ptr->wiphy)
1111 priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
1117 hif_drv = (struct host_if_drv *)priv->hif_drv;
1119 netdev_dbg(ndev, "Mac close\n");
1127 if ((wl->open_ifcs) > 0)
1133 netif_stop_queue(vif->ndev);
1135 wilc_deinit_host_int(vif->ndev);
1138 if (wl->open_ifcs == 0) {
1139 netdev_dbg(ndev, "Deinitializing wilc1000\n");
1141 wilc1000_wlan_deinit(ndev);
1142 WILC_WFI_deinit_mon_interface();
1145 up(&close_exit_sync);
1146 vif->mac_opened = 0;
1151 static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1155 u32 size = 0, length = 0;
1156 struct wilc_vif *vif;
1157 struct wilc_priv *priv;
1161 vif = netdev_priv(ndev);
1164 if (!wilc->initialized)
1170 struct iwreq *wrq = (struct iwreq *)req;
1172 size = wrq->u.data.length;
1174 if (size && wrq->u.data.pointer) {
1175 buff = memdup_user(wrq->u.data.pointer,
1176 wrq->u.data.length);
1178 return PTR_ERR(buff);
1180 if (strncasecmp(buff, "RSSI", length) == 0) {
1181 priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
1182 ret = wilc_get_rssi(vif, &rssi);
1183 netdev_info(ndev, "RSSI :%d\n", rssi);
1187 snprintf(buff, size, "rssi %d", rssi);
1189 if (copy_to_user(wrq->u.data.pointer, buff, size)) {
1190 netdev_err(ndev, "failed to copy\n");
1201 netdev_info(ndev, "Command - %d - has been received\n", cmd);
1214 void wilc_frmw_to_linux(struct wilc *wilc, u8 *buff, u32 size, u32 pkt_offset)
1216 unsigned int frame_len = 0;
1218 unsigned char *buff_to_send = NULL;
1219 struct sk_buff *skb;
1220 struct net_device *wilc_netdev;
1221 struct wilc_vif *vif;
1226 wilc_netdev = get_if_handler(wilc, buff);
1231 vif = netdev_priv(wilc_netdev);
1235 buff_to_send = buff;
1237 skb = dev_alloc_skb(frame_len);
1241 skb->dev = wilc_netdev;
1243 memcpy(skb_put(skb, frame_len), buff_to_send, frame_len);
1245 skb->protocol = eth_type_trans(skb, wilc_netdev);
1246 vif->netstats.rx_packets++;
1247 vif->netstats.rx_bytes += frame_len;
1248 skb->ip_summed = CHECKSUM_UNNECESSARY;
1249 stats = netif_rx(skb);
1250 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
1254 void WILC_WFI_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
1257 struct wilc_vif *vif;
1259 for (i = 0; i < wilc->vif_num; i++) {
1260 vif = netdev_priv(wilc->vif[i]->ndev);
1261 if (vif->monitor_flag) {
1262 WILC_WFI_monitor_rx(buff, size);
1267 vif = netdev_priv(wilc->vif[1]->ndev);
1268 if ((buff[0] == vif->g_struct_frame_reg[0].frame_type && vif->g_struct_frame_reg[0].reg) ||
1269 (buff[0] == vif->g_struct_frame_reg[1].frame_type && vif->g_struct_frame_reg[1].reg))
1270 WILC_WFI_p2p_rx(wilc->vif[1]->ndev, buff, size);
1273 void wilc_netdev_cleanup(struct wilc *wilc)
1276 struct wilc_vif *vif[NUM_CONCURRENT_IFC];
1278 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1279 unregister_inetaddr_notifier(&g_dev_notifier);
1281 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1282 vif[i] = netdev_priv(wilc->vif[i]->ndev);
1285 if (wilc && wilc->firmware)
1286 release_firmware(wilc->firmware);
1288 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1289 wilc_lock_timeout(wilc, &close_exit_sync, 5 * 1000);
1291 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1292 if (wilc->vif[i]->ndev)
1293 if (vif[i]->mac_opened)
1294 wilc_mac_close(wilc->vif[i]->ndev);
1296 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1297 unregister_netdev(wilc->vif[i]->ndev);
1298 wilc_free_wiphy(wilc->vif[i]->ndev);
1299 free_netdev(wilc->vif[i]->ndev);
1305 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
1307 int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
1308 int gpio, const struct wilc_hif_func *ops)
1311 struct wilc_vif *vif;
1312 struct net_device *ndev;
1315 sema_init(&close_exit_sync, 0);
1317 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
1322 wl->io_type = io_type;
1326 register_inetaddr_notifier(&g_dev_notifier);
1328 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1329 ndev = alloc_etherdev(sizeof(struct wilc_vif));
1333 vif = netdev_priv(ndev);
1334 memset(vif, 0, sizeof(struct wilc_vif));
1337 strcpy(ndev->name, "wlan%d");
1339 strcpy(ndev->name, "p2p%d");
1341 vif->idx = wl->vif_num;
1344 wl->vif[wl->vif_num]->ndev = ndev;
1346 ndev->netdev_ops = &wilc_netdev_ops;
1349 struct wireless_dev *wdev;
1351 wdev = wilc_create_wiphy(ndev, dev);
1354 SET_NETDEV_DEV(ndev, dev);
1357 netdev_err(ndev, "Can't register WILC Wiphy\n");
1361 vif->ndev->ieee80211_ptr = wdev;
1362 vif->ndev->ml_priv = vif;
1363 wdev->netdev = vif->ndev;
1364 vif->netstats.rx_packets = 0;
1365 vif->netstats.tx_packets = 0;
1366 vif->netstats.rx_bytes = 0;
1367 vif->netstats.tx_bytes = 0;
1370 ret = register_netdev(ndev);
1374 vif->iftype = STATION_MODE;
1375 vif->mac_opened = 0;
1380 EXPORT_SYMBOL_GPL(wilc_netdev_init);
1382 MODULE_LICENSE("GPL");