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 bool wilc_enable_ps = true;
16
dev_state_ev_handler(struct notifier_block * this,unsigned long event,void * ptr)17 static int dev_state_ev_handler(struct notifier_block *this,
18 unsigned long event, void *ptr)
19 {
20 struct in_ifaddr *dev_iface = ptr;
21 struct wilc_priv *priv;
22 struct host_if_drv *hif_drv;
23 struct net_device *dev;
24 u8 *ip_addr_buf;
25 struct wilc_vif *vif;
26 u8 null_ip[4] = {0};
27 char wlan_dev_name[5] = "wlan0";
28
29 if (!dev_iface || !dev_iface->ifa_dev || !dev_iface->ifa_dev->dev)
30 return NOTIFY_DONE;
31
32 if (memcmp(dev_iface->ifa_label, "wlan0", 5) &&
33 memcmp(dev_iface->ifa_label, "p2p0", 4))
34 return NOTIFY_DONE;
35
36 dev = (struct net_device *)dev_iface->ifa_dev->dev;
37 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
38 return NOTIFY_DONE;
39
40 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
41 if (!priv)
42 return NOTIFY_DONE;
43
44 hif_drv = (struct host_if_drv *)priv->hif_drv;
45 vif = netdev_priv(dev);
46 if (!vif || !hif_drv)
47 return NOTIFY_DONE;
48
49 switch (event) {
50 case NETDEV_UP:
51 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
52 hif_drv->ifc_up = 1;
53 wilc_optaining_ip = false;
54 del_timer(&wilc_during_ip_timer);
55 }
56
57 if (wilc_enable_ps)
58 wilc_set_power_mgmt(vif, 1, 0);
59
60 netdev_dbg(dev, "[%s] Up IP\n", dev_iface->ifa_label);
61
62 ip_addr_buf = (char *)&dev_iface->ifa_address;
63 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
64 ip_addr_buf[0], ip_addr_buf[1],
65 ip_addr_buf[2], ip_addr_buf[3]);
66 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
67
68 break;
69
70 case NETDEV_DOWN:
71 if (vif->iftype == STATION_MODE || vif->iftype == CLIENT_MODE) {
72 hif_drv->ifc_up = 0;
73 wilc_optaining_ip = false;
74 }
75
76 if (memcmp(dev_iface->ifa_label, wlan_dev_name, 5) == 0)
77 wilc_set_power_mgmt(vif, 0, 0);
78
79 wilc_resolve_disconnect_aberration(vif);
80
81 netdev_dbg(dev, "[%s] Down IP\n", dev_iface->ifa_label);
82
83 ip_addr_buf = null_ip;
84 netdev_dbg(dev, "IP add=%d:%d:%d:%d\n",
85 ip_addr_buf[0], ip_addr_buf[1],
86 ip_addr_buf[2], ip_addr_buf[3]);
87
88 wilc_setup_ipaddress(vif, ip_addr_buf, vif->idx);
89
90 break;
91
92 default:
93 break;
94 }
95
96 return NOTIFY_DONE;
97 }
98
isr_uh_routine(int irq,void * user_data)99 static irqreturn_t isr_uh_routine(int irq, void *user_data)
100 {
101 struct net_device *dev = user_data;
102 struct wilc_vif *vif = netdev_priv(dev);
103 struct wilc *wilc = vif->wilc;
104
105 if (wilc->close) {
106 netdev_err(dev, "Can't handle UH interrupt\n");
107 return IRQ_HANDLED;
108 }
109 return IRQ_WAKE_THREAD;
110 }
111
isr_bh_routine(int irq,void * userdata)112 static irqreturn_t isr_bh_routine(int irq, void *userdata)
113 {
114 struct net_device *dev = userdata;
115 struct wilc_vif *vif = netdev_priv(userdata);
116 struct wilc *wilc = vif->wilc;
117
118 if (wilc->close) {
119 netdev_err(dev, "Can't handle BH interrupt\n");
120 return IRQ_HANDLED;
121 }
122
123 wilc_handle_isr(wilc);
124
125 return IRQ_HANDLED;
126 }
127
init_irq(struct net_device * dev)128 static int init_irq(struct net_device *dev)
129 {
130 int ret = 0;
131 struct wilc_vif *vif = netdev_priv(dev);
132 struct wilc *wl = vif->wilc;
133
134 ret = gpiod_direction_input(wl->gpio_irq);
135 if (ret) {
136 netdev_err(dev, "could not obtain gpio for WILC_INTR\n");
137 return ret;
138 }
139
140 wl->dev_irq_num = gpiod_to_irq(wl->gpio_irq);
141
142 ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
143 isr_bh_routine,
144 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
145 "WILC_IRQ", dev);
146 if (ret < 0)
147 netdev_err(dev, "Failed to request IRQ\n");
148 else
149 netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n",
150 wl->dev_irq_num);
151
152 return ret;
153 }
154
deinit_irq(struct net_device * dev)155 static void deinit_irq(struct net_device *dev)
156 {
157 struct wilc_vif *vif = netdev_priv(dev);
158 struct wilc *wilc = vif->wilc;
159
160 /* Deinitialize IRQ */
161 if (wilc->dev_irq_num)
162 free_irq(wilc->dev_irq_num, wilc);
163 }
164
wilc_mac_indicate(struct wilc * wilc)165 void wilc_mac_indicate(struct wilc *wilc)
166 {
167 int status;
168
169 wilc_wlan_cfg_get_val(WID_STATUS, (unsigned char *)&status, 4);
170 if (wilc->mac_status == MAC_STATUS_INIT) {
171 wilc->mac_status = status;
172 complete(&wilc->sync_event);
173 } else {
174 wilc->mac_status = status;
175 }
176 }
177
get_if_handler(struct wilc * wilc,u8 * mac_header)178 static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
179 {
180 u8 *bssid, *bssid1;
181 int i = 0;
182
183 bssid = mac_header + 10;
184 bssid1 = mac_header + 4;
185
186 for (i = 0; i < wilc->vif_num; i++) {
187 if (wilc->vif[i]->mode == STATION_MODE)
188 if (ether_addr_equal_unaligned(bssid,
189 wilc->vif[i]->bssid))
190 return wilc->vif[i]->ndev;
191 if (wilc->vif[i]->mode == AP_MODE)
192 if (ether_addr_equal_unaligned(bssid1,
193 wilc->vif[i]->bssid))
194 return wilc->vif[i]->ndev;
195 }
196
197 return NULL;
198 }
199
wilc_wlan_set_bssid(struct net_device * wilc_netdev,u8 * bssid,u8 mode)200 int wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
201 {
202 struct wilc_vif *vif = netdev_priv(wilc_netdev);
203
204 memcpy(vif->bssid, bssid, 6);
205 vif->mode = mode;
206
207 return 0;
208 }
209
wilc_wlan_get_num_conn_ifcs(struct wilc * wilc)210 int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
211 {
212 u8 i = 0;
213 u8 null_bssid[6] = {0};
214 u8 ret_val = 0;
215
216 for (i = 0; i < wilc->vif_num; i++)
217 if (memcmp(wilc->vif[i]->bssid, null_bssid, 6))
218 ret_val++;
219
220 return ret_val;
221 }
222
linux_wlan_txq_task(void * vp)223 static int linux_wlan_txq_task(void *vp)
224 {
225 int ret;
226 u32 txq_count;
227 struct net_device *dev = vp;
228 struct wilc_vif *vif = netdev_priv(dev);
229 struct wilc *wl = vif->wilc;
230
231 complete(&wl->txq_thread_started);
232 while (1) {
233 wait_for_completion(&wl->txq_event);
234
235 if (wl->close) {
236 complete(&wl->txq_thread_started);
237
238 while (!kthread_should_stop())
239 schedule();
240 break;
241 }
242 do {
243 ret = wilc_wlan_handle_txq(dev, &txq_count);
244 if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
245 if (netif_queue_stopped(wl->vif[0]->ndev))
246 netif_wake_queue(wl->vif[0]->ndev);
247 if (netif_queue_stopped(wl->vif[1]->ndev))
248 netif_wake_queue(wl->vif[1]->ndev);
249 }
250 } while (ret == WILC_TX_ERR_NO_BUF && !wl->close);
251 }
252 return 0;
253 }
254
wilc_wlan_get_firmware(struct net_device * dev)255 static int wilc_wlan_get_firmware(struct net_device *dev)
256 {
257 struct wilc_vif *vif = netdev_priv(dev);
258 struct wilc *wilc = vif->wilc;
259 int chip_id, ret = 0;
260 const struct firmware *wilc_firmware;
261 char *firmware;
262
263 chip_id = wilc_get_chipid(wilc, false);
264
265 if (chip_id < 0x1003a0)
266 firmware = FIRMWARE_1002;
267 else
268 firmware = FIRMWARE_1003;
269
270 netdev_info(dev, "loading firmware %s\n", firmware);
271
272 if (!(&vif->ndev->dev))
273 goto fail;
274
275 if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
276 netdev_err(dev, "%s - firmware not available\n", firmware);
277 ret = -1;
278 goto fail;
279 }
280 wilc->firmware = wilc_firmware;
281
282 fail:
283
284 return ret;
285 }
286
linux_wlan_start_firmware(struct net_device * dev)287 static int linux_wlan_start_firmware(struct net_device *dev)
288 {
289 struct wilc_vif *vif = netdev_priv(dev);
290 struct wilc *wilc = vif->wilc;
291 int ret = 0;
292
293 ret = wilc_wlan_start(wilc);
294 if (ret < 0)
295 return ret;
296
297 if (!wait_for_completion_timeout(&wilc->sync_event,
298 msecs_to_jiffies(5000)))
299 return -ETIME;
300
301 return 0;
302 }
303
wilc1000_firmware_download(struct net_device * dev)304 static int wilc1000_firmware_download(struct net_device *dev)
305 {
306 struct wilc_vif *vif = netdev_priv(dev);
307 struct wilc *wilc = vif->wilc;
308 int ret = 0;
309
310 if (!wilc->firmware) {
311 netdev_err(dev, "Firmware buffer is NULL\n");
312 return -ENOBUFS;
313 }
314
315 ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
316 wilc->firmware->size);
317 if (ret < 0)
318 return ret;
319
320 release_firmware(wilc->firmware);
321 wilc->firmware = NULL;
322
323 netdev_dbg(dev, "Download Succeeded\n");
324
325 return 0;
326 }
327
linux_wlan_init_test_config(struct net_device * dev,struct wilc_vif * vif)328 static int linux_wlan_init_test_config(struct net_device *dev,
329 struct wilc_vif *vif)
330 {
331 unsigned char c_val[64];
332 struct wilc *wilc = vif->wilc;
333 struct wilc_priv *priv;
334 struct host_if_drv *hif_drv;
335
336 netdev_dbg(dev, "Start configuring Firmware\n");
337 priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
338 hif_drv = (struct host_if_drv *)priv->hif_drv;
339 netdev_dbg(dev, "Host = %p\n", hif_drv);
340 wilc_get_chipid(wilc, false);
341
342 *(int *)c_val = 1;
343
344 if (!wilc_wlan_cfg_set(vif, 1, WID_SET_DRV_HANDLER, c_val, 4, 0, 0))
345 goto fail;
346
347 c_val[0] = 0;
348 if (!wilc_wlan_cfg_set(vif, 0, WID_PC_TEST_MODE, c_val, 1, 0, 0))
349 goto fail;
350
351 c_val[0] = INFRASTRUCTURE;
352 if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, c_val, 1, 0, 0))
353 goto fail;
354
355 c_val[0] = AUTORATE;
356 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, c_val, 1, 0, 0))
357 goto fail;
358
359 c_val[0] = G_MIXED_11B_2_MODE;
360 if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, c_val, 1, 0,
361 0))
362 goto fail;
363
364 c_val[0] = 1;
365 if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_CHANNEL, c_val, 1, 0, 0))
366 goto fail;
367
368 c_val[0] = G_SHORT_PREAMBLE;
369 if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, c_val, 1, 0, 0))
370 goto fail;
371
372 c_val[0] = AUTO_PROT;
373 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, c_val, 1, 0, 0))
374 goto fail;
375
376 c_val[0] = ACTIVE_SCAN;
377 if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, c_val, 1, 0, 0))
378 goto fail;
379
380 c_val[0] = SITE_SURVEY_OFF;
381 if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, c_val, 1, 0, 0))
382 goto fail;
383
384 *((int *)c_val) = 0xffff;
385 if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, c_val, 2, 0, 0))
386 goto fail;
387
388 *((int *)c_val) = 2346;
389 if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, c_val, 2, 0, 0))
390 goto fail;
391
392 c_val[0] = 0;
393 if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, c_val, 1, 0, 0))
394 goto fail;
395
396 c_val[0] = 1;
397 if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, c_val, 1, 0, 0))
398 goto fail;
399
400 c_val[0] = NO_POWERSAVE;
401 if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, c_val, 1, 0, 0))
402 goto fail;
403
404 c_val[0] = NO_SECURITY; /* NO_ENCRYPT, 0x79 */
405 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, c_val, 1, 0, 0))
406 goto fail;
407
408 c_val[0] = OPEN_SYSTEM;
409 if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, c_val, 1, 0, 0))
410 goto fail;
411
412 strcpy(c_val, "123456790abcdef1234567890");
413 if (!wilc_wlan_cfg_set(vif, 0, WID_WEP_KEY_VALUE, c_val,
414 (strlen(c_val) + 1), 0, 0))
415 goto fail;
416
417 strcpy(c_val, "12345678");
418 if (!wilc_wlan_cfg_set(vif, 0, WID_11I_PSK, c_val, (strlen(c_val)), 0,
419 0))
420 goto fail;
421
422 strcpy(c_val, "password");
423 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_KEY, c_val, (strlen(c_val) + 1),
424 0, 0))
425 goto fail;
426
427 c_val[0] = 192;
428 c_val[1] = 168;
429 c_val[2] = 1;
430 c_val[3] = 112;
431 if (!wilc_wlan_cfg_set(vif, 0, WID_1X_SERV_ADDR, c_val, 4, 0, 0))
432 goto fail;
433
434 c_val[0] = 3;
435 if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, c_val, 1, 0, 0))
436 goto fail;
437
438 c_val[0] = 3;
439 if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, c_val, 1, 0, 0))
440 goto fail;
441
442 c_val[0] = NORMAL_ACK;
443 if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, c_val, 1, 0, 0))
444 goto fail;
445
446 c_val[0] = 0;
447 if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, c_val, 1,
448 0, 0))
449 goto fail;
450
451 c_val[0] = 48;
452 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, c_val, 1, 0,
453 0))
454 goto fail;
455
456 c_val[0] = 28;
457 if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, c_val, 1, 0,
458 0))
459 goto fail;
460
461 *((int *)c_val) = 100;
462 if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, c_val, 2, 0, 0))
463 goto fail;
464
465 c_val[0] = REKEY_DISABLE;
466 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, c_val, 1, 0, 0))
467 goto fail;
468
469 *((int *)c_val) = 84600;
470 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, c_val, 4, 0, 0))
471 goto fail;
472
473 *((int *)c_val) = 500;
474 if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, c_val, 4, 0,
475 0))
476 goto fail;
477
478 c_val[0] = 1;
479 if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, c_val, 1, 0,
480 0))
481 goto fail;
482
483 c_val[0] = G_SELF_CTS_PROT;
484 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, c_val, 1, 0, 0))
485 goto fail;
486
487 c_val[0] = 1;
488 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, c_val, 1, 0, 0))
489 goto fail;
490
491 c_val[0] = HT_MIXED_MODE;
492 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, c_val, 1, 0,
493 0))
494 goto fail;
495
496 c_val[0] = 1;
497 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, c_val, 1, 0,
498 0))
499 goto fail;
500
501 c_val[0] = DETECT_PROTECT_REPORT;
502 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, c_val, 1,
503 0, 0))
504 goto fail;
505
506 c_val[0] = RTS_CTS_NONHT_PROT;
507 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, c_val, 1, 0, 0))
508 goto fail;
509
510 c_val[0] = 0;
511 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, c_val, 1, 0,
512 0))
513 goto fail;
514
515 c_val[0] = MIMO_MODE;
516 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_SMPS_MODE, c_val, 1, 0, 0))
517 goto fail;
518
519 c_val[0] = 7;
520 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, c_val, 1, 0,
521 0))
522 goto fail;
523
524 c_val[0] = 1;
525 if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, c_val, 1,
526 1, 1))
527 goto fail;
528
529 return 0;
530
531 fail:
532 return -1;
533 }
534
wlan_deinit_locks(struct net_device * dev)535 static int wlan_deinit_locks(struct net_device *dev)
536 {
537 struct wilc_vif *vif = netdev_priv(dev);
538 struct wilc *wilc = vif->wilc;
539
540 mutex_destroy(&wilc->hif_cs);
541 mutex_destroy(&wilc->rxq_cs);
542 mutex_destroy(&wilc->txq_add_to_head_cs);
543
544 return 0;
545 }
546
wlan_deinitialize_threads(struct net_device * dev)547 static void wlan_deinitialize_threads(struct net_device *dev)
548 {
549 struct wilc_vif *vif = netdev_priv(dev);
550 struct wilc *wl = vif->wilc;
551
552 wl->close = 1;
553
554 complete(&wl->txq_event);
555
556 if (wl->txq_thread) {
557 kthread_stop(wl->txq_thread);
558 wl->txq_thread = NULL;
559 }
560 }
561
wilc_wlan_deinitialize(struct net_device * dev)562 static void wilc_wlan_deinitialize(struct net_device *dev)
563 {
564 struct wilc_vif *vif = netdev_priv(dev);
565 struct wilc *wl = vif->wilc;
566
567 if (!wl) {
568 netdev_err(dev, "wl is NULL\n");
569 return;
570 }
571
572 if (wl->initialized) {
573 netdev_info(dev, "Deinitializing wilc1000...\n");
574
575 if (!wl->dev_irq_num &&
576 wl->hif_func->disable_interrupt) {
577 mutex_lock(&wl->hif_cs);
578 wl->hif_func->disable_interrupt(wl);
579 mutex_unlock(&wl->hif_cs);
580 }
581 complete(&wl->txq_event);
582
583 wlan_deinitialize_threads(dev);
584 deinit_irq(dev);
585
586 wilc_wlan_stop(wl);
587 wilc_wlan_cleanup(dev);
588 wlan_deinit_locks(dev);
589
590 wl->initialized = false;
591
592 netdev_dbg(dev, "wilc1000 deinitialization Done\n");
593 } else {
594 netdev_dbg(dev, "wilc1000 is not initialized\n");
595 }
596 }
597
wlan_init_locks(struct net_device * dev)598 static int wlan_init_locks(struct net_device *dev)
599 {
600 struct wilc_vif *vif = netdev_priv(dev);
601 struct wilc *wl = vif->wilc;
602
603 mutex_init(&wl->hif_cs);
604 mutex_init(&wl->rxq_cs);
605
606 spin_lock_init(&wl->txq_spinlock);
607 mutex_init(&wl->txq_add_to_head_cs);
608
609 init_completion(&wl->txq_event);
610
611 init_completion(&wl->cfg_event);
612 init_completion(&wl->sync_event);
613 init_completion(&wl->txq_thread_started);
614
615 return 0;
616 }
617
wlan_initialize_threads(struct net_device * dev)618 static int wlan_initialize_threads(struct net_device *dev)
619 {
620 struct wilc_vif *vif = netdev_priv(dev);
621 struct wilc *wilc = vif->wilc;
622
623 wilc->txq_thread = kthread_run(linux_wlan_txq_task, (void *)dev,
624 "K_TXQ_TASK");
625 if (IS_ERR(wilc->txq_thread)) {
626 netdev_err(dev, "couldn't create TXQ thread\n");
627 wilc->close = 0;
628 return PTR_ERR(wilc->txq_thread);
629 }
630 wait_for_completion(&wilc->txq_thread_started);
631
632 return 0;
633 }
634
wilc_wlan_initialize(struct net_device * dev,struct wilc_vif * vif)635 static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
636 {
637 int ret = 0;
638 struct wilc *wl = vif->wilc;
639
640 if (!wl->initialized) {
641 wl->mac_status = MAC_STATUS_INIT;
642 wl->close = 0;
643
644 wlan_init_locks(dev);
645
646 ret = wilc_wlan_init(dev);
647 if (ret < 0) {
648 ret = -EIO;
649 goto fail_locks;
650 }
651
652 if (wl->gpio_irq && init_irq(dev)) {
653 ret = -EIO;
654 goto fail_locks;
655 }
656
657 ret = wlan_initialize_threads(dev);
658 if (ret < 0) {
659 ret = -EIO;
660 goto fail_wilc_wlan;
661 }
662
663 if (!wl->dev_irq_num &&
664 wl->hif_func->enable_interrupt &&
665 wl->hif_func->enable_interrupt(wl)) {
666 ret = -EIO;
667 goto fail_irq_init;
668 }
669
670 if (wilc_wlan_get_firmware(dev)) {
671 ret = -EIO;
672 goto fail_irq_enable;
673 }
674
675 ret = wilc1000_firmware_download(dev);
676 if (ret < 0) {
677 ret = -EIO;
678 goto fail_irq_enable;
679 }
680
681 ret = linux_wlan_start_firmware(dev);
682 if (ret < 0) {
683 ret = -EIO;
684 goto fail_irq_enable;
685 }
686
687 if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
688 int size;
689 char firmware_ver[20];
690
691 size = wilc_wlan_cfg_get_val(WID_FIRMWARE_VERSION,
692 firmware_ver,
693 sizeof(firmware_ver));
694 firmware_ver[size] = '\0';
695 netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
696 }
697 ret = linux_wlan_init_test_config(dev, vif);
698
699 if (ret < 0) {
700 netdev_err(dev, "Failed to configure firmware\n");
701 ret = -EIO;
702 goto fail_fw_start;
703 }
704
705 wl->initialized = true;
706 return 0;
707
708 fail_fw_start:
709 wilc_wlan_stop(wl);
710
711 fail_irq_enable:
712 if (!wl->dev_irq_num &&
713 wl->hif_func->disable_interrupt)
714 wl->hif_func->disable_interrupt(wl);
715 fail_irq_init:
716 if (wl->dev_irq_num)
717 deinit_irq(dev);
718
719 wlan_deinitialize_threads(dev);
720 fail_wilc_wlan:
721 wilc_wlan_cleanup(dev);
722 fail_locks:
723 wlan_deinit_locks(dev);
724 netdev_err(dev, "WLAN initialization FAILED\n");
725 } else {
726 netdev_dbg(dev, "wilc1000 already initialized\n");
727 }
728 return ret;
729 }
730
mac_init_fn(struct net_device * ndev)731 static int mac_init_fn(struct net_device *ndev)
732 {
733 netif_start_queue(ndev);
734 netif_stop_queue(ndev);
735
736 return 0;
737 }
738
wilc_mac_open(struct net_device * ndev)739 static int wilc_mac_open(struct net_device *ndev)
740 {
741 struct wilc_vif *vif = netdev_priv(ndev);
742 struct wilc *wl = vif->wilc;
743 unsigned char mac_add[ETH_ALEN] = {0};
744 int ret = 0;
745 int i = 0;
746
747 if (!wl || !wl->dev) {
748 netdev_err(ndev, "device not ready\n");
749 return -ENODEV;
750 }
751
752 netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
753
754 ret = wilc_init_host_int(ndev);
755 if (ret < 0)
756 return ret;
757
758 ret = wilc_wlan_initialize(ndev, vif);
759 if (ret < 0) {
760 wilc_deinit_host_int(ndev);
761 return ret;
762 }
763
764 for (i = 0; i < wl->vif_num; i++) {
765 if (ndev == wl->vif[i]->ndev) {
766 wilc_set_wfi_drv_handler(vif, wilc_get_vif_idx(vif),
767 vif->iftype, vif->ifc_id);
768 wilc_set_operation_mode(vif, vif->iftype);
769 break;
770 }
771 }
772
773 wilc_get_mac_address(vif, mac_add);
774 netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
775 memcpy(wl->vif[i]->src_addr, mac_add, ETH_ALEN);
776 memcpy(ndev->dev_addr, wl->vif[i]->src_addr, ETH_ALEN);
777
778 if (!is_valid_ether_addr(ndev->dev_addr)) {
779 netdev_err(ndev, "Wrong MAC address\n");
780 wilc_deinit_host_int(ndev);
781 wilc_wlan_deinitialize(ndev);
782 return -EINVAL;
783 }
784
785 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
786 vif->ndev->ieee80211_ptr,
787 vif->frame_reg[0].type,
788 vif->frame_reg[0].reg);
789 wilc_mgmt_frame_register(vif->ndev->ieee80211_ptr->wiphy,
790 vif->ndev->ieee80211_ptr,
791 vif->frame_reg[1].type,
792 vif->frame_reg[1].reg);
793 netif_wake_queue(ndev);
794 wl->open_ifcs++;
795 vif->mac_opened = 1;
796 return 0;
797 }
798
mac_stats(struct net_device * dev)799 static struct net_device_stats *mac_stats(struct net_device *dev)
800 {
801 struct wilc_vif *vif = netdev_priv(dev);
802
803 return &vif->netstats;
804 }
805
wilc_set_multicast_list(struct net_device * dev)806 static void wilc_set_multicast_list(struct net_device *dev)
807 {
808 struct netdev_hw_addr *ha;
809 struct wilc_vif *vif = netdev_priv(dev);
810 int i = 0;
811
812 if (dev->flags & IFF_PROMISC)
813 return;
814
815 if (dev->flags & IFF_ALLMULTI ||
816 dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
817 wilc_setup_multicast_filter(vif, false, 0);
818 return;
819 }
820
821 if (dev->mc.count == 0) {
822 wilc_setup_multicast_filter(vif, true, 0);
823 return;
824 }
825
826 netdev_for_each_mc_addr(ha, dev) {
827 memcpy(wilc_multicast_mac_addr_list[i], ha->addr, ETH_ALEN);
828 netdev_dbg(dev, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i,
829 wilc_multicast_mac_addr_list[i][0],
830 wilc_multicast_mac_addr_list[i][1],
831 wilc_multicast_mac_addr_list[i][2],
832 wilc_multicast_mac_addr_list[i][3],
833 wilc_multicast_mac_addr_list[i][4],
834 wilc_multicast_mac_addr_list[i][5]);
835 i++;
836 }
837
838 wilc_setup_multicast_filter(vif, true, (dev->mc.count));
839 }
840
linux_wlan_tx_complete(void * priv,int status)841 static void linux_wlan_tx_complete(void *priv, int status)
842 {
843 struct tx_complete_data *pv_data = priv;
844
845 dev_kfree_skb(pv_data->skb);
846 kfree(pv_data);
847 }
848
wilc_mac_xmit(struct sk_buff * skb,struct net_device * ndev)849 netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
850 {
851 struct wilc_vif *vif = netdev_priv(ndev);
852 struct wilc *wilc = vif->wilc;
853 struct tx_complete_data *tx_data = NULL;
854 int queue_count;
855 char *udp_buf;
856 struct iphdr *ih;
857 struct ethhdr *eth_h;
858
859 if (skb->dev != ndev) {
860 netdev_err(ndev, "Packet not destined to this device\n");
861 return 0;
862 }
863
864 tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
865 if (!tx_data) {
866 dev_kfree_skb(skb);
867 netif_wake_queue(ndev);
868 return 0;
869 }
870
871 tx_data->buff = skb->data;
872 tx_data->size = skb->len;
873 tx_data->skb = skb;
874
875 eth_h = (struct ethhdr *)(skb->data);
876 if (eth_h->h_proto == cpu_to_be16(0x8e88))
877 netdev_dbg(ndev, "EAPOL transmitted\n");
878
879 ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
880
881 udp_buf = (char *)ih + sizeof(struct iphdr);
882 if ((udp_buf[1] == 68 && udp_buf[3] == 67) ||
883 (udp_buf[1] == 67 && udp_buf[3] == 68))
884 netdev_dbg(ndev, "DHCP Message transmitted, type:%x %x %x\n",
885 udp_buf[248], udp_buf[249], udp_buf[250]);
886
887 vif->netstats.tx_packets++;
888 vif->netstats.tx_bytes += tx_data->size;
889 tx_data->bssid = wilc->vif[vif->idx]->bssid;
890 queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
891 tx_data->buff, tx_data->size,
892 linux_wlan_tx_complete);
893
894 if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
895 netif_stop_queue(wilc->vif[0]->ndev);
896 netif_stop_queue(wilc->vif[1]->ndev);
897 }
898
899 return 0;
900 }
901
wilc_mac_close(struct net_device * ndev)902 static int wilc_mac_close(struct net_device *ndev)
903 {
904 struct wilc_priv *priv;
905 struct wilc_vif *vif = netdev_priv(ndev);
906 struct host_if_drv *hif_drv;
907 struct wilc *wl;
908
909 if (!vif || !vif->ndev || !vif->ndev->ieee80211_ptr ||
910 !vif->ndev->ieee80211_ptr->wiphy)
911 return 0;
912
913 priv = wiphy_priv(vif->ndev->ieee80211_ptr->wiphy);
914 wl = vif->wilc;
915
916 if (!priv)
917 return 0;
918
919 hif_drv = (struct host_if_drv *)priv->hif_drv;
920
921 netdev_dbg(ndev, "Mac close\n");
922
923 if (!wl)
924 return 0;
925
926 if (!hif_drv)
927 return 0;
928
929 if (wl->open_ifcs > 0)
930 wl->open_ifcs--;
931 else
932 return 0;
933
934 if (vif->ndev) {
935 netif_stop_queue(vif->ndev);
936
937 wilc_deinit_host_int(vif->ndev);
938 }
939
940 if (wl->open_ifcs == 0) {
941 netdev_dbg(ndev, "Deinitializing wilc1000\n");
942 wl->close = 1;
943 wilc_wlan_deinitialize(ndev);
944 wilc_wfi_deinit_mon_interface();
945 }
946
947 vif->mac_opened = 0;
948
949 return 0;
950 }
951
wilc_frmw_to_linux(struct wilc * wilc,u8 * buff,u32 size,u32 pkt_offset)952 void wilc_frmw_to_linux(struct wilc *wilc, u8 *buff, u32 size, u32 pkt_offset)
953 {
954 unsigned int frame_len = 0;
955 int stats;
956 unsigned char *buff_to_send = NULL;
957 struct sk_buff *skb;
958 struct net_device *wilc_netdev;
959 struct wilc_vif *vif;
960
961 if (!wilc)
962 return;
963
964 wilc_netdev = get_if_handler(wilc, buff);
965 if (!wilc_netdev)
966 return;
967
968 buff += pkt_offset;
969 vif = netdev_priv(wilc_netdev);
970
971 if (size > 0) {
972 frame_len = size;
973 buff_to_send = buff;
974
975 skb = dev_alloc_skb(frame_len);
976 if (!skb)
977 return;
978
979 skb->dev = wilc_netdev;
980
981 skb_put_data(skb, buff_to_send, frame_len);
982
983 skb->protocol = eth_type_trans(skb, wilc_netdev);
984 vif->netstats.rx_packets++;
985 vif->netstats.rx_bytes += frame_len;
986 skb->ip_summed = CHECKSUM_UNNECESSARY;
987 stats = netif_rx(skb);
988 netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
989 }
990 }
991
wilc_wfi_mgmt_rx(struct wilc * wilc,u8 * buff,u32 size)992 void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
993 {
994 int i = 0;
995 struct wilc_vif *vif;
996
997 for (i = 0; i < wilc->vif_num; i++) {
998 vif = netdev_priv(wilc->vif[i]->ndev);
999 if (vif->monitor_flag) {
1000 wilc_wfi_monitor_rx(buff, size);
1001 return;
1002 }
1003 }
1004
1005 vif = netdev_priv(wilc->vif[1]->ndev);
1006 if ((buff[0] == vif->frame_reg[0].type && vif->frame_reg[0].reg) ||
1007 (buff[0] == vif->frame_reg[1].type && vif->frame_reg[1].reg))
1008 wilc_wfi_p2p_rx(wilc->vif[1]->ndev, buff, size);
1009 }
1010
1011 static struct notifier_block g_dev_notifier = {
1012 .notifier_call = dev_state_ev_handler
1013 };
1014
wilc_netdev_cleanup(struct wilc * wilc)1015 void wilc_netdev_cleanup(struct wilc *wilc)
1016 {
1017 int i;
1018
1019 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev))
1020 unregister_inetaddr_notifier(&g_dev_notifier);
1021
1022 if (wilc && wilc->firmware) {
1023 release_firmware(wilc->firmware);
1024 wilc->firmware = NULL;
1025 }
1026
1027 if (wilc && (wilc->vif[0]->ndev || wilc->vif[1]->ndev)) {
1028 for (i = 0; i < NUM_CONCURRENT_IFC; i++)
1029 if (wilc->vif[i]->ndev)
1030 if (wilc->vif[i]->mac_opened)
1031 wilc_mac_close(wilc->vif[i]->ndev);
1032
1033 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1034 unregister_netdev(wilc->vif[i]->ndev);
1035 wilc_free_wiphy(wilc->vif[i]->ndev);
1036 free_netdev(wilc->vif[i]->ndev);
1037 }
1038 }
1039
1040 kfree(wilc);
1041 }
1042 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
1043
1044 static const struct net_device_ops wilc_netdev_ops = {
1045 .ndo_init = mac_init_fn,
1046 .ndo_open = wilc_mac_open,
1047 .ndo_stop = wilc_mac_close,
1048 .ndo_start_xmit = wilc_mac_xmit,
1049 .ndo_get_stats = mac_stats,
1050 .ndo_set_rx_mode = wilc_set_multicast_list,
1051 };
1052
wilc_netdev_init(struct wilc ** wilc,struct device * dev,int io_type,const struct wilc_hif_func * ops)1053 int wilc_netdev_init(struct wilc **wilc, struct device *dev, int io_type,
1054 const struct wilc_hif_func *ops)
1055 {
1056 int i, ret;
1057 struct wilc_vif *vif;
1058 struct net_device *ndev;
1059 struct wilc *wl;
1060
1061 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
1062 if (!wl)
1063 return -ENOMEM;
1064
1065 *wilc = wl;
1066 wl->io_type = io_type;
1067 wl->hif_func = ops;
1068 INIT_LIST_HEAD(&wl->txq_head.list);
1069 INIT_LIST_HEAD(&wl->rxq_head.list);
1070
1071 register_inetaddr_notifier(&g_dev_notifier);
1072
1073 for (i = 0; i < NUM_CONCURRENT_IFC; i++) {
1074 struct wireless_dev *wdev;
1075
1076 ndev = alloc_etherdev(sizeof(struct wilc_vif));
1077 if (!ndev)
1078 return -ENOMEM;
1079
1080 vif = netdev_priv(ndev);
1081 memset(vif, 0, sizeof(struct wilc_vif));
1082
1083 if (i == 0) {
1084 strcpy(ndev->name, "wlan%d");
1085 vif->ifc_id = 1;
1086 } else {
1087 strcpy(ndev->name, "p2p%d");
1088 vif->ifc_id = 0;
1089 }
1090 vif->wilc = *wilc;
1091 vif->ndev = ndev;
1092 wl->vif[i] = vif;
1093 wl->vif_num = i;
1094 vif->idx = wl->vif_num;
1095
1096 ndev->netdev_ops = &wilc_netdev_ops;
1097
1098 wdev = wilc_create_wiphy(ndev, dev);
1099
1100 if (dev)
1101 SET_NETDEV_DEV(ndev, dev);
1102
1103 if (!wdev) {
1104 netdev_err(ndev, "Can't register WILC Wiphy\n");
1105 return -1;
1106 }
1107
1108 vif->ndev->ieee80211_ptr = wdev;
1109 vif->ndev->ml_priv = vif;
1110 wdev->netdev = vif->ndev;
1111 vif->netstats.rx_packets = 0;
1112 vif->netstats.tx_packets = 0;
1113 vif->netstats.rx_bytes = 0;
1114 vif->netstats.tx_bytes = 0;
1115
1116 ret = register_netdev(ndev);
1117 if (ret)
1118 return ret;
1119
1120 vif->iftype = STATION_MODE;
1121 vif->mac_opened = 0;
1122 }
1123
1124 return 0;
1125 }
1126 EXPORT_SYMBOL_GPL(wilc_netdev_init);
1127
1128 MODULE_LICENSE("GPL");
1129