Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / staging / wilc1000 / wilc_netdev.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4  * All rights reserved.
5  */
6
7 #include <linux/irq.h>
8 #include <linux/kthread.h>
9 #include <linux/firmware.h>
10 #include <linux/netdevice.h>
11 #include <linux/inetdevice.h>
12
13 #include "wilc_wfi_cfgoperations.h"
14
15 #define WILC_MULTICAST_TABLE_SIZE       8
16
17 static irqreturn_t isr_uh_routine(int irq, void *user_data)
18 {
19         struct net_device *dev = user_data;
20         struct wilc_vif *vif = netdev_priv(dev);
21         struct wilc *wilc = vif->wilc;
22
23         if (wilc->close) {
24                 netdev_err(dev, "Can't handle UH interrupt\n");
25                 return IRQ_HANDLED;
26         }
27         return IRQ_WAKE_THREAD;
28 }
29
30 static irqreturn_t isr_bh_routine(int irq, void *userdata)
31 {
32         struct net_device *dev = userdata;
33         struct wilc_vif *vif = netdev_priv(userdata);
34         struct wilc *wilc = vif->wilc;
35
36         if (wilc->close) {
37                 netdev_err(dev, "Can't handle BH interrupt\n");
38                 return IRQ_HANDLED;
39         }
40
41         wilc_handle_isr(wilc);
42
43         return IRQ_HANDLED;
44 }
45
46 static int init_irq(struct net_device *dev)
47 {
48         int ret = 0;
49         struct wilc_vif *vif = netdev_priv(dev);
50         struct wilc *wl = vif->wilc;
51
52         ret = gpiod_direction_input(wl->gpio_irq);
53         if (ret) {
54                 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
55                 return ret;
56         }
57
58         wl->dev_irq_num = gpiod_to_irq(wl->gpio_irq);
59
60         ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
61                                    isr_bh_routine,
62                                    IRQF_TRIGGER_LOW | IRQF_ONESHOT,
63                                    "WILC_IRQ", dev);
64         if (ret < 0)
65                 netdev_err(dev, "Failed to request IRQ\n");
66         else
67                 netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n",
68                            wl->dev_irq_num);
69
70         return ret;
71 }
72
73 static void deinit_irq(struct net_device *dev)
74 {
75         struct wilc_vif *vif = netdev_priv(dev);
76         struct wilc *wilc = vif->wilc;
77
78         /* Deinitialize IRQ */
79         if (wilc->dev_irq_num)
80                 free_irq(wilc->dev_irq_num, wilc);
81 }
82
83 void wilc_mac_indicate(struct wilc *wilc)
84 {
85         s8 status;
86
87         wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
88         if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
89                 wilc->mac_status = status;
90                 complete(&wilc->sync_event);
91         } else {
92                 wilc->mac_status = status;
93         }
94 }
95
96 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
97 {
98         u8 *bssid, *bssid1;
99         int i = 0;
100         struct net_device *ndev = NULL;
101
102         bssid = mac_header + 10;
103         bssid1 = mac_header + 4;
104
105         mutex_lock(&wilc->vif_mutex);
106         for (i = 0; i < wilc->vif_num; i++) {
107                 if (wilc->vif[i]->mode == WILC_STATION_MODE)
108                         if (ether_addr_equal_unaligned(bssid,
109                                                        wilc->vif[i]->bssid)) {
110                                 ndev = wilc->vif[i]->ndev;
111                                 goto out;
112                         }
113                 if (wilc->vif[i]->mode == WILC_AP_MODE)
114                         if (ether_addr_equal_unaligned(bssid1,
115                                                        wilc->vif[i]->bssid)) {
116                                 ndev = wilc->vif[i]->ndev;
117                                 goto out;
118                         }
119         }
120 out:
121         mutex_unlock(&wilc->vif_mutex);
122         return ndev;
123 }
124
125 void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
126 {
127         struct wilc_vif *vif = netdev_priv(wilc_netdev);
128
129         if (bssid)
130                 ether_addr_copy(vif->bssid, bssid);
131         else
132                 eth_zero_addr(vif->bssid);
133
134         vif->mode = mode;
135 }
136
137 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
138 {
139         u8 i = 0;
140         u8 ret_val = 0;
141
142         for (i = 0; i < wilc->vif_num; i++)
143                 if (!is_zero_ether_addr(wilc->vif[i]->bssid))
144                         ret_val++;
145
146         return ret_val;
147 }
148
149 static int wilc_txq_task(void *vp)
150 {
151         int ret;
152         u32 txq_count;
153         struct wilc *wl = vp;
154
155         complete(&wl->txq_thread_started);
156         while (1) {
157                 wait_for_completion(&wl->txq_event);
158
159                 if (wl->close) {
160                         complete(&wl->txq_thread_started);
161
162                         while (!kthread_should_stop())
163                                 schedule();
164                         break;
165                 }
166                 do {
167                         ret = wilc_wlan_handle_txq(wl, &txq_count);
168                         if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
169                                 int i;
170                                 struct wilc_vif *ifc;
171
172                                 mutex_lock(&wl->vif_mutex);
173                                 for (i = 0; i < wl->vif_num; i++) {
174                                         ifc = wl->vif[i];
175                                         if (ifc->mac_opened && ifc->ndev)
176                                                 netif_wake_queue(ifc->ndev);
177                                 }
178                                 mutex_unlock(&wl->vif_mutex);
179                         }
180                 } while (ret == -ENOBUFS && !wl->close);
181         }
182         return 0;
183 }
184
185 static int wilc_wlan_get_firmware(struct net_device *dev)
186 {
187         struct wilc_vif *vif = netdev_priv(dev);
188         struct wilc *wilc = vif->wilc;
189         int chip_id, ret = 0;
190         const struct firmware *wilc_firmware;
191         char *firmware;
192
193         chip_id = wilc_get_chipid(wilc, false);
194
195         if (chip_id < 0x1003a0)
196                 firmware = FIRMWARE_1002;
197         else
198                 firmware = FIRMWARE_1003;
199
200         netdev_info(dev, "loading firmware %s\n", firmware);
201
202         if (reject_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
203                 netdev_err(dev, "%s - firmware not available\n", firmware);
204                 ret = -1;
205                 goto fail;
206         }
207         wilc->firmware = wilc_firmware;
208
209 fail:
210
211         return ret;
212 }
213
214 static int wilc_start_firmware(struct net_device *dev)
215 {
216         struct wilc_vif *vif = netdev_priv(dev);
217         struct wilc *wilc = vif->wilc;
218         int ret = 0;
219
220         ret = wilc_wlan_start(wilc);
221         if (ret < 0)
222                 return ret;
223
224         if (!wait_for_completion_timeout(&wilc->sync_event,
225                                          msecs_to_jiffies(5000)))
226                 return -ETIME;
227
228         return 0;
229 }
230
231 static int wilc1000_firmware_download(struct net_device *dev)
232 {
233         struct wilc_vif *vif = netdev_priv(dev);
234         struct wilc *wilc = vif->wilc;
235         int ret = 0;
236
237         if (!wilc->firmware) {
238                 netdev_err(dev, "Firmware buffer is NULL\n");
239                 return -ENOBUFS;
240         }
241
242         ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
243                                           wilc->firmware->size);
244         if (ret < 0)
245                 return ret;
246
247         release_firmware(wilc->firmware);
248         wilc->firmware = NULL;
249
250         netdev_dbg(dev, "Download Succeeded\n");
251
252         return 0;
253 }
254
255 static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
256 {
257         struct wilc_priv *priv = &vif->priv;
258         struct host_if_drv *hif_drv;
259         u8 b;
260         u16 hw;
261         u32 w;
262
263         netdev_dbg(dev, "Start configuring Firmware\n");
264         hif_drv = (struct host_if_drv *)priv->hif_drv;
265         netdev_dbg(dev, "Host = %p\n", hif_drv);
266
267         w = vif->iftype;
268         cpu_to_le32s(&w);
269         if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
270                                0, 0))
271                 goto fail;
272
273         b = WILC_FW_BSS_TYPE_INFRA;
274         if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
275                 goto fail;
276
277         b = WILC_FW_TX_RATE_AUTO;
278         if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
279                 goto fail;
280
281         b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
282         if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
283                 goto fail;
284
285         b = WILC_FW_PREAMBLE_SHORT;
286         if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
287                 goto fail;
288
289         b = WILC_FW_11N_PROT_AUTO;
290         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
291                 goto fail;
292
293         b = WILC_FW_ACTIVE_SCAN;
294         if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
295                 goto fail;
296
297         b = WILC_FW_SITE_SURVEY_OFF;
298         if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
299                 goto fail;
300
301         hw = 0xffff;
302         cpu_to_le16s(&hw);
303         if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
304                 goto fail;
305
306         hw = 2346;
307         cpu_to_le16s(&hw);
308         if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
309                 goto fail;
310
311         b = 0;
312         if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
313                 goto fail;
314
315         b = 1;
316         if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
317                 goto fail;
318
319         b = WILC_FW_NO_POWERSAVE;
320         if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
321                 goto fail;
322
323         b = WILC_FW_SEC_NO;
324         if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
325                 goto fail;
326
327         b = WILC_FW_AUTH_OPEN_SYSTEM;
328         if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
329                 goto fail;
330
331         b = 3;
332         if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
333                 goto fail;
334
335         b = 3;
336         if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
337                 goto fail;
338
339         b = WILC_FW_ACK_POLICY_NORMAL;
340         if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
341                 goto fail;
342
343         b = 0;
344         if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
345                                0, 0))
346                 goto fail;
347
348         b = 48;
349         if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
350                 goto fail;
351
352         b = 28;
353         if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
354                 goto fail;
355
356         hw = 100;
357         cpu_to_le16s(&hw);
358         if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
359                 goto fail;
360
361         b = WILC_FW_REKEY_POLICY_DISABLE;
362         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
363                 goto fail;
364
365         w = 84600;
366         cpu_to_le32s(&w);
367         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
368                 goto fail;
369
370         w = 500;
371         cpu_to_le32s(&w);
372         if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
373                                0))
374                 goto fail;
375
376         b = 1;
377         if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
378                                0))
379                 goto fail;
380
381         b = WILC_FW_ERP_PROT_SELF_CTS;
382         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
383                 goto fail;
384
385         b = 1;
386         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
387                 goto fail;
388
389         b = WILC_FW_11N_OP_MODE_HT_MIXED;
390         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
391                 goto fail;
392
393         b = 1;
394         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
395                 goto fail;
396
397         b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
398         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
399                                0, 0))
400                 goto fail;
401
402         b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
403         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
404                 goto fail;
405
406         b = 0;
407         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
408                                0))
409                 goto fail;
410
411         b = 7;
412         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
413                 goto fail;
414
415         b = 1;
416         if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
417                                1, 1))
418                 goto fail;
419
420         return 0;
421
422 fail:
423         return -1;
424 }
425
426 static void wlan_deinit_locks(struct net_device *dev)
427 {
428         struct wilc_vif *vif = netdev_priv(dev);
429         struct wilc *wilc = vif->wilc;
430
431         mutex_destroy(&wilc->hif_cs);
432         mutex_destroy(&wilc->rxq_cs);
433         mutex_destroy(&wilc->cfg_cmd_lock);
434         mutex_destroy(&wilc->txq_add_to_head_cs);
435         mutex_destroy(&wilc->vif_mutex);
436 }
437
438 static void wlan_deinitialize_threads(struct net_device *dev)
439 {
440         struct wilc_vif *vif = netdev_priv(dev);
441         struct wilc *wl = vif->wilc;
442
443         wl->close = 1;
444
445         complete(&wl->txq_event);
446
447         if (wl->txq_thread) {
448                 kthread_stop(wl->txq_thread);
449                 wl->txq_thread = NULL;
450         }
451 }
452
453 static void wilc_wlan_deinitialize(struct net_device *dev)
454 {
455         struct wilc_vif *vif = netdev_priv(dev);
456         struct wilc *wl = vif->wilc;
457
458         if (!wl) {
459                 netdev_err(dev, "wl is NULL\n");
460                 return;
461         }
462
463         if (wl->initialized) {
464                 netdev_info(dev, "Deinitializing wilc1000...\n");
465
466                 if (!wl->dev_irq_num &&
467                     wl->hif_func->disable_interrupt) {
468                         mutex_lock(&wl->hif_cs);
469                         wl->hif_func->disable_interrupt(wl);
470                         mutex_unlock(&wl->hif_cs);
471                 }
472                 complete(&wl->txq_event);
473
474                 wlan_deinitialize_threads(dev);
475                 deinit_irq(dev);
476
477                 wilc_wlan_stop(wl);
478                 wilc_wlan_cleanup(dev);
479                 wlan_deinit_locks(dev);
480
481                 wl->initialized = false;
482
483                 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
484         } else {
485                 netdev_dbg(dev, "wilc1000 is not initialized\n");
486         }
487 }
488
489 static int wlan_initialize_threads(struct net_device *dev)
490 {
491         struct wilc_vif *vif = netdev_priv(dev);
492         struct wilc *wilc = vif->wilc;
493
494         wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
495                                        "K_TXQ_TASK");
496         if (IS_ERR(wilc->txq_thread)) {
497                 netdev_err(dev, "couldn't create TXQ thread\n");
498                 wilc->close = 0;
499                 return PTR_ERR(wilc->txq_thread);
500         }
501         wait_for_completion(&wilc->txq_thread_started);
502
503         return 0;
504 }
505
506 static int dev_state_ev_handler(struct notifier_block *this,
507                                 unsigned long event, void *ptr);
508 static struct notifier_block g_dev_notifier = {
509         .notifier_call = dev_state_ev_handler
510 };
511
512 static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
513 {
514         int ret = 0;
515         struct wilc *wl = vif->wilc;
516
517         if (!wl->initialized) {
518                 wl->mac_status = WILC_MAC_STATUS_INIT;
519                 wl->close = 0;
520
521                 ret = wilc_wlan_init(dev);
522                 if (ret < 0)
523                         return -EIO;
524
525                 ret = wlan_initialize_threads(dev);
526                 if (ret < 0) {
527                         ret = -EIO;
528                         goto fail_wilc_wlan;
529                 }
530
531                 if (wl->gpio_irq && init_irq(dev)) {
532                         ret = -EIO;
533                         goto fail_threads;
534                 }
535
536                 if (!wl->dev_irq_num &&
537                     wl->hif_func->enable_interrupt &&
538                     wl->hif_func->enable_interrupt(wl)) {
539                         ret = -EIO;
540                         goto fail_irq_init;
541                 }
542
543                 if (wilc_wlan_get_firmware(dev)) {
544                         ret = -EIO;
545                         goto fail_irq_enable;
546                 }
547
548                 ret = wilc1000_firmware_download(dev);
549                 if (ret < 0) {
550                         ret = -EIO;
551                         goto fail_irq_enable;
552                 }
553
554                 ret = wilc_start_firmware(dev);
555                 if (ret < 0) {
556                         ret = -EIO;
557                         goto fail_irq_enable;
558                 }
559
560                 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
561                         int size;
562                         char firmware_ver[20];
563
564                         size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
565                                                      firmware_ver,
566                                                      sizeof(firmware_ver));
567                         firmware_ver[size] = '\0';
568                         netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
569                 }
570                 ret = wilc_init_fw_config(dev, vif);
571
572                 if (ret < 0) {
573                         netdev_err(dev, "Failed to configure firmware\n");
574                         ret = -EIO;
575                         goto fail_fw_start;
576                 }
577                 register_inetaddr_notifier(&g_dev_notifier);
578                 wl->initialized = true;
579                 return 0;
580
581 fail_fw_start:
582                 wilc_wlan_stop(wl);
583
584 fail_irq_enable:
585                 if (!wl->dev_irq_num &&
586                     wl->hif_func->disable_interrupt)
587                         wl->hif_func->disable_interrupt(wl);
588 fail_irq_init:
589                 if (wl->dev_irq_num)
590                         deinit_irq(dev);
591 fail_threads:
592                 wlan_deinitialize_threads(dev);
593 fail_wilc_wlan:
594                 wilc_wlan_cleanup(dev);
595                 netdev_err(dev, "WLAN initialization FAILED\n");
596         } else {
597                 netdev_dbg(dev, "wilc1000 already initialized\n");
598         }
599         return ret;
600 }
601
602 static int mac_init_fn(struct net_device *ndev)
603 {
604         netif_start_queue(ndev);
605         netif_stop_queue(ndev);
606
607         return 0;
608 }
609
610 static int wilc_mac_open(struct net_device *ndev)
611 {
612         struct wilc_vif *vif = netdev_priv(ndev);
613         struct wilc *wl = vif->wilc;
614         struct wilc_priv *priv = wdev_priv(vif->ndev->ieee80211_ptr);
615         unsigned char mac_add[ETH_ALEN] = {0};
616         int ret = 0;
617
618         if (!wl || !wl->dev) {
619                 netdev_err(ndev, "device not ready\n");
620                 return -ENODEV;
621         }
622
623         netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
624
625         ret = wilc_init_host_int(ndev);
626         if (ret < 0)
627                 return ret;
628
629         ret = wilc_wlan_initialize(ndev, vif);
630         if (ret < 0) {
631                 wilc_deinit_host_int(ndev);
632                 return ret;
633         }
634
635         wilc_set_wfi_drv_handler(vif, wilc_get_vif_idx(vif), vif->iftype,
636                                  vif->idx);
637         wilc_set_operation_mode(vif, vif->iftype);
638
639         wilc_get_mac_address(vif, mac_add);
640         netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
641         ether_addr_copy(ndev->dev_addr, mac_add);
642
643         if (!is_valid_ether_addr(ndev->dev_addr)) {
644                 netdev_err(ndev, "Wrong MAC address\n");
645                 wilc_deinit_host_int(ndev);
646                 wilc_wlan_deinitialize(ndev);
647                 return -EINVAL;
648         }
649
650         wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
651                                  vif->ndev->ieee80211_ptr,
652                                  vif->frame_reg[0].type,
653                                  vif->frame_reg[0].reg);
654         wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
655                                  vif->ndev->ieee80211_ptr,
656                                  vif->frame_reg[1].type,
657                                  vif->frame_reg[1].reg);
658         netif_wake_queue(ndev);
659         wl->open_ifcs++;
660         priv->p2p.local_random = 0x01;
661         vif->mac_opened = 1;
662         return 0;
663 }
664
665 static struct net_device_stats *mac_stats(struct net_device *dev)
666 {
667         struct wilc_vif *vif = netdev_priv(dev);
668
669         return &vif->netstats;
670 }
671
672 static void wilc_set_multicast_list(struct net_device *dev)
673 {
674         struct netdev_hw_addr *ha;
675         struct wilc_vif *vif = netdev_priv(dev);
676         int i;
677         u8 *mc_list;
678         u8 *cur_mc;
679
680         if (dev->flags & IFF_PROMISC)
681                 return;
682
683         if (dev->flags & IFF_ALLMULTI ||
684             dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
685                 wilc_setup_multicast_filter(vif, 0, 0, NULL);
686                 return;
687         }
688
689         if (dev->mc.count == 0) {
690                 wilc_setup_multicast_filter(vif, 1, 0, NULL);
691                 return;
692         }
693
694         mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
695         if (!mc_list)
696                 return;
697
698         cur_mc = mc_list;
699         i = 0;
700         netdev_for_each_mc_addr(ha, dev) {
701                 memcpy(cur_mc, ha->addr, ETH_ALEN);
702                 netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
703                 i++;
704                 cur_mc += ETH_ALEN;
705         }
706
707         if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
708                 kfree(mc_list);
709 }
710
711 static void wilc_tx_complete(void *priv, int status)
712 {
713         struct tx_complete_data *pv_data = priv;
714
715         dev_kfree_skb(pv_data->skb);
716         kfree(pv_data);
717 }
718
719 netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
720 {
721         struct wilc_vif *vif = netdev_priv(ndev);
722         struct wilc *wilc = vif->wilc;
723         struct tx_complete_data *tx_data = NULL;
724         int queue_count;
725
726         if (skb->dev != ndev) {
727                 netdev_err(ndev, "Packet not destined to this device\n");
728                 return 0;
729         }
730
731         tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
732         if (!tx_data) {
733                 dev_kfree_skb(skb);
734                 netif_wake_queue(ndev);
735                 return 0;
736         }
737
738         tx_data->buff = skb->data;
739         tx_data->size = skb->len;
740         tx_data->skb  = skb;
741
742         vif->netstats.tx_packets++;
743         vif->netstats.tx_bytes += tx_data->size;
744         queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
745                                                 tx_data->buff, tx_data->size,
746                                                 wilc_tx_complete);
747
748         if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
749                 int i;
750
751                 mutex_lock(&wilc->vif_mutex);
752                 for (i = 0; i < wilc->vif_num; i++) {
753                         if (wilc->vif[i]->mac_opened)
754                                 netif_stop_queue(wilc->vif[i]->ndev);
755                 }
756                 mutex_unlock(&wilc->vif_mutex);
757         }
758
759         return 0;
760 }
761
762 static int wilc_mac_close(struct net_device *ndev)
763 {
764         struct wilc_vif *vif = netdev_priv(ndev);
765         struct wilc *wl = vif->wilc;
766
767         netdev_dbg(ndev, "Mac close\n");
768
769         if (wl->open_ifcs > 0)
770                 wl->open_ifcs--;
771         else
772                 return 0;
773
774         if (vif->ndev) {
775                 netif_stop_queue(vif->ndev);
776
777                 wilc_deinit_host_int(vif->ndev);
778         }
779
780         if (wl->open_ifcs == 0) {
781                 netdev_dbg(ndev, "Deinitializing wilc1000\n");
782                 wl->close = 1;
783                 unregister_inetaddr_notifier(&g_dev_notifier);
784                 wilc_wlan_deinitialize(ndev);
785         }
786
787         vif->mac_opened = 0;
788
789         return 0;
790 }
791
792 void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
793                        u32 pkt_offset)
794 {
795         unsigned int frame_len = 0;
796         int stats;
797         unsigned char *buff_to_send = NULL;
798         struct sk_buff *skb;
799         struct net_device *wilc_netdev;
800         struct wilc_vif *vif;
801
802         if (!wilc)
803                 return;
804
805         wilc_netdev = get_if_handler(wilc, buff);
806         if (!wilc_netdev)
807                 return;
808
809         buff += pkt_offset;
810         vif = netdev_priv(wilc_netdev);
811
812         if (size > 0) {
813                 frame_len = size;
814                 buff_to_send = buff;
815
816                 skb = dev_alloc_skb(frame_len);
817                 if (!skb)
818                         return;
819
820                 skb->dev = wilc_netdev;
821
822                 skb_put_data(skb, buff_to_send, frame_len);
823
824                 skb->protocol = eth_type_trans(skb, wilc_netdev);
825                 vif->netstats.rx_packets++;
826                 vif->netstats.rx_bytes += frame_len;
827                 skb->ip_summed = CHECKSUM_UNNECESSARY;
828                 stats = netif_rx(skb);
829                 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
830         }
831 }
832
833 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
834 {
835         int i = 0;
836         struct wilc_vif *vif;
837
838         mutex_lock(&wilc->vif_mutex);
839         for (i = 0; i < wilc->vif_num; i++) {
840                 u16 type = le16_to_cpup((__le16 *)buff);
841
842                 vif = netdev_priv(wilc->vif[i]->ndev);
843                 if ((type == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
844                     (type == vif->frame_reg[1].type && vif->frame_reg[1].reg)) {
845                         wilc_wfi_p2p_rx(vif, buff, size);
846                         break;
847                 }
848
849                 if (vif->monitor_flag) {
850                         wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
851                         break;
852                 }
853         }
854         mutex_unlock(&wilc->vif_mutex);
855 }
856
857 static const struct net_device_ops wilc_netdev_ops = {
858         .ndo_init = mac_init_fn,
859         .ndo_open = wilc_mac_open,
860         .ndo_stop = wilc_mac_close,
861         .ndo_start_xmit = wilc_mac_xmit,
862         .ndo_get_stats = mac_stats,
863         .ndo_set_rx_mode  = wilc_set_multicast_list,
864 };
865
866 static int dev_state_ev_handler(struct notifier_block *this,
867                                 unsigned long event, void *ptr)
868 {
869         struct in_ifaddr *dev_iface = ptr;
870         struct wilc_priv *priv;
871         struct host_if_drv *hif_drv;
872         struct net_device *dev;
873         struct wilc_vif *vif;
874
875         if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev)
876                 return NOTIFY_DONE;
877
878         dev  = (struct net_device *)dev_iface->ifa_dev->dev;
879         if (dev->netdev_ops != &wilc_netdev_ops)
880                 return NOTIFY_DONE;
881
882         if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
883                 return NOTIFY_DONE;
884
885         vif = netdev_priv(dev);
886         priv = &vif->priv;
887
888         hif_drv = (struct host_if_drv *)priv->hif_drv;
889
890         switch (event) {
891         case NETDEV_UP:
892                 if (vif->iftype == WILC_STATION_MODE ||
893                     vif->iftype == WILC_CLIENT_MODE) {
894                         hif_drv->ifc_up = 1;
895                         vif->obtaining_ip = false;
896                         del_timer(&vif->during_ip_timer);
897                 }
898
899                 if (vif->wilc->enable_ps)
900                         wilc_set_power_mgmt(vif, 1, 0);
901
902                 break;
903
904         case NETDEV_DOWN:
905                 if (vif->iftype == WILC_STATION_MODE ||
906                     vif->iftype == WILC_CLIENT_MODE) {
907                         hif_drv->ifc_up = 0;
908                         vif->obtaining_ip = false;
909                         wilc_set_power_mgmt(vif, 0, 0);
910                 }
911
912                 wilc_resolve_disconnect_aberration(vif);
913
914                 break;
915
916         default:
917                 break;
918         }
919
920         return NOTIFY_DONE;
921 }
922
923 void wilc_netdev_cleanup(struct wilc *wilc)
924 {
925         int i;
926
927         if (!wilc)
928                 return;
929
930         if (wilc->firmware) {
931                 release_firmware(wilc->firmware);
932                 wilc->firmware = NULL;
933         }
934
935         for (i = 0; i < wilc->vif_num; i++) {
936                 if (wilc->vif[i] && wilc->vif[i]->ndev)
937                         unregister_netdev(wilc->vif[i]->ndev);
938         }
939
940         wilc_wfi_deinit_mon_interface(wilc, false);
941         flush_workqueue(wilc->hif_workqueue);
942         destroy_workqueue(wilc->hif_workqueue);
943         wilc_wlan_cfg_deinit(wilc);
944         kfree(wilc->bus_data);
945         wiphy_unregister(wilc->wiphy);
946         wiphy_free(wilc->wiphy);
947 }
948 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
949
950 struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
951                                       int vif_type, enum nl80211_iftype type,
952                                       bool rtnl_locked)
953 {
954         struct net_device *ndev;
955         struct wilc_vif *vif;
956         int ret;
957
958         ndev = alloc_etherdev(sizeof(*vif));
959         if (!ndev)
960                 return ERR_PTR(-ENOMEM);
961
962         vif = netdev_priv(ndev);
963         ndev->ieee80211_ptr = &vif->priv.wdev;
964         strcpy(ndev->name, name);
965         vif->wilc = wl;
966         vif->ndev = ndev;
967         ndev->ml_priv = vif;
968
969         ndev->netdev_ops = &wilc_netdev_ops;
970
971         SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
972
973         vif->priv.wdev.wiphy = wl->wiphy;
974         vif->priv.wdev.netdev = ndev;
975         vif->priv.wdev.iftype = type;
976         vif->priv.dev = ndev;
977
978         if (rtnl_locked)
979                 ret = register_netdevice(ndev);
980         else
981                 ret = register_netdev(ndev);
982
983         if (ret) {
984                 free_netdev(ndev);
985                 return ERR_PTR(-EFAULT);
986         }
987
988         ndev->needs_free_netdev = true;
989         vif->iftype = vif_type;
990         vif->wilc->vif[wl->vif_num] = vif;
991         vif->idx = wl->vif_num;
992         wl->vif_num += 1;
993         vif->mac_opened = 0;
994         return vif;
995 }
996
997 MODULE_LICENSE("GPL");