gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / staging / wilc1000 / netdev.c
blobf94a17babd12a651031a7f1185f3f2896f96668f
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 struct wilc_vif *vif = netdev_priv(dev);
50 struct wilc *wl = vif->wilc;
51 int ret;
53 ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
54 isr_bh_routine,
55 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
56 "WILC_IRQ", dev);
57 if (ret) {
58 netdev_err(dev, "Failed to request IRQ [%d]\n", ret);
59 return ret;
61 netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n", wl->dev_irq_num);
63 return 0;
66 static void deinit_irq(struct net_device *dev)
68 struct wilc_vif *vif = netdev_priv(dev);
69 struct wilc *wilc = vif->wilc;
71 /* Deinitialize IRQ */
72 if (wilc->dev_irq_num)
73 free_irq(wilc->dev_irq_num, wilc);
76 void wilc_mac_indicate(struct wilc *wilc)
78 s8 status;
80 wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
81 if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
82 wilc->mac_status = status;
83 complete(&wilc->sync_event);
84 } else {
85 wilc->mac_status = status;
89 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
91 struct net_device *ndev = NULL;
92 struct wilc_vif *vif;
93 struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
95 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
96 if (vif->mode == WILC_STATION_MODE)
97 if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
98 ndev = vif->ndev;
99 goto out;
101 if (vif->mode == WILC_AP_MODE)
102 if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
103 ndev = vif->ndev;
104 goto out;
107 out:
108 return ndev;
111 void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
113 struct wilc_vif *vif = netdev_priv(wilc_netdev);
115 if (bssid)
116 ether_addr_copy(vif->bssid, bssid);
117 else
118 eth_zero_addr(vif->bssid);
120 vif->mode = mode;
123 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
125 int srcu_idx;
126 u8 ret_val = 0;
127 struct wilc_vif *vif;
129 srcu_idx = srcu_read_lock(&wilc->srcu);
130 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
131 if (!is_zero_ether_addr(vif->bssid))
132 ret_val++;
134 srcu_read_unlock(&wilc->srcu, srcu_idx);
135 return ret_val;
138 static int wilc_txq_task(void *vp)
140 int ret;
141 u32 txq_count;
142 struct wilc *wl = vp;
144 complete(&wl->txq_thread_started);
145 while (1) {
146 wait_for_completion(&wl->txq_event);
148 if (wl->close) {
149 complete(&wl->txq_thread_started);
151 while (!kthread_should_stop())
152 schedule();
153 break;
155 do {
156 ret = wilc_wlan_handle_txq(wl, &txq_count);
157 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
158 int srcu_idx;
159 struct wilc_vif *ifc;
161 srcu_idx = srcu_read_lock(&wl->srcu);
162 list_for_each_entry_rcu(ifc, &wl->vif_list,
163 list) {
164 if (ifc->mac_opened && ifc->ndev)
165 netif_wake_queue(ifc->ndev);
167 srcu_read_unlock(&wl->srcu, srcu_idx);
169 } while (ret == WILC_VMM_ENTRY_FULL_RETRY && !wl->close);
171 return 0;
174 static int wilc_wlan_get_firmware(struct net_device *dev)
176 struct wilc_vif *vif = netdev_priv(dev);
177 struct wilc *wilc = vif->wilc;
178 int chip_id;
179 const struct firmware *wilc_firmware;
180 char *firmware;
182 chip_id = wilc_get_chipid(wilc, false);
184 if (chip_id < 0x1003a0)
185 firmware = FIRMWARE_1002;
186 else
187 firmware = FIRMWARE_1003;
189 netdev_info(dev, "loading firmware %s\n", firmware);
191 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
192 netdev_err(dev, "%s - firmware not available\n", firmware);
193 return -EINVAL;
195 wilc->firmware = wilc_firmware;
197 return 0;
200 static int wilc_start_firmware(struct net_device *dev)
202 struct wilc_vif *vif = netdev_priv(dev);
203 struct wilc *wilc = vif->wilc;
204 int ret = 0;
206 ret = wilc_wlan_start(wilc);
207 if (ret)
208 return ret;
210 if (!wait_for_completion_timeout(&wilc->sync_event,
211 msecs_to_jiffies(5000)))
212 return -ETIME;
214 return 0;
217 static int wilc1000_firmware_download(struct net_device *dev)
219 struct wilc_vif *vif = netdev_priv(dev);
220 struct wilc *wilc = vif->wilc;
221 int ret = 0;
223 if (!wilc->firmware) {
224 netdev_err(dev, "Firmware buffer is NULL\n");
225 return -ENOBUFS;
228 ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
229 wilc->firmware->size);
230 if (ret)
231 return ret;
233 release_firmware(wilc->firmware);
234 wilc->firmware = NULL;
236 netdev_dbg(dev, "Download Succeeded\n");
238 return 0;
241 static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
243 struct wilc_priv *priv = &vif->priv;
244 struct host_if_drv *hif_drv;
245 u8 b;
246 u16 hw;
247 u32 w;
249 netdev_dbg(dev, "Start configuring Firmware\n");
250 hif_drv = (struct host_if_drv *)priv->hif_drv;
251 netdev_dbg(dev, "Host = %p\n", hif_drv);
253 w = vif->iftype;
254 cpu_to_le32s(&w);
255 if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
256 0, 0))
257 goto fail;
259 b = WILC_FW_BSS_TYPE_INFRA;
260 if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
261 goto fail;
263 b = WILC_FW_TX_RATE_AUTO;
264 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
265 goto fail;
267 b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
268 if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
269 goto fail;
271 b = WILC_FW_PREAMBLE_SHORT;
272 if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
273 goto fail;
275 b = WILC_FW_11N_PROT_AUTO;
276 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
277 goto fail;
279 b = WILC_FW_ACTIVE_SCAN;
280 if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
281 goto fail;
283 b = WILC_FW_SITE_SURVEY_OFF;
284 if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
285 goto fail;
287 hw = 0xffff;
288 cpu_to_le16s(&hw);
289 if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
290 goto fail;
292 hw = 2346;
293 cpu_to_le16s(&hw);
294 if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
295 goto fail;
297 b = 0;
298 if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
299 goto fail;
301 b = 1;
302 if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
303 goto fail;
305 b = WILC_FW_NO_POWERSAVE;
306 if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
307 goto fail;
309 b = WILC_FW_SEC_NO;
310 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
311 goto fail;
313 b = WILC_FW_AUTH_OPEN_SYSTEM;
314 if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
315 goto fail;
317 b = 3;
318 if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
319 goto fail;
321 b = 3;
322 if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
323 goto fail;
325 b = WILC_FW_ACK_POLICY_NORMAL;
326 if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
327 goto fail;
329 b = 0;
330 if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
331 0, 0))
332 goto fail;
334 b = 48;
335 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
336 goto fail;
338 b = 28;
339 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
340 goto fail;
342 hw = 100;
343 cpu_to_le16s(&hw);
344 if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
345 goto fail;
347 b = WILC_FW_REKEY_POLICY_DISABLE;
348 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
349 goto fail;
351 w = 84600;
352 cpu_to_le32s(&w);
353 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
354 goto fail;
356 w = 500;
357 cpu_to_le32s(&w);
358 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
360 goto fail;
362 b = 1;
363 if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
365 goto fail;
367 b = WILC_FW_ERP_PROT_SELF_CTS;
368 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
369 goto fail;
371 b = 1;
372 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
373 goto fail;
375 b = WILC_FW_11N_OP_MODE_HT_MIXED;
376 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
377 goto fail;
379 b = 1;
380 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
381 goto fail;
383 b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
384 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
385 0, 0))
386 goto fail;
388 b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
389 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
390 goto fail;
392 b = 0;
393 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
395 goto fail;
397 b = 7;
398 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
399 goto fail;
401 b = 1;
402 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
403 1, 1))
404 goto fail;
406 return 0;
408 fail:
409 return -EINVAL;
412 static void wlan_deinitialize_threads(struct net_device *dev)
414 struct wilc_vif *vif = netdev_priv(dev);
415 struct wilc *wl = vif->wilc;
417 wl->close = 1;
419 complete(&wl->txq_event);
421 if (wl->txq_thread) {
422 kthread_stop(wl->txq_thread);
423 wl->txq_thread = NULL;
427 static void wilc_wlan_deinitialize(struct net_device *dev)
429 struct wilc_vif *vif = netdev_priv(dev);
430 struct wilc *wl = vif->wilc;
432 if (!wl) {
433 netdev_err(dev, "wl is NULL\n");
434 return;
437 if (wl->initialized) {
438 netdev_info(dev, "Deinitializing wilc1000...\n");
440 if (!wl->dev_irq_num &&
441 wl->hif_func->disable_interrupt) {
442 mutex_lock(&wl->hif_cs);
443 wl->hif_func->disable_interrupt(wl);
444 mutex_unlock(&wl->hif_cs);
446 complete(&wl->txq_event);
448 wlan_deinitialize_threads(dev);
449 deinit_irq(dev);
451 wilc_wlan_stop(wl, vif);
452 wilc_wlan_cleanup(dev);
454 wl->initialized = false;
456 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
457 } else {
458 netdev_dbg(dev, "wilc1000 is not initialized\n");
462 static int wlan_initialize_threads(struct net_device *dev)
464 struct wilc_vif *vif = netdev_priv(dev);
465 struct wilc *wilc = vif->wilc;
467 wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
468 "K_TXQ_TASK");
469 if (IS_ERR(wilc->txq_thread)) {
470 netdev_err(dev, "couldn't create TXQ thread\n");
471 wilc->close = 0;
472 return PTR_ERR(wilc->txq_thread);
474 wait_for_completion(&wilc->txq_thread_started);
476 return 0;
479 static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
481 int ret = 0;
482 struct wilc *wl = vif->wilc;
484 if (!wl->initialized) {
485 wl->mac_status = WILC_MAC_STATUS_INIT;
486 wl->close = 0;
488 ret = wilc_wlan_init(dev);
489 if (ret)
490 return ret;
492 ret = wlan_initialize_threads(dev);
493 if (ret)
494 goto fail_wilc_wlan;
496 if (wl->dev_irq_num && init_irq(dev)) {
497 ret = -EIO;
498 goto fail_threads;
501 if (!wl->dev_irq_num &&
502 wl->hif_func->enable_interrupt &&
503 wl->hif_func->enable_interrupt(wl)) {
504 ret = -EIO;
505 goto fail_irq_init;
508 ret = wilc_wlan_get_firmware(dev);
509 if (ret)
510 goto fail_irq_enable;
512 ret = wilc1000_firmware_download(dev);
513 if (ret)
514 goto fail_irq_enable;
516 ret = wilc_start_firmware(dev);
517 if (ret)
518 goto fail_irq_enable;
520 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
521 int size;
522 char firmware_ver[20];
524 size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
525 firmware_ver,
526 sizeof(firmware_ver));
527 firmware_ver[size] = '\0';
528 netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
531 ret = wilc_init_fw_config(dev, vif);
532 if (ret) {
533 netdev_err(dev, "Failed to configure firmware\n");
534 goto fail_fw_start;
536 wl->initialized = true;
537 return 0;
539 fail_fw_start:
540 wilc_wlan_stop(wl, vif);
542 fail_irq_enable:
543 if (!wl->dev_irq_num &&
544 wl->hif_func->disable_interrupt)
545 wl->hif_func->disable_interrupt(wl);
546 fail_irq_init:
547 if (wl->dev_irq_num)
548 deinit_irq(dev);
549 fail_threads:
550 wlan_deinitialize_threads(dev);
551 fail_wilc_wlan:
552 wilc_wlan_cleanup(dev);
553 netdev_err(dev, "WLAN initialization FAILED\n");
554 } else {
555 netdev_dbg(dev, "wilc1000 already initialized\n");
557 return ret;
560 static int mac_init_fn(struct net_device *ndev)
562 netif_start_queue(ndev);
563 netif_stop_queue(ndev);
565 return 0;
568 static int wilc_mac_open(struct net_device *ndev)
570 struct wilc_vif *vif = netdev_priv(ndev);
571 struct wilc *wl = vif->wilc;
572 unsigned char mac_add[ETH_ALEN] = {0};
573 int ret = 0;
575 if (!wl || !wl->dev) {
576 netdev_err(ndev, "device not ready\n");
577 return -ENODEV;
580 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
582 ret = wilc_init_host_int(ndev);
583 if (ret)
584 return ret;
586 ret = wilc_wlan_initialize(ndev, vif);
587 if (ret) {
588 wilc_deinit_host_int(ndev);
589 return ret;
592 wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
593 vif->idx);
594 wilc_get_mac_address(vif, mac_add);
595 netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
596 ether_addr_copy(ndev->dev_addr, mac_add);
598 if (!is_valid_ether_addr(ndev->dev_addr)) {
599 netdev_err(ndev, "Wrong MAC address\n");
600 wilc_deinit_host_int(ndev);
601 wilc_wlan_deinitialize(ndev);
602 return -EINVAL;
605 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
606 vif->ndev->ieee80211_ptr,
607 vif->frame_reg[0].type,
608 vif->frame_reg[0].reg);
609 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
610 vif->ndev->ieee80211_ptr,
611 vif->frame_reg[1].type,
612 vif->frame_reg[1].reg);
613 netif_wake_queue(ndev);
614 wl->open_ifcs++;
615 vif->mac_opened = 1;
616 return 0;
619 static struct net_device_stats *mac_stats(struct net_device *dev)
621 struct wilc_vif *vif = netdev_priv(dev);
623 return &vif->netstats;
626 static void wilc_set_multicast_list(struct net_device *dev)
628 struct netdev_hw_addr *ha;
629 struct wilc_vif *vif = netdev_priv(dev);
630 int i;
631 u8 *mc_list;
632 u8 *cur_mc;
634 if (dev->flags & IFF_PROMISC)
635 return;
637 if (dev->flags & IFF_ALLMULTI ||
638 dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
639 wilc_setup_multicast_filter(vif, 0, 0, NULL);
640 return;
643 if (dev->mc.count == 0) {
644 wilc_setup_multicast_filter(vif, 1, 0, NULL);
645 return;
648 mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
649 if (!mc_list)
650 return;
652 cur_mc = mc_list;
653 i = 0;
654 netdev_for_each_mc_addr(ha, dev) {
655 memcpy(cur_mc, ha->addr, ETH_ALEN);
656 netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
657 i++;
658 cur_mc += ETH_ALEN;
661 if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
662 kfree(mc_list);
665 static void wilc_tx_complete(void *priv, int status)
667 struct tx_complete_data *pv_data = priv;
669 dev_kfree_skb(pv_data->skb);
670 kfree(pv_data);
673 netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
675 struct wilc_vif *vif = netdev_priv(ndev);
676 struct wilc *wilc = vif->wilc;
677 struct tx_complete_data *tx_data = NULL;
678 int queue_count;
680 if (skb->dev != ndev) {
681 netdev_err(ndev, "Packet not destined to this device\n");
682 return 0;
685 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
686 if (!tx_data) {
687 dev_kfree_skb(skb);
688 netif_wake_queue(ndev);
689 return 0;
692 tx_data->buff = skb->data;
693 tx_data->size = skb->len;
694 tx_data->skb = skb;
696 vif->netstats.tx_packets++;
697 vif->netstats.tx_bytes += tx_data->size;
698 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
699 tx_data->buff, tx_data->size,
700 wilc_tx_complete);
702 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
703 int srcu_idx;
704 struct wilc_vif *vif;
706 srcu_idx = srcu_read_lock(&wilc->srcu);
707 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
708 if (vif->mac_opened)
709 netif_stop_queue(vif->ndev);
711 srcu_read_unlock(&wilc->srcu, srcu_idx);
714 return 0;
717 static int wilc_mac_close(struct net_device *ndev)
719 struct wilc_vif *vif = netdev_priv(ndev);
720 struct wilc *wl = vif->wilc;
722 netdev_dbg(ndev, "Mac close\n");
724 if (wl->open_ifcs > 0)
725 wl->open_ifcs--;
726 else
727 return 0;
729 if (vif->ndev) {
730 netif_stop_queue(vif->ndev);
732 wilc_deinit_host_int(vif->ndev);
735 if (wl->open_ifcs == 0) {
736 netdev_dbg(ndev, "Deinitializing wilc1000\n");
737 wl->close = 1;
738 wilc_wlan_deinitialize(ndev);
741 vif->mac_opened = 0;
743 return 0;
746 void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
747 u32 pkt_offset)
749 unsigned int frame_len = 0;
750 int stats;
751 unsigned char *buff_to_send = NULL;
752 struct sk_buff *skb;
753 struct net_device *wilc_netdev;
754 struct wilc_vif *vif;
756 if (!wilc)
757 return;
759 wilc_netdev = get_if_handler(wilc, buff);
760 if (!wilc_netdev)
761 return;
763 buff += pkt_offset;
764 vif = netdev_priv(wilc_netdev);
766 if (size > 0) {
767 frame_len = size;
768 buff_to_send = buff;
770 skb = dev_alloc_skb(frame_len);
771 if (!skb)
772 return;
774 skb->dev = wilc_netdev;
776 skb_put_data(skb, buff_to_send, frame_len);
778 skb->protocol = eth_type_trans(skb, wilc_netdev);
779 vif->netstats.rx_packets++;
780 vif->netstats.rx_bytes += frame_len;
781 skb->ip_summed = CHECKSUM_UNNECESSARY;
782 stats = netif_rx(skb);
783 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
787 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
789 int srcu_idx;
790 struct wilc_vif *vif;
792 srcu_idx = srcu_read_lock(&wilc->srcu);
793 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
794 u16 type = le16_to_cpup((__le16 *)buff);
796 if (vif->priv.p2p_listen_state &&
797 ((type == vif->frame_reg[0].type &&
798 vif->frame_reg[0].reg) ||
799 (type == vif->frame_reg[1].type &&
800 vif->frame_reg[1].reg)))
801 wilc_wfi_p2p_rx(vif, buff, size);
803 if (vif->monitor_flag)
804 wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
806 srcu_read_unlock(&wilc->srcu, srcu_idx);
809 static const struct net_device_ops wilc_netdev_ops = {
810 .ndo_init = mac_init_fn,
811 .ndo_open = wilc_mac_open,
812 .ndo_stop = wilc_mac_close,
813 .ndo_start_xmit = wilc_mac_xmit,
814 .ndo_get_stats = mac_stats,
815 .ndo_set_rx_mode = wilc_set_multicast_list,
818 void wilc_netdev_cleanup(struct wilc *wilc)
820 struct wilc_vif *vif;
821 int srcu_idx, ifc_cnt = 0;
823 if (!wilc)
824 return;
826 if (wilc->firmware) {
827 release_firmware(wilc->firmware);
828 wilc->firmware = NULL;
831 srcu_idx = srcu_read_lock(&wilc->srcu);
832 list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
833 if (vif->ndev)
834 unregister_netdev(vif->ndev);
836 srcu_read_unlock(&wilc->srcu, srcu_idx);
838 wilc_wfi_deinit_mon_interface(wilc, false);
839 flush_workqueue(wilc->hif_workqueue);
840 destroy_workqueue(wilc->hif_workqueue);
842 while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) {
843 mutex_lock(&wilc->vif_mutex);
844 if (wilc->vif_num <= 0) {
845 mutex_unlock(&wilc->vif_mutex);
846 break;
848 vif = wilc_get_wl_to_vif(wilc);
849 if (!IS_ERR(vif))
850 list_del_rcu(&vif->list);
852 wilc->vif_num--;
853 mutex_unlock(&wilc->vif_mutex);
854 synchronize_srcu(&wilc->srcu);
855 ifc_cnt++;
858 wilc_wlan_cfg_deinit(wilc);
859 wlan_deinit_locks(wilc);
860 kfree(wilc->bus_data);
861 wiphy_unregister(wilc->wiphy);
862 wiphy_free(wilc->wiphy);
864 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
866 static u8 wilc_get_available_idx(struct wilc *wl)
868 int idx = 0;
869 struct wilc_vif *vif;
870 int srcu_idx;
872 srcu_idx = srcu_read_lock(&wl->srcu);
873 list_for_each_entry_rcu(vif, &wl->vif_list, list) {
874 if (vif->idx == 0)
875 idx = 1;
876 else
877 idx = 0;
879 srcu_read_unlock(&wl->srcu, srcu_idx);
880 return idx;
883 struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
884 int vif_type, enum nl80211_iftype type,
885 bool rtnl_locked)
887 struct net_device *ndev;
888 struct wilc_vif *vif;
889 int ret;
891 ndev = alloc_etherdev(sizeof(*vif));
892 if (!ndev)
893 return ERR_PTR(-ENOMEM);
895 vif = netdev_priv(ndev);
896 ndev->ieee80211_ptr = &vif->priv.wdev;
897 strcpy(ndev->name, name);
898 vif->wilc = wl;
899 vif->ndev = ndev;
900 ndev->ml_priv = vif;
902 ndev->netdev_ops = &wilc_netdev_ops;
904 SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
906 vif->priv.wdev.wiphy = wl->wiphy;
907 vif->priv.wdev.netdev = ndev;
908 vif->priv.wdev.iftype = type;
909 vif->priv.dev = ndev;
911 if (rtnl_locked)
912 ret = register_netdevice(ndev);
913 else
914 ret = register_netdev(ndev);
916 if (ret) {
917 free_netdev(ndev);
918 return ERR_PTR(-EFAULT);
921 ndev->needs_free_netdev = true;
922 vif->iftype = vif_type;
923 vif->idx = wilc_get_available_idx(wl);
924 vif->mac_opened = 0;
925 mutex_lock(&wl->vif_mutex);
926 list_add_tail_rcu(&vif->list, &wl->vif_list);
927 wl->vif_num += 1;
928 mutex_unlock(&wl->vif_mutex);
929 synchronize_srcu(&wl->srcu);
931 return vif;
934 MODULE_LICENSE("GPL");