interconnect: qcom: Fix Kconfig indentation
[linux/fpc-iii.git] / drivers / staging / wilc1000 / netdev.c
blobd2c0b0f7cf6335640e7c4b68b67cc783debb3bfa
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 u8 *bssid, *bssid1;
100 struct net_device *ndev = NULL;
101 struct wilc_vif *vif;
103 bssid = mac_header + 10;
104 bssid1 = mac_header + 4;
106 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
107 if (vif->mode == WILC_STATION_MODE)
108 if (ether_addr_equal_unaligned(bssid, vif->bssid)) {
109 ndev = vif->ndev;
110 goto out;
112 if (vif->mode == WILC_AP_MODE)
113 if (ether_addr_equal_unaligned(bssid1, vif->bssid)) {
114 ndev = vif->ndev;
115 goto out;
118 out:
119 return ndev;
122 void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
124 struct wilc_vif *vif = netdev_priv(wilc_netdev);
126 if (bssid)
127 ether_addr_copy(vif->bssid, bssid);
128 else
129 eth_zero_addr(vif->bssid);
131 vif->mode = mode;
134 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
136 int srcu_idx;
137 u8 ret_val = 0;
138 struct wilc_vif *vif;
140 srcu_idx = srcu_read_lock(&wilc->srcu);
141 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
142 if (!is_zero_ether_addr(vif->bssid))
143 ret_val++;
145 srcu_read_unlock(&wilc->srcu, srcu_idx);
146 return ret_val;
149 static int wilc_txq_task(void *vp)
151 int ret;
152 u32 txq_count;
153 struct wilc *wl = vp;
155 complete(&wl->txq_thread_started);
156 while (1) {
157 wait_for_completion(&wl->txq_event);
159 if (wl->close) {
160 complete(&wl->txq_thread_started);
162 while (!kthread_should_stop())
163 schedule();
164 break;
166 do {
167 ret = wilc_wlan_handle_txq(wl, &txq_count);
168 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
169 int srcu_idx;
170 struct wilc_vif *ifc;
172 srcu_idx = srcu_read_lock(&wl->srcu);
173 list_for_each_entry_rcu(ifc, &wl->vif_list,
174 list) {
175 if (ifc->mac_opened && ifc->ndev)
176 netif_wake_queue(ifc->ndev);
178 srcu_read_unlock(&wl->srcu, srcu_idx);
180 } while (ret == -ENOBUFS && !wl->close);
182 return 0;
185 static int wilc_wlan_get_firmware(struct net_device *dev)
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;
193 chip_id = wilc_get_chipid(wilc, false);
195 if (chip_id < 0x1003a0)
196 firmware = FIRMWARE_1002;
197 else
198 firmware = FIRMWARE_1003;
200 netdev_info(dev, "loading firmware %s\n", firmware);
202 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
203 netdev_err(dev, "%s - firmware not available\n", firmware);
204 ret = -1;
205 goto fail;
207 wilc->firmware = wilc_firmware;
209 fail:
211 return ret;
214 static int wilc_start_firmware(struct net_device *dev)
216 struct wilc_vif *vif = netdev_priv(dev);
217 struct wilc *wilc = vif->wilc;
218 int ret = 0;
220 ret = wilc_wlan_start(wilc);
221 if (ret < 0)
222 return ret;
224 if (!wait_for_completion_timeout(&wilc->sync_event,
225 msecs_to_jiffies(5000)))
226 return -ETIME;
228 return 0;
231 static int wilc1000_firmware_download(struct net_device *dev)
233 struct wilc_vif *vif = netdev_priv(dev);
234 struct wilc *wilc = vif->wilc;
235 int ret = 0;
237 if (!wilc->firmware) {
238 netdev_err(dev, "Firmware buffer is NULL\n");
239 return -ENOBUFS;
242 ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
243 wilc->firmware->size);
244 if (ret < 0)
245 return ret;
247 release_firmware(wilc->firmware);
248 wilc->firmware = NULL;
250 netdev_dbg(dev, "Download Succeeded\n");
252 return 0;
255 static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
257 struct wilc_priv *priv = &vif->priv;
258 struct host_if_drv *hif_drv;
259 u8 b;
260 u16 hw;
261 u32 w;
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);
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;
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;
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;
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;
285 b = WILC_FW_PREAMBLE_SHORT;
286 if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
287 goto fail;
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;
293 b = WILC_FW_ACTIVE_SCAN;
294 if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
295 goto fail;
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;
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;
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;
311 b = 0;
312 if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
313 goto fail;
315 b = 1;
316 if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
317 goto fail;
319 b = WILC_FW_NO_POWERSAVE;
320 if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
321 goto fail;
323 b = WILC_FW_SEC_NO;
324 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
325 goto fail;
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;
331 b = 3;
332 if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
333 goto fail;
335 b = 3;
336 if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
337 goto fail;
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;
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;
348 b = 48;
349 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
350 goto fail;
352 b = 28;
353 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
354 goto fail;
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;
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;
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;
370 w = 500;
371 cpu_to_le32s(&w);
372 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
374 goto fail;
376 b = 1;
377 if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
379 goto fail;
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;
385 b = 1;
386 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
387 goto fail;
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;
393 b = 1;
394 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
395 goto fail;
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;
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;
406 b = 0;
407 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
409 goto fail;
411 b = 7;
412 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
413 goto fail;
415 b = 1;
416 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
417 1, 1))
418 goto fail;
420 return 0;
422 fail:
423 return -1;
426 static void wlan_deinitialize_threads(struct net_device *dev)
428 struct wilc_vif *vif = netdev_priv(dev);
429 struct wilc *wl = vif->wilc;
431 wl->close = 1;
433 complete(&wl->txq_event);
435 if (wl->txq_thread) {
436 kthread_stop(wl->txq_thread);
437 wl->txq_thread = NULL;
441 static void wilc_wlan_deinitialize(struct net_device *dev)
443 struct wilc_vif *vif = netdev_priv(dev);
444 struct wilc *wl = vif->wilc;
446 if (!wl) {
447 netdev_err(dev, "wl is NULL\n");
448 return;
451 if (wl->initialized) {
452 netdev_info(dev, "Deinitializing wilc1000...\n");
454 if (!wl->dev_irq_num &&
455 wl->hif_func->disable_interrupt) {
456 mutex_lock(&wl->hif_cs);
457 wl->hif_func->disable_interrupt(wl);
458 mutex_unlock(&wl->hif_cs);
460 complete(&wl->txq_event);
462 wlan_deinitialize_threads(dev);
463 deinit_irq(dev);
465 wilc_wlan_stop(wl, vif);
466 wilc_wlan_cleanup(dev);
468 wl->initialized = false;
470 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
471 } else {
472 netdev_dbg(dev, "wilc1000 is not initialized\n");
476 static int wlan_initialize_threads(struct net_device *dev)
478 struct wilc_vif *vif = netdev_priv(dev);
479 struct wilc *wilc = vif->wilc;
481 wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
482 "K_TXQ_TASK");
483 if (IS_ERR(wilc->txq_thread)) {
484 netdev_err(dev, "couldn't create TXQ thread\n");
485 wilc->close = 0;
486 return PTR_ERR(wilc->txq_thread);
488 wait_for_completion(&wilc->txq_thread_started);
490 return 0;
493 static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
495 int ret = 0;
496 struct wilc *wl = vif->wilc;
498 if (!wl->initialized) {
499 wl->mac_status = WILC_MAC_STATUS_INIT;
500 wl->close = 0;
502 ret = wilc_wlan_init(dev);
503 if (ret < 0)
504 return -EIO;
506 ret = wlan_initialize_threads(dev);
507 if (ret < 0) {
508 ret = -EIO;
509 goto fail_wilc_wlan;
512 if (wl->gpio_irq && init_irq(dev)) {
513 ret = -EIO;
514 goto fail_threads;
517 if (!wl->dev_irq_num &&
518 wl->hif_func->enable_interrupt &&
519 wl->hif_func->enable_interrupt(wl)) {
520 ret = -EIO;
521 goto fail_irq_init;
524 if (wilc_wlan_get_firmware(dev)) {
525 ret = -EIO;
526 goto fail_irq_enable;
529 ret = wilc1000_firmware_download(dev);
530 if (ret < 0) {
531 ret = -EIO;
532 goto fail_irq_enable;
535 ret = wilc_start_firmware(dev);
536 if (ret < 0) {
537 ret = -EIO;
538 goto fail_irq_enable;
541 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
542 int size;
543 char firmware_ver[20];
545 size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
546 firmware_ver,
547 sizeof(firmware_ver));
548 firmware_ver[size] = '\0';
549 netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
551 ret = wilc_init_fw_config(dev, vif);
553 if (ret < 0) {
554 netdev_err(dev, "Failed to configure firmware\n");
555 ret = -EIO;
556 goto fail_fw_start;
558 wl->initialized = true;
559 return 0;
561 fail_fw_start:
562 wilc_wlan_stop(wl, vif);
564 fail_irq_enable:
565 if (!wl->dev_irq_num &&
566 wl->hif_func->disable_interrupt)
567 wl->hif_func->disable_interrupt(wl);
568 fail_irq_init:
569 if (wl->dev_irq_num)
570 deinit_irq(dev);
571 fail_threads:
572 wlan_deinitialize_threads(dev);
573 fail_wilc_wlan:
574 wilc_wlan_cleanup(dev);
575 netdev_err(dev, "WLAN initialization FAILED\n");
576 } else {
577 netdev_dbg(dev, "wilc1000 already initialized\n");
579 return ret;
582 static int mac_init_fn(struct net_device *ndev)
584 netif_start_queue(ndev);
585 netif_stop_queue(ndev);
587 return 0;
590 static int wilc_mac_open(struct net_device *ndev)
592 struct wilc_vif *vif = netdev_priv(ndev);
593 struct wilc *wl = vif->wilc;
594 struct wilc_priv *priv = wdev_priv(vif->ndev->ieee80211_ptr);
595 unsigned char mac_add[ETH_ALEN] = {0};
596 int ret = 0;
598 if (!wl || !wl->dev) {
599 netdev_err(ndev, "device not ready\n");
600 return -ENODEV;
603 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
605 ret = wilc_init_host_int(ndev);
606 if (ret < 0)
607 return ret;
609 ret = wilc_wlan_initialize(ndev, vif);
610 if (ret < 0) {
611 wilc_deinit_host_int(ndev);
612 return ret;
615 wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
616 vif->idx);
617 wilc_get_mac_address(vif, mac_add);
618 netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
619 ether_addr_copy(ndev->dev_addr, mac_add);
621 if (!is_valid_ether_addr(ndev->dev_addr)) {
622 netdev_err(ndev, "Wrong MAC address\n");
623 wilc_deinit_host_int(ndev);
624 wilc_wlan_deinitialize(ndev);
625 return -EINVAL;
628 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
629 vif->ndev->ieee80211_ptr,
630 vif->frame_reg[0].type,
631 vif->frame_reg[0].reg);
632 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
633 vif->ndev->ieee80211_ptr,
634 vif->frame_reg[1].type,
635 vif->frame_reg[1].reg);
636 netif_wake_queue(ndev);
637 wl->open_ifcs++;
638 priv->p2p.local_random = 0x01;
639 vif->mac_opened = 1;
640 return 0;
643 static struct net_device_stats *mac_stats(struct net_device *dev)
645 struct wilc_vif *vif = netdev_priv(dev);
647 return &vif->netstats;
650 static void wilc_set_multicast_list(struct net_device *dev)
652 struct netdev_hw_addr *ha;
653 struct wilc_vif *vif = netdev_priv(dev);
654 int i;
655 u8 *mc_list;
656 u8 *cur_mc;
658 if (dev->flags & IFF_PROMISC)
659 return;
661 if (dev->flags & IFF_ALLMULTI ||
662 dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
663 wilc_setup_multicast_filter(vif, 0, 0, NULL);
664 return;
667 if (dev->mc.count == 0) {
668 wilc_setup_multicast_filter(vif, 1, 0, NULL);
669 return;
672 mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
673 if (!mc_list)
674 return;
676 cur_mc = mc_list;
677 i = 0;
678 netdev_for_each_mc_addr(ha, dev) {
679 memcpy(cur_mc, ha->addr, ETH_ALEN);
680 netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
681 i++;
682 cur_mc += ETH_ALEN;
685 if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
686 kfree(mc_list);
689 static void wilc_tx_complete(void *priv, int status)
691 struct tx_complete_data *pv_data = priv;
693 dev_kfree_skb(pv_data->skb);
694 kfree(pv_data);
697 netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
699 struct wilc_vif *vif = netdev_priv(ndev);
700 struct wilc *wilc = vif->wilc;
701 struct tx_complete_data *tx_data = NULL;
702 int queue_count;
704 if (skb->dev != ndev) {
705 netdev_err(ndev, "Packet not destined to this device\n");
706 return 0;
709 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
710 if (!tx_data) {
711 dev_kfree_skb(skb);
712 netif_wake_queue(ndev);
713 return 0;
716 tx_data->buff = skb->data;
717 tx_data->size = skb->len;
718 tx_data->skb = skb;
720 vif->netstats.tx_packets++;
721 vif->netstats.tx_bytes += tx_data->size;
722 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
723 tx_data->buff, tx_data->size,
724 wilc_tx_complete);
726 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
727 int srcu_idx;
728 struct wilc_vif *vif;
730 srcu_idx = srcu_read_lock(&wilc->srcu);
731 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
732 if (vif->mac_opened)
733 netif_stop_queue(vif->ndev);
735 srcu_read_unlock(&wilc->srcu, srcu_idx);
738 return 0;
741 static int wilc_mac_close(struct net_device *ndev)
743 struct wilc_vif *vif = netdev_priv(ndev);
744 struct wilc *wl = vif->wilc;
746 netdev_dbg(ndev, "Mac close\n");
748 if (wl->open_ifcs > 0)
749 wl->open_ifcs--;
750 else
751 return 0;
753 if (vif->ndev) {
754 netif_stop_queue(vif->ndev);
756 wilc_deinit_host_int(vif->ndev);
759 if (wl->open_ifcs == 0) {
760 netdev_dbg(ndev, "Deinitializing wilc1000\n");
761 wl->close = 1;
762 wilc_wlan_deinitialize(ndev);
765 vif->mac_opened = 0;
767 return 0;
770 void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
771 u32 pkt_offset)
773 unsigned int frame_len = 0;
774 int stats;
775 unsigned char *buff_to_send = NULL;
776 struct sk_buff *skb;
777 struct net_device *wilc_netdev;
778 struct wilc_vif *vif;
780 if (!wilc)
781 return;
783 wilc_netdev = get_if_handler(wilc, buff);
784 if (!wilc_netdev)
785 return;
787 buff += pkt_offset;
788 vif = netdev_priv(wilc_netdev);
790 if (size > 0) {
791 frame_len = size;
792 buff_to_send = buff;
794 skb = dev_alloc_skb(frame_len);
795 if (!skb)
796 return;
798 skb->dev = wilc_netdev;
800 skb_put_data(skb, buff_to_send, frame_len);
802 skb->protocol = eth_type_trans(skb, wilc_netdev);
803 vif->netstats.rx_packets++;
804 vif->netstats.rx_bytes += frame_len;
805 skb->ip_summed = CHECKSUM_UNNECESSARY;
806 stats = netif_rx(skb);
807 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
811 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
813 int srcu_idx;
814 struct wilc_vif *vif;
816 srcu_idx = srcu_read_lock(&wilc->srcu);
817 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
818 u16 type = le16_to_cpup((__le16 *)buff);
820 if (vif->priv.p2p_listen_state &&
821 ((type == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
822 (type == vif->frame_reg[1].type && vif->frame_reg[1].reg)))
823 wilc_wfi_p2p_rx(vif, buff, size);
825 if (vif->monitor_flag)
826 wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
828 srcu_read_unlock(&wilc->srcu, srcu_idx);
831 static const struct net_device_ops wilc_netdev_ops = {
832 .ndo_init = mac_init_fn,
833 .ndo_open = wilc_mac_open,
834 .ndo_stop = wilc_mac_close,
835 .ndo_start_xmit = wilc_mac_xmit,
836 .ndo_get_stats = mac_stats,
837 .ndo_set_rx_mode = wilc_set_multicast_list,
840 void wilc_netdev_cleanup(struct wilc *wilc)
842 struct wilc_vif *vif;
843 int srcu_idx;
845 if (!wilc)
846 return;
848 if (wilc->firmware) {
849 release_firmware(wilc->firmware);
850 wilc->firmware = NULL;
853 srcu_idx = srcu_read_lock(&wilc->srcu);
854 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
855 if (vif->ndev)
856 unregister_netdev(vif->ndev);
858 srcu_read_unlock(&wilc->srcu, srcu_idx);
860 wilc_wfi_deinit_mon_interface(wilc, false);
861 flush_workqueue(wilc->hif_workqueue);
862 destroy_workqueue(wilc->hif_workqueue);
864 do {
865 mutex_lock(&wilc->vif_mutex);
866 if (wilc->vif_num <= 0) {
867 mutex_unlock(&wilc->vif_mutex);
868 break;
870 vif = wilc_get_wl_to_vif(wilc);
871 if (!IS_ERR(vif))
872 list_del_rcu(&vif->list);
874 wilc->vif_num--;
875 mutex_unlock(&wilc->vif_mutex);
876 synchronize_srcu(&wilc->srcu);
877 } while (1);
879 wilc_wlan_cfg_deinit(wilc);
880 wlan_deinit_locks(wilc);
881 kfree(wilc->bus_data);
882 wiphy_unregister(wilc->wiphy);
883 wiphy_free(wilc->wiphy);
885 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
887 static u8 wilc_get_available_idx(struct wilc *wl)
889 int idx = 0;
890 struct wilc_vif *vif;
891 int srcu_idx;
893 srcu_idx = srcu_read_lock(&wl->srcu);
894 list_for_each_entry_rcu(vif, &wl->vif_list, list) {
895 if (vif->idx == 0)
896 idx = 1;
897 else
898 idx = 0;
900 srcu_read_unlock(&wl->srcu, srcu_idx);
901 return idx;
904 struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
905 int vif_type, enum nl80211_iftype type,
906 bool rtnl_locked)
908 struct net_device *ndev;
909 struct wilc_vif *vif;
910 int ret;
912 ndev = alloc_etherdev(sizeof(*vif));
913 if (!ndev)
914 return ERR_PTR(-ENOMEM);
916 vif = netdev_priv(ndev);
917 ndev->ieee80211_ptr = &vif->priv.wdev;
918 strcpy(ndev->name, name);
919 vif->wilc = wl;
920 vif->ndev = ndev;
921 ndev->ml_priv = vif;
923 ndev->netdev_ops = &wilc_netdev_ops;
925 SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
927 vif->priv.wdev.wiphy = wl->wiphy;
928 vif->priv.wdev.netdev = ndev;
929 vif->priv.wdev.iftype = type;
930 vif->priv.dev = ndev;
932 if (rtnl_locked)
933 ret = register_netdevice(ndev);
934 else
935 ret = register_netdev(ndev);
937 if (ret) {
938 free_netdev(ndev);
939 return ERR_PTR(-EFAULT);
942 ndev->needs_free_netdev = true;
943 vif->iftype = vif_type;
944 vif->idx = wilc_get_available_idx(wl);
945 vif->mac_opened = 0;
946 mutex_lock(&wl->vif_mutex);
947 list_add_tail_rcu(&vif->list, &wl->vif_list);
948 wl->vif_num += 1;
949 mutex_unlock(&wl->vif_mutex);
950 synchronize_srcu(&wl->srcu);
952 return vif;
955 MODULE_LICENSE("GPL");