1 #include "wilc_wfi_cfgoperations.h"
2 #include "linux_wlan_common.h"
3 #include "wilc_wlan_if.h"
6 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/delay.h>
9 #include <linux/workqueue.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/gpio.h>
14 #include <linux/kthread.h>
15 #include <linux/firmware.h>
16 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/netdevice.h>
20 #include <linux/inetdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/skbuff.h>
26 #include <linux/semaphore.h>
29 #include "linux_wlan_sdio.h"
31 #include "linux_wlan_spi.h"
34 static int dev_state_ev_handler(struct notifier_block
*this, unsigned long event
, void *ptr
);
36 static struct notifier_block g_dev_notifier
= {
37 .notifier_call
= dev_state_ev_handler
42 static struct semaphore close_exit_sync
;
44 static int wlan_deinit_locks(struct net_device
*dev
);
45 static void wlan_deinitialize_threads(struct net_device
*dev
);
47 static void linux_wlan_tx_complete(void *priv
, int status
);
48 static int mac_init_fn(struct net_device
*ndev
);
49 static struct net_device_stats
*mac_stats(struct net_device
*dev
);
50 static int mac_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
);
51 static void wilc_set_multicast_list(struct net_device
*dev
);
52 struct wilc
*wilc_dev
;
53 bool wilc_enable_ps
= true;
55 static const struct net_device_ops wilc_netdev_ops
= {
56 .ndo_init
= mac_init_fn
,
57 .ndo_open
= wilc_mac_open
,
58 .ndo_stop
= wilc_mac_close
,
59 .ndo_start_xmit
= wilc_mac_xmit
,
60 .ndo_do_ioctl
= mac_ioctl
,
61 .ndo_get_stats
= mac_stats
,
62 .ndo_set_rx_mode
= wilc_set_multicast_list
,
66 static int dev_state_ev_handler(struct notifier_block
*this, unsigned long event
, void *ptr
)
68 struct in_ifaddr
*dev_iface
= (struct in_ifaddr
*)ptr
;
69 struct wilc_priv
*priv
;
70 struct host_if_drv
*hif_drv
;
71 struct net_device
*dev
;
73 perInterface_wlan_t
*nic
;
75 char wlan_dev_name
[5] = "wlan0";
77 if (!dev_iface
|| !dev_iface
->ifa_dev
|| !dev_iface
->ifa_dev
->dev
) {
78 PRINT_D(GENERIC_DBG
, "dev_iface = NULL\n");
82 if (memcmp(dev_iface
->ifa_label
, "wlan0", 5) &&
83 memcmp(dev_iface
->ifa_label
, "p2p0", 4)) {
84 PRINT_D(GENERIC_DBG
, "Interface is neither WLAN0 nor P2P0\n");
88 dev
= (struct net_device
*)dev_iface
->ifa_dev
->dev
;
89 if (!dev
->ieee80211_ptr
|| !dev
->ieee80211_ptr
->wiphy
) {
90 PRINT_D(GENERIC_DBG
, "No Wireless registerd\n");
93 priv
= wiphy_priv(dev
->ieee80211_ptr
->wiphy
);
95 PRINT_D(GENERIC_DBG
, "No Wireless Priv\n");
98 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
99 nic
= netdev_priv(dev
);
100 if (!nic
|| !hif_drv
) {
101 PRINT_D(GENERIC_DBG
, "No Wireless Priv\n");
105 PRINT_INFO(GENERIC_DBG
, "dev_state_ev_handler +++\n");
109 PRINT_D(GENERIC_DBG
, "dev_state_ev_handler event=NETDEV_UP %p\n", dev
);
111 PRINT_INFO(GENERIC_DBG
, "\n ============== IP Address Obtained ===============\n\n");
113 if (nic
->iftype
== STATION_MODE
|| nic
->iftype
== CLIENT_MODE
) {
115 wilc_optaining_ip
= false;
116 del_timer(&wilc_during_ip_timer
);
117 PRINT_D(GENERIC_DBG
, "IP obtained , enable scan\n");
121 wilc_set_power_mgmt(hif_drv
, 1, 0);
123 PRINT_D(GENERIC_DBG
, "[%s] Up IP\n", dev_iface
->ifa_label
);
125 ip_addr_buf
= (char *)&dev_iface
->ifa_address
;
126 PRINT_D(GENERIC_DBG
, "IP add=%d:%d:%d:%d\n",
127 ip_addr_buf
[0], ip_addr_buf
[1],
128 ip_addr_buf
[2], ip_addr_buf
[3]);
129 wilc_setup_ipaddress(hif_drv
, ip_addr_buf
, nic
->u8IfIdx
);
134 PRINT_D(GENERIC_DBG
, "dev_state_ev_handler event=NETDEV_DOWN %p\n", dev
);
136 PRINT_INFO(GENERIC_DBG
, "\n ============== IP Address Released ===============\n\n");
137 if (nic
->iftype
== STATION_MODE
|| nic
->iftype
== CLIENT_MODE
) {
139 wilc_optaining_ip
= false;
142 if (memcmp(dev_iface
->ifa_label
, wlan_dev_name
, 5) == 0)
143 wilc_set_power_mgmt(hif_drv
, 0, 0);
145 wilc_resolve_disconnect_aberration(hif_drv
);
147 PRINT_D(GENERIC_DBG
, "[%s] Down IP\n", dev_iface
->ifa_label
);
149 ip_addr_buf
= null_ip
;
150 PRINT_D(GENERIC_DBG
, "IP add=%d:%d:%d:%d\n",
151 ip_addr_buf
[0], ip_addr_buf
[1],
152 ip_addr_buf
[2], ip_addr_buf
[3]);
154 wilc_setup_ipaddress(hif_drv
, ip_addr_buf
, nic
->u8IfIdx
);
159 PRINT_INFO(GENERIC_DBG
, "dev_state_ev_handler event=default\n");
160 PRINT_INFO(GENERIC_DBG
, "[%s] unknown dev event: %lu\n", dev_iface
->ifa_label
, event
);
168 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
169 static irqreturn_t
isr_uh_routine(int irq
, void *user_data
)
171 perInterface_wlan_t
*nic
;
173 struct net_device
*dev
= (struct net_device
*)user_data
;
175 nic
= netdev_priv(dev
);
177 PRINT_D(INT_DBG
, "Interrupt received UH\n");
180 PRINT_ER("Driver is CLOSING: Can't handle UH interrupt\n");
183 return IRQ_WAKE_THREAD
;
186 static irqreturn_t
isr_bh_routine(int irq
, void *userdata
)
188 perInterface_wlan_t
*nic
;
191 nic
= netdev_priv(userdata
);
195 PRINT_ER("Driver is CLOSING: Can't handle BH interrupt\n");
199 PRINT_D(INT_DBG
, "Interrupt received BH\n");
200 wilc_handle_isr(wilc
);
205 static int init_irq(struct net_device
*dev
)
208 perInterface_wlan_t
*nic
;
211 nic
= netdev_priv(dev
);
214 if ((gpio_request(GPIO_NUM
, "WILC_INTR") == 0) &&
215 (gpio_direction_input(GPIO_NUM
) == 0)) {
216 wl
->dev_irq_num
= gpio_to_irq(GPIO_NUM
);
219 PRINT_ER("could not obtain gpio for WILC_INTR\n");
222 if (ret
!= -1 && request_threaded_irq(wl
->dev_irq_num
,
225 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
226 "WILC_IRQ", dev
) < 0) {
227 PRINT_ER("Failed to request IRQ for GPIO: %d\n", GPIO_NUM
);
230 PRINT_D(INIT_DBG
, "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
231 wl
->dev_irq_num
, GPIO_NUM
);
238 static void deinit_irq(struct net_device
*dev
)
240 perInterface_wlan_t
*nic
;
243 nic
= netdev_priv(dev
);
246 #if (defined WILC_SPI) || (defined WILC_SDIO_IRQ_GPIO)
247 if (&wilc
->dev_irq_num
!= 0) {
248 free_irq(wilc
->dev_irq_num
, wilc
);
255 void linux_wlan_dbg(u8
*buff
)
257 PRINT_D(INIT_DBG
, "%d\n", *buff
);
260 int linux_wlan_lock_timeout(void *vp
, u32 timeout
)
264 PRINT_D(LOCK_DBG
, "Locking %p\n", vp
);
266 error
= down_timeout((struct semaphore
*)vp
,
267 msecs_to_jiffies(timeout
));
269 PRINT_ER("Failed, mutex is NULL\n");
273 void linux_wlan_mac_indicate(struct wilc
*wilc
, int flag
)
277 if (flag
== WILC_MAC_INDICATE_STATUS
) {
278 wilc_wlan_cfg_get_val(WID_STATUS
,
279 (unsigned char *)&status
, 4);
280 if (wilc
->mac_status
== WILC_MAC_STATUS_INIT
) {
281 wilc
->mac_status
= status
;
282 up(&wilc
->sync_event
);
284 wilc
->mac_status
= status
;
286 } else if (flag
== WILC_MAC_INDICATE_SCAN
) {
287 PRINT_D(GENERIC_DBG
, "Scanning ...\n");
291 static struct net_device
*get_if_handler(struct wilc
*wilc
, u8
*mac_header
)
296 bssid
= mac_header
+ 10;
297 bssid1
= mac_header
+ 4;
299 for (i
= 0; i
< wilc
->vif_num
; i
++)
300 if (!memcmp(bssid1
, wilc
->vif
[i
].bssid
, ETH_ALEN
) ||
301 !memcmp(bssid
, wilc
->vif
[i
].bssid
, ETH_ALEN
))
302 return wilc
->vif
[i
].ndev
;
304 PRINT_INFO(INIT_DBG
, "Invalide handle\n");
305 for (i
= 0; i
< 25; i
++)
306 PRINT_D(INIT_DBG
, "%02x ", mac_header
[i
]);
307 bssid
= mac_header
+ 18;
308 bssid1
= mac_header
+ 12;
309 for (i
= 0; i
< wilc
->vif_num
; i
++)
310 if (!memcmp(bssid1
, wilc
->vif
[i
].bssid
, ETH_ALEN
) ||
311 !memcmp(bssid
, wilc
->vif
[i
].bssid
, ETH_ALEN
))
312 return wilc
->vif
[i
].ndev
;
314 PRINT_INFO(INIT_DBG
, "\n");
318 int wilc_wlan_set_bssid(struct net_device
*wilc_netdev
, u8
*bssid
)
322 perInterface_wlan_t
*nic
;
325 nic
= netdev_priv(wilc_netdev
);
328 for (i
= 0; i
< wilc
->vif_num
; i
++)
329 if (wilc
->vif
[i
].ndev
== wilc_netdev
) {
330 memcpy(wilc
->vif
[i
].bssid
, bssid
, 6);
338 int wilc_wlan_get_num_conn_ifcs(void)
341 u8 null_bssid
[6] = {0};
344 for (i
= 0; i
< wilc_dev
->vif_num
; i
++)
345 if (memcmp(wilc_dev
->vif
[i
].bssid
, null_bssid
, 6))
351 #define USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
353 static int linux_wlan_txq_task(void *vp
)
356 perInterface_wlan_t
*nic
;
358 struct net_device
*dev
= vp
;
359 #if defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
360 #define TX_BACKOFF_WEIGHT_INCR_STEP (1)
361 #define TX_BACKOFF_WEIGHT_DECR_STEP (1)
362 #define TX_BACKOFF_WEIGHT_MAX (7)
363 #define TX_BACKOFF_WEIGHT_MIN (0)
364 #define TX_BACKOFF_WEIGHT_UNIT_MS (10)
365 int backoff_weight
= TX_BACKOFF_WEIGHT_MIN
;
368 nic
= netdev_priv(dev
);
371 up(&wl
->txq_thread_started
);
373 PRINT_D(TX_DBG
, "txq_task Taking a nap :)\n");
374 down(&wl
->txq_event
);
375 PRINT_D(TX_DBG
, "txq_task Who waked me up :$\n");
378 up(&wl
->txq_thread_started
);
380 while (!kthread_should_stop())
383 PRINT_D(TX_DBG
, "TX thread stopped\n");
386 PRINT_D(TX_DBG
, "txq_task handle the sending packet and let me go to sleep.\n");
387 #if !defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
388 ret
= wilc_wlan_handle_txq(dev
, &txq_count
);
391 ret
= wilc_wlan_handle_txq(dev
, &txq_count
);
392 if (txq_count
< FLOW_CONTROL_LOWER_THRESHOLD
) {
393 PRINT_D(TX_DBG
, "Waking up queue\n");
395 if (netif_queue_stopped(wl
->vif
[0].ndev
))
396 netif_wake_queue(wl
->vif
[0].ndev
);
397 if (netif_queue_stopped(wl
->vif
[1].ndev
))
398 netif_wake_queue(wl
->vif
[1].ndev
);
401 if (ret
== WILC_TX_ERR_NO_BUF
) {
403 msleep(TX_BACKOFF_WEIGHT_UNIT_MS
<< backoff_weight
);
405 backoff_weight
+= TX_BACKOFF_WEIGHT_INCR_STEP
;
406 if (backoff_weight
> TX_BACKOFF_WEIGHT_MAX
)
407 backoff_weight
= TX_BACKOFF_WEIGHT_MAX
;
409 if (backoff_weight
> TX_BACKOFF_WEIGHT_MIN
) {
410 backoff_weight
-= TX_BACKOFF_WEIGHT_DECR_STEP
;
411 if (backoff_weight
< TX_BACKOFF_WEIGHT_MIN
)
412 backoff_weight
= TX_BACKOFF_WEIGHT_MIN
;
415 } while (ret
== WILC_TX_ERR_NO_BUF
&& !wl
->close
);
421 void linux_wlan_rx_complete(void)
423 PRINT_D(RX_DBG
, "RX completed\n");
426 int wilc_wlan_get_firmware(struct net_device
*dev
)
428 perInterface_wlan_t
*nic
;
431 const struct firmware
*wilc_firmware
;
434 nic
= netdev_priv(dev
);
437 if (nic
->iftype
== AP_MODE
) {
438 firmware
= AP_FIRMWARE
;
439 } else if (nic
->iftype
== STATION_MODE
) {
440 firmware
= STA_FIRMWARE
;
442 PRINT_D(INIT_DBG
, "Get P2P_CONCURRENCY_FIRMWARE\n");
443 firmware
= P2P_CONCURRENCY_FIRMWARE
;
447 PRINT_ER("NIC is NULL\n");
451 if (!(&nic
->wilc_netdev
->dev
)) {
452 PRINT_ER("&nic->wilc_netdev->dev is NULL\n");
457 if (request_firmware(&wilc_firmware
, firmware
, &wilc
->wilc_sdio_func
->dev
) != 0) {
458 PRINT_ER("%s - firmare not available\n", firmware
);
463 if (request_firmware(&wilc_firmware
, firmware
, &wilc
->wilc_spidev
->dev
) != 0) {
464 PRINT_ER("%s - firmare not available\n", firmware
);
469 wilc
->firmware
= wilc_firmware
;
476 static int linux_wlan_start_firmware(struct net_device
*dev
)
478 perInterface_wlan_t
*nic
;
482 nic
= netdev_priv(dev
);
485 PRINT_D(INIT_DBG
, "Starting Firmware ...\n");
486 ret
= wilc_wlan_start();
488 PRINT_ER("Failed to start Firmware\n");
492 PRINT_D(INIT_DBG
, "Waiting for Firmware to get ready ...\n");
493 ret
= linux_wlan_lock_timeout(&wilc
->sync_event
, 5000);
495 PRINT_D(INIT_DBG
, "Firmware start timed out");
498 PRINT_D(INIT_DBG
, "Firmware successfully started\n");
503 static int linux_wlan_firmware_download(struct net_device
*dev
)
505 perInterface_wlan_t
*nic
;
509 nic
= netdev_priv(dev
);
512 if (!wilc
->firmware
) {
513 PRINT_ER("Firmware buffer is NULL\n");
516 PRINT_D(INIT_DBG
, "Downloading Firmware ...\n");
517 ret
= wilc_wlan_firmware_download(wilc
->firmware
->data
,
518 wilc
->firmware
->size
);
522 PRINT_D(INIT_DBG
, "Freeing FW buffer ...\n");
523 PRINT_D(INIT_DBG
, "Releasing firmware\n");
524 release_firmware(wilc
->firmware
);
526 PRINT_D(INIT_DBG
, "Download Succeeded\n");
531 static int linux_wlan_init_test_config(struct net_device
*dev
, struct wilc
*p_nic
)
533 unsigned char c_val
[64];
534 unsigned char mac_add
[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xff};
536 struct wilc_priv
*priv
;
537 struct host_if_drv
*hif_drv
;
539 PRINT_D(TX_DBG
, "Start configuring Firmware\n");
540 get_random_bytes(&mac_add
[5], 1);
541 get_random_bytes(&mac_add
[4], 1);
542 priv
= wiphy_priv(dev
->ieee80211_ptr
->wiphy
);
543 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
544 PRINT_D(INIT_DBG
, "Host = %p\n", hif_drv
);
546 PRINT_D(INIT_DBG
, "MAC address is : %02x-%02x-%02x-%02x-%02x-%02x\n",
547 mac_add
[0], mac_add
[1], mac_add
[2],
548 mac_add
[3], mac_add
[4], mac_add
[5]);
553 if (!wilc_wlan_cfg_set(1, WID_SET_DRV_HANDLER
, c_val
, 4, 0, 0))
557 if (!wilc_wlan_cfg_set(0, WID_PC_TEST_MODE
, c_val
, 1, 0, 0))
560 c_val
[0] = INFRASTRUCTURE
;
561 if (!wilc_wlan_cfg_set(0, WID_BSS_TYPE
, c_val
, 1, 0, 0))
564 c_val
[0] = RATE_AUTO
;
565 if (!wilc_wlan_cfg_set(0, WID_CURRENT_TX_RATE
, c_val
, 1, 0, 0))
568 c_val
[0] = G_MIXED_11B_2_MODE
;
569 if (!wilc_wlan_cfg_set(0, WID_11G_OPERATING_MODE
, c_val
, 1, 0, 0))
573 if (!wilc_wlan_cfg_set(0, WID_CURRENT_CHANNEL
, c_val
, 1, 0, 0))
576 c_val
[0] = G_SHORT_PREAMBLE
;
577 if (!wilc_wlan_cfg_set(0, WID_PREAMBLE
, c_val
, 1, 0, 0))
580 c_val
[0] = AUTO_PROT
;
581 if (!wilc_wlan_cfg_set(0, WID_11N_PROT_MECH
, c_val
, 1, 0, 0))
584 c_val
[0] = ACTIVE_SCAN
;
585 if (!wilc_wlan_cfg_set(0, WID_SCAN_TYPE
, c_val
, 1, 0, 0))
588 c_val
[0] = SITE_SURVEY_OFF
;
589 if (!wilc_wlan_cfg_set(0, WID_SITE_SURVEY
, c_val
, 1, 0, 0))
592 *((int *)c_val
) = 0xffff;
593 if (!wilc_wlan_cfg_set(0, WID_RTS_THRESHOLD
, c_val
, 2, 0, 0))
596 *((int *)c_val
) = 2346;
597 if (!wilc_wlan_cfg_set(0, WID_FRAG_THRESHOLD
, c_val
, 2, 0, 0))
601 if (!wilc_wlan_cfg_set(0, WID_BCAST_SSID
, c_val
, 1, 0, 0))
605 if (!wilc_wlan_cfg_set(0, WID_QOS_ENABLE
, c_val
, 1, 0, 0))
608 c_val
[0] = NO_POWERSAVE
;
609 if (!wilc_wlan_cfg_set(0, WID_POWER_MANAGEMENT
, c_val
, 1, 0, 0))
612 c_val
[0] = NO_SECURITY
; /* NO_ENCRYPT, 0x79 */
613 if (!wilc_wlan_cfg_set(0, WID_11I_MODE
, c_val
, 1, 0, 0))
616 c_val
[0] = OPEN_SYSTEM
;
617 if (!wilc_wlan_cfg_set(0, WID_AUTH_TYPE
, c_val
, 1, 0, 0))
620 strcpy(c_val
, "123456790abcdef1234567890");
621 if (!wilc_wlan_cfg_set(0, WID_WEP_KEY_VALUE
, c_val
, (strlen(c_val
) + 1), 0, 0))
624 strcpy(c_val
, "12345678");
625 if (!wilc_wlan_cfg_set(0, WID_11I_PSK
, c_val
, (strlen(c_val
)), 0, 0))
628 strcpy(c_val
, "password");
629 if (!wilc_wlan_cfg_set(0, WID_1X_KEY
, c_val
, (strlen(c_val
) + 1), 0, 0))
636 if (!wilc_wlan_cfg_set(0, WID_1X_SERV_ADDR
, c_val
, 4, 0, 0))
640 if (!wilc_wlan_cfg_set(0, WID_LISTEN_INTERVAL
, c_val
, 1, 0, 0))
644 if (!wilc_wlan_cfg_set(0, WID_DTIM_PERIOD
, c_val
, 1, 0, 0))
647 c_val
[0] = NORMAL_ACK
;
648 if (!wilc_wlan_cfg_set(0, WID_ACK_POLICY
, c_val
, 1, 0, 0))
652 if (!wilc_wlan_cfg_set(0, WID_USER_CONTROL_ON_TX_POWER
, c_val
, 1, 0, 0))
656 if (!wilc_wlan_cfg_set(0, WID_TX_POWER_LEVEL_11A
, c_val
, 1, 0, 0))
660 if (!wilc_wlan_cfg_set(0, WID_TX_POWER_LEVEL_11B
, c_val
, 1, 0, 0))
663 *((int *)c_val
) = 100;
664 if (!wilc_wlan_cfg_set(0, WID_BEACON_INTERVAL
, c_val
, 2, 0, 0))
667 c_val
[0] = REKEY_DISABLE
;
668 if (!wilc_wlan_cfg_set(0, WID_REKEY_POLICY
, c_val
, 1, 0, 0))
671 *((int *)c_val
) = 84600;
672 if (!wilc_wlan_cfg_set(0, WID_REKEY_PERIOD
, c_val
, 4, 0, 0))
675 *((int *)c_val
) = 500;
676 if (!wilc_wlan_cfg_set(0, WID_REKEY_PACKET_COUNT
, c_val
, 4, 0, 0))
680 if (!wilc_wlan_cfg_set(0, WID_SHORT_SLOT_ALLOWED
, c_val
, 1, 0, 0))
683 c_val
[0] = G_SELF_CTS_PROT
;
684 if (!wilc_wlan_cfg_set(0, WID_11N_ERP_PROT_TYPE
, c_val
, 1, 0, 0))
688 if (!wilc_wlan_cfg_set(0, WID_11N_ENABLE
, c_val
, 1, 0, 0))
691 c_val
[0] = HT_MIXED_MODE
;
692 if (!wilc_wlan_cfg_set(0, WID_11N_OPERATING_MODE
, c_val
, 1, 0, 0))
696 if (!wilc_wlan_cfg_set(0, WID_11N_TXOP_PROT_DISABLE
, c_val
, 1, 0, 0))
699 memcpy(c_val
, mac_add
, 6);
701 if (!wilc_wlan_cfg_set(0, WID_MAC_ADDR
, c_val
, 6, 0, 0))
704 c_val
[0] = DETECT_PROTECT_REPORT
;
705 if (!wilc_wlan_cfg_set(0, WID_11N_OBSS_NONHT_DETECTION
, c_val
, 1, 0, 0))
708 c_val
[0] = RTS_CTS_NONHT_PROT
;
709 if (!wilc_wlan_cfg_set(0, WID_11N_HT_PROT_TYPE
, c_val
, 1, 0, 0))
713 if (!wilc_wlan_cfg_set(0, WID_11N_RIFS_PROT_ENABLE
, c_val
, 1, 0, 0))
716 c_val
[0] = MIMO_MODE
;
717 if (!wilc_wlan_cfg_set(0, WID_11N_SMPS_MODE
, c_val
, 1, 0, 0))
721 if (!wilc_wlan_cfg_set(0, WID_11N_CURRENT_TX_MCS
, c_val
, 1, 0, 0))
725 if (!wilc_wlan_cfg_set(0, WID_11N_IMMEDIATE_BA_ENABLED
, c_val
, 1, 1, 1))
734 void wilc1000_wlan_deinit(struct net_device
*dev
)
736 perInterface_wlan_t
*nic
;
739 nic
= netdev_priv(dev
);
743 netdev_err(dev
, "wl is NULL\n");
747 if (wl
->initialized
) {
748 netdev_info(dev
, "Deinitializing wilc1000...\n");
750 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
751 PRINT_D(INIT_DBG
, "skip wilc_bus_set_default_speed\n");
753 wilc_bus_set_default_speed();
756 PRINT_D(INIT_DBG
, "Disabling IRQ\n");
758 mutex_lock(&wl
->hif_cs
);
759 wilc_sdio_disable_interrupt();
760 mutex_unlock(&wl
->hif_cs
);
765 PRINT_D(INIT_DBG
, "Deinitializing Threads\n");
766 wlan_deinitialize_threads(dev
);
768 PRINT_D(INIT_DBG
, "Deinitializing IRQ\n");
773 PRINT_D(INIT_DBG
, "Deinitializing WILC Wlan\n");
774 wilc_wlan_cleanup(dev
);
775 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
776 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
777 PRINT_D(INIT_DBG
, "Disabling IRQ 2\n");
779 mutex_lock(&wl
->hif_cs
);
780 wilc_sdio_disable_interrupt();
781 mutex_unlock(&wl
->hif_cs
);
785 PRINT_D(INIT_DBG
, "Deinitializing Locks\n");
786 wlan_deinit_locks(dev
);
788 wl
->initialized
= false;
790 PRINT_D(INIT_DBG
, "wilc1000 deinitialization Done\n");
793 PRINT_D(INIT_DBG
, "wilc1000 is not initialized\n");
797 static int wlan_init_locks(struct net_device
*dev
)
799 perInterface_wlan_t
*nic
;
802 nic
= netdev_priv(dev
);
805 PRINT_D(INIT_DBG
, "Initializing Locks ...\n");
807 mutex_init(&wl
->hif_cs
);
808 mutex_init(&wl
->rxq_cs
);
810 spin_lock_init(&wl
->txq_spinlock
);
811 sema_init(&wl
->txq_add_to_head_cs
, 1);
813 sema_init(&wl
->txq_event
, 0);
815 sema_init(&wl
->cfg_event
, 0);
816 sema_init(&wl
->sync_event
, 0);
818 sema_init(&wl
->txq_thread_started
, 0);
823 static int wlan_deinit_locks(struct net_device
*dev
)
825 perInterface_wlan_t
*nic
;
828 nic
= netdev_priv(dev
);
831 PRINT_D(INIT_DBG
, "De-Initializing Locks\n");
834 mutex_destroy(&wilc
->hif_cs
);
837 mutex_destroy(&wilc
->rxq_cs
);
842 static int wlan_initialize_threads(struct net_device
*dev
)
844 perInterface_wlan_t
*nic
;
847 nic
= netdev_priv(dev
);
850 PRINT_D(INIT_DBG
, "Initializing Threads ...\n");
851 PRINT_D(INIT_DBG
, "Creating kthread for transmission\n");
852 wilc
->txq_thread
= kthread_run(linux_wlan_txq_task
, (void *)dev
,
854 if (!wilc
->txq_thread
) {
855 PRINT_ER("couldn't create TXQ thread\n");
859 down(&wilc
->txq_thread_started
);
864 static void wlan_deinitialize_threads(struct net_device
*dev
)
866 perInterface_wlan_t
*nic
;
868 nic
= netdev_priv(dev
);
872 PRINT_D(INIT_DBG
, "Deinitializing Threads\n");
877 if (wl
->txq_thread
) {
878 kthread_stop(wl
->txq_thread
);
879 wl
->txq_thread
= NULL
;
883 int wilc1000_wlan_init(struct net_device
*dev
, perInterface_wlan_t
*p_nic
)
885 perInterface_wlan_t
*nic
= p_nic
;
887 struct wilc
*wl
= nic
->wilc
;
889 if (!wl
->initialized
) {
890 wl
->mac_status
= WILC_MAC_STATUS_INIT
;
893 wlan_init_locks(dev
);
896 wl
->io_type
= HIF_SDIO
;
898 wl
->io_type
= HIF_SPI
;
900 ret
= wilc_wlan_init(dev
);
902 PRINT_ER("Initializing WILC_Wlan FAILED\n");
907 #if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
909 PRINT_ER("couldn't initialize IRQ\n");
915 ret
= wlan_initialize_threads(dev
);
917 PRINT_ER("Initializing Threads FAILED\n");
919 goto _fail_wilc_wlan_
;
922 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
923 if (wilc_sdio_enable_interrupt()) {
924 PRINT_ER("couldn't initialize IRQ\n");
926 goto _fail_irq_init_
;
930 if (wilc_wlan_get_firmware(dev
)) {
931 PRINT_ER("Can't get firmware\n");
933 goto _fail_irq_enable_
;
936 ret
= linux_wlan_firmware_download(dev
);
938 PRINT_ER("Failed to download firmware\n");
940 goto _fail_irq_enable_
;
943 ret
= linux_wlan_start_firmware(dev
);
945 PRINT_ER("Failed to start firmware\n");
947 goto _fail_irq_enable_
;
950 wilc_bus_set_max_speed();
952 if (wilc_wlan_cfg_get(1, WID_FIRMWARE_VERSION
, 1, 0)) {
954 char Firmware_ver
[20];
956 size
= wilc_wlan_cfg_get_val(
957 WID_FIRMWARE_VERSION
,
958 Firmware_ver
, sizeof(Firmware_ver
));
959 Firmware_ver
[size
] = '\0';
960 PRINT_D(INIT_DBG
, "***** Firmware Ver = %s *******\n", Firmware_ver
);
962 ret
= linux_wlan_init_test_config(dev
, wl
);
965 PRINT_ER("Failed to configure firmware\n");
967 goto _fail_fw_start_
;
970 wl
->initialized
= true;
977 #if (defined WILC_SDIO) && (!defined WILC_SDIO_IRQ_GPIO)
978 wilc_sdio_disable_interrupt();
981 #if (!defined WILC_SDIO) || (defined WILC_SDIO_IRQ_GPIO)
985 wlan_deinitialize_threads(dev
);
987 wilc_wlan_cleanup(dev
);
989 wlan_deinit_locks(dev
);
990 PRINT_ER("WLAN Iinitialization FAILED\n");
992 PRINT_D(INIT_DBG
, "wilc1000 already initialized\n");
997 static int mac_init_fn(struct net_device
*ndev
)
999 netif_start_queue(ndev
);
1000 netif_stop_queue(ndev
);
1005 int wilc_mac_open(struct net_device
*ndev
)
1007 perInterface_wlan_t
*nic
;
1009 unsigned char mac_add
[ETH_ALEN
] = {0};
1012 struct wilc_priv
*priv
;
1015 nic
= netdev_priv(ndev
);
1019 if (!wl
|| !wl
->wilc_spidev
) {
1020 netdev_err(ndev
, "wilc1000: SPI device not ready\n");
1024 nic
= netdev_priv(ndev
);
1025 priv
= wiphy_priv(nic
->wilc_netdev
->ieee80211_ptr
->wiphy
);
1026 PRINT_D(INIT_DBG
, "MAC OPEN[%p]\n", ndev
);
1028 ret
= wilc_init_host_int(ndev
);
1030 PRINT_ER("Failed to initialize host interface\n");
1035 PRINT_D(INIT_DBG
, "*** re-init ***\n");
1036 ret
= wilc1000_wlan_init(ndev
, nic
);
1038 PRINT_ER("Failed to initialize wilc1000\n");
1039 wilc_deinit_host_int(ndev
);
1043 wilc_set_machw_change_vir_if(ndev
, false);
1045 wilc_get_mac_address(priv
->hWILCWFIDrv
, mac_add
);
1046 PRINT_D(INIT_DBG
, "Mac address: %pM\n", mac_add
);
1048 for (i
= 0; i
< wl
->vif_num
; i
++) {
1049 if (ndev
== wl
->vif
[i
].ndev
) {
1050 memcpy(wl
->vif
[i
].src_addr
, mac_add
, ETH_ALEN
);
1051 wl
->vif
[i
].hif_drv
= priv
->hWILCWFIDrv
;
1056 memcpy(ndev
->dev_addr
, wl
->vif
[i
].src_addr
, ETH_ALEN
);
1058 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
1059 PRINT_ER("Error: Wrong MAC address\n");
1060 wilc_deinit_host_int(ndev
);
1061 wilc1000_wlan_deinit(ndev
);
1065 wilc_mgmt_frame_register(nic
->wilc_netdev
->ieee80211_ptr
->wiphy
,
1066 nic
->wilc_netdev
->ieee80211_ptr
,
1067 nic
->g_struct_frame_reg
[0].frame_type
,
1068 nic
->g_struct_frame_reg
[0].reg
);
1069 wilc_mgmt_frame_register(nic
->wilc_netdev
->ieee80211_ptr
->wiphy
,
1070 nic
->wilc_netdev
->ieee80211_ptr
,
1071 nic
->g_struct_frame_reg
[1].frame_type
,
1072 nic
->g_struct_frame_reg
[1].reg
);
1073 netif_wake_queue(ndev
);
1075 nic
->mac_opened
= 1;
1079 static struct net_device_stats
*mac_stats(struct net_device
*dev
)
1081 perInterface_wlan_t
*nic
= netdev_priv(dev
);
1083 return &nic
->netstats
;
1086 static void wilc_set_multicast_list(struct net_device
*dev
)
1088 struct netdev_hw_addr
*ha
;
1089 struct wilc_priv
*priv
;
1090 struct host_if_drv
*hif_drv
;
1093 priv
= wiphy_priv(dev
->ieee80211_ptr
->wiphy
);
1094 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
1099 PRINT_D(INIT_DBG
, "Setting Multicast List with count = %d.\n",
1102 if (dev
->flags
& IFF_PROMISC
) {
1103 PRINT_D(INIT_DBG
, "Set promiscuous mode ON, retrive all packets\n");
1107 if ((dev
->flags
& IFF_ALLMULTI
) ||
1108 (dev
->mc
.count
) > WILC_MULTICAST_TABLE_SIZE
) {
1109 PRINT_D(INIT_DBG
, "Disable multicast filter, retrive all multicast packets\n");
1110 wilc_setup_multicast_filter(hif_drv
, false, 0);
1114 if ((dev
->mc
.count
) == 0) {
1115 PRINT_D(INIT_DBG
, "Enable multicast filter, retrive directed packets only.\n");
1116 wilc_setup_multicast_filter(hif_drv
, true, 0);
1120 netdev_for_each_mc_addr(ha
, dev
) {
1121 memcpy(wilc_multicast_mac_addr_list
[i
], ha
->addr
, ETH_ALEN
);
1122 PRINT_D(INIT_DBG
, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i
,
1123 wilc_multicast_mac_addr_list
[i
][0],
1124 wilc_multicast_mac_addr_list
[i
][1],
1125 wilc_multicast_mac_addr_list
[i
][2],
1126 wilc_multicast_mac_addr_list
[i
][3],
1127 wilc_multicast_mac_addr_list
[i
][4],
1128 wilc_multicast_mac_addr_list
[i
][5]);
1132 wilc_setup_multicast_filter(hif_drv
, true, (dev
->mc
.count
));
1137 static void linux_wlan_tx_complete(void *priv
, int status
)
1139 struct tx_complete_data
*pv_data
= (struct tx_complete_data
*)priv
;
1142 PRINT_D(TX_DBG
, "Packet sent successfully - Size = %d - Address = %p - SKB = %p\n", pv_data
->size
, pv_data
->buff
, pv_data
->skb
);
1144 PRINT_D(TX_DBG
, "Couldn't send packet - Size = %d - Address = %p - SKB = %p\n", pv_data
->size
, pv_data
->buff
, pv_data
->skb
);
1145 dev_kfree_skb(pv_data
->skb
);
1149 int wilc_mac_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1151 perInterface_wlan_t
*nic
;
1152 struct tx_complete_data
*tx_data
= NULL
;
1156 struct ethhdr
*eth_h
;
1159 nic
= netdev_priv(ndev
);
1162 PRINT_D(TX_DBG
, "Sending packet just received from TCP/IP\n");
1164 if (skb
->dev
!= ndev
) {
1165 PRINT_ER("Packet not destined to this device\n");
1169 tx_data
= kmalloc(sizeof(*tx_data
), GFP_ATOMIC
);
1171 PRINT_ER("Failed to allocate memory for tx_data structure\n");
1173 netif_wake_queue(ndev
);
1177 tx_data
->buff
= skb
->data
;
1178 tx_data
->size
= skb
->len
;
1181 eth_h
= (struct ethhdr
*)(skb
->data
);
1182 if (eth_h
->h_proto
== 0x8e88)
1183 PRINT_D(INIT_DBG
, "EAPOL transmitted\n");
1185 ih
= (struct iphdr
*)(skb
->data
+ sizeof(struct ethhdr
));
1187 udp_buf
= (char *)ih
+ sizeof(struct iphdr
);
1188 if ((udp_buf
[1] == 68 && udp_buf
[3] == 67) ||
1189 (udp_buf
[1] == 67 && udp_buf
[3] == 68))
1190 PRINT_D(GENERIC_DBG
, "DHCP Message transmitted, type:%x %x %x\n",
1191 udp_buf
[248], udp_buf
[249], udp_buf
[250]);
1193 PRINT_D(TX_DBG
, "Sending packet - Size = %d - Address = %p - SKB = %p\n", tx_data
->size
, tx_data
->buff
, tx_data
->skb
);
1194 PRINT_D(TX_DBG
, "Adding tx packet to TX Queue\n");
1195 nic
->netstats
.tx_packets
++;
1196 nic
->netstats
.tx_bytes
+= tx_data
->size
;
1197 tx_data
->pBssid
= wilc
->vif
[nic
->u8IfIdx
].bssid
;
1198 queue_count
= wilc_wlan_txq_add_net_pkt(ndev
, (void *)tx_data
,
1199 tx_data
->buff
, tx_data
->size
,
1200 linux_wlan_tx_complete
);
1202 if (queue_count
> FLOW_CONTROL_UPPER_THRESHOLD
) {
1203 netif_stop_queue(wilc
->vif
[0].ndev
);
1204 netif_stop_queue(wilc
->vif
[1].ndev
);
1210 int wilc_mac_close(struct net_device
*ndev
)
1212 struct wilc_priv
*priv
;
1213 perInterface_wlan_t
*nic
;
1214 struct host_if_drv
*hif_drv
;
1217 nic
= netdev_priv(ndev
);
1219 if (!nic
|| !nic
->wilc_netdev
|| !nic
->wilc_netdev
->ieee80211_ptr
||
1220 !nic
->wilc_netdev
->ieee80211_ptr
->wiphy
) {
1221 PRINT_ER("nic = NULL\n");
1225 priv
= wiphy_priv(nic
->wilc_netdev
->ieee80211_ptr
->wiphy
);
1229 PRINT_ER("priv = NULL\n");
1233 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
1235 PRINT_D(GENERIC_DBG
, "Mac close\n");
1238 PRINT_ER("wl = NULL\n");
1243 PRINT_ER("hif_drv = NULL\n");
1247 if ((wl
->open_ifcs
) > 0) {
1250 PRINT_ER("ERROR: MAC close called while number of opened interfaces is zero\n");
1254 if (nic
->wilc_netdev
) {
1255 netif_stop_queue(nic
->wilc_netdev
);
1257 wilc_deinit_host_int(nic
->wilc_netdev
);
1260 if (wl
->open_ifcs
== 0) {
1261 PRINT_D(GENERIC_DBG
, "Deinitializing wilc1000\n");
1263 wilc1000_wlan_deinit(ndev
);
1264 WILC_WFI_deinit_mon_interface();
1267 up(&close_exit_sync
);
1268 nic
->mac_opened
= 0;
1273 static int mac_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
)
1277 u32 size
= 0, length
= 0;
1278 perInterface_wlan_t
*nic
;
1279 struct wilc_priv
*priv
;
1283 nic
= netdev_priv(ndev
);
1286 if (!wilc
->initialized
)
1292 struct iwreq
*wrq
= (struct iwreq
*) req
;
1294 size
= wrq
->u
.data
.length
;
1296 if (size
&& wrq
->u
.data
.pointer
) {
1297 buff
= memdup_user(wrq
->u
.data
.pointer
,
1298 wrq
->u
.data
.length
);
1300 return PTR_ERR(buff
);
1302 if (strncasecmp(buff
, "RSSI", length
) == 0) {
1303 priv
= wiphy_priv(nic
->wilc_netdev
->ieee80211_ptr
->wiphy
);
1304 ret
= wilc_get_rssi(priv
->hWILCWFIDrv
, &rssi
);
1306 PRINT_ER("Failed to send get rssi param's message queue ");
1307 PRINT_INFO(GENERIC_DBG
, "RSSI :%d\n", rssi
);
1311 snprintf(buff
, size
, "rssi %d", rssi
);
1313 if (copy_to_user(wrq
->u
.data
.pointer
, buff
, size
)) {
1314 PRINT_ER("%s: failed to copy data to user buffer\n", __func__
);
1325 PRINT_INFO(GENERIC_DBG
, "Command - %d - has been received\n", cmd
);
1338 void frmw_to_linux(struct wilc
*wilc
, u8
*buff
, u32 size
, u32 pkt_offset
)
1340 unsigned int frame_len
= 0;
1342 unsigned char *buff_to_send
= NULL
;
1343 struct sk_buff
*skb
;
1344 struct net_device
*wilc_netdev
;
1345 perInterface_wlan_t
*nic
;
1347 wilc_netdev
= get_if_handler(wilc
, buff
);
1352 nic
= netdev_priv(wilc_netdev
);
1356 buff_to_send
= buff
;
1358 skb
= dev_alloc_skb(frame_len
);
1360 PRINT_ER("Low memory - packet droped\n");
1364 if (!wilc
|| !wilc_netdev
)
1365 PRINT_ER("wilc_netdev in wilc is NULL");
1366 skb
->dev
= wilc_netdev
;
1369 PRINT_ER("skb->dev is NULL\n");
1371 memcpy(skb_put(skb
, frame_len
), buff_to_send
, frame_len
);
1373 skb
->protocol
= eth_type_trans(skb
, wilc_netdev
);
1374 nic
->netstats
.rx_packets
++;
1375 nic
->netstats
.rx_bytes
+= frame_len
;
1376 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1377 stats
= netif_rx(skb
);
1378 PRINT_D(RX_DBG
, "netif_rx ret value is: %d\n", stats
);
1382 void WILC_WFI_mgmt_rx(struct wilc
*wilc
, u8
*buff
, u32 size
)
1385 perInterface_wlan_t
*nic
;
1387 for (i
= 0; i
< wilc
->vif_num
; i
++) {
1388 nic
= netdev_priv(wilc
->vif
[i
].ndev
);
1389 if (nic
->monitor_flag
) {
1390 WILC_WFI_monitor_rx(buff
, size
);
1395 nic
= netdev_priv(wilc
->vif
[1].ndev
);
1396 if ((buff
[0] == nic
->g_struct_frame_reg
[0].frame_type
&& nic
->g_struct_frame_reg
[0].reg
) ||
1397 (buff
[0] == nic
->g_struct_frame_reg
[1].frame_type
&& nic
->g_struct_frame_reg
[1].reg
))
1398 WILC_WFI_p2p_rx(wilc
->vif
[1].ndev
, buff
, size
);
1401 void wilc_netdev_cleanup(struct wilc
*wilc
)
1404 perInterface_wlan_t
*nic
[NUM_CONCURRENT_IFC
];
1406 if (wilc
&& (wilc
->vif
[0].ndev
|| wilc
->vif
[1].ndev
)) {
1407 unregister_inetaddr_notifier(&g_dev_notifier
);
1409 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++)
1410 nic
[i
] = netdev_priv(wilc
->vif
[i
].ndev
);
1413 if (wilc
&& wilc
->firmware
)
1414 release_firmware(wilc
->firmware
);
1416 if (wilc
&& (wilc
->vif
[0].ndev
|| wilc
->vif
[1].ndev
)) {
1417 linux_wlan_lock_timeout(&close_exit_sync
, 12 * 1000);
1419 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++)
1420 if (wilc
->vif
[i
].ndev
)
1421 if (nic
[i
]->mac_opened
)
1422 wilc_mac_close(wilc
->vif
[i
].ndev
);
1424 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++) {
1425 unregister_netdev(wilc
->vif
[i
].ndev
);
1426 wilc_free_wiphy(wilc
->vif
[i
].ndev
);
1427 free_netdev(wilc
->vif
[i
].ndev
);
1433 #if defined(WILC_DEBUGFS)
1434 wilc_debugfs_remove();
1438 int wilc_netdev_init(struct wilc
**wilc
)
1441 perInterface_wlan_t
*nic
;
1442 struct net_device
*ndev
;
1444 sema_init(&close_exit_sync
, 0);
1446 wilc_dev
= kzalloc(sizeof(*wilc_dev
), GFP_KERNEL
);
1452 register_inetaddr_notifier(&g_dev_notifier
);
1454 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++) {
1455 ndev
= alloc_etherdev(sizeof(perInterface_wlan_t
));
1457 PRINT_ER("Failed to allocate ethernet dev\n");
1461 nic
= netdev_priv(ndev
);
1462 memset(nic
, 0, sizeof(perInterface_wlan_t
));
1465 strcpy(ndev
->name
, "wlan%d");
1467 strcpy(ndev
->name
, "p2p%d");
1469 nic
->u8IfIdx
= wilc_dev
->vif_num
;
1470 nic
->wilc_netdev
= ndev
;
1472 wilc_dev
->vif
[wilc_dev
->vif_num
].ndev
= ndev
;
1473 wilc_dev
->vif_num
++;
1474 ndev
->netdev_ops
= &wilc_netdev_ops
;
1477 struct wireless_dev
*wdev
;
1478 wdev
= wilc_create_wiphy(ndev
);
1481 SET_NETDEV_DEV(ndev
, &wilc_sdio_func
->dev
);
1485 PRINT_ER("Can't register WILC Wiphy\n");
1489 nic
->wilc_netdev
->ieee80211_ptr
= wdev
;
1490 nic
->wilc_netdev
->ml_priv
= nic
;
1491 wdev
->netdev
= nic
->wilc_netdev
;
1492 nic
->netstats
.rx_packets
= 0;
1493 nic
->netstats
.tx_packets
= 0;
1494 nic
->netstats
.rx_bytes
= 0;
1495 nic
->netstats
.tx_bytes
= 0;
1498 if (register_netdev(ndev
)) {
1499 PRINT_ER("Device couldn't be registered - %s\n",
1504 nic
->iftype
= STATION_MODE
;
1505 nic
->mac_opened
= 0;
1509 if (!wilc_spi_init()) {
1510 PRINT_ER("Can't initialize SPI\n");
1513 wilc_dev
->wilc_spidev
= wilc_spi_dev
;
1515 wilc_dev
->wilc_sdio_func
= wilc_sdio_func
;