treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / staging / wilc1000 / netdev.c
blobfce5bf2d82faaccef098023508ca9757c73ab5fb
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4 * All rights reserved.
5 */
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>
13 #include "cfg80211.h"
14 #include "wlan_cfg.h"
16 #define WILC_MULTICAST_TABLE_SIZE 8
18 static irqreturn_t isr_uh_routine(int irq, void *user_data)
20 struct net_device *dev = user_data;
21 struct wilc_vif *vif = netdev_priv(dev);
22 struct wilc *wilc = vif->wilc;
24 if (wilc->close) {
25 netdev_err(dev, "Can't handle UH interrupt\n");
26 return IRQ_HANDLED;
28 return IRQ_WAKE_THREAD;
31 static irqreturn_t isr_bh_routine(int irq, void *userdata)
33 struct net_device *dev = userdata;
34 struct wilc_vif *vif = netdev_priv(userdata);
35 struct wilc *wilc = vif->wilc;
37 if (wilc->close) {
38 netdev_err(dev, "Can't handle BH interrupt\n");
39 return IRQ_HANDLED;
42 wilc_handle_isr(wilc);
44 return IRQ_HANDLED;
47 static int init_irq(struct net_device *dev)
49 int ret = 0;
50 struct wilc_vif *vif = netdev_priv(dev);
51 struct wilc *wl = vif->wilc;
53 ret = gpiod_direction_input(wl->gpio_irq);
54 if (ret) {
55 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
56 return ret;
59 wl->dev_irq_num = gpiod_to_irq(wl->gpio_irq);
61 ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
62 isr_bh_routine,
63 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
64 "WILC_IRQ", dev);
65 if (ret < 0)
66 netdev_err(dev, "Failed to request IRQ\n");
67 else
68 netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n",
69 wl->dev_irq_num);
71 return ret;
74 static void deinit_irq(struct net_device *dev)
76 struct wilc_vif *vif = netdev_priv(dev);
77 struct wilc *wilc = vif->wilc;
79 /* Deinitialize IRQ */
80 if (wilc->dev_irq_num)
81 free_irq(wilc->dev_irq_num, wilc);
84 void wilc_mac_indicate(struct wilc *wilc)
86 s8 status;
88 wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
89 if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
90 wilc->mac_status = status;
91 complete(&wilc->sync_event);
92 } else {
93 wilc->mac_status = status;
97 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
99 struct net_device *ndev = NULL;
100 struct wilc_vif *vif;
101 struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
103 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
104 if (vif->mode == WILC_STATION_MODE)
105 if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
106 ndev = vif->ndev;
107 goto out;
109 if (vif->mode == WILC_AP_MODE)
110 if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
111 ndev = vif->ndev;
112 goto out;
115 out:
116 return ndev;
119 void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
121 struct wilc_vif *vif = netdev_priv(wilc_netdev);
123 if (bssid)
124 ether_addr_copy(vif->bssid, bssid);
125 else
126 eth_zero_addr(vif->bssid);
128 vif->mode = mode;
131 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
133 int srcu_idx;
134 u8 ret_val = 0;
135 struct wilc_vif *vif;
137 srcu_idx = srcu_read_lock(&wilc->srcu);
138 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
139 if (!is_zero_ether_addr(vif->bssid))
140 ret_val++;
142 srcu_read_unlock(&wilc->srcu, srcu_idx);
143 return ret_val;
146 static int wilc_txq_task(void *vp)
148 int ret;
149 u32 txq_count;
150 struct wilc *wl = vp;
152 complete(&wl->txq_thread_started);
153 while (1) {
154 wait_for_completion(&wl->txq_event);
156 if (wl->close) {
157 complete(&wl->txq_thread_started);
159 while (!kthread_should_stop())
160 schedule();
161 break;
163 do {
164 ret = wilc_wlan_handle_txq(wl, &txq_count);
165 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
166 int srcu_idx;
167 struct wilc_vif *ifc;
169 srcu_idx = srcu_read_lock(&wl->srcu);
170 list_for_each_entry_rcu(ifc, &wl->vif_list,
171 list) {
172 if (ifc->mac_opened && ifc->ndev)
173 netif_wake_queue(ifc->ndev);
175 srcu_read_unlock(&wl->srcu, srcu_idx);
177 } while (ret == WILC_VMM_ENTRY_FULL_RETRY && !wl->close);
179 return 0;
182 static int wilc_wlan_get_firmware(struct net_device *dev)
184 struct wilc_vif *vif = netdev_priv(dev);
185 struct wilc *wilc = vif->wilc;
186 int chip_id;
187 const struct firmware *wilc_firmware;
188 char *firmware;
190 chip_id = wilc_get_chipid(wilc, false);
192 if (chip_id < 0x1003a0)
193 firmware = FIRMWARE_1002;
194 else
195 firmware = FIRMWARE_1003;
197 netdev_info(dev, "loading firmware %s\n", firmware);
199 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
200 netdev_err(dev, "%s - firmware not available\n", firmware);
201 return -EINVAL;
203 wilc->firmware = wilc_firmware;
205 return 0;
208 static int wilc_start_firmware(struct net_device *dev)
210 struct wilc_vif *vif = netdev_priv(dev);
211 struct wilc *wilc = vif->wilc;
212 int ret = 0;
214 ret = wilc_wlan_start(wilc);
215 if (ret)
216 return ret;
218 if (!wait_for_completion_timeout(&wilc->sync_event,
219 msecs_to_jiffies(5000)))
220 return -ETIME;
222 return 0;
225 static int wilc1000_firmware_download(struct net_device *dev)
227 struct wilc_vif *vif = netdev_priv(dev);
228 struct wilc *wilc = vif->wilc;
229 int ret = 0;
231 if (!wilc->firmware) {
232 netdev_err(dev, "Firmware buffer is NULL\n");
233 return -ENOBUFS;
236 ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
237 wilc->firmware->size);
238 if (ret)
239 return ret;
241 release_firmware(wilc->firmware);
242 wilc->firmware = NULL;
244 netdev_dbg(dev, "Download Succeeded\n");
246 return 0;
249 static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
251 struct wilc_priv *priv = &vif->priv;
252 struct host_if_drv *hif_drv;
253 u8 b;
254 u16 hw;
255 u32 w;
257 netdev_dbg(dev, "Start configuring Firmware\n");
258 hif_drv = (struct host_if_drv *)priv->hif_drv;
259 netdev_dbg(dev, "Host = %p\n", hif_drv);
261 w = vif->iftype;
262 cpu_to_le32s(&w);
263 if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
264 0, 0))
265 goto fail;
267 b = WILC_FW_BSS_TYPE_INFRA;
268 if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
269 goto fail;
271 b = WILC_FW_TX_RATE_AUTO;
272 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
273 goto fail;
275 b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
276 if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
277 goto fail;
279 b = WILC_FW_PREAMBLE_SHORT;
280 if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
281 goto fail;
283 b = WILC_FW_11N_PROT_AUTO;
284 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
285 goto fail;
287 b = WILC_FW_ACTIVE_SCAN;
288 if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
289 goto fail;
291 b = WILC_FW_SITE_SURVEY_OFF;
292 if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
293 goto fail;
295 hw = 0xffff;
296 cpu_to_le16s(&hw);
297 if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
298 goto fail;
300 hw = 2346;
301 cpu_to_le16s(&hw);
302 if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
303 goto fail;
305 b = 0;
306 if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
307 goto fail;
309 b = 1;
310 if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
311 goto fail;
313 b = WILC_FW_NO_POWERSAVE;
314 if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
315 goto fail;
317 b = WILC_FW_SEC_NO;
318 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
319 goto fail;
321 b = WILC_FW_AUTH_OPEN_SYSTEM;
322 if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
323 goto fail;
325 b = 3;
326 if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
327 goto fail;
329 b = 3;
330 if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
331 goto fail;
333 b = WILC_FW_ACK_POLICY_NORMAL;
334 if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
335 goto fail;
337 b = 0;
338 if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
339 0, 0))
340 goto fail;
342 b = 48;
343 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
344 goto fail;
346 b = 28;
347 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
348 goto fail;
350 hw = 100;
351 cpu_to_le16s(&hw);
352 if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
353 goto fail;
355 b = WILC_FW_REKEY_POLICY_DISABLE;
356 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
357 goto fail;
359 w = 84600;
360 cpu_to_le32s(&w);
361 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
362 goto fail;
364 w = 500;
365 cpu_to_le32s(&w);
366 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
368 goto fail;
370 b = 1;
371 if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
373 goto fail;
375 b = WILC_FW_ERP_PROT_SELF_CTS;
376 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
377 goto fail;
379 b = 1;
380 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
381 goto fail;
383 b = WILC_FW_11N_OP_MODE_HT_MIXED;
384 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
385 goto fail;
387 b = 1;
388 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
389 goto fail;
391 b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
392 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
393 0, 0))
394 goto fail;
396 b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
397 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
398 goto fail;
400 b = 0;
401 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
403 goto fail;
405 b = 7;
406 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
407 goto fail;
409 b = 1;
410 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
411 1, 1))
412 goto fail;
414 return 0;
416 fail:
417 return -EINVAL;
420 static void wlan_deinitialize_threads(struct net_device *dev)
422 struct wilc_vif *vif = netdev_priv(dev);
423 struct wilc *wl = vif->wilc;
425 wl->close = 1;
427 complete(&wl->txq_event);
429 if (wl->txq_thread) {
430 kthread_stop(wl->txq_thread);
431 wl->txq_thread = NULL;
435 static void wilc_wlan_deinitialize(struct net_device *dev)
437 struct wilc_vif *vif = netdev_priv(dev);
438 struct wilc *wl = vif->wilc;
440 if (!wl) {
441 netdev_err(dev, "wl is NULL\n");
442 return;
445 if (wl->initialized) {
446 netdev_info(dev, "Deinitializing wilc1000...\n");
448 if (!wl->dev_irq_num &&
449 wl->hif_func->disable_interrupt) {
450 mutex_lock(&wl->hif_cs);
451 wl->hif_func->disable_interrupt(wl);
452 mutex_unlock(&wl->hif_cs);
454 complete(&wl->txq_event);
456 wlan_deinitialize_threads(dev);
457 deinit_irq(dev);
459 wilc_wlan_stop(wl, vif);
460 wilc_wlan_cleanup(dev);
462 wl->initialized = false;
464 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
465 } else {
466 netdev_dbg(dev, "wilc1000 is not initialized\n");
470 static int wlan_initialize_threads(struct net_device *dev)
472 struct wilc_vif *vif = netdev_priv(dev);
473 struct wilc *wilc = vif->wilc;
475 wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
476 "K_TXQ_TASK");
477 if (IS_ERR(wilc->txq_thread)) {
478 netdev_err(dev, "couldn't create TXQ thread\n");
479 wilc->close = 0;
480 return PTR_ERR(wilc->txq_thread);
482 wait_for_completion(&wilc->txq_thread_started);
484 return 0;
487 static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
489 int ret = 0;
490 struct wilc *wl = vif->wilc;
492 if (!wl->initialized) {
493 wl->mac_status = WILC_MAC_STATUS_INIT;
494 wl->close = 0;
496 ret = wilc_wlan_init(dev);
497 if (ret)
498 return ret;
500 ret = wlan_initialize_threads(dev);
501 if (ret)
502 goto fail_wilc_wlan;
504 if (wl->gpio_irq && init_irq(dev)) {
505 ret = -EIO;
506 goto fail_threads;
509 if (!wl->dev_irq_num &&
510 wl->hif_func->enable_interrupt &&
511 wl->hif_func->enable_interrupt(wl)) {
512 ret = -EIO;
513 goto fail_irq_init;
516 ret = wilc_wlan_get_firmware(dev);
517 if (ret)
518 goto fail_irq_enable;
520 ret = wilc1000_firmware_download(dev);
521 if (ret)
522 goto fail_irq_enable;
524 ret = wilc_start_firmware(dev);
525 if (ret)
526 goto fail_irq_enable;
528 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
529 int size;
530 char firmware_ver[20];
532 size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
533 firmware_ver,
534 sizeof(firmware_ver));
535 firmware_ver[size] = '\0';
536 netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
539 ret = wilc_init_fw_config(dev, vif);
540 if (ret) {
541 netdev_err(dev, "Failed to configure firmware\n");
542 goto fail_fw_start;
544 wl->initialized = true;
545 return 0;
547 fail_fw_start:
548 wilc_wlan_stop(wl, vif);
550 fail_irq_enable:
551 if (!wl->dev_irq_num &&
552 wl->hif_func->disable_interrupt)
553 wl->hif_func->disable_interrupt(wl);
554 fail_irq_init:
555 if (wl->dev_irq_num)
556 deinit_irq(dev);
557 fail_threads:
558 wlan_deinitialize_threads(dev);
559 fail_wilc_wlan:
560 wilc_wlan_cleanup(dev);
561 netdev_err(dev, "WLAN initialization FAILED\n");
562 } else {
563 netdev_dbg(dev, "wilc1000 already initialized\n");
565 return ret;
568 static int mac_init_fn(struct net_device *ndev)
570 netif_start_queue(ndev);
571 netif_stop_queue(ndev);
573 return 0;
576 static int wilc_mac_open(struct net_device *ndev)
578 struct wilc_vif *vif = netdev_priv(ndev);
579 struct wilc *wl = vif->wilc;
580 struct wilc_priv *priv = wdev_priv(vif->ndev->ieee80211_ptr);
581 unsigned char mac_add[ETH_ALEN] = {0};
582 int ret = 0;
584 if (!wl || !wl->dev) {
585 netdev_err(ndev, "device not ready\n");
586 return -ENODEV;
589 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
591 ret = wilc_init_host_int(ndev);
592 if (ret)
593 return ret;
595 ret = wilc_wlan_initialize(ndev, vif);
596 if (ret) {
597 wilc_deinit_host_int(ndev);
598 return ret;
601 wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
602 vif->idx);
603 wilc_get_mac_address(vif, mac_add);
604 netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
605 ether_addr_copy(ndev->dev_addr, mac_add);
607 if (!is_valid_ether_addr(ndev->dev_addr)) {
608 netdev_err(ndev, "Wrong MAC address\n");
609 wilc_deinit_host_int(ndev);
610 wilc_wlan_deinitialize(ndev);
611 return -EINVAL;
614 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
615 vif->ndev->ieee80211_ptr,
616 vif->frame_reg[0].type,
617 vif->frame_reg[0].reg);
618 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
619 vif->ndev->ieee80211_ptr,
620 vif->frame_reg[1].type,
621 vif->frame_reg[1].reg);
622 netif_wake_queue(ndev);
623 wl->open_ifcs++;
624 priv->p2p.local_random = 0x01;
625 vif->mac_opened = 1;
626 return 0;
629 static struct net_device_stats *mac_stats(struct net_device *dev)
631 struct wilc_vif *vif = netdev_priv(dev);
633 return &vif->netstats;
636 static void wilc_set_multicast_list(struct net_device *dev)
638 struct netdev_hw_addr *ha;
639 struct wilc_vif *vif = netdev_priv(dev);
640 int i;
641 u8 *mc_list;
642 u8 *cur_mc;
644 if (dev->flags & IFF_PROMISC)
645 return;
647 if (dev->flags & IFF_ALLMULTI ||
648 dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
649 wilc_setup_multicast_filter(vif, 0, 0, NULL);
650 return;
653 if (dev->mc.count == 0) {
654 wilc_setup_multicast_filter(vif, 1, 0, NULL);
655 return;
658 mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
659 if (!mc_list)
660 return;
662 cur_mc = mc_list;
663 i = 0;
664 netdev_for_each_mc_addr(ha, dev) {
665 memcpy(cur_mc, ha->addr, ETH_ALEN);
666 netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
667 i++;
668 cur_mc += ETH_ALEN;
671 if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
672 kfree(mc_list);
675 static void wilc_tx_complete(void *priv, int status)
677 struct tx_complete_data *pv_data = priv;
679 dev_kfree_skb(pv_data->skb);
680 kfree(pv_data);
683 netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
685 struct wilc_vif *vif = netdev_priv(ndev);
686 struct wilc *wilc = vif->wilc;
687 struct tx_complete_data *tx_data = NULL;
688 int queue_count;
690 if (skb->dev != ndev) {
691 netdev_err(ndev, "Packet not destined to this device\n");
692 return 0;
695 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
696 if (!tx_data) {
697 dev_kfree_skb(skb);
698 netif_wake_queue(ndev);
699 return 0;
702 tx_data->buff = skb->data;
703 tx_data->size = skb->len;
704 tx_data->skb = skb;
706 vif->netstats.tx_packets++;
707 vif->netstats.tx_bytes += tx_data->size;
708 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
709 tx_data->buff, tx_data->size,
710 wilc_tx_complete);
712 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
713 int srcu_idx;
714 struct wilc_vif *vif;
716 srcu_idx = srcu_read_lock(&wilc->srcu);
717 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
718 if (vif->mac_opened)
719 netif_stop_queue(vif->ndev);
721 srcu_read_unlock(&wilc->srcu, srcu_idx);
724 return 0;
727 static int wilc_mac_close(struct net_device *ndev)
729 struct wilc_vif *vif = netdev_priv(ndev);
730 struct wilc *wl = vif->wilc;
732 netdev_dbg(ndev, "Mac close\n");
734 if (wl->open_ifcs > 0)
735 wl->open_ifcs--;
736 else
737 return 0;
739 if (vif->ndev) {
740 netif_stop_queue(vif->ndev);
742 wilc_deinit_host_int(vif->ndev);
745 if (wl->open_ifcs == 0) {
746 netdev_dbg(ndev, "Deinitializing wilc1000\n");
747 wl->close = 1;
748 wilc_wlan_deinitialize(ndev);
751 vif->mac_opened = 0;
753 return 0;
756 void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
757 u32 pkt_offset)
759 unsigned int frame_len = 0;
760 int stats;
761 unsigned char *buff_to_send = NULL;
762 struct sk_buff *skb;
763 struct net_device *wilc_netdev;
764 struct wilc_vif *vif;
766 if (!wilc)
767 return;
769 wilc_netdev = get_if_handler(wilc, buff);
770 if (!wilc_netdev)
771 return;
773 buff += pkt_offset;
774 vif = netdev_priv(wilc_netdev);
776 if (size > 0) {
777 frame_len = size;
778 buff_to_send = buff;
780 skb = dev_alloc_skb(frame_len);
781 if (!skb)
782 return;
784 skb->dev = wilc_netdev;
786 skb_put_data(skb, buff_to_send, frame_len);
788 skb->protocol = eth_type_trans(skb, wilc_netdev);
789 vif->netstats.rx_packets++;
790 vif->netstats.rx_bytes += frame_len;
791 skb->ip_summed = CHECKSUM_UNNECESSARY;
792 stats = netif_rx(skb);
793 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
797 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
799 int srcu_idx;
800 struct wilc_vif *vif;
802 srcu_idx = srcu_read_lock(&wilc->srcu);
803 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
804 u16 type = le16_to_cpup((__le16 *)buff);
806 if (vif->priv.p2p_listen_state &&
807 ((type == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
808 (type == vif->frame_reg[1].type && vif->frame_reg[1].reg)))
809 wilc_wfi_p2p_rx(vif, buff, size);
811 if (vif->monitor_flag)
812 wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
814 srcu_read_unlock(&wilc->srcu, srcu_idx);
817 static const struct net_device_ops wilc_netdev_ops = {
818 .ndo_init = mac_init_fn,
819 .ndo_open = wilc_mac_open,
820 .ndo_stop = wilc_mac_close,
821 .ndo_start_xmit = wilc_mac_xmit,
822 .ndo_get_stats = mac_stats,
823 .ndo_set_rx_mode = wilc_set_multicast_list,
826 void wilc_netdev_cleanup(struct wilc *wilc)
828 struct wilc_vif *vif;
829 int srcu_idx, ifc_cnt = 0;
831 if (!wilc)
832 return;
834 if (wilc->firmware) {
835 release_firmware(wilc->firmware);
836 wilc->firmware = NULL;
839 srcu_idx = srcu_read_lock(&wilc->srcu);
840 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
841 if (vif->ndev)
842 unregister_netdev(vif->ndev);
844 srcu_read_unlock(&wilc->srcu, srcu_idx);
846 wilc_wfi_deinit_mon_interface(wilc, false);
847 flush_workqueue(wilc->hif_workqueue);
848 destroy_workqueue(wilc->hif_workqueue);
850 while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) {
851 mutex_lock(&wilc->vif_mutex);
852 if (wilc->vif_num <= 0) {
853 mutex_unlock(&wilc->vif_mutex);
854 break;
856 vif = wilc_get_wl_to_vif(wilc);
857 if (!IS_ERR(vif))
858 list_del_rcu(&vif->list);
860 wilc->vif_num--;
861 mutex_unlock(&wilc->vif_mutex);
862 synchronize_srcu(&wilc->srcu);
863 ifc_cnt++;
866 wilc_wlan_cfg_deinit(wilc);
867 wlan_deinit_locks(wilc);
868 kfree(wilc->bus_data);
869 wiphy_unregister(wilc->wiphy);
870 wiphy_free(wilc->wiphy);
872 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
874 static u8 wilc_get_available_idx(struct wilc *wl)
876 int idx = 0;
877 struct wilc_vif *vif;
878 int srcu_idx;
880 srcu_idx = srcu_read_lock(&wl->srcu);
881 list_for_each_entry_rcu(vif, &wl->vif_list, list) {
882 if (vif->idx == 0)
883 idx = 1;
884 else
885 idx = 0;
887 srcu_read_unlock(&wl->srcu, srcu_idx);
888 return idx;
891 struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
892 int vif_type, enum nl80211_iftype type,
893 bool rtnl_locked)
895 struct net_device *ndev;
896 struct wilc_vif *vif;
897 int ret;
899 ndev = alloc_etherdev(sizeof(*vif));
900 if (!ndev)
901 return ERR_PTR(-ENOMEM);
903 vif = netdev_priv(ndev);
904 ndev->ieee80211_ptr = &vif->priv.wdev;
905 strcpy(ndev->name, name);
906 vif->wilc = wl;
907 vif->ndev = ndev;
908 ndev->ml_priv = vif;
910 ndev->netdev_ops = &wilc_netdev_ops;
912 SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
914 vif->priv.wdev.wiphy = wl->wiphy;
915 vif->priv.wdev.netdev = ndev;
916 vif->priv.wdev.iftype = type;
917 vif->priv.dev = ndev;
919 if (rtnl_locked)
920 ret = register_netdevice(ndev);
921 else
922 ret = register_netdev(ndev);
924 if (ret) {
925 free_netdev(ndev);
926 return ERR_PTR(-EFAULT);
929 ndev->needs_free_netdev = true;
930 vif->iftype = vif_type;
931 vif->idx = wilc_get_available_idx(wl);
932 vif->mac_opened = 0;
933 mutex_lock(&wl->vif_mutex);
934 list_add_tail_rcu(&vif->list, &wl->vif_list);
935 wl->vif_num += 1;
936 mutex_unlock(&wl->vif_mutex);
937 synchronize_srcu(&wl->srcu);
939 return vif;
942 MODULE_LICENSE("GPL");