main.c revision 0681f989357416d7ef28ebaea4151ce70a6ae21c
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/freezer.h>
10#include <linux/etherdevice.h>
11#include <linux/netdevice.h>
12#include <linux/if_arp.h>
13
14#include <net/iw_handler.h>
15#include <net/ieee80211.h>
16
17#include "host.h"
18#include "decl.h"
19#include "dev.h"
20#include "wext.h"
21#include "debugfs.h"
22#include "assoc.h"
23
24#define DRIVER_RELEASE_VERSION "320.p0"
25const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
26#ifdef  DEBUG
27    "-dbg"
28#endif
29    "";
30
31
32/* Module parameters */
33unsigned int libertas_debug = 0;
34module_param(libertas_debug, int, 0644);
35EXPORT_SYMBOL_GPL(libertas_debug);
36
37
38#define WLAN_TX_PWR_DEFAULT		20	/*100mW */
39#define WLAN_TX_PWR_US_DEFAULT		20	/*100mW */
40#define WLAN_TX_PWR_JP_DEFAULT		16	/*50mW */
41#define WLAN_TX_PWR_FR_DEFAULT		20	/*100mW */
42#define WLAN_TX_PWR_EMEA_DEFAULT	20	/*100mW */
43
44/* Format { channel, frequency (MHz), maxtxpower } */
45/* band: 'B/G', region: USA FCC/Canada IC */
46static struct chan_freq_power channel_freq_power_US_BG[] = {
47	{1, 2412, WLAN_TX_PWR_US_DEFAULT},
48	{2, 2417, WLAN_TX_PWR_US_DEFAULT},
49	{3, 2422, WLAN_TX_PWR_US_DEFAULT},
50	{4, 2427, WLAN_TX_PWR_US_DEFAULT},
51	{5, 2432, WLAN_TX_PWR_US_DEFAULT},
52	{6, 2437, WLAN_TX_PWR_US_DEFAULT},
53	{7, 2442, WLAN_TX_PWR_US_DEFAULT},
54	{8, 2447, WLAN_TX_PWR_US_DEFAULT},
55	{9, 2452, WLAN_TX_PWR_US_DEFAULT},
56	{10, 2457, WLAN_TX_PWR_US_DEFAULT},
57	{11, 2462, WLAN_TX_PWR_US_DEFAULT}
58};
59
60/* band: 'B/G', region: Europe ETSI */
61static struct chan_freq_power channel_freq_power_EU_BG[] = {
62	{1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
63	{2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
64	{3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
65	{4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
66	{5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
67	{6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
68	{7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
69	{8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
70	{9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
71	{10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
72	{11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
73	{12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
74	{13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
75};
76
77/* band: 'B/G', region: Spain */
78static struct chan_freq_power channel_freq_power_SPN_BG[] = {
79	{10, 2457, WLAN_TX_PWR_DEFAULT},
80	{11, 2462, WLAN_TX_PWR_DEFAULT}
81};
82
83/* band: 'B/G', region: France */
84static struct chan_freq_power channel_freq_power_FR_BG[] = {
85	{10, 2457, WLAN_TX_PWR_FR_DEFAULT},
86	{11, 2462, WLAN_TX_PWR_FR_DEFAULT},
87	{12, 2467, WLAN_TX_PWR_FR_DEFAULT},
88	{13, 2472, WLAN_TX_PWR_FR_DEFAULT}
89};
90
91/* band: 'B/G', region: Japan */
92static struct chan_freq_power channel_freq_power_JPN_BG[] = {
93	{1, 2412, WLAN_TX_PWR_JP_DEFAULT},
94	{2, 2417, WLAN_TX_PWR_JP_DEFAULT},
95	{3, 2422, WLAN_TX_PWR_JP_DEFAULT},
96	{4, 2427, WLAN_TX_PWR_JP_DEFAULT},
97	{5, 2432, WLAN_TX_PWR_JP_DEFAULT},
98	{6, 2437, WLAN_TX_PWR_JP_DEFAULT},
99	{7, 2442, WLAN_TX_PWR_JP_DEFAULT},
100	{8, 2447, WLAN_TX_PWR_JP_DEFAULT},
101	{9, 2452, WLAN_TX_PWR_JP_DEFAULT},
102	{10, 2457, WLAN_TX_PWR_JP_DEFAULT},
103	{11, 2462, WLAN_TX_PWR_JP_DEFAULT},
104	{12, 2467, WLAN_TX_PWR_JP_DEFAULT},
105	{13, 2472, WLAN_TX_PWR_JP_DEFAULT},
106	{14, 2484, WLAN_TX_PWR_JP_DEFAULT}
107};
108
109/**
110 * the structure for channel, frequency and power
111 */
112struct region_cfp_table {
113	u8 region;
114	struct chan_freq_power *cfp_BG;
115	int cfp_no_BG;
116};
117
118/**
119 * the structure for the mapping between region and CFP
120 */
121static struct region_cfp_table region_cfp_table[] = {
122	{0x10,			/*US FCC */
123	 channel_freq_power_US_BG,
124	 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
125	 }
126	,
127	{0x20,			/*CANADA IC */
128	 channel_freq_power_US_BG,
129	 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
130	 }
131	,
132	{0x30, /*EU*/ channel_freq_power_EU_BG,
133	 sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
134	 }
135	,
136	{0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
137	 sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
138	 }
139	,
140	{0x32, /*FRANCE*/ channel_freq_power_FR_BG,
141	 sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
142	 }
143	,
144	{0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
145	 sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
146	 }
147	,
148/*Add new region here */
149};
150
151/**
152 * the rates supported
153 */
154u8 libertas_supported_rates[G_SUPPORTED_RATES] =
155    { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
1560 };
157
158/**
159 * the rates supported for ad-hoc G mode
160 */
161u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
162    { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
1630 };
164
165/**
166 * the rates supported for ad-hoc B mode
167 */
168u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
169
170/**
171 * the table to keep region code
172 */
173u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
174    { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
175
176/**
177 * Attributes exported through sysfs
178 */
179
180/**
181 * @brief Get function for sysfs attribute libertas_mpp
182 */
183static ssize_t libertas_mpp_get(struct device * dev,
184		struct device_attribute *attr, char * buf) {
185	struct cmd_ds_mesh_access mesh_access;
186
187	memset(&mesh_access, 0, sizeof(mesh_access));
188	libertas_prepare_and_send_command(to_net_dev(dev)->priv,
189			cmd_mesh_access,
190			cmd_act_mesh_get_mpp,
191			cmd_option_waitforrsp, 0, (void *)&mesh_access);
192
193	return snprintf(buf, 3, "%d\n", mesh_access.data[0]);
194}
195
196/**
197 * @brief Set function for sysfs attribute libertas_mpp
198 */
199static ssize_t libertas_mpp_set(struct device * dev,
200		struct device_attribute *attr, const char * buf, size_t count) {
201	struct cmd_ds_mesh_access mesh_access;
202
203	memset(&mesh_access, 0, sizeof(mesh_access));
204	sscanf(buf, "%d", &(mesh_access.data[0]));
205	libertas_prepare_and_send_command((to_net_dev(dev))->priv,
206			cmd_mesh_access,
207			cmd_act_mesh_set_mpp,
208			cmd_option_waitforrsp, 0, (void *)&mesh_access);
209	return strlen(buf);
210}
211
212/**
213 * libertas_mpp attribute to be exported per mshX interface
214 * through sysfs (/sys/class/net/mshX/libertas-mpp)
215 */
216static DEVICE_ATTR(libertas_mpp, 0644, libertas_mpp_get,
217		libertas_mpp_set );
218
219/**
220 *  @brief Check if the device can be open and wait if necessary.
221 *
222 *  @param dev     A pointer to net_device structure
223 *  @return 	   0
224 *
225 * For USB adapter, on some systems the device open handler will be
226 * called before FW ready. Use the following flag check and wait
227 * function to work around the issue.
228 *
229 */
230static int pre_open_check(struct net_device *dev)
231{
232	wlan_private *priv = (wlan_private *) dev->priv;
233	wlan_adapter *adapter = priv->adapter;
234	int i = 0;
235
236	while (!adapter->fw_ready && i < 20) {
237		i++;
238		msleep_interruptible(100);
239	}
240	if (!adapter->fw_ready) {
241		lbs_pr_err("firmware not ready\n");
242		return -1;
243	}
244
245	return 0;
246}
247
248/**
249 *  @brief This function opens the device
250 *
251 *  @param dev     A pointer to net_device structure
252 *  @return 	   0
253 */
254static int wlan_dev_open(struct net_device *dev)
255{
256	wlan_private *priv = (wlan_private *) dev->priv;
257	wlan_adapter *adapter = priv->adapter;
258
259	lbs_deb_enter(LBS_DEB_NET);
260
261	priv->open = 1;
262
263	if (adapter->connect_status == libertas_connected) {
264		netif_carrier_on(priv->dev);
265		netif_carrier_on(priv->mesh_dev);
266	} else {
267		netif_carrier_off(priv->dev);
268		netif_carrier_off(priv->mesh_dev);
269	}
270
271	lbs_deb_leave(LBS_DEB_NET);
272	return 0;
273}
274/**
275 *  @brief This function opens the mshX interface
276 *
277 *  @param dev     A pointer to net_device structure
278 *  @return 	   0
279 */
280static int mesh_open(struct net_device *dev)
281{
282	wlan_private *priv = (wlan_private *) dev->priv ;
283
284	if (pre_open_check(dev) == -1)
285		return -1;
286	priv->mesh_open = 1 ;
287	netif_wake_queue(priv->mesh_dev);
288	if (priv->infra_open == 0)
289		return wlan_dev_open(priv->dev) ;
290	return 0;
291}
292
293/**
294 *  @brief This function opens the ethX interface
295 *
296 *  @param dev     A pointer to net_device structure
297 *  @return 	   0
298 */
299static int wlan_open(struct net_device *dev)
300{
301	wlan_private *priv = (wlan_private *) dev->priv ;
302
303	if(pre_open_check(dev) == -1)
304		return -1;
305	priv->infra_open = 1 ;
306	netif_wake_queue(priv->dev);
307	if (priv->open == 0)
308		return wlan_dev_open(priv->dev) ;
309	return 0;
310}
311
312static int wlan_dev_close(struct net_device *dev)
313{
314	wlan_private *priv = dev->priv;
315
316	lbs_deb_enter(LBS_DEB_NET);
317
318	netif_carrier_off(priv->dev);
319	priv->open = 0;
320
321	lbs_deb_leave(LBS_DEB_NET);
322	return 0;
323}
324
325/**
326 *  @brief This function closes the mshX interface
327 *
328 *  @param dev     A pointer to net_device structure
329 *  @return 	   0
330 */
331static int mesh_close(struct net_device *dev)
332{
333	wlan_private *priv = (wlan_private *) (dev->priv);
334
335	priv->mesh_open = 0;
336	netif_stop_queue(priv->mesh_dev);
337	if (priv->infra_open == 0)
338		return wlan_dev_close(dev);
339	else
340		return 0;
341}
342
343/**
344 *  @brief This function closes the ethX interface
345 *
346 *  @param dev     A pointer to net_device structure
347 *  @return 	   0
348 */
349static int wlan_close(struct net_device *dev)
350{
351	wlan_private *priv = (wlan_private *) dev->priv;
352
353	netif_stop_queue(dev);
354	priv->infra_open = 0;
355	if (priv->mesh_open == 0)
356		return wlan_dev_close(dev);
357	else
358		return 0;
359}
360
361
362static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
363{
364	int ret = 0;
365	wlan_private *priv = dev->priv;
366
367	lbs_deb_enter(LBS_DEB_NET);
368
369	if (priv->dnld_sent || priv->adapter->TxLockFlag) {
370		priv->stats.tx_dropped++;
371		goto done;
372	}
373
374	netif_stop_queue(priv->dev);
375	netif_stop_queue(priv->mesh_dev);
376
377	if (libertas_process_tx(priv, skb) == 0)
378		dev->trans_start = jiffies;
379done:
380	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
381	return ret;
382}
383
384/**
385 * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
386 *
387 */
388static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
389{
390	wlan_private *priv = dev->priv;
391	int ret;
392
393	lbs_deb_enter(LBS_DEB_MESH);
394
395	SET_MESH_FRAME(skb);
396
397	ret = wlan_hard_start_xmit(skb, priv->dev);
398	lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
399	return ret;
400}
401
402/**
403 * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
404 *
405 */
406static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
407{
408	int ret;
409
410	lbs_deb_enter(LBS_DEB_NET);
411
412	UNSET_MESH_FRAME(skb);
413
414	ret = wlan_hard_start_xmit(skb, dev);
415	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
416	return ret;
417}
418
419static void wlan_tx_timeout(struct net_device *dev)
420{
421	wlan_private *priv = (wlan_private *) dev->priv;
422
423	lbs_deb_enter(LBS_DEB_TX);
424
425	lbs_pr_err("tx watch dog timeout\n");
426
427	priv->dnld_sent = DNLD_RES_RECEIVED;
428	dev->trans_start = jiffies;
429
430	if (priv->adapter->currenttxskb) {
431		if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
432			/* If we are here, we have not received feedback from
433			   the previous packet.  Assume TX_FAIL and move on. */
434			priv->adapter->eventcause = 0x01000000;
435			libertas_send_tx_feedback(priv);
436		} else
437			wake_up_interruptible(&priv->mainthread.waitq);
438	} else if (priv->adapter->connect_status == libertas_connected) {
439		netif_wake_queue(priv->dev);
440		netif_wake_queue(priv->mesh_dev);
441	}
442
443	lbs_deb_leave(LBS_DEB_TX);
444}
445
446/**
447 *  @brief This function returns the network statistics
448 *
449 *  @param dev     A pointer to wlan_private structure
450 *  @return 	   A pointer to net_device_stats structure
451 */
452static struct net_device_stats *wlan_get_stats(struct net_device *dev)
453{
454	wlan_private *priv = (wlan_private *) dev->priv;
455
456	return &priv->stats;
457}
458
459static int wlan_set_mac_address(struct net_device *dev, void *addr)
460{
461	int ret = 0;
462	wlan_private *priv = (wlan_private *) dev->priv;
463	wlan_adapter *adapter = priv->adapter;
464	struct sockaddr *phwaddr = addr;
465
466	lbs_deb_enter(LBS_DEB_NET);
467
468	memset(adapter->current_addr, 0, ETH_ALEN);
469
470	/* dev->dev_addr is 8 bytes */
471	lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
472
473	lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
474	memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
475
476	ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
477				    cmd_act_set,
478				    cmd_option_waitforrsp, 0, NULL);
479
480	if (ret) {
481		lbs_deb_net("set MAC address failed\n");
482		ret = -1;
483		goto done;
484	}
485
486	lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
487	memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
488	if (priv->mesh_dev)
489		memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
490
491done:
492	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
493	return ret;
494}
495
496static int wlan_copy_multicast_address(wlan_adapter * adapter,
497				     struct net_device *dev)
498{
499	int i = 0;
500	struct dev_mc_list *mcptr = dev->mc_list;
501
502	for (i = 0; i < dev->mc_count; i++) {
503		memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
504		mcptr = mcptr->next;
505	}
506
507	return i;
508
509}
510
511static void wlan_set_multicast_list(struct net_device *dev)
512{
513	wlan_private *priv = dev->priv;
514	wlan_adapter *adapter = priv->adapter;
515	int oldpacketfilter;
516
517	lbs_deb_enter(LBS_DEB_NET);
518
519	oldpacketfilter = adapter->currentpacketfilter;
520
521	if (dev->flags & IFF_PROMISC) {
522		lbs_deb_net("enable promiscuous mode\n");
523		adapter->currentpacketfilter |=
524		    cmd_act_mac_promiscuous_enable;
525		adapter->currentpacketfilter &=
526		    ~(cmd_act_mac_all_multicast_enable |
527		      cmd_act_mac_multicast_enable);
528	} else {
529		/* Multicast */
530		adapter->currentpacketfilter &=
531		    ~cmd_act_mac_promiscuous_enable;
532
533		if (dev->flags & IFF_ALLMULTI || dev->mc_count >
534		    MRVDRV_MAX_MULTICAST_LIST_SIZE) {
535			lbs_deb_net( "enabling all multicast\n");
536			adapter->currentpacketfilter |=
537			    cmd_act_mac_all_multicast_enable;
538			adapter->currentpacketfilter &=
539			    ~cmd_act_mac_multicast_enable;
540		} else {
541			adapter->currentpacketfilter &=
542			    ~cmd_act_mac_all_multicast_enable;
543
544			if (!dev->mc_count) {
545				lbs_deb_net("no multicast addresses, "
546				       "disabling multicast\n");
547				adapter->currentpacketfilter &=
548				    ~cmd_act_mac_multicast_enable;
549			} else {
550				int i;
551
552				adapter->currentpacketfilter |=
553				    cmd_act_mac_multicast_enable;
554
555				adapter->nr_of_multicastmacaddr =
556				    wlan_copy_multicast_address(adapter, dev);
557
558				lbs_deb_net("multicast addresses: %d\n",
559				       dev->mc_count);
560
561				for (i = 0; i < dev->mc_count; i++) {
562					lbs_deb_net("Multicast address %d:"
563					       MAC_FMT "\n", i,
564					       adapter->multicastlist[i][0],
565					       adapter->multicastlist[i][1],
566					       adapter->multicastlist[i][2],
567					       adapter->multicastlist[i][3],
568					       adapter->multicastlist[i][4],
569					       adapter->multicastlist[i][5]);
570				}
571				/* send multicast addresses to firmware */
572				libertas_prepare_and_send_command(priv,
573						      cmd_mac_multicast_adr,
574						      cmd_act_set, 0, 0,
575						      NULL);
576			}
577		}
578	}
579
580	if (adapter->currentpacketfilter != oldpacketfilter) {
581		libertas_set_mac_packet_filter(priv);
582	}
583
584	lbs_deb_leave(LBS_DEB_NET);
585}
586
587/**
588 *  @brief This function handles the major jobs in the WLAN driver.
589 *  It handles all events generated by firmware, RX data received
590 *  from firmware and TX data sent from kernel.
591 *
592 *  @param data    A pointer to wlan_thread structure
593 *  @return 	   0
594 */
595static int wlan_service_main_thread(void *data)
596{
597	struct wlan_thread *thread = data;
598	wlan_private *priv = thread->priv;
599	wlan_adapter *adapter = priv->adapter;
600	wait_queue_t wait;
601	u8 ireg = 0;
602
603	lbs_deb_enter(LBS_DEB_THREAD);
604
605	wlan_activate_thread(thread);
606
607	init_waitqueue_entry(&wait, current);
608
609	for (;;) {
610		lbs_deb_thread( "main-thread 111: intcounter=%d "
611		       "currenttxskb=%p dnld_sent=%d\n",
612		       adapter->intcounter,
613		       adapter->currenttxskb, priv->dnld_sent);
614
615		add_wait_queue(&thread->waitq, &wait);
616		set_current_state(TASK_INTERRUPTIBLE);
617		spin_lock_irq(&adapter->driver_lock);
618		if ((adapter->psstate == PS_STATE_SLEEP) ||
619		    (!adapter->intcounter
620		     && (priv->dnld_sent || adapter->cur_cmd ||
621			 list_empty(&adapter->cmdpendingq)))) {
622			lbs_deb_thread(
623			       "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
624			       adapter->connect_status, adapter->intcounter,
625			       adapter->psmode, adapter->psstate);
626			spin_unlock_irq(&adapter->driver_lock);
627			schedule();
628		} else
629			spin_unlock_irq(&adapter->driver_lock);
630
631
632		lbs_deb_thread(
633		       "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
634		       "dnld_sent=%d\n", adapter->intcounter,
635		       adapter->currenttxskb, priv->dnld_sent);
636
637		set_current_state(TASK_RUNNING);
638		remove_wait_queue(&thread->waitq, &wait);
639		try_to_freeze();
640
641		lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
642		       "dnld_sent=%d\n",
643		       adapter->intcounter,
644		       adapter->currenttxskb, priv->dnld_sent);
645
646		if (kthread_should_stop()
647		    || adapter->surpriseremoved) {
648			lbs_deb_thread(
649			       "main-thread: break from main thread: surpriseremoved=0x%x\n",
650			       adapter->surpriseremoved);
651			break;
652		}
653
654
655		spin_lock_irq(&adapter->driver_lock);
656		if (adapter->intcounter) {
657			u8 int_status;
658			adapter->intcounter = 0;
659			int_status = priv->hw_get_int_status(priv, &ireg);
660
661			if (int_status) {
662				lbs_deb_thread(
663				       "main-thread: reading HOST_INT_STATUS_REG failed\n");
664				spin_unlock_irq(&adapter->driver_lock);
665				continue;
666			}
667			adapter->hisregcpy |= ireg;
668		}
669
670		lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
671		       "dnld_sent=%d\n",
672		       adapter->intcounter,
673		       adapter->currenttxskb, priv->dnld_sent);
674
675		/* command response? */
676		if (adapter->hisregcpy & his_cmdupldrdy) {
677			lbs_deb_thread("main-thread: cmd response ready\n");
678
679			adapter->hisregcpy &= ~his_cmdupldrdy;
680			spin_unlock_irq(&adapter->driver_lock);
681			libertas_process_rx_command(priv);
682			spin_lock_irq(&adapter->driver_lock);
683		}
684
685		/* Any Card Event */
686		if (adapter->hisregcpy & his_cardevent) {
687			lbs_deb_thread("main-thread: Card Event Activity\n");
688
689			adapter->hisregcpy &= ~his_cardevent;
690
691			if (priv->hw_read_event_cause(priv)) {
692				lbs_pr_alert(
693				       "main-thread: hw_read_event_cause failed\n");
694				spin_unlock_irq(&adapter->driver_lock);
695				continue;
696			}
697			spin_unlock_irq(&adapter->driver_lock);
698			libertas_process_event(priv);
699		} else
700			spin_unlock_irq(&adapter->driver_lock);
701
702		/* Check if we need to confirm Sleep Request received previously */
703		if (adapter->psstate == PS_STATE_PRE_SLEEP) {
704			if (!priv->dnld_sent && !adapter->cur_cmd) {
705				if (adapter->connect_status ==
706				    libertas_connected) {
707					lbs_deb_thread(
708					       "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
709					       "dnld_sent=%d cur_cmd=%p, confirm now\n",
710					       adapter->intcounter,
711					       adapter->currenttxskb,
712					       priv->dnld_sent,
713					       adapter->cur_cmd);
714
715					libertas_ps_confirm_sleep(priv,
716						       (u16) adapter->psmode);
717				} else {
718					/* workaround for firmware sending
719					 * deauth/linkloss event immediately
720					 * after sleep request, remove this
721					 * after firmware fixes it
722					 */
723					adapter->psstate = PS_STATE_AWAKE;
724					lbs_pr_alert(
725					       "main-thread: ignore PS_SleepConfirm in non-connected state\n");
726				}
727			}
728		}
729
730		/* The PS state is changed during processing of Sleep Request
731		 * event above
732		 */
733		if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
734		    (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
735			continue;
736
737		/* Execute the next command */
738		if (!priv->dnld_sent && !priv->adapter->cur_cmd)
739			libertas_execute_next_command(priv);
740
741		/* Wake-up command waiters which can't sleep in
742		 * libertas_prepare_and_send_command
743		 */
744		if (!adapter->nr_cmd_pending)
745			wake_up_all(&adapter->cmd_pending);
746
747		libertas_tx_runqueue(priv);
748	}
749
750	del_timer(&adapter->command_timer);
751	adapter->nr_cmd_pending = 0;
752	wake_up_all(&adapter->cmd_pending);
753	wlan_deactivate_thread(thread);
754
755	lbs_deb_leave(LBS_DEB_THREAD);
756	return 0;
757}
758
759/**
760 * @brief This function adds the card. it will probe the
761 * card, allocate the wlan_priv and initialize the device.
762 *
763 *  @param card    A pointer to card
764 *  @return 	   A pointer to wlan_private structure
765 */
766wlan_private *libertas_add_card(void *card)
767{
768	struct net_device *dev = NULL;
769	wlan_private *priv = NULL;
770
771	lbs_deb_enter(LBS_DEB_NET);
772
773	/* Allocate an Ethernet device and register it */
774	if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
775		lbs_pr_err("init ethX device failed\n");
776		return NULL;
777	}
778	priv = dev->priv;
779
780	/* allocate buffer for wlan_adapter */
781	if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
782		lbs_pr_err("allocate buffer for wlan_adapter failed\n");
783		goto err_kzalloc;
784	}
785
786	priv->dev = dev;
787	priv->card = card;
788	priv->mesh_open = 0;
789	priv->infra_open = 0;
790
791	SET_MODULE_OWNER(dev);
792
793	/* Setup the OS Interface to our functions */
794	dev->open = wlan_open;
795	dev->hard_start_xmit = wlan_pre_start_xmit;
796	dev->stop = wlan_close;
797	dev->do_ioctl = libertas_do_ioctl;
798	dev->set_mac_address = wlan_set_mac_address;
799	dev->tx_timeout = wlan_tx_timeout;
800	dev->get_stats = wlan_get_stats;
801	dev->watchdog_timeo = 5 * HZ;
802	dev->ethtool_ops = &libertas_ethtool_ops;
803#ifdef	WIRELESS_EXT
804	dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
805#endif
806#define NETIF_F_DYNALLOC 16
807	dev->features |= NETIF_F_DYNALLOC;
808	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
809	dev->set_multicast_list = wlan_set_multicast_list;
810
811	INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
812	INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
813
814	spin_lock_init(&priv->adapter->driver_lock);
815	init_waitqueue_head(&priv->adapter->cmd_pending);
816	priv->adapter->nr_cmd_pending = 0;
817	goto done;
818
819err_kzalloc:
820	free_netdev(dev);
821	priv = NULL;
822done:
823	lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
824	return priv;
825}
826EXPORT_SYMBOL_GPL(libertas_add_card);
827
828int libertas_activate_card(wlan_private *priv, char *fw_name)
829{
830	struct net_device *dev = priv->dev;
831	int ret = -1;
832
833	lbs_deb_enter(LBS_DEB_MAIN);
834
835	lbs_deb_thread("Starting kthread...\n");
836	priv->mainthread.priv = priv;
837	wlan_create_thread(wlan_service_main_thread,
838			   &priv->mainthread, "wlan_main_service");
839
840	priv->assoc_thread =
841		create_singlethread_workqueue("libertas_assoc");
842	INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
843
844	/*
845	 * Register the device. Fillup the private data structure with
846	 * relevant information from the card and request for the required
847	 * IRQ.
848	 */
849	if (priv->hw_register_dev(priv) < 0) {
850		lbs_pr_err("failed to register WLAN device\n");
851		goto err_registerdev;
852	}
853
854	/* init FW and HW */
855	if (fw_name && libertas_init_fw(priv, fw_name)) {
856		lbs_pr_err("firmware init failed\n");
857		goto err_registerdev;
858	}
859
860	if (register_netdev(dev)) {
861		lbs_pr_err("cannot register ethX device\n");
862		goto err_init_fw;
863	}
864
865	lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
866
867	libertas_debugfs_init_one(priv, dev);
868
869	ret = 0;
870	goto done;
871
872err_init_fw:
873	priv->hw_unregister_dev(priv);
874err_registerdev:
875	destroy_workqueue(priv->assoc_thread);
876	/* Stop the thread servicing the interrupts */
877	wake_up_interruptible(&priv->mainthread.waitq);
878	wlan_terminate_thread(&priv->mainthread);
879	kfree(priv->adapter);
880	free_netdev(dev);
881done:
882	lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
883	return ret;
884}
885EXPORT_SYMBOL_GPL(libertas_activate_card);
886
887
888/**
889 * @brief This function adds mshX interface
890 *
891 *  @param priv    A pointer to the wlan_private structure
892 *  @return 	   0 if successful, -X otherwise
893 */
894int libertas_add_mesh(wlan_private *priv)
895{
896	struct net_device *mesh_dev = NULL;
897	int ret = 0;
898
899	lbs_deb_enter(LBS_DEB_MESH);
900
901	/* Allocate a virtual mesh device */
902	if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
903		lbs_deb_mesh("init mshX device failed\n");
904		ret = -ENOMEM;
905		goto done;
906	}
907	mesh_dev->priv = priv;
908	priv->mesh_dev = mesh_dev;
909
910	SET_MODULE_OWNER(mesh_dev);
911
912	mesh_dev->open = mesh_open;
913	mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
914	mesh_dev->stop = mesh_close;
915	mesh_dev->do_ioctl = libertas_do_ioctl;
916	mesh_dev->get_stats = wlan_get_stats;
917	mesh_dev->ethtool_ops = &libertas_ethtool_ops;
918	memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
919			sizeof(priv->dev->dev_addr));
920
921#ifdef	WIRELESS_EXT
922	mesh_dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
923#endif
924#define NETIF_F_DYNALLOC 16
925
926	/* Register virtual mesh interface */
927	ret = register_netdev(mesh_dev);
928	if (ret) {
929		lbs_pr_err("cannot register mshX virtual interface\n");
930		goto err_free;
931	}
932
933	ret = device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
934	if (ret)
935		goto err_unregister;
936
937	/* Everything successful */
938	ret = 0;
939	goto done;
940
941
942err_unregister:
943	unregister_netdev(mesh_dev);
944
945err_free:
946	free_netdev(mesh_dev);
947
948done:
949	lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
950	return ret;
951}
952EXPORT_SYMBOL_GPL(libertas_add_mesh);
953
954static void wake_pending_cmdnodes(wlan_private *priv)
955{
956	struct cmd_ctrl_node *cmdnode;
957	unsigned long flags;
958
959	lbs_deb_enter(LBS_DEB_CMD);
960
961	spin_lock_irqsave(&priv->adapter->driver_lock, flags);
962	list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
963		cmdnode->cmdwaitqwoken = 1;
964		wake_up_interruptible(&cmdnode->cmdwait_q);
965	}
966	spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
967}
968
969
970int libertas_remove_card(wlan_private *priv)
971{
972	wlan_adapter *adapter;
973	struct net_device *dev;
974	union iwreq_data wrqu;
975
976	lbs_deb_enter(LBS_DEB_NET);
977
978	if (!priv)
979		goto out;
980
981	adapter = priv->adapter;
982
983	if (!adapter)
984		goto out;
985
986	dev = priv->dev;
987
988	netif_stop_queue(priv->dev);
989	netif_carrier_off(priv->dev);
990
991	wake_pending_cmdnodes(priv);
992
993	unregister_netdev(dev);
994
995	cancel_delayed_work(&priv->assoc_work);
996	destroy_workqueue(priv->assoc_thread);
997
998	if (adapter->psmode == wlan802_11powermodemax_psp) {
999		adapter->psmode = wlan802_11powermodecam;
1000		libertas_ps_wakeup(priv, cmd_option_waitforrsp);
1001	}
1002
1003	memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1004	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1005	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1006
1007	adapter->surpriseremoved = 1;
1008
1009	/* Stop the thread servicing the interrupts */
1010	wlan_terminate_thread(&priv->mainthread);
1011
1012	libertas_debugfs_remove_one(priv);
1013
1014	lbs_deb_net("free adapter\n");
1015	libertas_free_adapter(priv);
1016
1017	lbs_deb_net("unregister finish\n");
1018
1019	priv->dev = NULL;
1020	free_netdev(dev);
1021
1022out:
1023	lbs_deb_leave(LBS_DEB_NET);
1024	return 0;
1025}
1026EXPORT_SYMBOL_GPL(libertas_remove_card);
1027
1028
1029void libertas_remove_mesh(wlan_private *priv)
1030{
1031	struct net_device *mesh_dev;
1032
1033	lbs_deb_enter(LBS_DEB_NET);
1034
1035	if (!priv)
1036		goto out;
1037
1038	mesh_dev = priv->mesh_dev;
1039
1040	netif_stop_queue(mesh_dev);
1041	netif_carrier_off(priv->mesh_dev);
1042
1043	device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
1044	unregister_netdev(mesh_dev);
1045
1046	priv->mesh_dev = NULL ;
1047	free_netdev(mesh_dev);
1048
1049out:
1050	lbs_deb_leave(LBS_DEB_NET);
1051}
1052EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1053
1054/**
1055 *  @brief This function finds the CFP in
1056 *  region_cfp_table based on region and band parameter.
1057 *
1058 *  @param region  The region code
1059 *  @param band	   The band
1060 *  @param cfp_no  A pointer to CFP number
1061 *  @return 	   A pointer to CFP
1062 */
1063struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1064{
1065	int i, end;
1066
1067	lbs_deb_enter(LBS_DEB_MAIN);
1068
1069	end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1070
1071	for (i = 0; i < end ; i++) {
1072		lbs_deb_main("region_cfp_table[i].region=%d\n",
1073			region_cfp_table[i].region);
1074		if (region_cfp_table[i].region == region) {
1075			*cfp_no = region_cfp_table[i].cfp_no_BG;
1076			lbs_deb_leave(LBS_DEB_MAIN);
1077			return region_cfp_table[i].cfp_BG;
1078		}
1079	}
1080
1081	lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1082	return NULL;
1083}
1084
1085int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1086{
1087	wlan_adapter *adapter = priv->adapter;
1088	int ret = 0;
1089	int i = 0;
1090
1091	struct chan_freq_power *cfp;
1092	int cfp_no;
1093
1094	lbs_deb_enter(LBS_DEB_MAIN);
1095
1096	memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1097
1098	{
1099		cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1100		if (cfp != NULL) {
1101			adapter->region_channel[i].nrcfp = cfp_no;
1102			adapter->region_channel[i].CFP = cfp;
1103		} else {
1104			lbs_deb_main("wrong region code %#x in band B/G\n",
1105			       region);
1106			ret = -1;
1107			goto out;
1108		}
1109		adapter->region_channel[i].valid = 1;
1110		adapter->region_channel[i].region = region;
1111		adapter->region_channel[i].band = band;
1112		i++;
1113	}
1114out:
1115	lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1116	return ret;
1117}
1118
1119/**
1120 *  @brief This function handles the interrupt. it will change PS
1121 *  state if applicable. it will wake up main_thread to handle
1122 *  the interrupt event as well.
1123 *
1124 *  @param dev     A pointer to net_device structure
1125 *  @return 	   n/a
1126 */
1127void libertas_interrupt(struct net_device *dev)
1128{
1129	wlan_private *priv = dev->priv;
1130
1131	lbs_deb_enter(LBS_DEB_THREAD);
1132
1133	lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1134	       priv->adapter->intcounter);
1135
1136	priv->adapter->intcounter++;
1137
1138	if (priv->adapter->psstate == PS_STATE_SLEEP) {
1139		priv->adapter->psstate = PS_STATE_AWAKE;
1140		netif_wake_queue(dev);
1141		netif_wake_queue(priv->mesh_dev);
1142	}
1143
1144	wake_up_interruptible(&priv->mainthread.waitq);
1145
1146	lbs_deb_leave(LBS_DEB_THREAD);
1147}
1148EXPORT_SYMBOL_GPL(libertas_interrupt);
1149
1150static int libertas_init_module(void)
1151{
1152	lbs_deb_enter(LBS_DEB_MAIN);
1153	libertas_debugfs_init();
1154	lbs_deb_leave(LBS_DEB_MAIN);
1155	return 0;
1156}
1157
1158static void libertas_exit_module(void)
1159{
1160	lbs_deb_enter(LBS_DEB_MAIN);
1161
1162	libertas_debugfs_remove();
1163
1164	lbs_deb_leave(LBS_DEB_MAIN);
1165}
1166
1167module_init(libertas_init_module);
1168module_exit(libertas_exit_module);
1169
1170MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1171MODULE_AUTHOR("Marvell International Ltd.");
1172MODULE_LICENSE("GPL");
1173