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>
28 static int dev_state_ev_handler(struct notifier_block
*this, unsigned long event
, void *ptr
);
30 static struct notifier_block g_dev_notifier
= {
31 .notifier_call
= dev_state_ev_handler
36 static struct semaphore close_exit_sync
;
38 static int wlan_deinit_locks(struct net_device
*dev
);
39 static void wlan_deinitialize_threads(struct net_device
*dev
);
41 static void linux_wlan_tx_complete(void *priv
, int status
);
42 static int mac_init_fn(struct net_device
*ndev
);
43 static struct net_device_stats
*mac_stats(struct net_device
*dev
);
44 static int mac_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
);
45 static void wilc_set_multicast_list(struct net_device
*dev
);
47 bool wilc_enable_ps
= true;
49 static const struct net_device_ops wilc_netdev_ops
= {
50 .ndo_init
= mac_init_fn
,
51 .ndo_open
= wilc_mac_open
,
52 .ndo_stop
= wilc_mac_close
,
53 .ndo_start_xmit
= wilc_mac_xmit
,
54 .ndo_do_ioctl
= mac_ioctl
,
55 .ndo_get_stats
= mac_stats
,
56 .ndo_set_rx_mode
= wilc_set_multicast_list
,
60 static int dev_state_ev_handler(struct notifier_block
*this, unsigned long event
, void *ptr
)
62 struct in_ifaddr
*dev_iface
= (struct in_ifaddr
*)ptr
;
63 struct wilc_priv
*priv
;
64 struct host_if_drv
*hif_drv
;
65 struct net_device
*dev
;
69 char wlan_dev_name
[5] = "wlan0";
71 if (!dev_iface
|| !dev_iface
->ifa_dev
|| !dev_iface
->ifa_dev
->dev
) {
72 PRINT_D(GENERIC_DBG
, "dev_iface = NULL\n");
76 if (memcmp(dev_iface
->ifa_label
, "wlan0", 5) &&
77 memcmp(dev_iface
->ifa_label
, "p2p0", 4)) {
78 PRINT_D(GENERIC_DBG
, "Interface is neither WLAN0 nor P2P0\n");
82 dev
= (struct net_device
*)dev_iface
->ifa_dev
->dev
;
83 if (!dev
->ieee80211_ptr
|| !dev
->ieee80211_ptr
->wiphy
) {
84 PRINT_D(GENERIC_DBG
, "No Wireless registerd\n");
87 priv
= wiphy_priv(dev
->ieee80211_ptr
->wiphy
);
89 PRINT_D(GENERIC_DBG
, "No Wireless Priv\n");
92 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
93 vif
= netdev_priv(dev
);
94 if (!vif
|| !hif_drv
) {
95 PRINT_D(GENERIC_DBG
, "No Wireless Priv\n");
99 PRINT_INFO(GENERIC_DBG
, "dev_state_ev_handler +++\n");
103 PRINT_D(GENERIC_DBG
, "dev_state_ev_handler event=NETDEV_UP %p\n", dev
);
105 PRINT_INFO(GENERIC_DBG
, "\n ============== IP Address Obtained ===============\n\n");
107 if (vif
->iftype
== STATION_MODE
|| vif
->iftype
== CLIENT_MODE
) {
109 wilc_optaining_ip
= false;
110 del_timer(&wilc_during_ip_timer
);
111 PRINT_D(GENERIC_DBG
, "IP obtained , enable scan\n");
115 wilc_set_power_mgmt(vif
, 1, 0);
117 PRINT_D(GENERIC_DBG
, "[%s] Up IP\n", dev_iface
->ifa_label
);
119 ip_addr_buf
= (char *)&dev_iface
->ifa_address
;
120 PRINT_D(GENERIC_DBG
, "IP add=%d:%d:%d:%d\n",
121 ip_addr_buf
[0], ip_addr_buf
[1],
122 ip_addr_buf
[2], ip_addr_buf
[3]);
123 wilc_setup_ipaddress(vif
, ip_addr_buf
, vif
->u8IfIdx
);
128 PRINT_D(GENERIC_DBG
, "dev_state_ev_handler event=NETDEV_DOWN %p\n", dev
);
130 PRINT_INFO(GENERIC_DBG
, "\n ============== IP Address Released ===============\n\n");
131 if (vif
->iftype
== STATION_MODE
|| vif
->iftype
== CLIENT_MODE
) {
133 wilc_optaining_ip
= false;
136 if (memcmp(dev_iface
->ifa_label
, wlan_dev_name
, 5) == 0)
137 wilc_set_power_mgmt(vif
, 0, 0);
139 wilc_resolve_disconnect_aberration(vif
);
141 PRINT_D(GENERIC_DBG
, "[%s] Down IP\n", dev_iface
->ifa_label
);
143 ip_addr_buf
= null_ip
;
144 PRINT_D(GENERIC_DBG
, "IP add=%d:%d:%d:%d\n",
145 ip_addr_buf
[0], ip_addr_buf
[1],
146 ip_addr_buf
[2], ip_addr_buf
[3]);
148 wilc_setup_ipaddress(vif
, ip_addr_buf
, vif
->u8IfIdx
);
153 PRINT_INFO(GENERIC_DBG
, "dev_state_ev_handler event=default\n");
154 PRINT_INFO(GENERIC_DBG
, "[%s] unknown dev event: %lu\n", dev_iface
->ifa_label
, event
);
162 static irqreturn_t
isr_uh_routine(int irq
, void *user_data
)
164 struct wilc_vif
*vif
;
166 struct net_device
*dev
= (struct net_device
*)user_data
;
168 vif
= netdev_priv(dev
);
170 PRINT_D(INT_DBG
, "Interrupt received UH\n");
173 PRINT_ER("Driver is CLOSING: Can't handle UH interrupt\n");
176 return IRQ_WAKE_THREAD
;
179 static irqreturn_t
isr_bh_routine(int irq
, void *userdata
)
181 struct wilc_vif
*vif
;
184 vif
= netdev_priv(userdata
);
188 PRINT_ER("Driver is CLOSING: Can't handle BH interrupt\n");
192 PRINT_D(INT_DBG
, "Interrupt received BH\n");
193 wilc_handle_isr(wilc
);
198 static int init_irq(struct net_device
*dev
)
201 struct wilc_vif
*vif
;
204 vif
= netdev_priv(dev
);
207 if ((gpio_request(wl
->gpio
, "WILC_INTR") == 0) &&
208 (gpio_direction_input(wl
->gpio
) == 0)) {
209 wl
->dev_irq_num
= gpio_to_irq(wl
->gpio
);
212 PRINT_ER("could not obtain gpio for WILC_INTR\n");
215 if (ret
!= -1 && request_threaded_irq(wl
->dev_irq_num
,
218 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
219 "WILC_IRQ", dev
) < 0) {
220 PRINT_ER("Failed to request IRQ for GPIO: %d\n", wl
->gpio
);
224 PRINT_D(INIT_DBG
, "IRQ request succeeded IRQ-NUM= %d on GPIO: %d\n",
225 wl
->dev_irq_num
, wl
->gpio
);
231 static void deinit_irq(struct net_device
*dev
)
233 struct wilc_vif
*vif
;
236 vif
= netdev_priv(dev
);
239 /* Deintialize IRQ */
240 if (wilc
->dev_irq_num
) {
241 free_irq(wilc
->dev_irq_num
, wilc
);
242 gpio_free(wilc
->gpio
);
246 void wilc_dbg(u8
*buff
)
248 PRINT_D(INIT_DBG
, "%d\n", *buff
);
251 int wilc_lock_timeout(struct wilc
*nic
, void *vp
, u32 timeout
)
253 /* FIXME: replace with mutex_lock or wait_for_completion */
256 PRINT_D(LOCK_DBG
, "Locking %p\n", vp
);
258 error
= down_timeout((struct semaphore
*)vp
,
259 msecs_to_jiffies(timeout
));
261 PRINT_ER("Failed, mutex is NULL\n");
265 void wilc_mac_indicate(struct wilc
*wilc
, int flag
)
269 if (flag
== WILC_MAC_INDICATE_STATUS
) {
270 wilc_wlan_cfg_get_val(WID_STATUS
,
271 (unsigned char *)&status
, 4);
272 if (wilc
->mac_status
== WILC_MAC_STATUS_INIT
) {
273 wilc
->mac_status
= status
;
274 up(&wilc
->sync_event
);
276 wilc
->mac_status
= status
;
278 } else if (flag
== WILC_MAC_INDICATE_SCAN
) {
279 PRINT_D(GENERIC_DBG
, "Scanning ...\n");
283 static struct net_device
*get_if_handler(struct wilc
*wilc
, u8
*mac_header
)
288 bssid
= mac_header
+ 10;
289 bssid1
= mac_header
+ 4;
291 for (i
= 0; i
< wilc
->vif_num
; i
++) {
292 if (wilc
->vif
[i
]->mode
== STATION_MODE
)
293 if (!memcmp(bssid
, wilc
->vif
[i
]->bssid
, ETH_ALEN
))
294 return wilc
->vif
[i
]->ndev
;
295 if (wilc
->vif
[i
]->mode
== AP_MODE
)
296 if (!memcmp(bssid1
, wilc
->vif
[i
]->bssid
, ETH_ALEN
))
297 return wilc
->vif
[i
]->ndev
;
303 int wilc_wlan_set_bssid(struct net_device
*wilc_netdev
, u8
*bssid
, u8 mode
)
307 struct wilc_vif
*vif
;
310 vif
= netdev_priv(wilc_netdev
);
313 for (i
= 0; i
< wilc
->vif_num
; i
++)
314 if (wilc
->vif
[i
]->ndev
== wilc_netdev
) {
315 memcpy(wilc
->vif
[i
]->bssid
, bssid
, 6);
316 wilc
->vif
[i
]->mode
= mode
;
324 int wilc_wlan_get_num_conn_ifcs(struct wilc
*wilc
)
327 u8 null_bssid
[6] = {0};
330 for (i
= 0; i
< wilc
->vif_num
; i
++)
331 if (memcmp(wilc
->vif
[i
]->bssid
, null_bssid
, 6))
337 #define USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
339 static int linux_wlan_txq_task(void *vp
)
342 struct wilc_vif
*vif
;
344 struct net_device
*dev
= vp
;
345 #if defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
346 #define TX_BACKOFF_WEIGHT_INCR_STEP (1)
347 #define TX_BACKOFF_WEIGHT_DECR_STEP (1)
348 #define TX_BACKOFF_WEIGHT_MAX (7)
349 #define TX_BACKOFF_WEIGHT_MIN (0)
350 #define TX_BACKOFF_WEIGHT_UNIT_MS (10)
351 int backoff_weight
= TX_BACKOFF_WEIGHT_MIN
;
354 vif
= netdev_priv(dev
);
357 up(&wl
->txq_thread_started
);
359 PRINT_D(TX_DBG
, "txq_task Taking a nap :)\n");
360 down(&wl
->txq_event
);
361 PRINT_D(TX_DBG
, "txq_task Who waked me up :$\n");
364 up(&wl
->txq_thread_started
);
366 while (!kthread_should_stop())
369 PRINT_D(TX_DBG
, "TX thread stopped\n");
372 PRINT_D(TX_DBG
, "txq_task handle the sending packet and let me go to sleep.\n");
373 #if !defined USE_TX_BACKOFF_DELAY_IF_NO_BUFFERS
374 ret
= wilc_wlan_handle_txq(dev
, &txq_count
);
377 ret
= wilc_wlan_handle_txq(dev
, &txq_count
);
378 if (txq_count
< FLOW_CONTROL_LOWER_THRESHOLD
) {
379 PRINT_D(TX_DBG
, "Waking up queue\n");
381 if (netif_queue_stopped(wl
->vif
[0]->ndev
))
382 netif_wake_queue(wl
->vif
[0]->ndev
);
383 if (netif_queue_stopped(wl
->vif
[1]->ndev
))
384 netif_wake_queue(wl
->vif
[1]->ndev
);
387 if (ret
== WILC_TX_ERR_NO_BUF
) {
388 backoff_weight
+= TX_BACKOFF_WEIGHT_INCR_STEP
;
389 if (backoff_weight
> TX_BACKOFF_WEIGHT_MAX
)
390 backoff_weight
= TX_BACKOFF_WEIGHT_MAX
;
392 if (backoff_weight
> TX_BACKOFF_WEIGHT_MIN
) {
393 backoff_weight
-= TX_BACKOFF_WEIGHT_DECR_STEP
;
394 if (backoff_weight
< TX_BACKOFF_WEIGHT_MIN
)
395 backoff_weight
= TX_BACKOFF_WEIGHT_MIN
;
398 } while (ret
== WILC_TX_ERR_NO_BUF
&& !wl
->close
);
404 void wilc_rx_complete(struct wilc
*nic
)
406 PRINT_D(RX_DBG
, "RX completed\n");
409 int wilc_wlan_get_firmware(struct net_device
*dev
)
411 struct wilc_vif
*vif
;
413 int chip_id
, ret
= 0;
414 const struct firmware
*wilc_firmware
;
417 vif
= netdev_priv(dev
);
420 chip_id
= wilc_get_chipid(wilc
, 0);
422 if (chip_id
< 0x1003a0)
423 firmware
= FIRMWARE_1002
;
425 firmware
= FIRMWARE_1003
;
427 netdev_info(dev
, "loading firmware %s\n", firmware
);
430 PRINT_ER("vif is NULL\n");
434 if (!(&vif
->ndev
->dev
)) {
435 PRINT_ER("&vif->ndev->dev is NULL\n");
439 if (request_firmware(&wilc_firmware
, firmware
, wilc
->dev
) != 0) {
440 PRINT_ER("%s - firmare not available\n", firmware
);
444 wilc
->firmware
= wilc_firmware
;
451 static int linux_wlan_start_firmware(struct net_device
*dev
)
453 struct wilc_vif
*vif
;
457 vif
= netdev_priv(dev
);
460 PRINT_D(INIT_DBG
, "Starting Firmware ...\n");
461 ret
= wilc_wlan_start(wilc
);
463 PRINT_ER("Failed to start Firmware\n");
467 PRINT_D(INIT_DBG
, "Waiting for Firmware to get ready ...\n");
468 ret
= wilc_lock_timeout(wilc
, &wilc
->sync_event
, 5000);
470 PRINT_D(INIT_DBG
, "Firmware start timed out");
473 PRINT_D(INIT_DBG
, "Firmware successfully started\n");
478 static int wilc1000_firmware_download(struct net_device
*dev
)
480 struct wilc_vif
*vif
;
484 vif
= netdev_priv(dev
);
487 if (!wilc
->firmware
) {
488 PRINT_ER("Firmware buffer is NULL\n");
491 PRINT_D(INIT_DBG
, "Downloading Firmware ...\n");
492 ret
= wilc_wlan_firmware_download(wilc
, wilc
->firmware
->data
,
493 wilc
->firmware
->size
);
497 PRINT_D(INIT_DBG
, "Freeing FW buffer ...\n");
498 PRINT_D(INIT_DBG
, "Releasing firmware\n");
499 release_firmware(wilc
->firmware
);
500 wilc
->firmware
= NULL
;
502 PRINT_D(INIT_DBG
, "Download Succeeded\n");
507 static int linux_wlan_init_test_config(struct net_device
*dev
,
508 struct wilc_vif
*vif
)
510 unsigned char c_val
[64];
511 unsigned char mac_add
[] = {0x00, 0x80, 0xC2, 0x5E, 0xa2, 0xff};
512 struct wilc
*wilc
= vif
->wilc
;
513 struct wilc_priv
*priv
;
514 struct host_if_drv
*hif_drv
;
516 PRINT_D(TX_DBG
, "Start configuring Firmware\n");
517 priv
= wiphy_priv(dev
->ieee80211_ptr
->wiphy
);
518 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
519 PRINT_D(INIT_DBG
, "Host = %p\n", hif_drv
);
520 wilc_get_mac_address(vif
, mac_add
);
522 PRINT_D(INIT_DBG
, "MAC address is : %02x-%02x-%02x-%02x-%02x-%02x\n",
523 mac_add
[0], mac_add
[1], mac_add
[2],
524 mac_add
[3], mac_add
[4], mac_add
[5]);
525 wilc_get_chipid(wilc
, 0);
529 if (!wilc_wlan_cfg_set(wilc
, 1, WID_SET_DRV_HANDLER
, c_val
, 4, 0, 0))
533 if (!wilc_wlan_cfg_set(wilc
, 0, WID_PC_TEST_MODE
, c_val
, 1, 0, 0))
536 c_val
[0] = INFRASTRUCTURE
;
537 if (!wilc_wlan_cfg_set(wilc
, 0, WID_BSS_TYPE
, c_val
, 1, 0, 0))
540 c_val
[0] = RATE_AUTO
;
541 if (!wilc_wlan_cfg_set(wilc
, 0, WID_CURRENT_TX_RATE
, c_val
, 1, 0, 0))
544 c_val
[0] = G_MIXED_11B_2_MODE
;
545 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11G_OPERATING_MODE
, c_val
, 1, 0,
550 if (!wilc_wlan_cfg_set(wilc
, 0, WID_CURRENT_CHANNEL
, c_val
, 1, 0, 0))
553 c_val
[0] = G_SHORT_PREAMBLE
;
554 if (!wilc_wlan_cfg_set(wilc
, 0, WID_PREAMBLE
, c_val
, 1, 0, 0))
557 c_val
[0] = AUTO_PROT
;
558 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_PROT_MECH
, c_val
, 1, 0, 0))
561 c_val
[0] = ACTIVE_SCAN
;
562 if (!wilc_wlan_cfg_set(wilc
, 0, WID_SCAN_TYPE
, c_val
, 1, 0, 0))
565 c_val
[0] = SITE_SURVEY_OFF
;
566 if (!wilc_wlan_cfg_set(wilc
, 0, WID_SITE_SURVEY
, c_val
, 1, 0, 0))
569 *((int *)c_val
) = 0xffff;
570 if (!wilc_wlan_cfg_set(wilc
, 0, WID_RTS_THRESHOLD
, c_val
, 2, 0, 0))
573 *((int *)c_val
) = 2346;
574 if (!wilc_wlan_cfg_set(wilc
, 0, WID_FRAG_THRESHOLD
, c_val
, 2, 0, 0))
578 if (!wilc_wlan_cfg_set(wilc
, 0, WID_BCAST_SSID
, c_val
, 1, 0, 0))
582 if (!wilc_wlan_cfg_set(wilc
, 0, WID_QOS_ENABLE
, c_val
, 1, 0, 0))
585 c_val
[0] = NO_POWERSAVE
;
586 if (!wilc_wlan_cfg_set(wilc
, 0, WID_POWER_MANAGEMENT
, c_val
, 1, 0, 0))
589 c_val
[0] = NO_SECURITY
; /* NO_ENCRYPT, 0x79 */
590 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11I_MODE
, c_val
, 1, 0, 0))
593 c_val
[0] = OPEN_SYSTEM
;
594 if (!wilc_wlan_cfg_set(wilc
, 0, WID_AUTH_TYPE
, c_val
, 1, 0, 0))
597 strcpy(c_val
, "123456790abcdef1234567890");
598 if (!wilc_wlan_cfg_set(wilc
, 0, WID_WEP_KEY_VALUE
, c_val
,
599 (strlen(c_val
) + 1), 0, 0))
602 strcpy(c_val
, "12345678");
603 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11I_PSK
, c_val
, (strlen(c_val
)), 0,
607 strcpy(c_val
, "password");
608 if (!wilc_wlan_cfg_set(wilc
, 0, WID_1X_KEY
, c_val
, (strlen(c_val
) + 1),
616 if (!wilc_wlan_cfg_set(wilc
, 0, WID_1X_SERV_ADDR
, c_val
, 4, 0, 0))
620 if (!wilc_wlan_cfg_set(wilc
, 0, WID_LISTEN_INTERVAL
, c_val
, 1, 0, 0))
624 if (!wilc_wlan_cfg_set(wilc
, 0, WID_DTIM_PERIOD
, c_val
, 1, 0, 0))
627 c_val
[0] = NORMAL_ACK
;
628 if (!wilc_wlan_cfg_set(wilc
, 0, WID_ACK_POLICY
, c_val
, 1, 0, 0))
632 if (!wilc_wlan_cfg_set(wilc
, 0, WID_USER_CONTROL_ON_TX_POWER
, c_val
, 1,
637 if (!wilc_wlan_cfg_set(wilc
, 0, WID_TX_POWER_LEVEL_11A
, c_val
, 1, 0,
642 if (!wilc_wlan_cfg_set(wilc
, 0, WID_TX_POWER_LEVEL_11B
, c_val
, 1, 0,
646 *((int *)c_val
) = 100;
647 if (!wilc_wlan_cfg_set(wilc
, 0, WID_BEACON_INTERVAL
, c_val
, 2, 0, 0))
650 c_val
[0] = REKEY_DISABLE
;
651 if (!wilc_wlan_cfg_set(wilc
, 0, WID_REKEY_POLICY
, c_val
, 1, 0, 0))
654 *((int *)c_val
) = 84600;
655 if (!wilc_wlan_cfg_set(wilc
, 0, WID_REKEY_PERIOD
, c_val
, 4, 0, 0))
658 *((int *)c_val
) = 500;
659 if (!wilc_wlan_cfg_set(wilc
, 0, WID_REKEY_PACKET_COUNT
, c_val
, 4, 0,
664 if (!wilc_wlan_cfg_set(wilc
, 0, WID_SHORT_SLOT_ALLOWED
, c_val
, 1, 0,
668 c_val
[0] = G_SELF_CTS_PROT
;
669 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_ERP_PROT_TYPE
, c_val
, 1, 0, 0))
673 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_ENABLE
, c_val
, 1, 0, 0))
676 c_val
[0] = HT_MIXED_MODE
;
677 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_OPERATING_MODE
, c_val
, 1, 0,
682 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_TXOP_PROT_DISABLE
, c_val
, 1, 0,
686 memcpy(c_val
, mac_add
, 6);
688 if (!wilc_wlan_cfg_set(wilc
, 0, WID_MAC_ADDR
, c_val
, 6, 0, 0))
691 c_val
[0] = DETECT_PROTECT_REPORT
;
692 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_OBSS_NONHT_DETECTION
, c_val
, 1,
696 c_val
[0] = RTS_CTS_NONHT_PROT
;
697 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_HT_PROT_TYPE
, c_val
, 1, 0, 0))
701 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_RIFS_PROT_ENABLE
, c_val
, 1, 0,
705 c_val
[0] = MIMO_MODE
;
706 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_SMPS_MODE
, c_val
, 1, 0, 0))
710 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_CURRENT_TX_MCS
, c_val
, 1, 0,
715 if (!wilc_wlan_cfg_set(wilc
, 0, WID_11N_IMMEDIATE_BA_ENABLED
, c_val
, 1,
725 void wilc1000_wlan_deinit(struct net_device
*dev
)
727 struct wilc_vif
*vif
;
730 vif
= netdev_priv(dev
);
734 netdev_err(dev
, "wl is NULL\n");
738 if (wl
->initialized
) {
739 netdev_info(dev
, "Deinitializing wilc1000...\n");
741 PRINT_D(INIT_DBG
, "Disabling IRQ\n");
742 if (!wl
->dev_irq_num
&&
743 wl
->hif_func
->disable_interrupt
) {
744 mutex_lock(&wl
->hif_cs
);
745 wl
->hif_func
->disable_interrupt(wl
);
746 mutex_unlock(&wl
->hif_cs
);
751 PRINT_D(INIT_DBG
, "Deinitializing Threads\n");
752 wlan_deinitialize_threads(dev
);
754 PRINT_D(INIT_DBG
, "Deinitializing IRQ\n");
759 PRINT_D(INIT_DBG
, "Deinitializing WILC Wlan\n");
760 wilc_wlan_cleanup(dev
);
761 #if defined(PLAT_ALLWINNER_A20) || defined(PLAT_ALLWINNER_A23) || defined(PLAT_ALLWINNER_A31)
762 if (!wl
->dev_irq_num
&&
763 wl
->hif_func
->disable_interrupt
) {
765 PRINT_D(INIT_DBG
, "Disabling IRQ 2\n");
767 mutex_lock(&wl
->hif_cs
);
768 wl
->hif_func
->disable_interrupt(wl
);
769 mutex_unlock(&wl
->hif_cs
);
773 PRINT_D(INIT_DBG
, "Deinitializing Locks\n");
774 wlan_deinit_locks(dev
);
776 wl
->initialized
= false;
778 PRINT_D(INIT_DBG
, "wilc1000 deinitialization Done\n");
781 PRINT_D(INIT_DBG
, "wilc1000 is not initialized\n");
785 static int wlan_init_locks(struct net_device
*dev
)
787 struct wilc_vif
*vif
;
790 vif
= netdev_priv(dev
);
793 PRINT_D(INIT_DBG
, "Initializing Locks ...\n");
795 mutex_init(&wl
->hif_cs
);
796 mutex_init(&wl
->rxq_cs
);
798 spin_lock_init(&wl
->txq_spinlock
);
799 sema_init(&wl
->txq_add_to_head_cs
, 1);
801 sema_init(&wl
->txq_event
, 0);
803 sema_init(&wl
->cfg_event
, 0);
804 sema_init(&wl
->sync_event
, 0);
806 sema_init(&wl
->txq_thread_started
, 0);
811 static int wlan_deinit_locks(struct net_device
*dev
)
813 struct wilc_vif
*vif
;
816 vif
= netdev_priv(dev
);
819 PRINT_D(INIT_DBG
, "De-Initializing Locks\n");
822 mutex_destroy(&wilc
->hif_cs
);
825 mutex_destroy(&wilc
->rxq_cs
);
830 static int wlan_initialize_threads(struct net_device
*dev
)
832 struct wilc_vif
*vif
;
835 vif
= netdev_priv(dev
);
838 PRINT_D(INIT_DBG
, "Initializing Threads ...\n");
839 PRINT_D(INIT_DBG
, "Creating kthread for transmission\n");
840 wilc
->txq_thread
= kthread_run(linux_wlan_txq_task
, (void *)dev
,
842 if (!wilc
->txq_thread
) {
843 PRINT_ER("couldn't create TXQ thread\n");
847 down(&wilc
->txq_thread_started
);
852 static void wlan_deinitialize_threads(struct net_device
*dev
)
854 struct wilc_vif
*vif
;
856 vif
= netdev_priv(dev
);
860 PRINT_D(INIT_DBG
, "Deinitializing Threads\n");
865 if (wl
->txq_thread
) {
866 kthread_stop(wl
->txq_thread
);
867 wl
->txq_thread
= NULL
;
871 int wilc1000_wlan_init(struct net_device
*dev
, struct wilc_vif
*vif
)
874 struct wilc
*wl
= vif
->wilc
;
876 if (!wl
->initialized
) {
877 wl
->mac_status
= WILC_MAC_STATUS_INIT
;
880 wlan_init_locks(dev
);
882 ret
= wilc_wlan_init(dev
);
884 PRINT_ER("Initializing WILC_Wlan FAILED\n");
889 if (wl
->gpio
>= 0 && init_irq(dev
)) {
890 PRINT_ER("couldn't initialize IRQ\n");
895 ret
= wlan_initialize_threads(dev
);
897 PRINT_ER("Initializing Threads FAILED\n");
899 goto _fail_wilc_wlan_
;
902 if (!wl
->dev_irq_num
&&
903 wl
->hif_func
->enable_interrupt
&&
904 wl
->hif_func
->enable_interrupt(wl
)) {
905 PRINT_ER("couldn't initialize IRQ\n");
907 goto _fail_irq_init_
;
910 if (wilc_wlan_get_firmware(dev
)) {
911 PRINT_ER("Can't get firmware\n");
913 goto _fail_irq_enable_
;
916 ret
= wilc1000_firmware_download(dev
);
918 PRINT_ER("Failed to download firmware\n");
920 goto _fail_irq_enable_
;
923 ret
= linux_wlan_start_firmware(dev
);
925 PRINT_ER("Failed to start firmware\n");
927 goto _fail_irq_enable_
;
930 if (wilc_wlan_cfg_get(wl
, 1, WID_FIRMWARE_VERSION
, 1, 0)) {
932 char Firmware_ver
[20];
934 size
= wilc_wlan_cfg_get_val(
935 WID_FIRMWARE_VERSION
,
936 Firmware_ver
, sizeof(Firmware_ver
));
937 Firmware_ver
[size
] = '\0';
938 PRINT_D(INIT_DBG
, "***** Firmware Ver = %s *******\n", Firmware_ver
);
940 ret
= linux_wlan_init_test_config(dev
, vif
);
943 PRINT_ER("Failed to configure firmware\n");
945 goto _fail_fw_start_
;
948 wl
->initialized
= true;
955 if (!wl
->dev_irq_num
&&
956 wl
->hif_func
->disable_interrupt
)
957 wl
->hif_func
->disable_interrupt(wl
);
962 wlan_deinitialize_threads(dev
);
964 wilc_wlan_cleanup(dev
);
966 wlan_deinit_locks(dev
);
967 PRINT_ER("WLAN Iinitialization FAILED\n");
969 PRINT_D(INIT_DBG
, "wilc1000 already initialized\n");
974 static int mac_init_fn(struct net_device
*ndev
)
976 netif_start_queue(ndev
);
977 netif_stop_queue(ndev
);
982 int wilc_mac_open(struct net_device
*ndev
)
984 struct wilc_vif
*vif
;
987 unsigned char mac_add
[ETH_ALEN
] = {0};
990 struct wilc_priv
*priv
;
993 vif
= netdev_priv(ndev
);
996 if (!wl
|| !wl
->dev
) {
997 netdev_err(ndev
, "wilc1000: SPI device not ready\n");
1001 vif
= netdev_priv(ndev
);
1003 priv
= wiphy_priv(vif
->ndev
->ieee80211_ptr
->wiphy
);
1004 PRINT_D(INIT_DBG
, "MAC OPEN[%p]\n", ndev
);
1006 ret
= wilc_init_host_int(ndev
);
1008 PRINT_ER("Failed to initialize host interface\n");
1013 PRINT_D(INIT_DBG
, "*** re-init ***\n");
1014 ret
= wilc1000_wlan_init(ndev
, vif
);
1016 PRINT_ER("Failed to initialize wilc1000\n");
1017 wilc_deinit_host_int(ndev
);
1021 wilc_get_mac_address(vif
, mac_add
);
1022 PRINT_D(INIT_DBG
, "Mac address: %pM\n", mac_add
);
1024 for (i
= 0; i
< wl
->vif_num
; i
++) {
1025 if (ndev
== wl
->vif
[i
]->ndev
) {
1026 memcpy(wl
->vif
[i
]->src_addr
, mac_add
, ETH_ALEN
);
1027 wilc_set_operation_mode(vif
, vif
->iftype
);
1032 memcpy(ndev
->dev_addr
, wl
->vif
[i
]->src_addr
, ETH_ALEN
);
1034 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
1035 PRINT_ER("Error: Wrong MAC address\n");
1036 wilc_deinit_host_int(ndev
);
1037 wilc1000_wlan_deinit(ndev
);
1041 wilc_mgmt_frame_register(vif
->ndev
->ieee80211_ptr
->wiphy
,
1042 vif
->ndev
->ieee80211_ptr
,
1043 vif
->g_struct_frame_reg
[0].frame_type
,
1044 vif
->g_struct_frame_reg
[0].reg
);
1045 wilc_mgmt_frame_register(vif
->ndev
->ieee80211_ptr
->wiphy
,
1046 vif
->ndev
->ieee80211_ptr
,
1047 vif
->g_struct_frame_reg
[1].frame_type
,
1048 vif
->g_struct_frame_reg
[1].reg
);
1049 netif_wake_queue(ndev
);
1051 vif
->mac_opened
= 1;
1055 static struct net_device_stats
*mac_stats(struct net_device
*dev
)
1057 struct wilc_vif
*vif
= netdev_priv(dev
);
1059 return &vif
->netstats
;
1062 static void wilc_set_multicast_list(struct net_device
*dev
)
1064 struct netdev_hw_addr
*ha
;
1065 struct wilc_priv
*priv
;
1066 struct host_if_drv
*hif_drv
;
1067 struct wilc_vif
*vif
;
1070 priv
= wiphy_priv(dev
->ieee80211_ptr
->wiphy
);
1071 vif
= netdev_priv(dev
);
1072 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
1077 PRINT_D(INIT_DBG
, "Setting Multicast List with count = %d.\n",
1080 if (dev
->flags
& IFF_PROMISC
) {
1081 PRINT_D(INIT_DBG
, "Set promiscuous mode ON, retrive all packets\n");
1085 if ((dev
->flags
& IFF_ALLMULTI
) ||
1086 (dev
->mc
.count
) > WILC_MULTICAST_TABLE_SIZE
) {
1087 PRINT_D(INIT_DBG
, "Disable multicast filter, retrive all multicast packets\n");
1088 wilc_setup_multicast_filter(vif
, false, 0);
1092 if ((dev
->mc
.count
) == 0) {
1093 PRINT_D(INIT_DBG
, "Enable multicast filter, retrive directed packets only.\n");
1094 wilc_setup_multicast_filter(vif
, true, 0);
1098 netdev_for_each_mc_addr(ha
, dev
) {
1099 memcpy(wilc_multicast_mac_addr_list
[i
], ha
->addr
, ETH_ALEN
);
1100 PRINT_D(INIT_DBG
, "Entry[%d]: %x:%x:%x:%x:%x:%x\n", i
,
1101 wilc_multicast_mac_addr_list
[i
][0],
1102 wilc_multicast_mac_addr_list
[i
][1],
1103 wilc_multicast_mac_addr_list
[i
][2],
1104 wilc_multicast_mac_addr_list
[i
][3],
1105 wilc_multicast_mac_addr_list
[i
][4],
1106 wilc_multicast_mac_addr_list
[i
][5]);
1110 wilc_setup_multicast_filter(vif
, true, (dev
->mc
.count
));
1115 static void linux_wlan_tx_complete(void *priv
, int status
)
1117 struct tx_complete_data
*pv_data
= (struct tx_complete_data
*)priv
;
1120 PRINT_D(TX_DBG
, "Packet sent successfully - Size = %d - Address = %p - SKB = %p\n", pv_data
->size
, pv_data
->buff
, pv_data
->skb
);
1122 PRINT_D(TX_DBG
, "Couldn't send packet - Size = %d - Address = %p - SKB = %p\n", pv_data
->size
, pv_data
->buff
, pv_data
->skb
);
1123 dev_kfree_skb(pv_data
->skb
);
1127 int wilc_mac_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1129 struct wilc_vif
*vif
;
1130 struct tx_complete_data
*tx_data
= NULL
;
1134 struct ethhdr
*eth_h
;
1137 vif
= netdev_priv(ndev
);
1140 PRINT_D(TX_DBG
, "Sending packet just received from TCP/IP\n");
1142 if (skb
->dev
!= ndev
) {
1143 PRINT_ER("Packet not destined to this device\n");
1147 tx_data
= kmalloc(sizeof(*tx_data
), GFP_ATOMIC
);
1149 PRINT_ER("Failed to allocate memory for tx_data structure\n");
1151 netif_wake_queue(ndev
);
1155 tx_data
->buff
= skb
->data
;
1156 tx_data
->size
= skb
->len
;
1159 eth_h
= (struct ethhdr
*)(skb
->data
);
1160 if (eth_h
->h_proto
== 0x8e88)
1161 PRINT_D(INIT_DBG
, "EAPOL transmitted\n");
1163 ih
= (struct iphdr
*)(skb
->data
+ sizeof(struct ethhdr
));
1165 udp_buf
= (char *)ih
+ sizeof(struct iphdr
);
1166 if ((udp_buf
[1] == 68 && udp_buf
[3] == 67) ||
1167 (udp_buf
[1] == 67 && udp_buf
[3] == 68))
1168 PRINT_D(GENERIC_DBG
, "DHCP Message transmitted, type:%x %x %x\n",
1169 udp_buf
[248], udp_buf
[249], udp_buf
[250]);
1171 PRINT_D(TX_DBG
, "Sending packet - Size = %d - Address = %p - SKB = %p\n", tx_data
->size
, tx_data
->buff
, tx_data
->skb
);
1172 PRINT_D(TX_DBG
, "Adding tx packet to TX Queue\n");
1173 vif
->netstats
.tx_packets
++;
1174 vif
->netstats
.tx_bytes
+= tx_data
->size
;
1175 tx_data
->pBssid
= wilc
->vif
[vif
->u8IfIdx
]->bssid
;
1176 queue_count
= wilc_wlan_txq_add_net_pkt(ndev
, (void *)tx_data
,
1177 tx_data
->buff
, tx_data
->size
,
1178 linux_wlan_tx_complete
);
1180 if (queue_count
> FLOW_CONTROL_UPPER_THRESHOLD
) {
1181 netif_stop_queue(wilc
->vif
[0]->ndev
);
1182 netif_stop_queue(wilc
->vif
[1]->ndev
);
1188 int wilc_mac_close(struct net_device
*ndev
)
1190 struct wilc_priv
*priv
;
1191 struct wilc_vif
*vif
;
1192 struct host_if_drv
*hif_drv
;
1195 vif
= netdev_priv(ndev
);
1197 if (!vif
|| !vif
->ndev
|| !vif
->ndev
->ieee80211_ptr
||
1198 !vif
->ndev
->ieee80211_ptr
->wiphy
) {
1199 PRINT_ER("vif = NULL\n");
1203 priv
= wiphy_priv(vif
->ndev
->ieee80211_ptr
->wiphy
);
1207 PRINT_ER("priv = NULL\n");
1211 hif_drv
= (struct host_if_drv
*)priv
->hWILCWFIDrv
;
1213 PRINT_D(GENERIC_DBG
, "Mac close\n");
1216 PRINT_ER("wl = NULL\n");
1221 PRINT_ER("hif_drv = NULL\n");
1225 if ((wl
->open_ifcs
) > 0) {
1228 PRINT_ER("ERROR: MAC close called while number of opened interfaces is zero\n");
1233 netif_stop_queue(vif
->ndev
);
1235 wilc_deinit_host_int(vif
->ndev
);
1238 if (wl
->open_ifcs
== 0) {
1239 PRINT_D(GENERIC_DBG
, "Deinitializing wilc1000\n");
1241 wilc1000_wlan_deinit(ndev
);
1242 WILC_WFI_deinit_mon_interface();
1245 up(&close_exit_sync
);
1246 vif
->mac_opened
= 0;
1251 static int mac_ioctl(struct net_device
*ndev
, struct ifreq
*req
, int cmd
)
1255 u32 size
= 0, length
= 0;
1256 struct wilc_vif
*vif
;
1257 struct wilc_priv
*priv
;
1261 vif
= netdev_priv(ndev
);
1264 if (!wilc
->initialized
)
1270 struct iwreq
*wrq
= (struct iwreq
*) req
;
1272 size
= wrq
->u
.data
.length
;
1274 if (size
&& wrq
->u
.data
.pointer
) {
1275 buff
= memdup_user(wrq
->u
.data
.pointer
,
1276 wrq
->u
.data
.length
);
1278 return PTR_ERR(buff
);
1280 if (strncasecmp(buff
, "RSSI", length
) == 0) {
1281 priv
= wiphy_priv(vif
->ndev
->ieee80211_ptr
->wiphy
);
1282 ret
= wilc_get_rssi(vif
, &rssi
);
1284 PRINT_ER("Failed to send get rssi param's message queue ");
1285 PRINT_INFO(GENERIC_DBG
, "RSSI :%d\n", rssi
);
1289 snprintf(buff
, size
, "rssi %d", rssi
);
1291 if (copy_to_user(wrq
->u
.data
.pointer
, buff
, size
)) {
1292 PRINT_ER("%s: failed to copy data to user buffer\n", __func__
);
1303 PRINT_INFO(GENERIC_DBG
, "Command - %d - has been received\n", cmd
);
1316 void wilc_frmw_to_linux(struct wilc
*wilc
, u8
*buff
, u32 size
, u32 pkt_offset
)
1318 unsigned int frame_len
= 0;
1320 unsigned char *buff_to_send
= NULL
;
1321 struct sk_buff
*skb
;
1322 struct net_device
*wilc_netdev
;
1323 struct wilc_vif
*vif
;
1325 wilc_netdev
= get_if_handler(wilc
, buff
);
1330 vif
= netdev_priv(wilc_netdev
);
1334 buff_to_send
= buff
;
1336 skb
= dev_alloc_skb(frame_len
);
1338 PRINT_ER("Low memory - packet droped\n");
1342 if (!wilc
|| !wilc_netdev
)
1343 PRINT_ER("wilc_netdev in wilc is NULL");
1344 skb
->dev
= wilc_netdev
;
1347 PRINT_ER("skb->dev is NULL\n");
1349 memcpy(skb_put(skb
, frame_len
), buff_to_send
, frame_len
);
1351 skb
->protocol
= eth_type_trans(skb
, wilc_netdev
);
1352 vif
->netstats
.rx_packets
++;
1353 vif
->netstats
.rx_bytes
+= frame_len
;
1354 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1355 stats
= netif_rx(skb
);
1356 PRINT_D(RX_DBG
, "netif_rx ret value is: %d\n", stats
);
1360 void WILC_WFI_mgmt_rx(struct wilc
*wilc
, u8
*buff
, u32 size
)
1363 struct wilc_vif
*vif
;
1365 for (i
= 0; i
< wilc
->vif_num
; i
++) {
1366 vif
= netdev_priv(wilc
->vif
[i
]->ndev
);
1367 if (vif
->monitor_flag
) {
1368 WILC_WFI_monitor_rx(buff
, size
);
1373 vif
= netdev_priv(wilc
->vif
[1]->ndev
);
1374 if ((buff
[0] == vif
->g_struct_frame_reg
[0].frame_type
&& vif
->g_struct_frame_reg
[0].reg
) ||
1375 (buff
[0] == vif
->g_struct_frame_reg
[1].frame_type
&& vif
->g_struct_frame_reg
[1].reg
))
1376 WILC_WFI_p2p_rx(wilc
->vif
[1]->ndev
, buff
, size
);
1379 void wilc_netdev_cleanup(struct wilc
*wilc
)
1382 struct wilc_vif
*vif
[NUM_CONCURRENT_IFC
];
1384 if (wilc
&& (wilc
->vif
[0]->ndev
|| wilc
->vif
[1]->ndev
)) {
1385 unregister_inetaddr_notifier(&g_dev_notifier
);
1387 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++)
1388 vif
[i
] = netdev_priv(wilc
->vif
[i
]->ndev
);
1391 if (wilc
&& wilc
->firmware
)
1392 release_firmware(wilc
->firmware
);
1394 if (wilc
&& (wilc
->vif
[0]->ndev
|| wilc
->vif
[1]->ndev
)) {
1395 wilc_lock_timeout(wilc
, &close_exit_sync
, 5 * 1000);
1397 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++)
1398 if (wilc
->vif
[i
]->ndev
)
1399 if (vif
[i
]->mac_opened
)
1400 wilc_mac_close(wilc
->vif
[i
]->ndev
);
1402 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++) {
1403 unregister_netdev(wilc
->vif
[i
]->ndev
);
1404 wilc_free_wiphy(wilc
->vif
[i
]->ndev
);
1405 free_netdev(wilc
->vif
[i
]->ndev
);
1411 EXPORT_SYMBOL_GPL(wilc_netdev_cleanup
);
1413 int wilc_netdev_init(struct wilc
**wilc
, struct device
*dev
, int io_type
,
1414 int gpio
, const struct wilc_hif_func
*ops
)
1417 struct wilc_vif
*vif
;
1418 struct net_device
*ndev
;
1421 sema_init(&close_exit_sync
, 0);
1423 wl
= kzalloc(sizeof(*wl
), GFP_KERNEL
);
1428 wl
->io_type
= io_type
;
1432 register_inetaddr_notifier(&g_dev_notifier
);
1434 for (i
= 0; i
< NUM_CONCURRENT_IFC
; i
++) {
1435 ndev
= alloc_etherdev(sizeof(struct wilc_vif
));
1437 PRINT_ER("Failed to allocate ethernet dev\n");
1441 vif
= netdev_priv(ndev
);
1442 memset(vif
, 0, sizeof(struct wilc_vif
));
1445 strcpy(ndev
->name
, "wlan%d");
1447 strcpy(ndev
->name
, "p2p%d");
1449 vif
->u8IfIdx
= wl
->vif_num
;
1452 wl
->vif
[wl
->vif_num
]->ndev
= ndev
;
1454 ndev
->netdev_ops
= &wilc_netdev_ops
;
1457 struct wireless_dev
*wdev
;
1458 wdev
= wilc_create_wiphy(ndev
, dev
);
1461 SET_NETDEV_DEV(ndev
, dev
);
1464 PRINT_ER("Can't register WILC Wiphy\n");
1468 vif
->ndev
->ieee80211_ptr
= wdev
;
1469 vif
->ndev
->ml_priv
= vif
;
1470 wdev
->netdev
= vif
->ndev
;
1471 vif
->netstats
.rx_packets
= 0;
1472 vif
->netstats
.tx_packets
= 0;
1473 vif
->netstats
.rx_bytes
= 0;
1474 vif
->netstats
.tx_bytes
= 0;
1477 if (register_netdev(ndev
)) {
1478 PRINT_ER("Device couldn't be registered - %s\n",
1483 vif
->iftype
= STATION_MODE
;
1484 vif
->mac_opened
= 0;
1489 EXPORT_SYMBOL_GPL(wilc_netdev_init
);
1491 MODULE_LICENSE("GPL");