• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/net/wireless/libertas/
1/**
2  * This file contains the major functions in WLAN
3  * driver. It includes init, exit, open, close and main
4  * thread etc..
5  */
6
7#include <linux/moduleparam.h>
8#include <linux/delay.h>
9#include <linux/etherdevice.h>
10#include <linux/netdevice.h>
11#include <linux/if_arp.h>
12#include <linux/kthread.h>
13#include <linux/kfifo.h>
14#include <linux/slab.h>
15#include <net/cfg80211.h>
16
17#include "host.h"
18#include "decl.h"
19#include "dev.h"
20#include "cfg.h"
21#include "debugfs.h"
22#include "cmd.h"
23
24#define DRIVER_RELEASE_VERSION "323.p0"
25const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
26#ifdef  DEBUG
27    "-dbg"
28#endif
29    "";
30
31
32/* Module parameters */
33unsigned int lbs_debug;
34EXPORT_SYMBOL_GPL(lbs_debug);
35module_param_named(libertas_debug, lbs_debug, int, 0644);
36
37
38/* This global structure is used to send the confirm_sleep command as
39 * fast as possible down to the firmware. */
40struct cmd_confirm_sleep confirm_sleep;
41
42
43/**
44 * the table to keep region code
45 */
46u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
47    { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
48
49/**
50 * FW rate table.  FW refers to rates by their index in this table, not by the
51 * rate value itself.  Values of 0x00 are
52 * reserved positions.
53 */
54static u8 fw_data_rates[MAX_RATES] =
55    { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
56      0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
57};
58
59/**
60 *  @brief use index to get the data rate
61 *
62 *  @param idx                The index of data rate
63 *  @return 	   		data rate or 0
64 */
65u32 lbs_fw_index_to_data_rate(u8 idx)
66{
67	if (idx >= sizeof(fw_data_rates))
68		idx = 0;
69	return fw_data_rates[idx];
70}
71
72/**
73 *  @brief use rate to get the index
74 *
75 *  @param rate                 data rate
76 *  @return 	   		index or 0
77 */
78u8 lbs_data_rate_to_fw_index(u32 rate)
79{
80	u8 i;
81
82	if (!rate)
83		return 0;
84
85	for (i = 0; i < sizeof(fw_data_rates); i++) {
86		if (rate == fw_data_rates[i])
87			return i;
88	}
89	return 0;
90}
91
92
93/**
94 *  @brief This function opens the ethX interface
95 *
96 *  @param dev     A pointer to net_device structure
97 *  @return 	   0 or -EBUSY if monitor mode active
98 */
99static int lbs_dev_open(struct net_device *dev)
100{
101	struct lbs_private *priv = dev->ml_priv;
102	int ret = 0;
103
104	lbs_deb_enter(LBS_DEB_NET);
105
106	spin_lock_irq(&priv->driver_lock);
107
108	if (priv->connect_status == LBS_CONNECTED)
109		netif_carrier_on(dev);
110	else
111		netif_carrier_off(dev);
112
113	if (!priv->tx_pending_len)
114		netif_wake_queue(dev);
115
116	spin_unlock_irq(&priv->driver_lock);
117	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
118	return ret;
119}
120
121/**
122 *  @brief This function closes the ethX interface
123 *
124 *  @param dev     A pointer to net_device structure
125 *  @return 	   0
126 */
127static int lbs_eth_stop(struct net_device *dev)
128{
129	struct lbs_private *priv = dev->ml_priv;
130
131	lbs_deb_enter(LBS_DEB_NET);
132
133	spin_lock_irq(&priv->driver_lock);
134	netif_stop_queue(dev);
135	spin_unlock_irq(&priv->driver_lock);
136
137	schedule_work(&priv->mcast_work);
138
139	lbs_deb_leave(LBS_DEB_NET);
140	return 0;
141}
142
143static void lbs_tx_timeout(struct net_device *dev)
144{
145	struct lbs_private *priv = dev->ml_priv;
146
147	lbs_deb_enter(LBS_DEB_TX);
148
149	lbs_pr_err("tx watch dog timeout\n");
150
151	dev->trans_start = jiffies; /* prevent tx timeout */
152
153	if (priv->currenttxskb)
154		lbs_send_tx_feedback(priv, 0);
155
156	/* XX: Shouldn't we also call into the hw-specific driver
157	   to kick it somehow? */
158	lbs_host_to_card_done(priv);
159
160
161	lbs_deb_leave(LBS_DEB_TX);
162}
163
164void lbs_host_to_card_done(struct lbs_private *priv)
165{
166	unsigned long flags;
167
168	lbs_deb_enter(LBS_DEB_THREAD);
169
170	spin_lock_irqsave(&priv->driver_lock, flags);
171
172	priv->dnld_sent = DNLD_RES_RECEIVED;
173
174	/* Wake main thread if commands are pending */
175	if (!priv->cur_cmd || priv->tx_pending_len > 0) {
176		if (!priv->wakeup_dev_required)
177			wake_up_interruptible(&priv->waitq);
178	}
179
180	spin_unlock_irqrestore(&priv->driver_lock, flags);
181	lbs_deb_leave(LBS_DEB_THREAD);
182}
183EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
184
185int lbs_set_mac_address(struct net_device *dev, void *addr)
186{
187	int ret = 0;
188	struct lbs_private *priv = dev->ml_priv;
189	struct sockaddr *phwaddr = addr;
190	struct cmd_ds_802_11_mac_address cmd;
191
192	lbs_deb_enter(LBS_DEB_NET);
193
194	/* In case it was called from the mesh device */
195	dev = priv->dev;
196
197	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
198	cmd.action = cpu_to_le16(CMD_ACT_SET);
199	memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
200
201	ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
202	if (ret) {
203		lbs_deb_net("set MAC address failed\n");
204		goto done;
205	}
206
207	memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
208	memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
209	if (priv->mesh_dev)
210		memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
211
212done:
213	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
214	return ret;
215}
216
217
218static inline int mac_in_list(unsigned char *list, int list_len,
219			      unsigned char *mac)
220{
221	while (list_len) {
222		if (!memcmp(list, mac, ETH_ALEN))
223			return 1;
224		list += ETH_ALEN;
225		list_len--;
226	}
227	return 0;
228}
229
230
231static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
232			       struct net_device *dev, int nr_addrs)
233{
234	int i = nr_addrs;
235	struct netdev_hw_addr *ha;
236	int cnt;
237
238	if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
239		return nr_addrs;
240
241	netif_addr_lock_bh(dev);
242	cnt = netdev_mc_count(dev);
243	netdev_for_each_mc_addr(ha, dev) {
244		if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
245			lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
246				    ha->addr);
247			cnt--;
248			continue;
249		}
250
251		if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
252			break;
253		memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
254		lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
255			    ha->addr);
256		i++;
257		cnt--;
258	}
259	netif_addr_unlock_bh(dev);
260	if (cnt)
261		return -EOVERFLOW;
262
263	return i;
264}
265
266static void lbs_set_mcast_worker(struct work_struct *work)
267{
268	struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
269	struct cmd_ds_mac_multicast_adr mcast_cmd;
270	int dev_flags;
271	int nr_addrs;
272	int old_mac_control = priv->mac_control;
273
274	lbs_deb_enter(LBS_DEB_NET);
275
276	dev_flags = priv->dev->flags;
277	if (priv->mesh_dev)
278		dev_flags |= priv->mesh_dev->flags;
279
280	if (dev_flags & IFF_PROMISC) {
281		priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
282		priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
283				       CMD_ACT_MAC_MULTICAST_ENABLE);
284		goto out_set_mac_control;
285	} else if (dev_flags & IFF_ALLMULTI) {
286	do_allmulti:
287		priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
288		priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
289				       CMD_ACT_MAC_MULTICAST_ENABLE);
290		goto out_set_mac_control;
291	}
292
293	/* Once for priv->dev, again for priv->mesh_dev if it exists */
294	nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
295	if (nr_addrs >= 0 && priv->mesh_dev)
296		nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
297	if (nr_addrs < 0)
298		goto do_allmulti;
299
300	if (nr_addrs) {
301		int size = offsetof(struct cmd_ds_mac_multicast_adr,
302				    maclist[6*nr_addrs]);
303
304		mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
305		mcast_cmd.hdr.size = cpu_to_le16(size);
306		mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
307
308		lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
309
310		priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
311	} else
312		priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
313
314	priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
315			       CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
316 out_set_mac_control:
317	if (priv->mac_control != old_mac_control)
318		lbs_set_mac_control(priv);
319
320	lbs_deb_leave(LBS_DEB_NET);
321}
322
323void lbs_set_multicast_list(struct net_device *dev)
324{
325	struct lbs_private *priv = dev->ml_priv;
326
327	schedule_work(&priv->mcast_work);
328}
329
330/**
331 *  @brief This function handles the major jobs in the LBS driver.
332 *  It handles all events generated by firmware, RX data received
333 *  from firmware and TX data sent from kernel.
334 *
335 *  @param data    A pointer to lbs_thread structure
336 *  @return 	   0
337 */
338static int lbs_thread(void *data)
339{
340	struct net_device *dev = data;
341	struct lbs_private *priv = dev->ml_priv;
342	wait_queue_t wait;
343
344	lbs_deb_enter(LBS_DEB_THREAD);
345
346	init_waitqueue_entry(&wait, current);
347
348	for (;;) {
349		int shouldsleep;
350		u8 resp_idx;
351
352		lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
353				priv->currenttxskb, priv->dnld_sent);
354
355		add_wait_queue(&priv->waitq, &wait);
356		set_current_state(TASK_INTERRUPTIBLE);
357		spin_lock_irq(&priv->driver_lock);
358
359		if (kthread_should_stop())
360			shouldsleep = 0;	/* Bye */
361		else if (priv->surpriseremoved)
362			shouldsleep = 1;	/* We need to wait until we're _told_ to die */
363		else if (priv->psstate == PS_STATE_SLEEP)
364			shouldsleep = 1;	/* Sleep mode. Nothing we can do till it wakes */
365		else if (priv->cmd_timed_out)
366			shouldsleep = 0;	/* Command timed out. Recover */
367		else if (!priv->fw_ready)
368			shouldsleep = 1;	/* Firmware not ready. We're waiting for it */
369		else if (priv->dnld_sent)
370			shouldsleep = 1;	/* Something is en route to the device already */
371		else if (priv->tx_pending_len > 0)
372			shouldsleep = 0;	/* We've a packet to send */
373		else if (priv->resp_len[priv->resp_idx])
374			shouldsleep = 0;	/* We have a command response */
375		else if (priv->cur_cmd)
376			shouldsleep = 1;	/* Can't send a command; one already running */
377		else if (!list_empty(&priv->cmdpendingq) &&
378					!(priv->wakeup_dev_required))
379			shouldsleep = 0;	/* We have a command to send */
380		else if (kfifo_len(&priv->event_fifo))
381			shouldsleep = 0;	/* We have an event to process */
382		else
383			shouldsleep = 1;	/* No command */
384
385		if (shouldsleep) {
386			lbs_deb_thread("sleeping, connect_status %d, "
387				"psmode %d, psstate %d\n",
388				priv->connect_status,
389				priv->psmode, priv->psstate);
390			spin_unlock_irq(&priv->driver_lock);
391			schedule();
392		} else
393			spin_unlock_irq(&priv->driver_lock);
394
395		lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
396			       priv->currenttxskb, priv->dnld_sent);
397
398		set_current_state(TASK_RUNNING);
399		remove_wait_queue(&priv->waitq, &wait);
400
401		lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
402			       priv->currenttxskb, priv->dnld_sent);
403
404		if (kthread_should_stop()) {
405			lbs_deb_thread("break from main thread\n");
406			break;
407		}
408
409		if (priv->surpriseremoved) {
410			lbs_deb_thread("adapter removed; waiting to die...\n");
411			continue;
412		}
413
414		lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
415		       priv->currenttxskb, priv->dnld_sent);
416
417		/* Process any pending command response */
418		spin_lock_irq(&priv->driver_lock);
419		resp_idx = priv->resp_idx;
420		if (priv->resp_len[resp_idx]) {
421			spin_unlock_irq(&priv->driver_lock);
422			lbs_process_command_response(priv,
423				priv->resp_buf[resp_idx],
424				priv->resp_len[resp_idx]);
425			spin_lock_irq(&priv->driver_lock);
426			priv->resp_len[resp_idx] = 0;
427		}
428		spin_unlock_irq(&priv->driver_lock);
429
430		/* Process hardware events, e.g. card removed, link lost */
431		spin_lock_irq(&priv->driver_lock);
432		while (kfifo_len(&priv->event_fifo)) {
433			u32 event;
434
435			if (kfifo_out(&priv->event_fifo,
436				(unsigned char *) &event, sizeof(event)) !=
437				sizeof(event))
438					break;
439			spin_unlock_irq(&priv->driver_lock);
440			lbs_process_event(priv, event);
441			spin_lock_irq(&priv->driver_lock);
442		}
443		spin_unlock_irq(&priv->driver_lock);
444
445		if (priv->wakeup_dev_required) {
446			lbs_deb_thread("Waking up device...\n");
447			/* Wake up device */
448			if (priv->exit_deep_sleep(priv))
449				lbs_deb_thread("Wakeup device failed\n");
450			continue;
451		}
452
453		/* command timeout stuff */
454		if (priv->cmd_timed_out && priv->cur_cmd) {
455			struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
456
457			lbs_pr_info("Timeout submitting command 0x%04x\n",
458				le16_to_cpu(cmdnode->cmdbuf->command));
459			lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
460			if (priv->reset_card)
461				priv->reset_card(priv);
462		}
463		priv->cmd_timed_out = 0;
464
465		if (!priv->fw_ready)
466			continue;
467
468		/* Check if we need to confirm Sleep Request received previously */
469		if (priv->psstate == PS_STATE_PRE_SLEEP &&
470		    !priv->dnld_sent && !priv->cur_cmd) {
471			if (priv->connect_status == LBS_CONNECTED) {
472				lbs_deb_thread("pre-sleep, currenttxskb %p, "
473					"dnld_sent %d, cur_cmd %p\n",
474					priv->currenttxskb, priv->dnld_sent,
475					priv->cur_cmd);
476
477				lbs_ps_confirm_sleep(priv);
478			} else {
479				priv->psstate = PS_STATE_AWAKE;
480				lbs_pr_alert("ignore PS_SleepConfirm in "
481					"non-connected state\n");
482			}
483		}
484
485		/* The PS state is changed during processing of Sleep Request
486		 * event above
487		 */
488		if ((priv->psstate == PS_STATE_SLEEP) ||
489		    (priv->psstate == PS_STATE_PRE_SLEEP))
490			continue;
491
492		if (priv->is_deep_sleep)
493			continue;
494
495		/* Execute the next command */
496		if (!priv->dnld_sent && !priv->cur_cmd)
497			lbs_execute_next_command(priv);
498
499		spin_lock_irq(&priv->driver_lock);
500		if (!priv->dnld_sent && priv->tx_pending_len > 0) {
501			int ret = priv->hw_host_to_card(priv, MVMS_DAT,
502							priv->tx_pending_buf,
503							priv->tx_pending_len);
504			if (ret) {
505				lbs_deb_tx("host_to_card failed %d\n", ret);
506				priv->dnld_sent = DNLD_RES_RECEIVED;
507			}
508			priv->tx_pending_len = 0;
509			if (!priv->currenttxskb) {
510				/* We can wake the queues immediately if we aren't
511				   waiting for TX feedback */
512				if (priv->connect_status == LBS_CONNECTED)
513					netif_wake_queue(priv->dev);
514				if (priv->mesh_dev &&
515				    lbs_mesh_connected(priv))
516					netif_wake_queue(priv->mesh_dev);
517			}
518		}
519		spin_unlock_irq(&priv->driver_lock);
520	}
521
522	del_timer(&priv->command_timer);
523	del_timer(&priv->auto_deepsleep_timer);
524
525	lbs_deb_leave(LBS_DEB_THREAD);
526	return 0;
527}
528
529int lbs_suspend(struct lbs_private *priv)
530{
531	int ret;
532
533	lbs_deb_enter(LBS_DEB_FW);
534
535	if (priv->is_deep_sleep) {
536		ret = lbs_set_deep_sleep(priv, 0);
537		if (ret) {
538			lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
539			return ret;
540		}
541		priv->deep_sleep_required = 1;
542	}
543
544	ret = lbs_set_host_sleep(priv, 1);
545
546	netif_device_detach(priv->dev);
547	if (priv->mesh_dev)
548		netif_device_detach(priv->mesh_dev);
549
550	lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
551	return ret;
552}
553EXPORT_SYMBOL_GPL(lbs_suspend);
554
555int lbs_resume(struct lbs_private *priv)
556{
557	int ret;
558
559	lbs_deb_enter(LBS_DEB_FW);
560
561	ret = lbs_set_host_sleep(priv, 0);
562
563	netif_device_attach(priv->dev);
564	if (priv->mesh_dev)
565		netif_device_attach(priv->mesh_dev);
566
567	if (priv->deep_sleep_required) {
568		priv->deep_sleep_required = 0;
569		ret = lbs_set_deep_sleep(priv, 1);
570		if (ret)
571			lbs_pr_err("deep sleep activation failed: %d\n", ret);
572	}
573
574	lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
575	return ret;
576}
577EXPORT_SYMBOL_GPL(lbs_resume);
578
579/**
580 * @brief This function gets the HW spec from the firmware and sets
581 *        some basic parameters.
582 *
583 *  @param priv    A pointer to struct lbs_private structure
584 *  @return 	   0 or -1
585 */
586static int lbs_setup_firmware(struct lbs_private *priv)
587{
588	int ret = -1;
589	s16 curlevel = 0, minlevel = 0, maxlevel = 0;
590
591	lbs_deb_enter(LBS_DEB_FW);
592
593	/* Read MAC address from firmware */
594	memset(priv->current_addr, 0xff, ETH_ALEN);
595	ret = lbs_update_hw_spec(priv);
596	if (ret)
597		goto done;
598
599	/* Read power levels if available */
600	ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
601	if (ret == 0) {
602		priv->txpower_cur = curlevel;
603		priv->txpower_min = minlevel;
604		priv->txpower_max = maxlevel;
605	}
606
607	/* Send cmd to FW to enable 11D function */
608	ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
609
610	lbs_set_mac_control(priv);
611done:
612	lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
613	return ret;
614}
615
616/**
617 *  This function handles the timeout of command sending.
618 *  It will re-send the same command again.
619 */
620static void lbs_cmd_timeout_handler(unsigned long data)
621{
622	struct lbs_private *priv = (struct lbs_private *)data;
623	unsigned long flags;
624
625	lbs_deb_enter(LBS_DEB_CMD);
626	spin_lock_irqsave(&priv->driver_lock, flags);
627
628	if (!priv->cur_cmd)
629		goto out;
630
631	lbs_pr_info("command 0x%04x timed out\n",
632		le16_to_cpu(priv->cur_cmd->cmdbuf->command));
633
634	priv->cmd_timed_out = 1;
635	wake_up_interruptible(&priv->waitq);
636out:
637	spin_unlock_irqrestore(&priv->driver_lock, flags);
638	lbs_deb_leave(LBS_DEB_CMD);
639}
640
641/**
642 *  This function put the device back to deep sleep mode when timer expires
643 *  and no activity (command, event, data etc.) is detected.
644 */
645static void auto_deepsleep_timer_fn(unsigned long data)
646{
647	struct lbs_private *priv = (struct lbs_private *)data;
648
649	lbs_deb_enter(LBS_DEB_CMD);
650
651	if (priv->is_activity_detected) {
652		priv->is_activity_detected = 0;
653	} else {
654		if (priv->is_auto_deep_sleep_enabled &&
655		    (!priv->wakeup_dev_required) &&
656		    (priv->connect_status != LBS_CONNECTED)) {
657			struct cmd_header cmd;
658
659			lbs_deb_main("Entering auto deep sleep mode...\n");
660			memset(&cmd, 0, sizeof(cmd));
661			cmd.size = cpu_to_le16(sizeof(cmd));
662			lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
663					sizeof(cmd));
664		}
665	}
666	mod_timer(&priv->auto_deepsleep_timer , jiffies +
667				(priv->auto_deep_sleep_timeout * HZ)/1000);
668	lbs_deb_leave(LBS_DEB_CMD);
669}
670
671int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
672{
673	lbs_deb_enter(LBS_DEB_SDIO);
674
675	priv->is_auto_deep_sleep_enabled = 1;
676	if (priv->is_deep_sleep)
677		priv->wakeup_dev_required = 1;
678	mod_timer(&priv->auto_deepsleep_timer ,
679			jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
680
681	lbs_deb_leave(LBS_DEB_SDIO);
682	return 0;
683}
684
685int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
686{
687	lbs_deb_enter(LBS_DEB_SDIO);
688
689	priv->is_auto_deep_sleep_enabled = 0;
690	priv->auto_deep_sleep_timeout = 0;
691	del_timer(&priv->auto_deepsleep_timer);
692
693	lbs_deb_leave(LBS_DEB_SDIO);
694	return 0;
695}
696
697static int lbs_init_adapter(struct lbs_private *priv)
698{
699	int ret;
700
701	lbs_deb_enter(LBS_DEB_MAIN);
702
703	memset(priv->current_addr, 0xff, ETH_ALEN);
704
705	priv->connect_status = LBS_DISCONNECTED;
706	priv->channel = DEFAULT_AD_HOC_CHANNEL;
707	priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
708	priv->radio_on = 1;
709	priv->psmode = LBS802_11POWERMODECAM;
710	priv->psstate = PS_STATE_FULL_POWER;
711	priv->is_deep_sleep = 0;
712	priv->is_auto_deep_sleep_enabled = 0;
713	priv->deep_sleep_required = 0;
714	priv->wakeup_dev_required = 0;
715	init_waitqueue_head(&priv->ds_awake_q);
716	init_waitqueue_head(&priv->scan_q);
717	priv->authtype_auto = 1;
718	priv->is_host_sleep_configured = 0;
719	priv->is_host_sleep_activated = 0;
720	init_waitqueue_head(&priv->host_sleep_q);
721	mutex_init(&priv->lock);
722
723	setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
724		(unsigned long)priv);
725	setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
726			(unsigned long)priv);
727
728	INIT_LIST_HEAD(&priv->cmdfreeq);
729	INIT_LIST_HEAD(&priv->cmdpendingq);
730
731	spin_lock_init(&priv->driver_lock);
732
733	/* Allocate the command buffers */
734	if (lbs_allocate_cmd_buffer(priv)) {
735		lbs_pr_err("Out of memory allocating command buffers\n");
736		ret = -ENOMEM;
737		goto out;
738	}
739	priv->resp_idx = 0;
740	priv->resp_len[0] = priv->resp_len[1] = 0;
741
742	/* Create the event FIFO */
743	ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
744	if (ret) {
745		lbs_pr_err("Out of memory allocating event FIFO buffer\n");
746		goto out;
747	}
748
749out:
750	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
751
752	return ret;
753}
754
755static void lbs_free_adapter(struct lbs_private *priv)
756{
757	lbs_deb_enter(LBS_DEB_MAIN);
758
759	lbs_free_cmd_buffer(priv);
760	kfifo_free(&priv->event_fifo);
761	del_timer(&priv->command_timer);
762	del_timer(&priv->auto_deepsleep_timer);
763
764	lbs_deb_leave(LBS_DEB_MAIN);
765}
766
767static const struct net_device_ops lbs_netdev_ops = {
768	.ndo_open 		= lbs_dev_open,
769	.ndo_stop		= lbs_eth_stop,
770	.ndo_start_xmit		= lbs_hard_start_xmit,
771	.ndo_set_mac_address	= lbs_set_mac_address,
772	.ndo_tx_timeout 	= lbs_tx_timeout,
773	.ndo_set_multicast_list = lbs_set_multicast_list,
774	.ndo_change_mtu		= eth_change_mtu,
775	.ndo_validate_addr	= eth_validate_addr,
776};
777
778/**
779 * @brief This function adds the card. it will probe the
780 * card, allocate the lbs_priv and initialize the device.
781 *
782 *  @param card    A pointer to card
783 *  @return 	   A pointer to struct lbs_private structure
784 */
785struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
786{
787	struct net_device *dev;
788	struct wireless_dev *wdev;
789	struct lbs_private *priv = NULL;
790
791	lbs_deb_enter(LBS_DEB_MAIN);
792
793	/* Allocate an Ethernet device and register it */
794	wdev = lbs_cfg_alloc(dmdev);
795	if (IS_ERR(wdev)) {
796		lbs_pr_err("cfg80211 init failed\n");
797		goto done;
798	}
799
800	wdev->iftype = NL80211_IFTYPE_STATION;
801	priv = wdev_priv(wdev);
802	priv->wdev = wdev;
803
804	if (lbs_init_adapter(priv)) {
805		lbs_pr_err("failed to initialize adapter structure.\n");
806		goto err_wdev;
807	}
808
809	dev = alloc_netdev(0, "wlan%d", ether_setup);
810	if (!dev) {
811		dev_err(dmdev, "no memory for network device instance\n");
812		goto err_adapter;
813	}
814
815	dev->ieee80211_ptr = wdev;
816	dev->ml_priv = priv;
817	SET_NETDEV_DEV(dev, dmdev);
818	wdev->netdev = dev;
819	priv->dev = dev;
820
821 	dev->netdev_ops = &lbs_netdev_ops;
822	dev->watchdog_timeo = 5 * HZ;
823	dev->ethtool_ops = &lbs_ethtool_ops;
824	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
825
826	priv->card = card;
827
828	strcpy(dev->name, "wlan%d");
829
830	lbs_deb_thread("Starting main thread...\n");
831	init_waitqueue_head(&priv->waitq);
832	priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
833	if (IS_ERR(priv->main_thread)) {
834		lbs_deb_thread("Error creating main thread.\n");
835		goto err_ndev;
836	}
837
838	priv->work_thread = create_singlethread_workqueue("lbs_worker");
839	INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
840
841	priv->wol_criteria = 0xffffffff;
842	priv->wol_gpio = 0xff;
843	priv->wol_gap = 20;
844
845	goto done;
846
847 err_ndev:
848	free_netdev(dev);
849
850 err_adapter:
851	lbs_free_adapter(priv);
852
853 err_wdev:
854	lbs_cfg_free(priv);
855
856	priv = NULL;
857
858done:
859	lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
860	return priv;
861}
862EXPORT_SYMBOL_GPL(lbs_add_card);
863
864
865void lbs_remove_card(struct lbs_private *priv)
866{
867	struct net_device *dev = priv->dev;
868
869	lbs_deb_enter(LBS_DEB_MAIN);
870
871	lbs_remove_mesh(priv);
872	lbs_scan_deinit(priv);
873
874	dev = priv->dev;
875
876	cancel_work_sync(&priv->mcast_work);
877
878	/* worker thread destruction blocks on the in-flight command which
879	 * should have been cleared already in lbs_stop_card().
880	 */
881	lbs_deb_main("destroying worker thread\n");
882	destroy_workqueue(priv->work_thread);
883	lbs_deb_main("done destroying worker thread\n");
884
885	if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
886		priv->psmode = LBS802_11POWERMODECAM;
887		lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
888	}
889
890	if (priv->is_deep_sleep) {
891		priv->is_deep_sleep = 0;
892		wake_up_interruptible(&priv->ds_awake_q);
893	}
894
895	priv->is_host_sleep_configured = 0;
896	priv->is_host_sleep_activated = 0;
897	wake_up_interruptible(&priv->host_sleep_q);
898
899	/* Stop the thread servicing the interrupts */
900	priv->surpriseremoved = 1;
901	kthread_stop(priv->main_thread);
902
903	lbs_free_adapter(priv);
904	lbs_cfg_free(priv);
905
906	priv->dev = NULL;
907	free_netdev(dev);
908
909	lbs_deb_leave(LBS_DEB_MAIN);
910}
911EXPORT_SYMBOL_GPL(lbs_remove_card);
912
913
914int lbs_rtap_supported(struct lbs_private *priv)
915{
916	if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
917		return 1;
918
919	/* newer firmware use a capability mask */
920	return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
921		(priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
922}
923
924
925int lbs_start_card(struct lbs_private *priv)
926{
927	struct net_device *dev = priv->dev;
928	int ret = -1;
929
930	lbs_deb_enter(LBS_DEB_MAIN);
931
932	/* poke the firmware */
933	ret = lbs_setup_firmware(priv);
934	if (ret)
935		goto done;
936
937	if (lbs_cfg_register(priv)) {
938		lbs_pr_err("cannot register device\n");
939		goto done;
940	}
941
942	lbs_update_channel(priv);
943
944	lbs_init_mesh(priv);
945
946	lbs_debugfs_init_one(priv, dev);
947
948	lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
949
950	ret = 0;
951
952done:
953	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
954	return ret;
955}
956EXPORT_SYMBOL_GPL(lbs_start_card);
957
958
959void lbs_stop_card(struct lbs_private *priv)
960{
961	struct net_device *dev;
962	struct cmd_ctrl_node *cmdnode;
963	unsigned long flags;
964
965	lbs_deb_enter(LBS_DEB_MAIN);
966
967	if (!priv)
968		goto out;
969	dev = priv->dev;
970
971	netif_stop_queue(dev);
972	netif_carrier_off(dev);
973
974	lbs_debugfs_remove_one(priv);
975	lbs_deinit_mesh(priv);
976
977	/* Delete the timeout of the currently processing command */
978	del_timer_sync(&priv->command_timer);
979	del_timer_sync(&priv->auto_deepsleep_timer);
980
981	/* Flush pending command nodes */
982	spin_lock_irqsave(&priv->driver_lock, flags);
983	lbs_deb_main("clearing pending commands\n");
984	list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
985		cmdnode->result = -ENOENT;
986		cmdnode->cmdwaitqwoken = 1;
987		wake_up_interruptible(&cmdnode->cmdwait_q);
988	}
989
990	/* Flush the command the card is currently processing */
991	if (priv->cur_cmd) {
992		lbs_deb_main("clearing current command\n");
993		priv->cur_cmd->result = -ENOENT;
994		priv->cur_cmd->cmdwaitqwoken = 1;
995		wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
996	}
997	lbs_deb_main("done clearing commands\n");
998	spin_unlock_irqrestore(&priv->driver_lock, flags);
999
1000	unregister_netdev(dev);
1001
1002out:
1003	lbs_deb_leave(LBS_DEB_MAIN);
1004}
1005EXPORT_SYMBOL_GPL(lbs_stop_card);
1006
1007
1008void lbs_queue_event(struct lbs_private *priv, u32 event)
1009{
1010	unsigned long flags;
1011
1012	lbs_deb_enter(LBS_DEB_THREAD);
1013	spin_lock_irqsave(&priv->driver_lock, flags);
1014
1015	if (priv->psstate == PS_STATE_SLEEP)
1016		priv->psstate = PS_STATE_AWAKE;
1017
1018	kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1019
1020	wake_up_interruptible(&priv->waitq);
1021
1022	spin_unlock_irqrestore(&priv->driver_lock, flags);
1023	lbs_deb_leave(LBS_DEB_THREAD);
1024}
1025EXPORT_SYMBOL_GPL(lbs_queue_event);
1026
1027void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
1028{
1029	lbs_deb_enter(LBS_DEB_THREAD);
1030
1031	if (priv->psstate == PS_STATE_SLEEP)
1032		priv->psstate = PS_STATE_AWAKE;
1033
1034	/* Swap buffers by flipping the response index */
1035	BUG_ON(resp_idx > 1);
1036	priv->resp_idx = resp_idx;
1037
1038	wake_up_interruptible(&priv->waitq);
1039
1040	lbs_deb_leave(LBS_DEB_THREAD);
1041}
1042EXPORT_SYMBOL_GPL(lbs_notify_command_response);
1043
1044static int __init lbs_init_module(void)
1045{
1046	lbs_deb_enter(LBS_DEB_MAIN);
1047	memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1048	confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1049	confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1050	confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
1051	lbs_debugfs_init();
1052	lbs_deb_leave(LBS_DEB_MAIN);
1053	return 0;
1054}
1055
1056static void __exit lbs_exit_module(void)
1057{
1058	lbs_deb_enter(LBS_DEB_MAIN);
1059	lbs_debugfs_remove();
1060	lbs_deb_leave(LBS_DEB_MAIN);
1061}
1062
1063module_init(lbs_init_module);
1064module_exit(lbs_exit_module);
1065
1066MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1067MODULE_AUTHOR("Marvell International Ltd.");
1068MODULE_LICENSE("GPL");
1069