blob: faad01f6f2d115f94ac8bf720d85c92c21eec33d [file] [log] [blame]
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001#include "wilc_wfi_cfgoperations.h"
2#include "linux_wlan_common.h"
3#include "wilc_wlan_if.h"
4#include "wilc_wlan.h"
Johnny Kimc5c77ba2015-05-11 14:30:56 +09005
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>
Guenter Roeckf1a99832015-05-27 13:02:16 -070012#include <linux/gpio.h>
Johnny Kimc5c77ba2015-05-11 14:30:56 +090013
14#include <linux/kthread.h>
15#include <linux/firmware.h>
16#include <linux/delay.h>
17
18#include <linux/init.h>
19#include <linux/netdevice.h>
Johnny Kimc5c77ba2015-05-11 14:30:56 +090020#include <linux/inetdevice.h>
Johnny Kimc5c77ba2015-05-11 14:30:56 +090021#include <linux/etherdevice.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/skbuff.h>
25
Johnny Kimc5c77ba2015-05-11 14:30:56 +090026#include <linux/semaphore.h>
27
28#ifdef WILC_SDIO
29#include "linux_wlan_sdio.h"
30#else
31#include "linux_wlan_spi.h"
32#endif
33
Johnny Kimc5c77ba2015-05-11 14:30:56 +090034static int dev_state_ev_handler(struct notifier_block *this, unsigned long event, void *ptr);
35
36static struct notifier_block g_dev_notifier = {
37 .notifier_call = dev_state_ev_handler
38};
Johnny Kimc5c77ba2015-05-11 14:30:56 +090039
Johnny Kimc5c77ba2015-05-11 14:30:56 +090040#define IRQ_WAIT 1
41#define IRQ_NO_WAIT 0
Johnny Kimc5c77ba2015-05-11 14:30:56 +090042static struct semaphore close_exit_sync;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090043
Glen Lee7c67c052015-10-27 18:27:50 +090044static int wlan_deinit_locks(struct net_device *dev);
Glen Lee32dd51b2015-10-27 18:27:52 +090045static void wlan_deinitialize_threads(struct net_device *dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090046
Johnny Kimc5c77ba2015-05-11 14:30:56 +090047static void linux_wlan_tx_complete(void *priv, int status);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090048static int mac_init_fn(struct net_device *ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090049static struct net_device_stats *mac_stats(struct net_device *dev);
50static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd);
51static void wilc_set_multicast_list(struct net_device *dev);
Arnd Bergmann0e1af732015-11-16 15:04:54 +010052struct wilc *wilc_dev;
53bool wilc_enable_ps = true;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090054
Johnny Kimc5c77ba2015-05-11 14:30:56 +090055static const struct net_device_ops wilc_netdev_ops = {
56 .ndo_init = mac_init_fn,
Arnd Bergmann0e1af732015-11-16 15:04:54 +010057 .ndo_open = wilc_mac_open,
58 .ndo_stop = wilc_mac_close,
59 .ndo_start_xmit = wilc_mac_xmit,
Johnny Kimc5c77ba2015-05-11 14:30:56 +090060 .ndo_do_ioctl = mac_ioctl,
61 .ndo_get_stats = mac_stats,
62 .ndo_set_rx_mode = wilc_set_multicast_list,
63
64};
Johnny Kimc5c77ba2015-05-11 14:30:56 +090065
Johnny Kimc5c77ba2015-05-11 14:30:56 +090066static int dev_state_ev_handler(struct notifier_block *this, unsigned long event, void *ptr)
67{
68 struct in_ifaddr *dev_iface = (struct in_ifaddr *)ptr;
Chaehyun Lim27268872015-09-15 14:06:13 +090069 struct wilc_priv *priv;
Leo Kimf24374a2015-11-05 14:36:12 +090070 struct host_if_drv *hif_drv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090071 struct net_device *dev;
Leo Kimeac3e8f2015-11-05 14:36:16 +090072 u8 *ip_addr_buf;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090073 perInterface_wlan_t *nic;
Greg Kroah-Hartman63d03e42015-06-02 14:16:04 +090074 u8 null_ip[4] = {0};
Johnny Kimc5c77ba2015-05-11 14:30:56 +090075 char wlan_dev_name[5] = "wlan0";
76
Leo Kim3a147c02015-11-05 14:36:10 +090077 if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +090078 PRINT_D(GENERIC_DBG, "dev_iface = NULL\n");
79 return NOTIFY_DONE;
80 }
81
Leo Kim582f8a22015-11-05 14:36:21 +090082 if (memcmp(dev_iface->ifa_label, "wlan0", 5) &&
83 memcmp(dev_iface->ifa_label, "p2p0", 4)) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +090084 PRINT_D(GENERIC_DBG, "Interface is neither WLAN0 nor P2P0\n");
85 return NOTIFY_DONE;
86 }
87
88 dev = (struct net_device *)dev_iface->ifa_dev->dev;
Leo Kim3a147c02015-11-05 14:36:10 +090089 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +090090 PRINT_D(GENERIC_DBG, "No Wireless registerd\n");
91 return NOTIFY_DONE;
92 }
93 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
Leo Kim3a147c02015-11-05 14:36:10 +090094 if (!priv) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +090095 PRINT_D(GENERIC_DBG, "No Wireless Priv\n");
96 return NOTIFY_DONE;
97 }
Leo Kimf24374a2015-11-05 14:36:12 +090098 hif_drv = (struct host_if_drv *)priv->hWILCWFIDrv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090099 nic = netdev_priv(dev);
Leo Kimf24374a2015-11-05 14:36:12 +0900100 if (!nic || !hif_drv) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900101 PRINT_D(GENERIC_DBG, "No Wireless Priv\n");
102 return NOTIFY_DONE;
103 }
104
Leo Kim98b89842015-11-05 14:36:18 +0900105 PRINT_INFO(GENERIC_DBG, "dev_state_ev_handler +++\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900106
107 switch (event) {
108 case NETDEV_UP:
Leo Kim98b89842015-11-05 14:36:18 +0900109 PRINT_D(GENERIC_DBG, "dev_state_ev_handler event=NETDEV_UP %p\n", dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900110
111 PRINT_INFO(GENERIC_DBG, "\n ============== IP Address Obtained ===============\n\n");
112
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900113 if (nic->iftype == STATION_MODE || nic->iftype == CLIENT_MODE) {
Leo Kimf24374a2015-11-05 14:36:12 +0900114 hif_drv->IFC_UP = 1;
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100115 wilc_optaining_ip = false;
116 del_timer(&wilc_during_ip_timer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900117 PRINT_D(GENERIC_DBG, "IP obtained , enable scan\n");
118 }
119
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100120 if (wilc_enable_ps)
121 wilc_set_power_mgmt(hif_drv, 1, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900122
123 PRINT_D(GENERIC_DBG, "[%s] Up IP\n", dev_iface->ifa_label);
124
Leo Kimeac3e8f2015-11-05 14:36:16 +0900125 ip_addr_buf = (char *)&dev_iface->ifa_address;
126 PRINT_D(GENERIC_DBG, "IP add=%d:%d:%d:%d\n",
127 ip_addr_buf[0], ip_addr_buf[1],
128 ip_addr_buf[2], ip_addr_buf[3]);
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100129 wilc_setup_ipaddress(hif_drv, ip_addr_buf, nic->u8IfIdx);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900130
131 break;
132
133 case NETDEV_DOWN:
Leo Kim98b89842015-11-05 14:36:18 +0900134 PRINT_D(GENERIC_DBG, "dev_state_ev_handler event=NETDEV_DOWN %p\n", dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900135
136 PRINT_INFO(GENERIC_DBG, "\n ============== IP Address Released ===============\n\n");
137 if (nic->iftype == STATION_MODE || nic->iftype == CLIENT_MODE) {
Leo Kimf24374a2015-11-05 14:36:12 +0900138 hif_drv->IFC_UP = 0;
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100139 wilc_optaining_ip = false;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900140 }
141
142 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100143 wilc_set_power_mgmt(hif_drv, 0, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900144
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100145 wilc_resolve_disconnect_aberration(hif_drv);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900146
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900147 PRINT_D(GENERIC_DBG, "[%s] Down IP\n", dev_iface->ifa_label);
148
Leo Kimeac3e8f2015-11-05 14:36:16 +0900149 ip_addr_buf = null_ip;
150 PRINT_D(GENERIC_DBG, "IP add=%d:%d:%d:%d\n",
151 ip_addr_buf[0], ip_addr_buf[1],
152 ip_addr_buf[2], ip_addr_buf[3]);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900153
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100154 wilc_setup_ipaddress(hif_drv, ip_addr_buf, nic->u8IfIdx);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900155
156 break;
157
158 default:
Leo Kim98b89842015-11-05 14:36:18 +0900159 PRINT_INFO(GENERIC_DBG, "dev_state_ev_handler event=default\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900160 PRINT_INFO(GENERIC_DBG, "[%s] unknown dev event: %lu\n", dev_iface->ifa_label, event);
161
162 break;
163 }
164
165 return NOTIFY_DONE;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900166}
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900167
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900168#if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
169static irqreturn_t isr_uh_routine(int irq, void *user_data)
170{
Glen Lee39483622015-10-27 18:27:37 +0900171 perInterface_wlan_t *nic;
172 struct wilc *wilc;
173 struct net_device *dev = (struct net_device *)user_data;
174
175 nic = netdev_priv(dev);
176 wilc = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900177 PRINT_D(INT_DBG, "Interrupt received UH\n");
178
Glen Lee39483622015-10-27 18:27:37 +0900179 if (wilc->close) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900180 PRINT_ER("Driver is CLOSING: Can't handle UH interrupt\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900181 return IRQ_HANDLED;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900182 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900183 return IRQ_WAKE_THREAD;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900184}
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900185
Arnd Bergmann1608c402015-11-16 15:04:53 +0100186static irqreturn_t isr_bh_routine(int irq, void *userdata)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900187{
Glen Lee2e7933d2015-10-27 18:27:38 +0900188 perInterface_wlan_t *nic;
189 struct wilc *wilc;
190
191 nic = netdev_priv(userdata);
192 wilc = nic->wilc;
193
Glen Lee2e7933d2015-10-27 18:27:38 +0900194 if (wilc->close) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900195 PRINT_ER("Driver is CLOSING: Can't handle BH interrupt\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900196 return IRQ_HANDLED;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900197 }
198
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900199 PRINT_D(INT_DBG, "Interrupt received BH\n");
Glen Lee50b929e2015-10-27 18:27:40 +0900200 wilc_handle_isr(wilc);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900201
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900202 return IRQ_HANDLED;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900203}
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900204
Glen Lee2c1d05d2015-10-20 17:14:03 +0900205static int init_irq(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900206{
207 int ret = 0;
Glen Lee2c1d05d2015-10-20 17:14:03 +0900208 perInterface_wlan_t *nic;
209 struct wilc *wl;
210
211 nic = netdev_priv(dev);
212 wl = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900213
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900214 if ((gpio_request(GPIO_NUM, "WILC_INTR") == 0) &&
215 (gpio_direction_input(GPIO_NUM) == 0)) {
Glen Lee2c1d05d2015-10-20 17:14:03 +0900216 wl->dev_irq_num = gpio_to_irq(GPIO_NUM);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900217 } else {
218 ret = -1;
219 PRINT_ER("could not obtain gpio for WILC_INTR\n");
220 }
221
Leo Kim83231b72015-11-06 11:12:22 +0900222 if (ret != -1 && request_threaded_irq(wl->dev_irq_num,
223 isr_uh_routine,
224 isr_bh_routine,
225 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
226 "WILC_IRQ", dev) < 0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900227 PRINT_ER("Failed to request IRQ for GPIO: %d\n", GPIO_NUM);
228 ret = -1;
229 } else {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900230 PRINT_D(INIT_DBG, "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
Glen Lee2c1d05d2015-10-20 17:14:03 +0900231 wl->dev_irq_num, GPIO_NUM);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900232 }
233
234 return ret;
235}
236#endif
237
Glen Leeec5cc752015-10-27 18:27:39 +0900238static void deinit_irq(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900239{
Glen Leeec5cc752015-10-27 18:27:39 +0900240 perInterface_wlan_t *nic;
241 struct wilc *wilc;
242
243 nic = netdev_priv(dev);
244 wilc = nic->wilc;
245
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900246#if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
Glen Leeec5cc752015-10-27 18:27:39 +0900247 if (&wilc->dev_irq_num != 0) {
248 free_irq(wilc->dev_irq_num, wilc);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900249
250 gpio_free(GPIO_NUM);
251 }
252#endif
253}
254
Glen Leeef2b7842015-09-24 18:14:55 +0900255void linux_wlan_dbg(u8 *buff)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900256{
257 PRINT_D(INIT_DBG, "%d\n", *buff);
258}
259
Glen Leeb002e202015-09-24 18:15:05 +0900260int linux_wlan_lock_timeout(void *vp, u32 timeout)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900261{
262 int error = -1;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900263
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900264 PRINT_D(LOCK_DBG, "Locking %p\n", vp);
Leo Kim3a147c02015-11-05 14:36:10 +0900265 if (vp)
Leo Kim582f8a22015-11-05 14:36:21 +0900266 error = down_timeout((struct semaphore *)vp,
267 msecs_to_jiffies(timeout));
Chandra S Gorentla78174ad2015-08-08 17:41:36 +0530268 else
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900269 PRINT_ER("Failed, mutex is NULL\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900270 return error;
271}
272
Glen Lee64f2b712015-10-27 18:27:43 +0900273void linux_wlan_mac_indicate(struct wilc *wilc, int flag)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900274{
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900275 int status;
276
277 if (flag == WILC_MAC_INDICATE_STATUS) {
Leo Kim582f8a22015-11-05 14:36:21 +0900278 wilc_wlan_cfg_get_val(WID_STATUS,
279 (unsigned char *)&status, 4);
Glen Lee64f2b712015-10-27 18:27:43 +0900280 if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
281 wilc->mac_status = status;
282 up(&wilc->sync_event);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900283 } else {
Glen Lee64f2b712015-10-27 18:27:43 +0900284 wilc->mac_status = status;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900285 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900286 } else if (flag == WILC_MAC_INDICATE_SCAN) {
287 PRINT_D(GENERIC_DBG, "Scanning ...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900288 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900289}
290
Arnd Bergmann1608c402015-11-16 15:04:53 +0100291static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900292{
Leo Kim660786e2015-11-05 14:36:27 +0900293 u8 *bssid, *bssid1;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900294 int i = 0;
295
Leo Kimd2392222015-11-05 14:36:26 +0900296 bssid = mac_header + 10;
Leo Kim660786e2015-11-05 14:36:27 +0900297 bssid1 = mac_header + 4;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900298
Glen Lee4ad81fd2015-10-27 18:27:57 +0900299 for (i = 0; i < wilc->vif_num; i++)
Leo Kim660786e2015-11-05 14:36:27 +0900300 if (!memcmp(bssid1, wilc->vif[i].bssid, ETH_ALEN) ||
Leo Kimd2392222015-11-05 14:36:26 +0900301 !memcmp(bssid, wilc->vif[i].bssid, ETH_ALEN))
Glen Lee4ad81fd2015-10-27 18:27:57 +0900302 return wilc->vif[i].ndev;
Tony Cho8259a532015-10-20 14:26:47 +0900303
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900304 PRINT_INFO(INIT_DBG, "Invalide handle\n");
Kim, Leofc4b95d2015-07-28 17:47:37 +0900305 for (i = 0; i < 25; i++)
Leo Kim51456c22015-11-05 14:36:25 +0900306 PRINT_D(INIT_DBG, "%02x ", mac_header[i]);
Leo Kimd2392222015-11-05 14:36:26 +0900307 bssid = mac_header + 18;
Leo Kim660786e2015-11-05 14:36:27 +0900308 bssid1 = mac_header + 12;
Glen Lee4ad81fd2015-10-27 18:27:57 +0900309 for (i = 0; i < wilc->vif_num; i++)
Leo Kim660786e2015-11-05 14:36:27 +0900310 if (!memcmp(bssid1, wilc->vif[i].bssid, ETH_ALEN) ||
Leo Kimd2392222015-11-05 14:36:26 +0900311 !memcmp(bssid, wilc->vif[i].bssid, ETH_ALEN))
Glen Lee4ad81fd2015-10-27 18:27:57 +0900312 return wilc->vif[i].ndev;
Tony Cho8259a532015-10-20 14:26:47 +0900313
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900314 PRINT_INFO(INIT_DBG, "\n");
315 return NULL;
316}
317
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100318int wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900319{
320 int i = 0;
321 int ret = -1;
Glen Lee472791a2015-10-27 18:27:44 +0900322 perInterface_wlan_t *nic;
323 struct wilc *wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900324
Glen Lee472791a2015-10-27 18:27:44 +0900325 nic = netdev_priv(wilc_netdev);
326 wilc = nic->wilc;
327
328 for (i = 0; i < wilc->vif_num; i++)
329 if (wilc->vif[i].ndev == wilc_netdev) {
Leo Kimf66dee72015-11-05 14:36:28 +0900330 memcpy(wilc->vif[i].bssid, bssid, 6);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900331 ret = 0;
332 break;
333 }
Tony Cho8259a532015-10-20 14:26:47 +0900334
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900335 return ret;
336}
337
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100338int wilc_wlan_get_num_conn_ifcs(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900339{
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900340 u8 i = 0;
341 u8 null_bssid[6] = {0};
342 u8 ret_val = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900343
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100344 for (i = 0; i < wilc_dev->vif_num; i++)
345 if (memcmp(wilc_dev->vif[i].bssid, null_bssid, 6))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900346 ret_val++;
Tony Cho8259a532015-10-20 14:26:47 +0900347
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900348 return ret_val;
349}
350
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900351#define USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
352
353static int linux_wlan_txq_task(void *vp)
354{
355 int ret, txq_count;
Glen Lee88687582015-10-27 18:27:46 +0900356 perInterface_wlan_t *nic;
357 struct wilc *wl;
358 struct net_device *dev = vp;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900359#if defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
360#define TX_BACKOFF_WEIGHT_INCR_STEP (1)
361#define TX_BACKOFF_WEIGHT_DECR_STEP (1)
362#define TX_BACKOFF_WEIGHT_MAX (7)
363#define TX_BACKOFF_WEIGHT_MIN (0)
364#define TX_BACKOFF_WEIGHT_UNIT_MS (10)
365 int backoff_weight = TX_BACKOFF_WEIGHT_MIN;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900366#endif
367
Glen Lee88687582015-10-27 18:27:46 +0900368 nic = netdev_priv(dev);
369 wl = nic->wilc;
370
Glen Lee88687582015-10-27 18:27:46 +0900371 up(&wl->txq_thread_started);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900372 while (1) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900373 PRINT_D(TX_DBG, "txq_task Taking a nap :)\n");
Glen Lee88687582015-10-27 18:27:46 +0900374 down(&wl->txq_event);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900375 PRINT_D(TX_DBG, "txq_task Who waked me up :$\n");
376
Glen Lee88687582015-10-27 18:27:46 +0900377 if (wl->close) {
Glen Lee88687582015-10-27 18:27:46 +0900378 up(&wl->txq_thread_started);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900379
380 while (!kthread_should_stop())
381 schedule();
382
383 PRINT_D(TX_DBG, "TX thread stopped\n");
384 break;
385 }
386 PRINT_D(TX_DBG, "txq_task handle the sending packet and let me go to sleep.\n");
387#if !defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
Glen Leea1332ca2015-10-27 18:27:47 +0900388 ret = wilc_wlan_handle_txq(dev, &txq_count);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900389#else
390 do {
Glen Leea1332ca2015-10-27 18:27:47 +0900391 ret = wilc_wlan_handle_txq(dev, &txq_count);
Leo Kim98b89842015-11-05 14:36:18 +0900392 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900393 PRINT_D(TX_DBG, "Waking up queue\n");
Leo Kim98b89842015-11-05 14:36:18 +0900394
Glen Lee88687582015-10-27 18:27:46 +0900395 if (netif_queue_stopped(wl->vif[0].ndev))
396 netif_wake_queue(wl->vif[0].ndev);
397 if (netif_queue_stopped(wl->vif[1].ndev))
398 netif_wake_queue(wl->vif[1].ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900399 }
400
Leo Kim98b89842015-11-05 14:36:18 +0900401 if (ret == WILC_TX_ERR_NO_BUF) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900402 do {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900403 msleep(TX_BACKOFF_WEIGHT_UNIT_MS << backoff_weight);
Leo Kim98b89842015-11-05 14:36:18 +0900404 } while (0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900405 backoff_weight += TX_BACKOFF_WEIGHT_INCR_STEP;
Kim, Leofc4b95d2015-07-28 17:47:37 +0900406 if (backoff_weight > TX_BACKOFF_WEIGHT_MAX)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900407 backoff_weight = TX_BACKOFF_WEIGHT_MAX;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900408 } else {
409 if (backoff_weight > TX_BACKOFF_WEIGHT_MIN) {
410 backoff_weight -= TX_BACKOFF_WEIGHT_DECR_STEP;
Kim, Leofc4b95d2015-07-28 17:47:37 +0900411 if (backoff_weight < TX_BACKOFF_WEIGHT_MIN)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900412 backoff_weight = TX_BACKOFF_WEIGHT_MIN;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900413 }
414 }
Leo Kim98b89842015-11-05 14:36:18 +0900415 } while (ret == WILC_TX_ERR_NO_BUF && !wl->close);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900416#endif
417 }
418 return 0;
419}
420
Glen Leec0cadaa2015-09-24 18:14:54 +0900421void linux_wlan_rx_complete(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900422{
423 PRINT_D(RX_DBG, "RX completed\n");
424}
425
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100426int wilc_wlan_get_firmware(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900427{
Glen Lee65c8adc2015-10-29 12:18:49 +0900428 perInterface_wlan_t *nic;
429 struct wilc *wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900430 int ret = 0;
431 const struct firmware *wilc_firmware;
432 char *firmware;
433
Glen Lee65c8adc2015-10-29 12:18:49 +0900434 nic = netdev_priv(dev);
435 wilc = nic->wilc;
436
Leo Kim1b7a93a2015-11-05 14:36:29 +0900437 if (nic->iftype == AP_MODE) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900438 firmware = AP_FIRMWARE;
Leo Kim1b7a93a2015-11-05 14:36:29 +0900439 } else if (nic->iftype == STATION_MODE) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900440 firmware = STA_FIRMWARE;
Leo Kim1b7a93a2015-11-05 14:36:29 +0900441 } else {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900442 PRINT_D(INIT_DBG, "Get P2P_CONCURRENCY_FIRMWARE\n");
443 firmware = P2P_CONCURRENCY_FIRMWARE;
444 }
445
Leo Kim3a147c02015-11-05 14:36:10 +0900446 if (!nic) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900447 PRINT_ER("NIC is NULL\n");
448 goto _fail_;
449 }
450
Leo Kim3a147c02015-11-05 14:36:10 +0900451 if (!(&nic->wilc_netdev->dev)) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900452 PRINT_ER("&nic->wilc_netdev->dev is NULL\n");
453 goto _fail_;
454 }
455
Arnd Bergmannb03314e2015-11-16 15:05:01 +0100456 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900457 PRINT_ER("%s - firmare not available\n", firmware);
458 ret = -1;
459 goto _fail_;
460 }
Glen Lee65c8adc2015-10-29 12:18:49 +0900461 wilc->firmware = wilc_firmware;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900462
463_fail_:
464
465 return ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900466}
467
Glen Lee9bf3d722015-10-29 12:18:46 +0900468static int linux_wlan_start_firmware(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900469{
Glen Lee9bf3d722015-10-29 12:18:46 +0900470 perInterface_wlan_t *nic;
471 struct wilc *wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900472 int ret = 0;
Glen Lee9bf3d722015-10-29 12:18:46 +0900473
474 nic = netdev_priv(dev);
475 wilc = nic->wilc;
476
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900477 PRINT_D(INIT_DBG, "Starting Firmware ...\n");
Glen Leee42563b2015-10-01 16:03:33 +0900478 ret = wilc_wlan_start();
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900479 if (ret < 0) {
480 PRINT_ER("Failed to start Firmware\n");
Leo Kim0aeea1a2015-11-05 14:36:30 +0900481 return ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900482 }
483
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900484 PRINT_D(INIT_DBG, "Waiting for Firmware to get ready ...\n");
Glen Lee9bf3d722015-10-29 12:18:46 +0900485 ret = linux_wlan_lock_timeout(&wilc->sync_event, 5000);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900486 if (ret) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900487 PRINT_D(INIT_DBG, "Firmware start timed out");
Leo Kim0aeea1a2015-11-05 14:36:30 +0900488 return ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900489 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900490 PRINT_D(INIT_DBG, "Firmware successfully started\n");
491
Leo Kim0aeea1a2015-11-05 14:36:30 +0900492 return 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900493}
Leo Kima40b22c2015-11-05 14:36:32 +0900494
Glen Leeed760b62015-10-29 12:18:42 +0900495static int linux_wlan_firmware_download(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900496{
Glen Leeed760b62015-10-29 12:18:42 +0900497 perInterface_wlan_t *nic;
498 struct wilc *wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900499 int ret = 0;
500
Glen Leeed760b62015-10-29 12:18:42 +0900501 nic = netdev_priv(dev);
502 wilc = nic->wilc;
503
504 if (!wilc->firmware) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900505 PRINT_ER("Firmware buffer is NULL\n");
Leo Kim14b18212015-11-05 14:36:31 +0900506 return -ENOBUFS;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900507 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900508 PRINT_D(INIT_DBG, "Downloading Firmware ...\n");
Glen Leeed760b62015-10-29 12:18:42 +0900509 ret = wilc_wlan_firmware_download(wilc->firmware->data,
510 wilc->firmware->size);
Kim, Leofc4b95d2015-07-28 17:47:37 +0900511 if (ret < 0)
Leo Kim14b18212015-11-05 14:36:31 +0900512 return ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900513
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900514 PRINT_D(INIT_DBG, "Freeing FW buffer ...\n");
515 PRINT_D(INIT_DBG, "Releasing firmware\n");
Glen Leeed760b62015-10-29 12:18:42 +0900516 release_firmware(wilc->firmware);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900517
Chandra S Gorentla17aacd42015-08-08 17:41:35 +0530518 PRINT_D(INIT_DBG, "Download Succeeded\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900519
Leo Kim14b18212015-11-05 14:36:31 +0900520 return 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900521}
522
Tony Chof61c5af2015-10-14 20:46:30 +0900523static int linux_wlan_init_test_config(struct net_device *dev, struct wilc *p_nic)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900524{
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900525 unsigned char c_val[64];
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900526 unsigned char mac_add[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xff};
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900527
Chaehyun Lim27268872015-09-15 14:06:13 +0900528 struct wilc_priv *priv;
Leo Kim0fa66c72015-11-05 14:36:13 +0900529 struct host_if_drv *hif_drv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900530
531 PRINT_D(TX_DBG, "Start configuring Firmware\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900532 get_random_bytes(&mac_add[5], 1);
533 get_random_bytes(&mac_add[4], 1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900534 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
Leo Kim0fa66c72015-11-05 14:36:13 +0900535 hif_drv = (struct host_if_drv *)priv->hWILCWFIDrv;
536 PRINT_D(INIT_DBG, "Host = %p\n", hif_drv);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900537
Leo Kim582f8a22015-11-05 14:36:21 +0900538 PRINT_D(INIT_DBG, "MAC address is : %02x-%02x-%02x-%02x-%02x-%02x\n",
539 mac_add[0], mac_add[1], mac_add[2],
540 mac_add[3], mac_add[4], mac_add[5]);
Madhusudhanan Ravindran21175ef2015-06-09 11:19:06 +0000541 wilc_get_chipid(0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900542
Johnny Kime5d57e92015-08-20 16:32:53 +0900543 *(int *)c_val = 1;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900544
Glen Lee1028e5a2015-10-01 16:03:40 +0900545 if (!wilc_wlan_cfg_set(1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900546 goto _fail_;
547
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900548 c_val[0] = 0;
Glen Lee1028e5a2015-10-01 16:03:40 +0900549 if (!wilc_wlan_cfg_set(0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900550 goto _fail_;
551
552 c_val[0] = INFRASTRUCTURE;
Glen Lee1028e5a2015-10-01 16:03:40 +0900553 if (!wilc_wlan_cfg_set(0, WID_BSS_TYPE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900554 goto _fail_;
555
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900556 c_val[0] = RATE_AUTO;
Glen Lee1028e5a2015-10-01 16:03:40 +0900557 if (!wilc_wlan_cfg_set(0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900558 goto _fail_;
559
560 c_val[0] = G_MIXED_11B_2_MODE;
Glen Lee1028e5a2015-10-01 16:03:40 +0900561 if (!wilc_wlan_cfg_set(0, WID_11G_OPERATING_MODE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900562 goto _fail_;
563
564 c_val[0] = 1;
Glen Lee1028e5a2015-10-01 16:03:40 +0900565 if (!wilc_wlan_cfg_set(0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900566 goto _fail_;
567
568 c_val[0] = G_SHORT_PREAMBLE;
Glen Lee1028e5a2015-10-01 16:03:40 +0900569 if (!wilc_wlan_cfg_set(0, WID_PREAMBLE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900570 goto _fail_;
571
572 c_val[0] = AUTO_PROT;
Glen Lee1028e5a2015-10-01 16:03:40 +0900573 if (!wilc_wlan_cfg_set(0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900574 goto _fail_;
575
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900576 c_val[0] = ACTIVE_SCAN;
Glen Lee1028e5a2015-10-01 16:03:40 +0900577 if (!wilc_wlan_cfg_set(0, WID_SCAN_TYPE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900578 goto _fail_;
579
580 c_val[0] = SITE_SURVEY_OFF;
Glen Lee1028e5a2015-10-01 16:03:40 +0900581 if (!wilc_wlan_cfg_set(0, WID_SITE_SURVEY, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900582 goto _fail_;
583
Leo Kim98b89842015-11-05 14:36:18 +0900584 *((int *)c_val) = 0xffff;
Glen Lee1028e5a2015-10-01 16:03:40 +0900585 if (!wilc_wlan_cfg_set(0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900586 goto _fail_;
587
588 *((int *)c_val) = 2346;
Glen Lee1028e5a2015-10-01 16:03:40 +0900589 if (!wilc_wlan_cfg_set(0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900590 goto _fail_;
591
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900592 c_val[0] = 0;
Glen Lee1028e5a2015-10-01 16:03:40 +0900593 if (!wilc_wlan_cfg_set(0, WID_BCAST_SSID, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900594 goto _fail_;
595
596 c_val[0] = 1;
Glen Lee1028e5a2015-10-01 16:03:40 +0900597 if (!wilc_wlan_cfg_set(0, WID_QOS_ENABLE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900598 goto _fail_;
599
600 c_val[0] = NO_POWERSAVE;
Glen Lee1028e5a2015-10-01 16:03:40 +0900601 if (!wilc_wlan_cfg_set(0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900602 goto _fail_;
603
Arnd Bergmannb4d04c12015-11-16 15:04:56 +0100604 c_val[0] = NO_SECURITY; /* NO_ENCRYPT, 0x79 */
Glen Lee1028e5a2015-10-01 16:03:40 +0900605 if (!wilc_wlan_cfg_set(0, WID_11I_MODE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900606 goto _fail_;
607
608 c_val[0] = OPEN_SYSTEM;
Glen Lee1028e5a2015-10-01 16:03:40 +0900609 if (!wilc_wlan_cfg_set(0, WID_AUTH_TYPE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900610 goto _fail_;
611
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900612 strcpy(c_val, "123456790abcdef1234567890");
Glen Lee1028e5a2015-10-01 16:03:40 +0900613 if (!wilc_wlan_cfg_set(0, WID_WEP_KEY_VALUE, c_val, (strlen(c_val) + 1), 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900614 goto _fail_;
615
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900616 strcpy(c_val, "12345678");
Glen Lee1028e5a2015-10-01 16:03:40 +0900617 if (!wilc_wlan_cfg_set(0, WID_11I_PSK, c_val, (strlen(c_val)), 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900618 goto _fail_;
619
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900620 strcpy(c_val, "password");
Glen Lee1028e5a2015-10-01 16:03:40 +0900621 if (!wilc_wlan_cfg_set(0, WID_1X_KEY, c_val, (strlen(c_val) + 1), 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900622 goto _fail_;
623
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900624 c_val[0] = 192;
625 c_val[1] = 168;
626 c_val[2] = 1;
627 c_val[3] = 112;
Glen Lee1028e5a2015-10-01 16:03:40 +0900628 if (!wilc_wlan_cfg_set(0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900629 goto _fail_;
630
631 c_val[0] = 3;
Glen Lee1028e5a2015-10-01 16:03:40 +0900632 if (!wilc_wlan_cfg_set(0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900633 goto _fail_;
634
635 c_val[0] = 3;
Glen Lee1028e5a2015-10-01 16:03:40 +0900636 if (!wilc_wlan_cfg_set(0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900637 goto _fail_;
638
639 c_val[0] = NORMAL_ACK;
Glen Lee1028e5a2015-10-01 16:03:40 +0900640 if (!wilc_wlan_cfg_set(0, WID_ACK_POLICY, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900641 goto _fail_;
642
643 c_val[0] = 0;
Glen Lee1028e5a2015-10-01 16:03:40 +0900644 if (!wilc_wlan_cfg_set(0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900645 goto _fail_;
646
647 c_val[0] = 48;
Glen Lee1028e5a2015-10-01 16:03:40 +0900648 if (!wilc_wlan_cfg_set(0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900649 goto _fail_;
650
651 c_val[0] = 28;
Glen Lee1028e5a2015-10-01 16:03:40 +0900652 if (!wilc_wlan_cfg_set(0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900653 goto _fail_;
654
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900655 *((int *)c_val) = 100;
Glen Lee1028e5a2015-10-01 16:03:40 +0900656 if (!wilc_wlan_cfg_set(0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900657 goto _fail_;
658
659 c_val[0] = REKEY_DISABLE;
Glen Lee1028e5a2015-10-01 16:03:40 +0900660 if (!wilc_wlan_cfg_set(0, WID_REKEY_POLICY, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900661 goto _fail_;
662
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900663 *((int *)c_val) = 84600;
Glen Lee1028e5a2015-10-01 16:03:40 +0900664 if (!wilc_wlan_cfg_set(0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900665 goto _fail_;
666
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900667 *((int *)c_val) = 500;
Glen Lee1028e5a2015-10-01 16:03:40 +0900668 if (!wilc_wlan_cfg_set(0, WID_REKEY_PACKET_COUNT, c_val, 4, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900669 goto _fail_;
670
671 c_val[0] = 1;
Glen Lee1028e5a2015-10-01 16:03:40 +0900672 if (!wilc_wlan_cfg_set(0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900673 goto _fail_;
674
675 c_val[0] = G_SELF_CTS_PROT;
Glen Lee1028e5a2015-10-01 16:03:40 +0900676 if (!wilc_wlan_cfg_set(0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900677 goto _fail_;
678
Leo Kim98b89842015-11-05 14:36:18 +0900679 c_val[0] = 1;
Glen Lee1028e5a2015-10-01 16:03:40 +0900680 if (!wilc_wlan_cfg_set(0, WID_11N_ENABLE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900681 goto _fail_;
682
683 c_val[0] = HT_MIXED_MODE;
Glen Lee1028e5a2015-10-01 16:03:40 +0900684 if (!wilc_wlan_cfg_set(0, WID_11N_OPERATING_MODE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900685 goto _fail_;
686
Leo Kim98b89842015-11-05 14:36:18 +0900687 c_val[0] = 1;
Glen Lee1028e5a2015-10-01 16:03:40 +0900688 if (!wilc_wlan_cfg_set(0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900689 goto _fail_;
690
691 memcpy(c_val, mac_add, 6);
692
Glen Lee1028e5a2015-10-01 16:03:40 +0900693 if (!wilc_wlan_cfg_set(0, WID_MAC_ADDR, c_val, 6, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900694 goto _fail_;
695
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900696 c_val[0] = DETECT_PROTECT_REPORT;
Glen Lee1028e5a2015-10-01 16:03:40 +0900697 if (!wilc_wlan_cfg_set(0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900698 goto _fail_;
699
700 c_val[0] = RTS_CTS_NONHT_PROT;
Glen Lee1028e5a2015-10-01 16:03:40 +0900701 if (!wilc_wlan_cfg_set(0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900702 goto _fail_;
703
704 c_val[0] = 0;
Glen Lee1028e5a2015-10-01 16:03:40 +0900705 if (!wilc_wlan_cfg_set(0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900706 goto _fail_;
707
708 c_val[0] = MIMO_MODE;
Glen Lee1028e5a2015-10-01 16:03:40 +0900709 if (!wilc_wlan_cfg_set(0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900710 goto _fail_;
711
712 c_val[0] = 7;
Glen Lee1028e5a2015-10-01 16:03:40 +0900713 if (!wilc_wlan_cfg_set(0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0, 0))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900714 goto _fail_;
715
Leo Kim98b89842015-11-05 14:36:18 +0900716 c_val[0] = 1;
Glen Lee1028e5a2015-10-01 16:03:40 +0900717 if (!wilc_wlan_cfg_set(0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1, 1, 1))
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900718 goto _fail_;
719
720 return 0;
721
722_fail_:
723 return -1;
724}
725
Glen Lee53dc0cf2015-10-20 17:13:57 +0900726void wilc1000_wlan_deinit(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900727{
Glen Lee53dc0cf2015-10-20 17:13:57 +0900728 perInterface_wlan_t *nic;
729 struct wilc *wl;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900730
Glen Lee53dc0cf2015-10-20 17:13:57 +0900731 nic = netdev_priv(dev);
732 wl = nic->wilc;
733
734 if (!wl) {
735 netdev_err(dev, "wl is NULL\n");
736 return;
737 }
738
739 if (wl->initialized) {
740 netdev_info(dev, "Deinitializing wilc1000...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900741
742#if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900743 PRINT_D(INIT_DBG, "skip wilc_bus_set_default_speed\n");
744#else
745 wilc_bus_set_default_speed();
746#endif
747
748 PRINT_D(INIT_DBG, "Disabling IRQ\n");
Glen Leed59177c2015-09-17 17:47:25 +0900749#ifdef WILC_SDIO
Glen Lee53dc0cf2015-10-20 17:13:57 +0900750 mutex_lock(&wl->hif_cs);
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100751 wilc_sdio_disable_interrupt();
Glen Lee53dc0cf2015-10-20 17:13:57 +0900752 mutex_unlock(&wl->hif_cs);
Glen Leed59177c2015-09-17 17:47:25 +0900753#endif
Leo Kim3a147c02015-11-05 14:36:10 +0900754 if (&wl->txq_event)
Glen Lee53dc0cf2015-10-20 17:13:57 +0900755 up(&wl->txq_event);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900756
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900757 PRINT_D(INIT_DBG, "Deinitializing Threads\n");
Glen Lee32dd51b2015-10-27 18:27:52 +0900758 wlan_deinitialize_threads(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900759
760 PRINT_D(INIT_DBG, "Deinitializing IRQ\n");
Glen Leeec5cc752015-10-27 18:27:39 +0900761 deinit_irq(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900762
Glen Lee8cec7412015-10-01 16:03:34 +0900763 wilc_wlan_stop();
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900764
765 PRINT_D(INIT_DBG, "Deinitializing WILC Wlan\n");
Glen Lee2de7cbe2015-10-27 18:27:54 +0900766 wilc_wlan_cleanup(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900767#if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
768 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
769 PRINT_D(INIT_DBG, "Disabling IRQ 2\n");
770
Glen Lee53dc0cf2015-10-20 17:13:57 +0900771 mutex_lock(&wl->hif_cs);
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100772 wilc_sdio_disable_interrupt();
Glen Lee53dc0cf2015-10-20 17:13:57 +0900773 mutex_unlock(&wl->hif_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900774 #endif
775#endif
776
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900777 PRINT_D(INIT_DBG, "Deinitializing Locks\n");
Glen Lee7c67c052015-10-27 18:27:50 +0900778 wlan_deinit_locks(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900779
Glen Lee53dc0cf2015-10-20 17:13:57 +0900780 wl->initialized = false;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900781
782 PRINT_D(INIT_DBG, "wilc1000 deinitialization Done\n");
783
784 } else {
785 PRINT_D(INIT_DBG, "wilc1000 is not initialized\n");
786 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900787}
788
Arnd Bergmann1608c402015-11-16 15:04:53 +0100789static int wlan_init_locks(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900790{
Glen Lee38afb392015-10-20 17:13:53 +0900791 perInterface_wlan_t *nic;
792 struct wilc *wl;
793
794 nic = netdev_priv(dev);
795 wl = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900796
797 PRINT_D(INIT_DBG, "Initializing Locks ...\n");
798
Glen Lee38afb392015-10-20 17:13:53 +0900799 mutex_init(&wl->hif_cs);
800 mutex_init(&wl->rxq_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900801
Glen Lee38afb392015-10-20 17:13:53 +0900802 spin_lock_init(&wl->txq_spinlock);
803 sema_init(&wl->txq_add_to_head_cs, 1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900804
Glen Lee38afb392015-10-20 17:13:53 +0900805 sema_init(&wl->txq_event, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900806
Glen Lee38afb392015-10-20 17:13:53 +0900807 sema_init(&wl->cfg_event, 0);
808 sema_init(&wl->sync_event, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900809
Glen Lee38afb392015-10-20 17:13:53 +0900810 sema_init(&wl->txq_thread_started, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900811
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900812 return 0;
813}
814
Glen Lee7c67c052015-10-27 18:27:50 +0900815static int wlan_deinit_locks(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900816{
Glen Lee7c67c052015-10-27 18:27:50 +0900817 perInterface_wlan_t *nic;
818 struct wilc *wilc;
819
820 nic = netdev_priv(dev);
821 wilc = nic->wilc;
822
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900823 PRINT_D(INIT_DBG, "De-Initializing Locks\n");
824
Leo Kim3a147c02015-11-05 14:36:10 +0900825 if (&wilc->hif_cs)
Glen Lee7c67c052015-10-27 18:27:50 +0900826 mutex_destroy(&wilc->hif_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900827
Leo Kim3a147c02015-11-05 14:36:10 +0900828 if (&wilc->rxq_cs)
Glen Lee7c67c052015-10-27 18:27:50 +0900829 mutex_destroy(&wilc->rxq_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900830
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900831 return 0;
832}
Leo Kima40b22c2015-11-05 14:36:32 +0900833
Arnd Bergmann1608c402015-11-16 15:04:53 +0100834static int wlan_initialize_threads(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900835{
Glen Lee75a94662015-10-27 18:27:45 +0900836 perInterface_wlan_t *nic;
837 struct wilc *wilc;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900838
Glen Lee75a94662015-10-27 18:27:45 +0900839 nic = netdev_priv(dev);
840 wilc = nic->wilc;
841
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900842 PRINT_D(INIT_DBG, "Initializing Threads ...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900843 PRINT_D(INIT_DBG, "Creating kthread for transmission\n");
Glen Lee88687582015-10-27 18:27:46 +0900844 wilc->txq_thread = kthread_run(linux_wlan_txq_task, (void *)dev,
Glen Lee75a94662015-10-27 18:27:45 +0900845 "K_TXQ_TASK");
846 if (!wilc->txq_thread) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900847 PRINT_ER("couldn't create TXQ thread\n");
Leo Kim6bc72c52015-11-05 14:36:33 +0900848 wilc->close = 0;
849 return -ENOBUFS;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900850 }
Glen Lee75a94662015-10-27 18:27:45 +0900851 down(&wilc->txq_thread_started);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900852
853 return 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900854}
855
Glen Lee32dd51b2015-10-27 18:27:52 +0900856static void wlan_deinitialize_threads(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900857{
Glen Lee32dd51b2015-10-27 18:27:52 +0900858 perInterface_wlan_t *nic;
859 struct wilc *wl;
Glen Lee32dd51b2015-10-27 18:27:52 +0900860 nic = netdev_priv(dev);
861 wl = nic->wilc;
862
863 wl->close = 1;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900864 PRINT_D(INIT_DBG, "Deinitializing Threads\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900865
Leo Kim3a147c02015-11-05 14:36:10 +0900866 if (&wl->txq_event)
Glen Lee32dd51b2015-10-27 18:27:52 +0900867 up(&wl->txq_event);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900868
Leo Kim3a147c02015-11-05 14:36:10 +0900869 if (wl->txq_thread) {
Glen Lee32dd51b2015-10-27 18:27:52 +0900870 kthread_stop(wl->txq_thread);
871 wl->txq_thread = NULL;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900872 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900873}
874
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900875int wilc1000_wlan_init(struct net_device *dev, perInterface_wlan_t *p_nic)
876{
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900877 perInterface_wlan_t *nic = p_nic;
878 int ret = 0;
Glen Lee0fa683b2015-10-20 17:13:52 +0900879 struct wilc *wl = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900880
Glen Lee0fa683b2015-10-20 17:13:52 +0900881 if (!wl->initialized) {
882 wl->mac_status = WILC_MAC_STATUS_INIT;
883 wl->close = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900884
Glen Lee38afb392015-10-20 17:13:53 +0900885 wlan_init_locks(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900886
Arnd Bergmann4bd7baf2015-11-16 15:04:59 +0100887#ifdef WILC_SDIO
888 wl->io_type = HIF_SDIO;
889#else
890 wl->io_type = HIF_SPI;
891#endif
892 ret = wilc_wlan_init(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900893 if (ret < 0) {
894 PRINT_ER("Initializing WILC_Wlan FAILED\n");
895 ret = -EIO;
896 goto _fail_locks_;
897 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900898
Tony Chob46d6882015-10-20 14:26:54 +0900899#if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
Glen Lee2c1d05d2015-10-20 17:14:03 +0900900 if (init_irq(dev)) {
Tony Chob46d6882015-10-20 14:26:54 +0900901 PRINT_ER("couldn't initialize IRQ\n");
902 ret = -EIO;
903 goto _fail_locks_;
904 }
905#endif
906
Glen Lee75a94662015-10-27 18:27:45 +0900907 ret = wlan_initialize_threads(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900908 if (ret < 0) {
909 PRINT_ER("Initializing Threads FAILED\n");
910 ret = -EIO;
911 goto _fail_wilc_wlan_;
912 }
913
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900914#if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100915 if (wilc_sdio_enable_interrupt()) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900916 PRINT_ER("couldn't initialize IRQ\n");
917 ret = -EIO;
918 goto _fail_irq_init_;
919 }
920#endif
921
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100922 if (wilc_wlan_get_firmware(dev)) {
Chandra S Gorentla17aacd42015-08-08 17:41:35 +0530923 PRINT_ER("Can't get firmware\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900924 ret = -EIO;
925 goto _fail_irq_enable_;
926 }
927
Glen Leeed760b62015-10-29 12:18:42 +0900928 ret = linux_wlan_firmware_download(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900929 if (ret < 0) {
930 PRINT_ER("Failed to download firmware\n");
931 ret = -EIO;
932 goto _fail_irq_enable_;
933 }
934
Glen Lee9bf3d722015-10-29 12:18:46 +0900935 ret = linux_wlan_start_firmware(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900936 if (ret < 0) {
937 PRINT_ER("Failed to start firmware\n");
938 ret = -EIO;
939 goto _fail_irq_enable_;
940 }
941
942 wilc_bus_set_max_speed();
943
Glen Lee07056a82015-10-01 16:03:41 +0900944 if (wilc_wlan_cfg_get(1, WID_FIRMWARE_VERSION, 1, 0)) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900945 int size;
946 char Firmware_ver[20];
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900947
Glen Lee894de36b2015-10-01 16:03:42 +0900948 size = wilc_wlan_cfg_get_val(
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900949 WID_FIRMWARE_VERSION,
950 Firmware_ver, sizeof(Firmware_ver));
951 Firmware_ver[size] = '\0';
952 PRINT_D(INIT_DBG, "***** Firmware Ver = %s *******\n", Firmware_ver);
953 }
Glen Lee0fa683b2015-10-20 17:13:52 +0900954 ret = linux_wlan_init_test_config(dev, wl);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900955
956 if (ret < 0) {
957 PRINT_ER("Failed to configure firmware\n");
958 ret = -EIO;
959 goto _fail_fw_start_;
960 }
961
Glen Lee0fa683b2015-10-20 17:13:52 +0900962 wl->initialized = true;
Leo Kim98b89842015-11-05 14:36:18 +0900963 return 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900964
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900965_fail_fw_start_:
Glen Lee8cec7412015-10-01 16:03:34 +0900966 wilc_wlan_stop();
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900967
968_fail_irq_enable_:
969#if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100970 wilc_sdio_disable_interrupt();
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900971_fail_irq_init_:
Arnd Bergmann7a8fd842015-06-01 21:06:45 +0200972#endif
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900973#if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
Glen Leeec5cc752015-10-27 18:27:39 +0900974 deinit_irq(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900975
976#endif
Glen Lee32dd51b2015-10-27 18:27:52 +0900977 wlan_deinitialize_threads(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900978_fail_wilc_wlan_:
Glen Lee2de7cbe2015-10-27 18:27:54 +0900979 wilc_wlan_cleanup(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900980_fail_locks_:
Glen Lee7c67c052015-10-27 18:27:50 +0900981 wlan_deinit_locks(dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900982 PRINT_ER("WLAN Iinitialization FAILED\n");
983 } else {
984 PRINT_D(INIT_DBG, "wilc1000 already initialized\n");
985 }
986 return ret;
987}
988
Arnd Bergmann1608c402015-11-16 15:04:53 +0100989static int mac_init_fn(struct net_device *ndev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900990{
Leo Kim98b89842015-11-05 14:36:18 +0900991 netif_start_queue(ndev);
992 netif_stop_queue(ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900993
994 return 0;
995}
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900996
Arnd Bergmann0e1af732015-11-16 15:04:54 +0100997int wilc_mac_open(struct net_device *ndev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900998{
999 perInterface_wlan_t *nic;
1000
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001001 unsigned char mac_add[ETH_ALEN] = {0};
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001002 int ret = 0;
1003 int i = 0;
Chaehyun Lim27268872015-09-15 14:06:13 +09001004 struct wilc_priv *priv;
Glen Leef3c13662015-10-20 17:13:54 +09001005 struct wilc *wl;
1006
1007 nic = netdev_priv(ndev);
1008 wl = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001009
Arnd Bergmannb03314e2015-11-16 15:05:01 +01001010 if (!wl|| !wl->dev) {
Chandra S Gorentla7d056522015-09-15 18:09:51 +05301011 netdev_err(ndev, "wilc1000: SPI device not ready\n");
1012 return -ENODEV;
1013 }
Arnd Bergmannb03314e2015-11-16 15:05:01 +01001014
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001015 nic = netdev_priv(ndev);
1016 priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
1017 PRINT_D(INIT_DBG, "MAC OPEN[%p]\n", ndev);
1018
Chaehyun Limdd4b6a82015-09-20 15:51:25 +09001019 ret = wilc_init_host_int(ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001020 if (ret < 0) {
1021 PRINT_ER("Failed to initialize host interface\n");
1022
1023 return ret;
1024 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001025
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001026 PRINT_D(INIT_DBG, "*** re-init ***\n");
1027 ret = wilc1000_wlan_init(ndev, nic);
1028 if (ret < 0) {
1029 PRINT_ER("Failed to initialize wilc1000\n");
Chaehyun Lima9a16822015-09-20 15:51:24 +09001030 wilc_deinit_host_int(ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001031 return ret;
1032 }
1033
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001034 wilc_set_machw_change_vir_if(ndev, false);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001035
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001036 wilc_get_mac_address(priv->hWILCWFIDrv, mac_add);
Alexander Kuleshov310a28f2015-09-04 00:48:14 +06001037 PRINT_D(INIT_DBG, "Mac address: %pM\n", mac_add);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001038
Glen Leef3c13662015-10-20 17:13:54 +09001039 for (i = 0; i < wl->vif_num; i++) {
1040 if (ndev == wl->vif[i].ndev) {
1041 memcpy(wl->vif[i].src_addr, mac_add, ETH_ALEN);
1042 wl->vif[i].hif_drv = priv->hWILCWFIDrv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001043 break;
1044 }
1045 }
1046
Glen Leef3c13662015-10-20 17:13:54 +09001047 memcpy(ndev->dev_addr, wl->vif[i].src_addr, ETH_ALEN);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001048
1049 if (!is_valid_ether_addr(ndev->dev_addr)) {
1050 PRINT_ER("Error: Wrong MAC address\n");
Leo Kim339d2442015-11-05 14:36:34 +09001051 wilc_deinit_host_int(ndev);
1052 wilc1000_wlan_deinit(ndev);
1053 return -EINVAL;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001054 }
1055
Leo Kim582f8a22015-11-05 14:36:21 +09001056 wilc_mgmt_frame_register(nic->wilc_netdev->ieee80211_ptr->wiphy,
1057 nic->wilc_netdev->ieee80211_ptr,
1058 nic->g_struct_frame_reg[0].frame_type,
1059 nic->g_struct_frame_reg[0].reg);
1060 wilc_mgmt_frame_register(nic->wilc_netdev->ieee80211_ptr->wiphy,
1061 nic->wilc_netdev->ieee80211_ptr,
1062 nic->g_struct_frame_reg[1].frame_type,
1063 nic->g_struct_frame_reg[1].reg);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001064 netif_wake_queue(ndev);
Glen Leef3c13662015-10-20 17:13:54 +09001065 wl->open_ifcs++;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001066 nic->mac_opened = 1;
1067 return 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001068}
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001069
Arnd Bergmann1608c402015-11-16 15:04:53 +01001070static struct net_device_stats *mac_stats(struct net_device *dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001071{
1072 perInterface_wlan_t *nic = netdev_priv(dev);
1073
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001074 return &nic->netstats;
1075}
1076
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001077static void wilc_set_multicast_list(struct net_device *dev)
1078{
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001079 struct netdev_hw_addr *ha;
Chaehyun Lim27268872015-09-15 14:06:13 +09001080 struct wilc_priv *priv;
Leo Kimef7606c2015-11-05 14:36:14 +09001081 struct host_if_drv *hif_drv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001082 int i = 0;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +09001083
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001084 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
Leo Kimef7606c2015-11-05 14:36:14 +09001085 hif_drv = (struct host_if_drv *)priv->hWILCWFIDrv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001086
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001087 if (!dev)
1088 return;
1089
Leo Kim582f8a22015-11-05 14:36:21 +09001090 PRINT_D(INIT_DBG, "Setting Multicast List with count = %d.\n",
1091 dev->mc.count);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001092
1093 if (dev->flags & IFF_PROMISC) {
Chandra S Gorentla17aacd42015-08-08 17:41:35 +05301094 PRINT_D(INIT_DBG, "Set promiscuous mode ON, retrive all packets\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001095 return;
1096 }
1097
Leo Kim582f8a22015-11-05 14:36:21 +09001098 if ((dev->flags & IFF_ALLMULTI) ||
1099 (dev->mc.count) > WILC_MULTICAST_TABLE_SIZE) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001100 PRINT_D(INIT_DBG, "Disable multicast filter, retrive all multicast packets\n");
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001101 wilc_setup_multicast_filter(hif_drv, false, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001102 return;
1103 }
1104
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001105 if ((dev->mc.count) == 0) {
1106 PRINT_D(INIT_DBG, "Enable multicast filter, retrive directed packets only.\n");
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001107 wilc_setup_multicast_filter(hif_drv, true, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001108 return;
1109 }
1110
Leo Kimc8537e62015-11-06 11:12:24 +09001111 netdev_for_each_mc_addr(ha, dev) {
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001112 memcpy(wilc_multicast_mac_addr_list[i], ha->addr, ETH_ALEN);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001113 PRINT_D(INIT_DBG, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001114 wilc_multicast_mac_addr_list[i][0],
1115 wilc_multicast_mac_addr_list[i][1],
1116 wilc_multicast_mac_addr_list[i][2],
1117 wilc_multicast_mac_addr_list[i][3],
1118 wilc_multicast_mac_addr_list[i][4],
1119 wilc_multicast_mac_addr_list[i][5]);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001120 i++;
1121 }
1122
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001123 wilc_setup_multicast_filter(hif_drv, true, (dev->mc.count));
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001124
1125 return;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001126}
1127
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001128static void linux_wlan_tx_complete(void *priv, int status)
1129{
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001130 struct tx_complete_data *pv_data = (struct tx_complete_data *)priv;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +09001131
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05301132 if (status == 1)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001133 PRINT_D(TX_DBG, "Packet sent successfully - Size = %d - Address = %p - SKB = %p\n", pv_data->size, pv_data->buff, pv_data->skb);
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05301134 else
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001135 PRINT_D(TX_DBG, "Couldn't send packet - Size = %d - Address = %p - SKB = %p\n", pv_data->size, pv_data->buff, pv_data->skb);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001136 dev_kfree_skb(pv_data->skb);
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001137 kfree(pv_data);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001138}
1139
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001140int wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001141{
1142 perInterface_wlan_t *nic;
1143 struct tx_complete_data *tx_data = NULL;
Leo Kim44ec3b72015-11-05 14:36:38 +09001144 int queue_count;
Leo Kimfd8f0362015-11-05 14:36:39 +09001145 char *udp_buf;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001146 struct iphdr *ih;
1147 struct ethhdr *eth_h;
Glen Leeb7495be2015-10-27 18:28:01 +09001148 struct wilc *wilc;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +09001149
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001150 nic = netdev_priv(ndev);
Glen Leeb7495be2015-10-27 18:28:01 +09001151 wilc = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001152
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001153 PRINT_D(TX_DBG, "Sending packet just received from TCP/IP\n");
1154
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001155 if (skb->dev != ndev) {
1156 PRINT_ER("Packet not destined to this device\n");
1157 return 0;
1158 }
1159
Leo Kimb38e9032015-11-06 11:12:23 +09001160 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
Leo Kim3a147c02015-11-05 14:36:10 +09001161 if (!tx_data) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001162 PRINT_ER("Failed to allocate memory for tx_data structure\n");
1163 dev_kfree_skb(skb);
1164 netif_wake_queue(ndev);
1165 return 0;
1166 }
1167
1168 tx_data->buff = skb->data;
1169 tx_data->size = skb->len;
1170 tx_data->skb = skb;
1171
1172 eth_h = (struct ethhdr *)(skb->data);
Kim, Leofc4b95d2015-07-28 17:47:37 +09001173 if (eth_h->h_proto == 0x8e88)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001174 PRINT_D(INIT_DBG, "EAPOL transmitted\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001175
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001176 ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
1177
Leo Kimfd8f0362015-11-05 14:36:39 +09001178 udp_buf = (char *)ih + sizeof(struct iphdr);
1179 if ((udp_buf[1] == 68 && udp_buf[3] == 67) ||
1180 (udp_buf[1] == 67 && udp_buf[3] == 68))
1181 PRINT_D(GENERIC_DBG, "DHCP Message transmitted, type:%x %x %x\n",
1182 udp_buf[248], udp_buf[249], udp_buf[250]);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001183
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001184 PRINT_D(TX_DBG, "Sending packet - Size = %d - Address = %p - SKB = %p\n", tx_data->size, tx_data->buff, tx_data->skb);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001185 PRINT_D(TX_DBG, "Adding tx packet to TX Queue\n");
1186 nic->netstats.tx_packets++;
1187 nic->netstats.tx_bytes += tx_data->size;
Glen Leeb7495be2015-10-27 18:28:01 +09001188 tx_data->pBssid = wilc->vif[nic->u8IfIdx].bssid;
Leo Kim44ec3b72015-11-05 14:36:38 +09001189 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
1190 tx_data->buff, tx_data->size,
1191 linux_wlan_tx_complete);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001192
Leo Kim44ec3b72015-11-05 14:36:38 +09001193 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
Glen Leeb7495be2015-10-27 18:28:01 +09001194 netif_stop_queue(wilc->vif[0].ndev);
1195 netif_stop_queue(wilc->vif[1].ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001196 }
1197
1198 return 0;
1199}
1200
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001201int wilc_mac_close(struct net_device *ndev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001202{
Chaehyun Lim27268872015-09-15 14:06:13 +09001203 struct wilc_priv *priv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001204 perInterface_wlan_t *nic;
Leo Kim2db2c8a2015-11-05 14:36:15 +09001205 struct host_if_drv *hif_drv;
Glen Leeca64ad62015-10-20 17:13:55 +09001206 struct wilc *wl;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001207
1208 nic = netdev_priv(ndev);
1209
Leo Kim3a147c02015-11-05 14:36:10 +09001210 if (!nic || !nic->wilc_netdev || !nic->wilc_netdev->ieee80211_ptr ||
1211 !nic->wilc_netdev->ieee80211_ptr->wiphy) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001212 PRINT_ER("nic = NULL\n");
1213 return 0;
1214 }
1215
1216 priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
Glen Leeca64ad62015-10-20 17:13:55 +09001217 wl = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001218
Leo Kim3a147c02015-11-05 14:36:10 +09001219 if (!priv) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001220 PRINT_ER("priv = NULL\n");
1221 return 0;
1222 }
1223
Leo Kim2db2c8a2015-11-05 14:36:15 +09001224 hif_drv = (struct host_if_drv *)priv->hWILCWFIDrv;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001225
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001226 PRINT_D(GENERIC_DBG, "Mac close\n");
1227
Glen Leeca64ad62015-10-20 17:13:55 +09001228 if (!wl) {
1229 PRINT_ER("wl = NULL\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001230 return 0;
1231 }
1232
Leo Kim2db2c8a2015-11-05 14:36:15 +09001233 if (!hif_drv) {
1234 PRINT_ER("hif_drv = NULL\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001235 return 0;
1236 }
1237
Glen Leeca64ad62015-10-20 17:13:55 +09001238 if ((wl->open_ifcs) > 0) {
1239 wl->open_ifcs--;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001240 } else {
1241 PRINT_ER("ERROR: MAC close called while number of opened interfaces is zero\n");
1242 return 0;
1243 }
1244
Leo Kim3a147c02015-11-05 14:36:10 +09001245 if (nic->wilc_netdev) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001246 netif_stop_queue(nic->wilc_netdev);
1247
Chaehyun Lima9a16822015-09-20 15:51:24 +09001248 wilc_deinit_host_int(nic->wilc_netdev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001249 }
1250
Glen Leeca64ad62015-10-20 17:13:55 +09001251 if (wl->open_ifcs == 0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001252 PRINT_D(GENERIC_DBG, "Deinitializing wilc1000\n");
Glen Leeca64ad62015-10-20 17:13:55 +09001253 wl->close = 1;
Glen Lee53dc0cf2015-10-20 17:13:57 +09001254 wilc1000_wlan_deinit(ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001255 WILC_WFI_deinit_mon_interface();
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001256 }
1257
Greg Kroah-Hartman8990d852015-09-03 20:07:58 -07001258 up(&close_exit_sync);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001259 nic->mac_opened = 0;
1260
1261 return 0;
1262}
1263
Arnd Bergmann1608c402015-11-16 15:04:53 +01001264static int mac_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001265{
Greg Kroah-Hartman63d03e42015-06-02 14:16:04 +09001266 u8 *buff = NULL;
Chaehyun Limca356ad2015-06-11 14:35:57 +09001267 s8 rssi;
Chaehyun Lim4e4467f2015-06-11 14:35:55 +09001268 u32 size = 0, length = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001269 perInterface_wlan_t *nic;
Chaehyun Lim27268872015-09-15 14:06:13 +09001270 struct wilc_priv *priv;
Leo Kim9457b052015-11-05 14:36:37 +09001271 s32 ret = 0;
Glen Lee07320b62015-10-27 18:27:53 +09001272 struct wilc *wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001273
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001274 nic = netdev_priv(ndev);
Glen Lee07320b62015-10-27 18:27:53 +09001275 wilc = nic->wilc;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001276
Glen Lee07320b62015-10-27 18:27:53 +09001277 if (!wilc->initialized)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001278 return 0;
1279
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001280 switch (cmd) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001281 case SIOCSIWPRIV:
1282 {
Leo Kim98b89842015-11-05 14:36:18 +09001283 struct iwreq *wrq = (struct iwreq *) req;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001284
1285 size = wrq->u.data.length;
1286
1287 if (size && wrq->u.data.pointer) {
Leo Kim582f8a22015-11-05 14:36:21 +09001288 buff = memdup_user(wrq->u.data.pointer,
1289 wrq->u.data.length);
Sudip Mukherjee360e27a2015-09-04 15:34:15 +05301290 if (IS_ERR(buff))
1291 return PTR_ERR(buff);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001292
1293 if (strncasecmp(buff, "RSSI", length) == 0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001294 priv = wiphy_priv(nic->wilc_netdev->ieee80211_ptr->wiphy);
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001295 ret = wilc_get_rssi(priv->hWILCWFIDrv, &rssi);
Leo Kim9457b052015-11-05 14:36:37 +09001296 if (ret)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001297 PRINT_ER("Failed to send get rssi param's message queue ");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001298 PRINT_INFO(GENERIC_DBG, "RSSI :%d\n", rssi);
1299
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001300 rssi += 5;
1301
1302 snprintf(buff, size, "rssi %d", rssi);
1303
1304 if (copy_to_user(wrq->u.data.pointer, buff, size)) {
Chaehyun Lim60cb1e22015-06-19 09:06:28 +09001305 PRINT_ER("%s: failed to copy data to user buffer\n", __func__);
Leo Kim9457b052015-11-05 14:36:37 +09001306 ret = -EFAULT;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001307 goto done;
1308 }
1309 }
1310 }
1311 }
1312 break;
1313
1314 default:
1315 {
1316 PRINT_INFO(GENERIC_DBG, "Command - %d - has been received\n", cmd);
Leo Kim9457b052015-11-05 14:36:37 +09001317 ret = -EOPNOTSUPP;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001318 goto done;
1319 }
1320 }
1321
1322done:
1323
Shraddha Barke642ac6c2015-08-10 13:30:33 +05301324 kfree(buff);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001325
Leo Kim9457b052015-11-05 14:36:37 +09001326 return ret;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001327}
1328
Glen Leecb1991a2015-10-27 18:27:56 +09001329void frmw_to_linux(struct wilc *wilc, u8 *buff, u32 size, u32 pkt_offset)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001330{
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001331 unsigned int frame_len = 0;
1332 int stats;
1333 unsigned char *buff_to_send = NULL;
1334 struct sk_buff *skb;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001335 struct net_device *wilc_netdev;
1336 perInterface_wlan_t *nic;
1337
Leo Kim7e725b42015-11-05 14:36:24 +09001338 wilc_netdev = get_if_handler(wilc, buff);
Leo Kim3a147c02015-11-05 14:36:10 +09001339 if (!wilc_netdev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001340 return;
1341
1342 buff += pkt_offset;
1343 nic = netdev_priv(wilc_netdev);
1344
1345 if (size > 0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001346 frame_len = size;
1347 buff_to_send = buff;
1348
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001349 skb = dev_alloc_skb(frame_len);
Leo Kim3a147c02015-11-05 14:36:10 +09001350 if (!skb) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001351 PRINT_ER("Low memory - packet droped\n");
1352 return;
1353 }
1354
Leo Kim3a147c02015-11-05 14:36:10 +09001355 if (!wilc || !wilc_netdev)
Glen Leecb1991a2015-10-27 18:27:56 +09001356 PRINT_ER("wilc_netdev in wilc is NULL");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001357 skb->dev = wilc_netdev;
1358
Leo Kim3a147c02015-11-05 14:36:10 +09001359 if (!skb->dev)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001360 PRINT_ER("skb->dev is NULL\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001361
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001362 memcpy(skb_put(skb, frame_len), buff_to_send, frame_len);
1363
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001364 skb->protocol = eth_type_trans(skb, wilc_netdev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001365 nic->netstats.rx_packets++;
1366 nic->netstats.rx_bytes += frame_len;
1367 skb->ip_summed = CHECKSUM_UNNECESSARY;
1368 stats = netif_rx(skb);
1369 PRINT_D(RX_DBG, "netif_rx ret value is: %d\n", stats);
1370 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001371}
1372
Glen Lee11f4b2e2015-10-27 18:27:51 +09001373void WILC_WFI_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001374{
1375 int i = 0;
1376 perInterface_wlan_t *nic;
1377
Glen Lee11f4b2e2015-10-27 18:27:51 +09001378 for (i = 0; i < wilc->vif_num; i++) {
1379 nic = netdev_priv(wilc->vif[i].ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001380 if (nic->monitor_flag) {
1381 WILC_WFI_monitor_rx(buff, size);
1382 return;
1383 }
1384 }
1385
Leo Kim98b89842015-11-05 14:36:18 +09001386 nic = netdev_priv(wilc->vif[1].ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001387 if ((buff[0] == nic->g_struct_frame_reg[0].frame_type && nic->g_struct_frame_reg[0].reg) ||
Kim, Leofc4b95d2015-07-28 17:47:37 +09001388 (buff[0] == nic->g_struct_frame_reg[1].frame_type && nic->g_struct_frame_reg[1].reg))
Glen Lee11f4b2e2015-10-27 18:27:51 +09001389 WILC_WFI_p2p_rx(wilc->vif[1].ndev, buff, size);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001390}
1391
Arnd Bergmann857c7b02015-11-16 15:05:00 +01001392void wilc_netdev_cleanup(struct wilc *wilc)
Tony Cho4875c492015-10-20 14:26:52 +09001393{
1394 int i = 0;
1395 perInterface_wlan_t *nic[NUM_CONCURRENT_IFC];
1396
Leo Kim83231b72015-11-06 11:12:22 +09001397 if (wilc && (wilc->vif[0].ndev || wilc->vif[1].ndev)) {
Tony Cho4875c492015-10-20 14:26:52 +09001398 unregister_inetaddr_notifier(&g_dev_notifier);
1399
1400 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
Glen Lee90b984c2015-10-29 12:18:50 +09001401 nic[i] = netdev_priv(wilc->vif[i].ndev);
Tony Cho4875c492015-10-20 14:26:52 +09001402 }
1403
Glen Lee90b984c2015-10-29 12:18:50 +09001404 if (wilc && wilc->firmware)
1405 release_firmware(wilc->firmware);
Tony Cho4875c492015-10-20 14:26:52 +09001406
Leo Kim5191d772015-11-05 14:36:22 +09001407 if (wilc && (wilc->vif[0].ndev || wilc->vif[1].ndev)) {
Tony Cho4875c492015-10-20 14:26:52 +09001408 linux_wlan_lock_timeout(&close_exit_sync, 12 * 1000);
1409
1410 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
Glen Lee90b984c2015-10-29 12:18:50 +09001411 if (wilc->vif[i].ndev)
Tony Cho4875c492015-10-20 14:26:52 +09001412 if (nic[i]->mac_opened)
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001413 wilc_mac_close(wilc->vif[i].ndev);
Tony Cho4875c492015-10-20 14:26:52 +09001414
1415 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
Glen Lee90b984c2015-10-29 12:18:50 +09001416 unregister_netdev(wilc->vif[i].ndev);
1417 wilc_free_wiphy(wilc->vif[i].ndev);
1418 free_netdev(wilc->vif[i].ndev);
Tony Cho4875c492015-10-20 14:26:52 +09001419 }
1420 }
1421
Glen Lee90b984c2015-10-29 12:18:50 +09001422 kfree(wilc);
Tony Cho4875c492015-10-20 14:26:52 +09001423
1424#if defined(WILC_DEBUGFS)
1425 wilc_debugfs_remove();
1426#endif
Tony Cho4875c492015-10-20 14:26:52 +09001427}
1428
Tony Cho12ba5412015-10-20 14:26:56 +09001429int wilc_netdev_init(struct wilc **wilc)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001430{
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001431 int i;
1432 perInterface_wlan_t *nic;
1433 struct net_device *ndev;
1434
Greg Kroah-Hartman642768e2015-09-03 19:56:21 -07001435 sema_init(&close_exit_sync, 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001436
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001437 wilc_dev = kzalloc(sizeof(*wilc_dev), GFP_KERNEL);
1438 if (!wilc_dev)
Chaehyun Limac61ef82015-09-08 00:36:36 +09001439 return -ENOMEM;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001440
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001441 *wilc = wilc_dev;
Tony Cho12ba5412015-10-20 14:26:56 +09001442
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001443 register_inetaddr_notifier(&g_dev_notifier);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001444
1445 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001446 ndev = alloc_etherdev(sizeof(perInterface_wlan_t));
1447 if (!ndev) {
1448 PRINT_ER("Failed to allocate ethernet dev\n");
1449 return -1;
1450 }
1451
1452 nic = netdev_priv(ndev);
1453 memset(nic, 0, sizeof(perInterface_wlan_t));
1454
Leo Kim84d3b872015-11-05 14:36:19 +09001455 if (i == 0)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001456 strcpy(ndev->name, "wlan%d");
Leo Kim84d3b872015-11-05 14:36:19 +09001457 else
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001458 strcpy(ndev->name, "p2p%d");
1459
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001460 nic->u8IfIdx = wilc_dev->vif_num;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001461 nic->wilc_netdev = ndev;
Glen Lee38b3001d2015-10-20 17:13:51 +09001462 nic->wilc = *wilc;
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001463 wilc_dev->vif[wilc_dev->vif_num].ndev = ndev;
1464 wilc_dev->vif_num++;
Arnd Bergmanne5af0562015-05-29 22:52:12 +02001465 ndev->netdev_ops = &wilc_netdev_ops;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001466
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001467 {
1468 struct wireless_dev *wdev;
Chaehyun Lim8459fd52015-09-20 15:51:09 +09001469 wdev = wilc_create_wiphy(ndev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001470
1471 #ifdef WILC_SDIO
Arnd Bergmann0e1af732015-11-16 15:04:54 +01001472 SET_NETDEV_DEV(ndev, &wilc_sdio_func->dev);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001473 #endif
1474
Leo Kim3a147c02015-11-05 14:36:10 +09001475 if (!wdev) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001476 PRINT_ER("Can't register WILC Wiphy\n");
1477 return -1;
1478 }
1479
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001480 nic->wilc_netdev->ieee80211_ptr = wdev;
1481 nic->wilc_netdev->ml_priv = nic;
1482 wdev->netdev = nic->wilc_netdev;
1483 nic->netstats.rx_packets = 0;
1484 nic->netstats.tx_packets = 0;
1485 nic->netstats.rx_bytes = 0;
1486 nic->netstats.tx_bytes = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001487 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001488
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001489 if (register_netdev(ndev)) {
Leo Kim582f8a22015-11-05 14:36:21 +09001490 PRINT_ER("Device couldn't be registered - %s\n",
1491 ndev->name);
Leo Kim98b89842015-11-05 14:36:18 +09001492 return -1;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001493 }
1494
1495 nic->iftype = STATION_MODE;
1496 nic->mac_opened = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001497 }
1498
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001499 return 0;
1500}