• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500-V1.0.1.40_1.0.68/src/linux/linux-2.6/drivers/net/wireless/libertas/
1/**
2  * This file contains the handling of command.
3  * It prepares command and sends it to firmware when it is ready.
4  */
5
6#include <net/iw_handler.h>
7#include "host.h"
8#include "hostcmd.h"
9#include "decl.h"
10#include "defs.h"
11#include "dev.h"
12#include "join.h"
13#include "wext.h"
14
15static void cleanup_cmdnode(struct cmd_ctrl_node *ptempnode);
16
17static u16 commands_allowed_in_ps[] = {
18	cmd_802_11_rssi,
19};
20
21/**
22 *  @brief This function checks if the commans is allowed
23 *  in PS mode not.
24 *
25 *  @param command the command ID
26 *  @return 	   TRUE or FALSE
27 */
28static u8 is_command_allowed_in_ps(__le16 command)
29{
30	int i;
31
32	for (i = 0; i < ARRAY_SIZE(commands_allowed_in_ps); i++) {
33		if (command == cpu_to_le16(commands_allowed_in_ps[i]))
34			return 1;
35	}
36
37	return 0;
38}
39
40static int wlan_cmd_hw_spec(wlan_private * priv, struct cmd_ds_command *cmd)
41{
42	struct cmd_ds_get_hw_spec *hwspec = &cmd->params.hwspec;
43
44	lbs_deb_enter(LBS_DEB_CMD);
45
46	cmd->command = cpu_to_le16(cmd_get_hw_spec);
47	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_get_hw_spec) + S_DS_GEN);
48	memcpy(hwspec->permanentaddr, priv->adapter->current_addr, ETH_ALEN);
49
50	lbs_deb_leave(LBS_DEB_CMD);
51	return 0;
52}
53
54static int wlan_cmd_802_11_ps_mode(wlan_private * priv,
55				   struct cmd_ds_command *cmd,
56				   u16 cmd_action)
57{
58	struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
59	wlan_adapter *adapter = priv->adapter;
60
61	lbs_deb_enter(LBS_DEB_CMD);
62
63	cmd->command = cpu_to_le16(cmd_802_11_ps_mode);
64	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
65				S_DS_GEN);
66	psm->action = cpu_to_le16(cmd_action);
67	psm->multipledtim = 0;
68	switch (cmd_action) {
69	case cmd_subcmd_enter_ps:
70		lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
71		lbs_deb_cmd("locallisteninterval = %d\n",
72		       adapter->locallisteninterval);
73
74		psm->locallisteninterval =
75		    cpu_to_le16(adapter->locallisteninterval);
76		psm->nullpktinterval =
77		    cpu_to_le16(adapter->nullpktinterval);
78		psm->multipledtim =
79		    cpu_to_le16(priv->adapter->multipledtim);
80		break;
81
82	case cmd_subcmd_exit_ps:
83		lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
84		break;
85
86	case cmd_subcmd_sleep_confirmed:
87		lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
88		break;
89
90	default:
91		break;
92	}
93
94	lbs_deb_leave(LBS_DEB_CMD);
95	return 0;
96}
97
98static int wlan_cmd_802_11_inactivity_timeout(wlan_private * priv,
99					      struct cmd_ds_command *cmd,
100					      u16 cmd_action, void *pdata_buf)
101{
102	u16 *timeout = pdata_buf;
103
104	cmd->command = cpu_to_le16(cmd_802_11_inactivity_timeout);
105	cmd->size =
106	    cpu_to_le16(sizeof(struct cmd_ds_802_11_inactivity_timeout)
107			     + S_DS_GEN);
108
109	cmd->params.inactivity_timeout.action = cpu_to_le16(cmd_action);
110
111	if (cmd_action)
112		cmd->params.inactivity_timeout.timeout = cpu_to_le16(*timeout);
113	else
114		cmd->params.inactivity_timeout.timeout = 0;
115
116	return 0;
117}
118
119static int wlan_cmd_802_11_sleep_params(wlan_private * priv,
120					struct cmd_ds_command *cmd,
121					u16 cmd_action)
122{
123	wlan_adapter *adapter = priv->adapter;
124	struct cmd_ds_802_11_sleep_params *sp = &cmd->params.sleep_params;
125
126	lbs_deb_enter(LBS_DEB_CMD);
127
128	cmd->size = cpu_to_le16((sizeof(struct cmd_ds_802_11_sleep_params)) +
129				S_DS_GEN);
130	cmd->command = cpu_to_le16(cmd_802_11_sleep_params);
131
132	if (cmd_action == cmd_act_get) {
133		memset(&adapter->sp, 0, sizeof(struct sleep_params));
134		memset(sp, 0, sizeof(struct cmd_ds_802_11_sleep_params));
135		sp->action = cpu_to_le16(cmd_action);
136	} else if (cmd_action == cmd_act_set) {
137		sp->action = cpu_to_le16(cmd_action);
138		sp->error = cpu_to_le16(adapter->sp.sp_error);
139		sp->offset = cpu_to_le16(adapter->sp.sp_offset);
140		sp->stabletime = cpu_to_le16(adapter->sp.sp_stabletime);
141		sp->calcontrol = (u8) adapter->sp.sp_calcontrol;
142		sp->externalsleepclk = (u8) adapter->sp.sp_extsleepclk;
143		sp->reserved = cpu_to_le16(adapter->sp.sp_reserved);
144	}
145
146	lbs_deb_leave(LBS_DEB_CMD);
147	return 0;
148}
149
150static int wlan_cmd_802_11_set_wep(wlan_private * priv,
151                                   struct cmd_ds_command *cmd,
152                                   u32 cmd_act,
153                                   void * pdata_buf)
154{
155	struct cmd_ds_802_11_set_wep *wep = &cmd->params.wep;
156	wlan_adapter *adapter = priv->adapter;
157	int ret = 0;
158	struct assoc_request * assoc_req = pdata_buf;
159
160	lbs_deb_enter(LBS_DEB_CMD);
161
162	cmd->command = cpu_to_le16(cmd_802_11_set_wep);
163	cmd->size = cpu_to_le16(sizeof(*wep) + S_DS_GEN);
164
165	if (cmd_act == cmd_act_add) {
166		int i;
167
168		if (!assoc_req) {
169			lbs_deb_cmd("Invalid association request!");
170			ret = -1;
171			goto done;
172		}
173
174		wep->action = cpu_to_le16(cmd_act_add);
175
176		/* default tx key index */
177		wep->keyindex = cpu_to_le16((u16)(assoc_req->wep_tx_keyidx &
178						  (u32)cmd_WEP_KEY_INDEX_MASK));
179
180		lbs_deb_cmd("Tx key Index: %u\n", le16_to_cpu(wep->keyindex));
181
182		/* Copy key types and material to host command structure */
183		for (i = 0; i < 4; i++) {
184			struct WLAN_802_11_KEY * pkey = &assoc_req->wep_keys[i];
185
186			switch (pkey->len) {
187			case KEY_LEN_WEP_40:
188				wep->keytype[i] =
189					cpu_to_le16(cmd_type_wep_40_bit);
190				memmove(&wep->keymaterial[i], pkey->key,
191				        pkey->len);
192				break;
193			case KEY_LEN_WEP_104:
194				wep->keytype[i] =
195					cpu_to_le16(cmd_type_wep_104_bit);
196				memmove(&wep->keymaterial[i], pkey->key,
197				        pkey->len);
198				break;
199			case 0:
200				break;
201			default:
202				lbs_deb_cmd("Invalid WEP key %d length of %d\n",
203				       i, pkey->len);
204				ret = -1;
205				goto done;
206				break;
207			}
208		}
209	} else if (cmd_act == cmd_act_remove) {
210		/* ACT_REMOVE clears _all_ WEP keys */
211		wep->action = cpu_to_le16(cmd_act_remove);
212
213		/* default tx key index */
214		wep->keyindex = cpu_to_le16((u16)(adapter->wep_tx_keyidx &
215						  (u32)cmd_WEP_KEY_INDEX_MASK));
216	}
217
218	ret = 0;
219
220done:
221	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
222	return ret;
223}
224
225static int wlan_cmd_802_11_enable_rsn(wlan_private * priv,
226				      struct cmd_ds_command *cmd,
227				      u16 cmd_action,
228				      void * pdata_buf)
229{
230	struct cmd_ds_802_11_enable_rsn *penableRSN = &cmd->params.enbrsn;
231	u32 * enable = pdata_buf;
232
233	lbs_deb_enter(LBS_DEB_CMD);
234
235	cmd->command = cpu_to_le16(cmd_802_11_enable_rsn);
236	cmd->size = cpu_to_le16(sizeof(*penableRSN) + S_DS_GEN);
237	penableRSN->action = cpu_to_le16(cmd_action);
238
239	if (cmd_action == cmd_act_set) {
240		if (*enable)
241			penableRSN->enable = cpu_to_le16(cmd_enable_rsn);
242		else
243			penableRSN->enable = cpu_to_le16(cmd_enable_rsn);
244	}
245
246	lbs_deb_leave(LBS_DEB_CMD);
247	return 0;
248}
249
250
251static void set_one_wpa_key(struct MrvlIEtype_keyParamSet * pkeyparamset,
252                            struct WLAN_802_11_KEY * pkey)
253{
254	pkeyparamset->keytypeid = cpu_to_le16(pkey->type);
255
256	if (pkey->flags & KEY_INFO_WPA_ENABLED) {
257		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
258	}
259	if (pkey->flags & KEY_INFO_WPA_UNICAST) {
260		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
261	}
262	if (pkey->flags & KEY_INFO_WPA_MCAST) {
263		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
264	}
265
266	pkeyparamset->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
267	pkeyparamset->keylen = cpu_to_le16(pkey->len);
268	memcpy(pkeyparamset->key, pkey->key, pkey->len);
269	pkeyparamset->length = cpu_to_le16(  sizeof(pkeyparamset->keytypeid)
270	                                        + sizeof(pkeyparamset->keyinfo)
271	                                        + sizeof(pkeyparamset->keylen)
272	                                        + sizeof(pkeyparamset->key));
273}
274
275static int wlan_cmd_802_11_key_material(wlan_private * priv,
276					struct cmd_ds_command *cmd,
277					u16 cmd_action,
278					u32 cmd_oid, void *pdata_buf)
279{
280	struct cmd_ds_802_11_key_material *pkeymaterial =
281	    &cmd->params.keymaterial;
282	struct assoc_request * assoc_req = pdata_buf;
283	int ret = 0;
284	int index = 0;
285
286	lbs_deb_enter(LBS_DEB_CMD);
287
288	cmd->command = cpu_to_le16(cmd_802_11_key_material);
289	pkeymaterial->action = cpu_to_le16(cmd_action);
290
291	if (cmd_action == cmd_act_get) {
292		cmd->size = cpu_to_le16(S_DS_GEN + sizeof (pkeymaterial->action));
293		ret = 0;
294		goto done;
295	}
296
297	memset(&pkeymaterial->keyParamSet, 0, sizeof(pkeymaterial->keyParamSet));
298
299	if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
300		set_one_wpa_key(&pkeymaterial->keyParamSet[index],
301		                &assoc_req->wpa_unicast_key);
302		index++;
303	}
304
305	if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
306		set_one_wpa_key(&pkeymaterial->keyParamSet[index],
307		                &assoc_req->wpa_mcast_key);
308		index++;
309	}
310
311	cmd->size = cpu_to_le16(  S_DS_GEN
312	                        + sizeof (pkeymaterial->action)
313	                        + (index * sizeof(struct MrvlIEtype_keyParamSet)));
314
315	ret = 0;
316
317done:
318	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
319	return ret;
320}
321
322static int wlan_cmd_802_11_reset(wlan_private * priv,
323				 struct cmd_ds_command *cmd, int cmd_action)
324{
325	struct cmd_ds_802_11_reset *reset = &cmd->params.reset;
326
327	cmd->command = cpu_to_le16(cmd_802_11_reset);
328	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
329	reset->action = cpu_to_le16(cmd_action);
330
331	return 0;
332}
333
334static int wlan_cmd_802_11_get_log(wlan_private * priv,
335				   struct cmd_ds_command *cmd)
336{
337	cmd->command = cpu_to_le16(cmd_802_11_get_log);
338	cmd->size =
339		cpu_to_le16(sizeof(struct cmd_ds_802_11_get_log) + S_DS_GEN);
340
341	return 0;
342}
343
344static int wlan_cmd_802_11_get_stat(wlan_private * priv,
345				    struct cmd_ds_command *cmd)
346{
347	cmd->command = cpu_to_le16(cmd_802_11_get_stat);
348	cmd->size =
349	    cpu_to_le16(sizeof(struct cmd_ds_802_11_get_stat) + S_DS_GEN);
350
351	return 0;
352}
353
354static int wlan_cmd_802_11_snmp_mib(wlan_private * priv,
355				    struct cmd_ds_command *cmd,
356				    int cmd_action,
357				    int cmd_oid, void *pdata_buf)
358{
359	struct cmd_ds_802_11_snmp_mib *pSNMPMIB = &cmd->params.smib;
360	wlan_adapter *adapter = priv->adapter;
361	u8 ucTemp;
362
363	lbs_deb_enter(LBS_DEB_CMD);
364
365	lbs_deb_cmd("SNMP_CMD: cmd_oid = 0x%x\n", cmd_oid);
366
367	cmd->command = cpu_to_le16(cmd_802_11_snmp_mib);
368	cmd->size = cpu_to_le16(sizeof(*pSNMPMIB) + S_DS_GEN);
369
370	switch (cmd_oid) {
371	case OID_802_11_INFRASTRUCTURE_MODE:
372	{
373		u8 mode = (u8) (size_t) pdata_buf;
374		pSNMPMIB->querytype = cpu_to_le16(cmd_act_set);
375		pSNMPMIB->oid = cpu_to_le16((u16) desired_bsstype_i);
376		pSNMPMIB->bufsize = sizeof(u8);
377		if (mode == IW_MODE_ADHOC) {
378			ucTemp = SNMP_MIB_VALUE_ADHOC;
379		} else {
380			/* Infra and Auto modes */
381			ucTemp = SNMP_MIB_VALUE_INFRA;
382		}
383
384		memmove(pSNMPMIB->value, &ucTemp, sizeof(u8));
385
386		break;
387	}
388
389	case OID_802_11D_ENABLE:
390		{
391			u32 ulTemp;
392
393			pSNMPMIB->oid = cpu_to_le16((u16) dot11d_i);
394
395			if (cmd_action == cmd_act_set) {
396				pSNMPMIB->querytype = cmd_act_set;
397				pSNMPMIB->bufsize = sizeof(u16);
398				ulTemp = *(u32 *)pdata_buf;
399				*((__le16 *)(pSNMPMIB->value)) =
400				    cpu_to_le16((u16) ulTemp);
401			}
402			break;
403		}
404
405	case OID_802_11_FRAGMENTATION_THRESHOLD:
406		{
407			u32 ulTemp;
408
409			pSNMPMIB->oid = cpu_to_le16((u16) fragthresh_i);
410
411			if (cmd_action == cmd_act_get) {
412				pSNMPMIB->querytype = cpu_to_le16(cmd_act_get);
413			} else if (cmd_action == cmd_act_set) {
414				pSNMPMIB->querytype = cpu_to_le16(cmd_act_set);
415				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
416				ulTemp = *((u32 *) pdata_buf);
417				*((__le16 *)(pSNMPMIB->value)) =
418				    cpu_to_le16((u16) ulTemp);
419
420			}
421
422			break;
423		}
424
425	case OID_802_11_RTS_THRESHOLD:
426		{
427
428			u32 ulTemp;
429			pSNMPMIB->oid = le16_to_cpu((u16) rtsthresh_i);
430
431			if (cmd_action == cmd_act_get) {
432				pSNMPMIB->querytype = cpu_to_le16(cmd_act_get);
433			} else if (cmd_action == cmd_act_set) {
434				pSNMPMIB->querytype = cpu_to_le16(cmd_act_set);
435				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
436				ulTemp = *((u32 *)pdata_buf);
437				*(__le16 *)(pSNMPMIB->value) =
438				    cpu_to_le16((u16) ulTemp);
439
440			}
441			break;
442		}
443	case OID_802_11_TX_RETRYCOUNT:
444		pSNMPMIB->oid = cpu_to_le16((u16) short_retrylim_i);
445
446		if (cmd_action == cmd_act_get) {
447			pSNMPMIB->querytype = cpu_to_le16(cmd_act_get);
448		} else if (cmd_action == cmd_act_set) {
449			pSNMPMIB->querytype = cpu_to_le16(cmd_act_set);
450			pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
451			*((__le16 *)(pSNMPMIB->value)) =
452			    cpu_to_le16((u16) adapter->txretrycount);
453		}
454
455		break;
456	default:
457		break;
458	}
459
460	lbs_deb_cmd(
461	       "SNMP_CMD: command=0x%x, size=0x%x, seqnum=0x%x, result=0x%x\n",
462	       le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
463	       le16_to_cpu(cmd->seqnum), le16_to_cpu(cmd->result));
464
465	lbs_deb_cmd(
466	       "SNMP_CMD: action=0x%x, oid=0x%x, oidsize=0x%x, value=0x%x\n",
467	       le16_to_cpu(pSNMPMIB->querytype), le16_to_cpu(pSNMPMIB->oid),
468	       le16_to_cpu(pSNMPMIB->bufsize),
469	       le16_to_cpu(*(__le16 *) pSNMPMIB->value));
470
471	lbs_deb_leave(LBS_DEB_CMD);
472	return 0;
473}
474
475static int wlan_cmd_802_11_radio_control(wlan_private * priv,
476					 struct cmd_ds_command *cmd,
477					 int cmd_action)
478{
479	wlan_adapter *adapter = priv->adapter;
480	struct cmd_ds_802_11_radio_control *pradiocontrol = &cmd->params.radio;
481
482	lbs_deb_enter(LBS_DEB_CMD);
483
484	cmd->size =
485	    cpu_to_le16((sizeof(struct cmd_ds_802_11_radio_control)) +
486			     S_DS_GEN);
487	cmd->command = cpu_to_le16(cmd_802_11_radio_control);
488
489	pradiocontrol->action = cpu_to_le16(cmd_action);
490
491	switch (adapter->preamble) {
492	case cmd_type_short_preamble:
493		pradiocontrol->control = cpu_to_le16(SET_SHORT_PREAMBLE);
494		break;
495
496	case cmd_type_long_preamble:
497		pradiocontrol->control = cpu_to_le16(SET_LONG_PREAMBLE);
498		break;
499
500	case cmd_type_auto_preamble:
501	default:
502		pradiocontrol->control = cpu_to_le16(SET_AUTO_PREAMBLE);
503		break;
504	}
505
506	if (adapter->radioon)
507		pradiocontrol->control |= cpu_to_le16(TURN_ON_RF);
508	else
509		pradiocontrol->control &= cpu_to_le16(~TURN_ON_RF);
510
511	lbs_deb_leave(LBS_DEB_CMD);
512	return 0;
513}
514
515static int wlan_cmd_802_11_rf_tx_power(wlan_private * priv,
516				       struct cmd_ds_command *cmd,
517				       u16 cmd_action, void *pdata_buf)
518{
519
520	struct cmd_ds_802_11_rf_tx_power *prtp = &cmd->params.txp;
521
522	lbs_deb_enter(LBS_DEB_CMD);
523
524	cmd->size =
525	    cpu_to_le16((sizeof(struct cmd_ds_802_11_rf_tx_power)) + S_DS_GEN);
526	cmd->command = cpu_to_le16(cmd_802_11_rf_tx_power);
527	prtp->action = cpu_to_le16(cmd_action);
528
529	lbs_deb_cmd("RF_TX_POWER_CMD: size:%d cmd:0x%x Act:%d\n",
530		    le16_to_cpu(cmd->size), le16_to_cpu(cmd->command),
531		    le16_to_cpu(prtp->action));
532
533	switch (cmd_action) {
534	case cmd_act_tx_power_opt_get:
535		prtp->action = cpu_to_le16(cmd_act_get);
536		prtp->currentlevel = 0;
537		break;
538
539	case cmd_act_tx_power_opt_set_high:
540		prtp->action = cpu_to_le16(cmd_act_set);
541		prtp->currentlevel = cpu_to_le16(cmd_act_tx_power_index_high);
542		break;
543
544	case cmd_act_tx_power_opt_set_mid:
545		prtp->action = cpu_to_le16(cmd_act_set);
546		prtp->currentlevel = cpu_to_le16(cmd_act_tx_power_index_mid);
547		break;
548
549	case cmd_act_tx_power_opt_set_low:
550		prtp->action = cpu_to_le16(cmd_act_set);
551		prtp->currentlevel = cpu_to_le16(*((u16 *) pdata_buf));
552		break;
553	}
554
555	lbs_deb_leave(LBS_DEB_CMD);
556	return 0;
557}
558
559static int wlan_cmd_802_11_rf_antenna(wlan_private * priv,
560				      struct cmd_ds_command *cmd,
561				      u16 cmd_action, void *pdata_buf)
562{
563	struct cmd_ds_802_11_rf_antenna *rant = &cmd->params.rant;
564
565	cmd->command = cpu_to_le16(cmd_802_11_rf_antenna);
566	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rf_antenna) +
567				S_DS_GEN);
568
569	rant->action = cpu_to_le16(cmd_action);
570	if ((cmd_action == cmd_act_set_rx) || (cmd_action == cmd_act_set_tx)) {
571		rant->antennamode = cpu_to_le16((u16) (*(u32 *) pdata_buf));
572	}
573
574	return 0;
575}
576
577static int wlan_cmd_802_11_rate_adapt_rateset(wlan_private * priv,
578					      struct cmd_ds_command *cmd,
579					      u16 cmd_action)
580{
581	struct cmd_ds_802_11_rate_adapt_rateset
582	*rateadapt = &cmd->params.rateset;
583	wlan_adapter *adapter = priv->adapter;
584
585	cmd->size =
586	    cpu_to_le16(sizeof(struct cmd_ds_802_11_rate_adapt_rateset)
587			     + S_DS_GEN);
588	cmd->command = cpu_to_le16(cmd_802_11_rate_adapt_rateset);
589
590	lbs_deb_enter(LBS_DEB_CMD);
591
592	rateadapt->action = cpu_to_le16(cmd_action);
593	rateadapt->enablehwauto = cpu_to_le16(adapter->enablehwauto);
594	rateadapt->bitmap = cpu_to_le16(adapter->ratebitmap);
595
596	lbs_deb_leave(LBS_DEB_CMD);
597	return 0;
598}
599
600static int wlan_cmd_802_11_data_rate(wlan_private * priv,
601				     struct cmd_ds_command *cmd,
602				     u16 cmd_action)
603{
604	struct cmd_ds_802_11_data_rate *pdatarate = &cmd->params.drate;
605	wlan_adapter *adapter = priv->adapter;
606
607	lbs_deb_enter(LBS_DEB_CMD);
608
609	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_data_rate) +
610			     S_DS_GEN);
611
612	cmd->command = cpu_to_le16(cmd_802_11_data_rate);
613
614	memset(pdatarate, 0, sizeof(struct cmd_ds_802_11_data_rate));
615
616	pdatarate->action = cpu_to_le16(cmd_action);
617
618	if (cmd_action == cmd_act_set_tx_fix_rate) {
619		pdatarate->datarate[0] = libertas_data_rate_to_index(adapter->datarate);
620		lbs_deb_cmd("Setting FW for fixed rate 0x%02X\n",
621		       adapter->datarate);
622	} else if (cmd_action == cmd_act_set_tx_auto) {
623		lbs_deb_cmd("Setting FW for AUTO rate\n");
624	}
625
626	lbs_deb_leave(LBS_DEB_CMD);
627	return 0;
628}
629
630static int wlan_cmd_mac_multicast_adr(wlan_private * priv,
631				      struct cmd_ds_command *cmd,
632				      u16 cmd_action)
633{
634	struct cmd_ds_mac_multicast_adr *pMCastAdr = &cmd->params.madr;
635	wlan_adapter *adapter = priv->adapter;
636
637	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mac_multicast_adr) +
638			     S_DS_GEN);
639	cmd->command = cpu_to_le16(cmd_mac_multicast_adr);
640
641	pMCastAdr->action = cpu_to_le16(cmd_action);
642	pMCastAdr->nr_of_adrs =
643	    cpu_to_le16((u16) adapter->nr_of_multicastmacaddr);
644	memcpy(pMCastAdr->maclist, adapter->multicastlist,
645	       adapter->nr_of_multicastmacaddr * ETH_ALEN);
646
647	return 0;
648}
649
650static int wlan_cmd_802_11_rf_channel(wlan_private * priv,
651				      struct cmd_ds_command *cmd,
652				      int option, void *pdata_buf)
653{
654	struct cmd_ds_802_11_rf_channel *rfchan = &cmd->params.rfchannel;
655
656	cmd->command = cpu_to_le16(cmd_802_11_rf_channel);
657	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rf_channel) +
658				S_DS_GEN);
659
660	if (option == cmd_opt_802_11_rf_channel_set) {
661		rfchan->currentchannel = cpu_to_le16(*((u16 *) pdata_buf));
662	}
663
664	rfchan->action = cpu_to_le16(option);
665
666	return 0;
667}
668
669static int wlan_cmd_802_11_rssi(wlan_private * priv,
670				struct cmd_ds_command *cmd)
671{
672	wlan_adapter *adapter = priv->adapter;
673
674	cmd->command = cpu_to_le16(cmd_802_11_rssi);
675	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
676	cmd->params.rssi.N = cpu_to_le16(priv->adapter->bcn_avg_factor);
677
678	/* reset Beacon SNR/NF/RSSI values */
679	adapter->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
680	adapter->SNR[TYPE_BEACON][TYPE_AVG] = 0;
681	adapter->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
682	adapter->NF[TYPE_BEACON][TYPE_AVG] = 0;
683	adapter->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
684	adapter->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
685
686	return 0;
687}
688
689static int wlan_cmd_reg_access(wlan_private * priv,
690			       struct cmd_ds_command *cmdptr,
691			       u8 cmd_action, void *pdata_buf)
692{
693	struct wlan_offset_value *offval;
694
695	lbs_deb_enter(LBS_DEB_CMD);
696
697	offval = (struct wlan_offset_value *)pdata_buf;
698
699	switch (cmdptr->command) {
700	case cmd_mac_reg_access:
701		{
702			struct cmd_ds_mac_reg_access *macreg;
703
704			cmdptr->size =
705			    cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
706					+ S_DS_GEN);
707			macreg =
708			    (struct cmd_ds_mac_reg_access *)&cmdptr->params.
709			    macreg;
710
711			macreg->action = cpu_to_le16(cmd_action);
712			macreg->offset = cpu_to_le16((u16) offval->offset);
713			macreg->value = cpu_to_le32(offval->value);
714
715			break;
716		}
717
718	case cmd_bbp_reg_access:
719		{
720			struct cmd_ds_bbp_reg_access *bbpreg;
721
722			cmdptr->size =
723			    cpu_to_le16(sizeof
724					     (struct cmd_ds_bbp_reg_access)
725					     + S_DS_GEN);
726			bbpreg =
727			    (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
728			    bbpreg;
729
730			bbpreg->action = cpu_to_le16(cmd_action);
731			bbpreg->offset = cpu_to_le16((u16) offval->offset);
732			bbpreg->value = (u8) offval->value;
733
734			break;
735		}
736
737	case cmd_rf_reg_access:
738		{
739			struct cmd_ds_rf_reg_access *rfreg;
740
741			cmdptr->size =
742			    cpu_to_le16(sizeof
743					     (struct cmd_ds_rf_reg_access) +
744					     S_DS_GEN);
745			rfreg =
746			    (struct cmd_ds_rf_reg_access *)&cmdptr->params.
747			    rfreg;
748
749			rfreg->action = cpu_to_le16(cmd_action);
750			rfreg->offset = cpu_to_le16((u16) offval->offset);
751			rfreg->value = (u8) offval->value;
752
753			break;
754		}
755
756	default:
757		break;
758	}
759
760	lbs_deb_leave(LBS_DEB_CMD);
761	return 0;
762}
763
764static int wlan_cmd_802_11_mac_address(wlan_private * priv,
765				       struct cmd_ds_command *cmd,
766				       u16 cmd_action)
767{
768	wlan_adapter *adapter = priv->adapter;
769
770	cmd->command = cpu_to_le16(cmd_802_11_mac_address);
771	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_mac_address) +
772			     S_DS_GEN);
773	cmd->result = 0;
774
775	cmd->params.macadd.action = cpu_to_le16(cmd_action);
776
777	if (cmd_action == cmd_act_set) {
778		memcpy(cmd->params.macadd.macadd,
779		       adapter->current_addr, ETH_ALEN);
780		lbs_dbg_hex("SET_CMD: MAC ADDRESS-", adapter->current_addr, 6);
781	}
782
783	return 0;
784}
785
786static int wlan_cmd_802_11_eeprom_access(wlan_private * priv,
787					 struct cmd_ds_command *cmd,
788					 int cmd_action, void *pdata_buf)
789{
790	struct wlan_ioctl_regrdwr *ea = pdata_buf;
791
792	lbs_deb_enter(LBS_DEB_CMD);
793
794	cmd->command = cpu_to_le16(cmd_802_11_eeprom_access);
795	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_eeprom_access) +
796				S_DS_GEN);
797	cmd->result = 0;
798
799	cmd->params.rdeeprom.action = cpu_to_le16(ea->action);
800	cmd->params.rdeeprom.offset = cpu_to_le16(ea->offset);
801	cmd->params.rdeeprom.bytecount = cpu_to_le16(ea->NOB);
802	cmd->params.rdeeprom.value = 0;
803
804	return 0;
805}
806
807static int wlan_cmd_bt_access(wlan_private * priv,
808			       struct cmd_ds_command *cmd,
809			       u16 cmd_action, void *pdata_buf)
810{
811	struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
812	lbs_deb_cmd("BT CMD(%d)\n", cmd_action);
813
814	cmd->command = cpu_to_le16(cmd_bt_access);
815	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
816	cmd->result = 0;
817	bt_access->action = cpu_to_le16(cmd_action);
818
819	switch (cmd_action) {
820	case cmd_act_bt_access_add:
821		memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
822		lbs_dbg_hex("BT_ADD: blinded mac address-", bt_access->addr1, 6);
823		break;
824	case cmd_act_bt_access_del:
825		memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
826		lbs_dbg_hex("BT_DEL: blinded mac address-", bt_access->addr1, 6);
827		break;
828	case cmd_act_bt_access_list:
829		bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
830		break;
831	case cmd_act_bt_access_reset:
832		break;
833	case cmd_act_bt_access_set_invert:
834		bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
835		break;
836	case cmd_act_bt_access_get_invert:
837		break;
838	default:
839		break;
840	}
841	return 0;
842}
843
844static int wlan_cmd_fwt_access(wlan_private * priv,
845			       struct cmd_ds_command *cmd,
846			       u16 cmd_action, void *pdata_buf)
847{
848	struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
849	lbs_deb_cmd("FWT CMD(%d)\n", cmd_action);
850
851	cmd->command = cpu_to_le16(cmd_fwt_access);
852	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
853	cmd->result = 0;
854
855	if (pdata_buf)
856		memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
857	else
858		memset(fwt_access, 0, sizeof(*fwt_access));
859
860	fwt_access->action = cpu_to_le16(cmd_action);
861
862	return 0;
863}
864
865static int wlan_cmd_mesh_access(wlan_private * priv,
866				struct cmd_ds_command *cmd,
867				u16 cmd_action, void *pdata_buf)
868{
869	struct cmd_ds_mesh_access *mesh_access = &cmd->params.mesh;
870	lbs_deb_cmd("FWT CMD(%d)\n", cmd_action);
871
872	cmd->command = cpu_to_le16(cmd_mesh_access);
873	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mesh_access) + S_DS_GEN);
874	cmd->result = 0;
875
876	if (pdata_buf)
877		memcpy(mesh_access, pdata_buf, sizeof(*mesh_access));
878	else
879		memset(mesh_access, 0, sizeof(*mesh_access));
880
881	mesh_access->action = cpu_to_le16(cmd_action);
882
883	return 0;
884}
885
886void libertas_queue_cmd(wlan_adapter * adapter, struct cmd_ctrl_node *cmdnode, u8 addtail)
887{
888	unsigned long flags;
889	struct cmd_ds_command *cmdptr;
890
891	lbs_deb_enter(LBS_DEB_CMD);
892
893	if (!cmdnode) {
894		lbs_deb_cmd("QUEUE_CMD: cmdnode is NULL\n");
895		goto done;
896	}
897
898	cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
899	if (!cmdptr) {
900		lbs_deb_cmd("QUEUE_CMD: cmdptr is NULL\n");
901		goto done;
902	}
903
904	/* Exit_PS command needs to be queued in the header always. */
905	if (cmdptr->command == cmd_802_11_ps_mode) {
906		struct cmd_ds_802_11_ps_mode *psm = &cmdptr->params.psmode;
907		if (psm->action == cpu_to_le16(cmd_subcmd_exit_ps)) {
908			if (adapter->psstate != PS_STATE_FULL_POWER)
909				addtail = 0;
910		}
911	}
912
913	spin_lock_irqsave(&adapter->driver_lock, flags);
914
915	if (addtail)
916		list_add_tail((struct list_head *)cmdnode,
917			      &adapter->cmdpendingq);
918	else
919		list_add((struct list_head *)cmdnode, &adapter->cmdpendingq);
920
921	spin_unlock_irqrestore(&adapter->driver_lock, flags);
922
923	lbs_deb_cmd("QUEUE_CMD: Inserted node=%p, cmd=0x%x in cmdpendingq\n",
924	       cmdnode,
925	       le16_to_cpu(((struct cmd_ds_gen*)cmdnode->bufvirtualaddr)->command));
926
927done:
928	lbs_deb_leave(LBS_DEB_CMD);
929}
930
931static int DownloadcommandToStation(wlan_private * priv,
932				    struct cmd_ctrl_node *cmdnode)
933{
934	unsigned long flags;
935	struct cmd_ds_command *cmdptr;
936	wlan_adapter *adapter = priv->adapter;
937	int ret = 0;
938	u16 cmdsize;
939	u16 command;
940
941	lbs_deb_enter(LBS_DEB_CMD);
942
943	if (!adapter || !cmdnode) {
944		lbs_deb_cmd("DNLD_CMD: adapter = %p, cmdnode = %p\n",
945		       adapter, cmdnode);
946		if (cmdnode) {
947			spin_lock_irqsave(&adapter->driver_lock, flags);
948			__libertas_cleanup_and_insert_cmd(priv, cmdnode);
949			spin_unlock_irqrestore(&adapter->driver_lock, flags);
950		}
951		ret = -1;
952		goto done;
953	}
954
955	cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
956
957
958	spin_lock_irqsave(&adapter->driver_lock, flags);
959	if (!cmdptr || !cmdptr->size) {
960		lbs_deb_cmd("DNLD_CMD: cmdptr is Null or cmd size is Zero, "
961		       "Not sending\n");
962		__libertas_cleanup_and_insert_cmd(priv, cmdnode);
963		spin_unlock_irqrestore(&adapter->driver_lock, flags);
964		ret = -1;
965		goto done;
966	}
967
968	adapter->cur_cmd = cmdnode;
969	adapter->cur_cmd_retcode = 0;
970	spin_unlock_irqrestore(&adapter->driver_lock, flags);
971	lbs_deb_cmd("DNLD_CMD:: Before download, size of cmd = %d\n",
972		    le16_to_cpu(cmdptr->size));
973
974	cmdsize = cmdptr->size;
975
976	command = cpu_to_le16(cmdptr->command);
977
978	cmdnode->cmdwaitqwoken = 0;
979	cmdsize = cpu_to_le16(cmdsize);
980
981	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmdptr, cmdsize);
982
983	if (ret != 0) {
984		lbs_deb_cmd("DNLD_CMD: Host to Card failed\n");
985		spin_lock_irqsave(&adapter->driver_lock, flags);
986		__libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
987		adapter->cur_cmd = NULL;
988		spin_unlock_irqrestore(&adapter->driver_lock, flags);
989		ret = -1;
990		goto done;
991	}
992
993	lbs_deb_cmd("DNLD_CMD: Sent command 0x%x @ %lu\n", command, jiffies);
994	lbs_dbg_hex("DNLD_CMD: command", cmdnode->bufvirtualaddr, cmdsize);
995
996	/* Setup the timer after transmit command */
997	if (command == cmd_802_11_scan || command == cmd_802_11_authenticate
998	    || command == cmd_802_11_associate)
999		mod_timer(&adapter->command_timer, jiffies + (10*HZ));
1000	else
1001		mod_timer(&adapter->command_timer, jiffies + (5*HZ));
1002
1003	ret = 0;
1004
1005done:
1006	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1007	return ret;
1008}
1009
1010static int wlan_cmd_mac_control(wlan_private * priv,
1011				struct cmd_ds_command *cmd)
1012{
1013	struct cmd_ds_mac_control *mac = &cmd->params.macctrl;
1014
1015	lbs_deb_enter(LBS_DEB_CMD);
1016
1017	cmd->command = cpu_to_le16(cmd_mac_control);
1018	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mac_control) + S_DS_GEN);
1019	mac->action = cpu_to_le16(priv->adapter->currentpacketfilter);
1020
1021	lbs_deb_cmd("wlan_cmd_mac_control(): action=0x%X size=%d\n",
1022		    le16_to_cpu(mac->action), le16_to_cpu(cmd->size));
1023
1024	lbs_deb_leave(LBS_DEB_CMD);
1025	return 0;
1026}
1027
1028/**
1029 *  This function inserts command node to cmdfreeq
1030 *  after cleans it. Requires adapter->driver_lock held.
1031 */
1032void __libertas_cleanup_and_insert_cmd(wlan_private * priv, struct cmd_ctrl_node *ptempcmd)
1033{
1034	wlan_adapter *adapter = priv->adapter;
1035
1036	if (!ptempcmd)
1037		goto done;
1038
1039	cleanup_cmdnode(ptempcmd);
1040	list_add_tail((struct list_head *)ptempcmd, &adapter->cmdfreeq);
1041done:
1042	return;
1043}
1044
1045void libertas_cleanup_and_insert_cmd(wlan_private * priv, struct cmd_ctrl_node *ptempcmd)
1046{
1047	unsigned long flags;
1048
1049	spin_lock_irqsave(&priv->adapter->driver_lock, flags);
1050	__libertas_cleanup_and_insert_cmd(priv, ptempcmd);
1051	spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
1052}
1053
1054int libertas_set_radio_control(wlan_private * priv)
1055{
1056	int ret = 0;
1057
1058	lbs_deb_enter(LBS_DEB_CMD);
1059
1060	ret = libertas_prepare_and_send_command(priv,
1061				    cmd_802_11_radio_control,
1062				    cmd_act_set,
1063				    cmd_option_waitforrsp, 0, NULL);
1064
1065	lbs_deb_cmd("RADIO_SET: on or off: 0x%X, preamble = 0x%X\n",
1066	       priv->adapter->radioon, priv->adapter->preamble);
1067
1068	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1069	return ret;
1070}
1071
1072int libertas_set_mac_packet_filter(wlan_private * priv)
1073{
1074	int ret = 0;
1075
1076	lbs_deb_enter(LBS_DEB_CMD);
1077
1078	lbs_deb_cmd("libertas_set_mac_packet_filter value = %x\n",
1079	       priv->adapter->currentpacketfilter);
1080
1081	/* Send MAC control command to station */
1082	ret = libertas_prepare_and_send_command(priv,
1083				    cmd_mac_control, 0, 0, 0, NULL);
1084
1085	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1086	return ret;
1087}
1088
1089/**
1090 *  @brief This function prepare the command before send to firmware.
1091 *
1092 *  @param priv		A pointer to wlan_private structure
1093 *  @param cmd_no	command number
1094 *  @param cmd_action	command action: GET or SET
1095 *  @param wait_option	wait option: wait response or not
1096 *  @param cmd_oid	cmd oid: treated as sub command
1097 *  @param pdata_buf	A pointer to informaion buffer
1098 *  @return 		0 or -1
1099 */
1100int libertas_prepare_and_send_command(wlan_private * priv,
1101			  u16 cmd_no,
1102			  u16 cmd_action,
1103			  u16 wait_option, u32 cmd_oid, void *pdata_buf)
1104{
1105	int ret = 0;
1106	wlan_adapter *adapter = priv->adapter;
1107	struct cmd_ctrl_node *cmdnode;
1108	struct cmd_ds_command *cmdptr;
1109	unsigned long flags;
1110
1111	lbs_deb_enter(LBS_DEB_CMD);
1112
1113	if (!adapter) {
1114		lbs_deb_cmd("PREP_CMD: adapter is Null\n");
1115		ret = -1;
1116		goto done;
1117	}
1118
1119	if (adapter->surpriseremoved) {
1120		lbs_deb_cmd("PREP_CMD: Card is Removed\n");
1121		ret = -1;
1122		goto done;
1123	}
1124
1125	cmdnode = libertas_get_free_cmd_ctrl_node(priv);
1126
1127	if (cmdnode == NULL) {
1128		lbs_deb_cmd("PREP_CMD: No free cmdnode\n");
1129
1130		/* Wake up main thread to execute next command */
1131		wake_up_interruptible(&priv->mainthread.waitq);
1132		ret = -1;
1133		goto done;
1134	}
1135
1136	libertas_set_cmd_ctrl_node(priv, cmdnode, cmd_oid, wait_option, pdata_buf);
1137
1138	cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
1139
1140	lbs_deb_cmd("PREP_CMD: Val of cmd ptr=%p, command=0x%X\n",
1141	       cmdptr, cmd_no);
1142
1143	if (!cmdptr) {
1144		lbs_deb_cmd("PREP_CMD: bufvirtualaddr of cmdnode is NULL\n");
1145		libertas_cleanup_and_insert_cmd(priv, cmdnode);
1146		ret = -1;
1147		goto done;
1148	}
1149
1150	/* Set sequence number, command and INT option */
1151	adapter->seqnum++;
1152	cmdptr->seqnum = cpu_to_le16(adapter->seqnum);
1153
1154	cmdptr->command = cpu_to_le16(cmd_no);
1155	cmdptr->result = 0;
1156
1157	switch (cmd_no) {
1158	case cmd_get_hw_spec:
1159		ret = wlan_cmd_hw_spec(priv, cmdptr);
1160		break;
1161	case cmd_802_11_ps_mode:
1162		ret = wlan_cmd_802_11_ps_mode(priv, cmdptr, cmd_action);
1163		break;
1164
1165	case cmd_802_11_scan:
1166		ret = libertas_cmd_80211_scan(priv, cmdptr, pdata_buf);
1167		break;
1168
1169	case cmd_mac_control:
1170		ret = wlan_cmd_mac_control(priv, cmdptr);
1171		break;
1172
1173	case cmd_802_11_associate:
1174	case cmd_802_11_reassociate:
1175		ret = libertas_cmd_80211_associate(priv, cmdptr, pdata_buf);
1176		break;
1177
1178	case cmd_802_11_deauthenticate:
1179		ret = libertas_cmd_80211_deauthenticate(priv, cmdptr);
1180		break;
1181
1182	case cmd_802_11_set_wep:
1183		ret = wlan_cmd_802_11_set_wep(priv, cmdptr, cmd_action, pdata_buf);
1184		break;
1185
1186	case cmd_802_11_ad_hoc_start:
1187		ret = libertas_cmd_80211_ad_hoc_start(priv, cmdptr, pdata_buf);
1188		break;
1189	case cmd_code_dnld:
1190		break;
1191
1192	case cmd_802_11_reset:
1193		ret = wlan_cmd_802_11_reset(priv, cmdptr, cmd_action);
1194		break;
1195
1196	case cmd_802_11_get_log:
1197		ret = wlan_cmd_802_11_get_log(priv, cmdptr);
1198		break;
1199
1200	case cmd_802_11_authenticate:
1201		ret = libertas_cmd_80211_authenticate(priv, cmdptr, pdata_buf);
1202		break;
1203
1204	case cmd_802_11_get_stat:
1205		ret = wlan_cmd_802_11_get_stat(priv, cmdptr);
1206		break;
1207
1208	case cmd_802_11_snmp_mib:
1209		ret = wlan_cmd_802_11_snmp_mib(priv, cmdptr,
1210					       cmd_action, cmd_oid, pdata_buf);
1211		break;
1212
1213	case cmd_mac_reg_access:
1214	case cmd_bbp_reg_access:
1215	case cmd_rf_reg_access:
1216		ret = wlan_cmd_reg_access(priv, cmdptr, cmd_action, pdata_buf);
1217		break;
1218
1219	case cmd_802_11_rf_channel:
1220		ret = wlan_cmd_802_11_rf_channel(priv, cmdptr,
1221						 cmd_action, pdata_buf);
1222		break;
1223
1224	case cmd_802_11_rf_tx_power:
1225		ret = wlan_cmd_802_11_rf_tx_power(priv, cmdptr,
1226						  cmd_action, pdata_buf);
1227		break;
1228
1229	case cmd_802_11_radio_control:
1230		ret = wlan_cmd_802_11_radio_control(priv, cmdptr, cmd_action);
1231		break;
1232
1233	case cmd_802_11_rf_antenna:
1234		ret = wlan_cmd_802_11_rf_antenna(priv, cmdptr,
1235						 cmd_action, pdata_buf);
1236		break;
1237
1238	case cmd_802_11_data_rate:
1239		ret = wlan_cmd_802_11_data_rate(priv, cmdptr, cmd_action);
1240		break;
1241	case cmd_802_11_rate_adapt_rateset:
1242		ret = wlan_cmd_802_11_rate_adapt_rateset(priv,
1243							 cmdptr, cmd_action);
1244		break;
1245
1246	case cmd_mac_multicast_adr:
1247		ret = wlan_cmd_mac_multicast_adr(priv, cmdptr, cmd_action);
1248		break;
1249
1250	case cmd_802_11_ad_hoc_join:
1251		ret = libertas_cmd_80211_ad_hoc_join(priv, cmdptr, pdata_buf);
1252		break;
1253
1254	case cmd_802_11_rssi:
1255		ret = wlan_cmd_802_11_rssi(priv, cmdptr);
1256		break;
1257
1258	case cmd_802_11_ad_hoc_stop:
1259		ret = libertas_cmd_80211_ad_hoc_stop(priv, cmdptr);
1260		break;
1261
1262	case cmd_802_11_enable_rsn:
1263		ret = wlan_cmd_802_11_enable_rsn(priv, cmdptr, cmd_action,
1264				pdata_buf);
1265		break;
1266
1267	case cmd_802_11_key_material:
1268		ret = wlan_cmd_802_11_key_material(priv, cmdptr, cmd_action,
1269				cmd_oid, pdata_buf);
1270		break;
1271
1272	case cmd_802_11_pairwise_tsc:
1273		break;
1274	case cmd_802_11_group_tsc:
1275		break;
1276
1277	case cmd_802_11_mac_address:
1278		ret = wlan_cmd_802_11_mac_address(priv, cmdptr, cmd_action);
1279		break;
1280
1281	case cmd_802_11_eeprom_access:
1282		ret = wlan_cmd_802_11_eeprom_access(priv, cmdptr,
1283						    cmd_action, pdata_buf);
1284		break;
1285
1286	case cmd_802_11_set_afc:
1287	case cmd_802_11_get_afc:
1288
1289		cmdptr->command = cpu_to_le16(cmd_no);
1290		cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1291					   S_DS_GEN);
1292
1293		memmove(&cmdptr->params.afc,
1294			pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1295
1296		ret = 0;
1297		goto done;
1298
1299	case cmd_802_11d_domain_info:
1300		ret = libertas_cmd_802_11d_domain_info(priv, cmdptr,
1301						   cmd_no, cmd_action);
1302		break;
1303
1304	case cmd_802_11_sleep_params:
1305		ret = wlan_cmd_802_11_sleep_params(priv, cmdptr, cmd_action);
1306		break;
1307	case cmd_802_11_inactivity_timeout:
1308		ret = wlan_cmd_802_11_inactivity_timeout(priv, cmdptr,
1309							 cmd_action, pdata_buf);
1310		libertas_set_cmd_ctrl_node(priv, cmdnode, 0, 0, pdata_buf);
1311		break;
1312
1313	case cmd_802_11_tpc_cfg:
1314		cmdptr->command = cpu_to_le16(cmd_802_11_tpc_cfg);
1315		cmdptr->size =
1316		    cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1317				     S_DS_GEN);
1318
1319		memmove(&cmdptr->params.tpccfg,
1320			pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1321
1322		ret = 0;
1323		break;
1324	case cmd_802_11_led_gpio_ctrl:
1325		{
1326			struct mrvlietypes_ledgpio *gpio =
1327			    (struct mrvlietypes_ledgpio*)
1328			    cmdptr->params.ledgpio.data;
1329
1330			memmove(&cmdptr->params.ledgpio,
1331				pdata_buf,
1332				sizeof(struct cmd_ds_802_11_led_ctrl));
1333
1334			cmdptr->command =
1335			    cpu_to_le16(cmd_802_11_led_gpio_ctrl);
1336
1337#define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
1338			cmdptr->size =
1339			    cpu_to_le16(gpio->header.len + S_DS_GEN +
1340					     ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
1341			gpio->header.len = cpu_to_le16(gpio->header.len);
1342
1343			ret = 0;
1344			break;
1345		}
1346	case cmd_802_11_pwr_cfg:
1347		cmdptr->command = cpu_to_le16(cmd_802_11_pwr_cfg);
1348		cmdptr->size =
1349		    cpu_to_le16(sizeof(struct cmd_ds_802_11_pwr_cfg) +
1350				     S_DS_GEN);
1351		memmove(&cmdptr->params.pwrcfg, pdata_buf,
1352			sizeof(struct cmd_ds_802_11_pwr_cfg));
1353
1354		ret = 0;
1355		break;
1356	case cmd_bt_access:
1357		ret = wlan_cmd_bt_access(priv, cmdptr, cmd_action, pdata_buf);
1358		break;
1359
1360	case cmd_fwt_access:
1361		ret = wlan_cmd_fwt_access(priv, cmdptr, cmd_action, pdata_buf);
1362		break;
1363
1364	case cmd_mesh_access:
1365		ret = wlan_cmd_mesh_access(priv, cmdptr, cmd_action, pdata_buf);
1366		break;
1367
1368	case cmd_get_tsf:
1369		cmdptr->command = cpu_to_le16(cmd_get_tsf);
1370		cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
1371					   S_DS_GEN);
1372		ret = 0;
1373		break;
1374	case cmd_802_11_tx_rate_query:
1375		cmdptr->command = cpu_to_le16(cmd_802_11_tx_rate_query);
1376		cmdptr->size = cpu_to_le16(sizeof(struct cmd_tx_rate_query) +
1377					   S_DS_GEN);
1378		adapter->txrate = 0;
1379		ret = 0;
1380		break;
1381	default:
1382		lbs_deb_cmd("PREP_CMD: unknown command- %#x\n", cmd_no);
1383		ret = -1;
1384		break;
1385	}
1386
1387	/* return error, since the command preparation failed */
1388	if (ret != 0) {
1389		lbs_deb_cmd("PREP_CMD: command preparation failed\n");
1390		libertas_cleanup_and_insert_cmd(priv, cmdnode);
1391		ret = -1;
1392		goto done;
1393	}
1394
1395	cmdnode->cmdwaitqwoken = 0;
1396
1397	libertas_queue_cmd(adapter, cmdnode, 1);
1398	adapter->nr_cmd_pending++;
1399	wake_up_interruptible(&priv->mainthread.waitq);
1400
1401	if (wait_option & cmd_option_waitforrsp) {
1402		lbs_deb_cmd("PREP_CMD: Wait for CMD response\n");
1403		might_sleep();
1404		wait_event_interruptible(cmdnode->cmdwait_q,
1405					 cmdnode->cmdwaitqwoken);
1406	}
1407
1408	spin_lock_irqsave(&adapter->driver_lock, flags);
1409	if (adapter->cur_cmd_retcode) {
1410		lbs_deb_cmd("PREP_CMD: command failed with return code=%d\n",
1411		       adapter->cur_cmd_retcode);
1412		adapter->cur_cmd_retcode = 0;
1413		ret = -1;
1414	}
1415	spin_unlock_irqrestore(&adapter->driver_lock, flags);
1416
1417done:
1418	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1419	return ret;
1420}
1421EXPORT_SYMBOL_GPL(libertas_prepare_and_send_command);
1422
1423/**
1424 *  @brief This function allocates the command buffer and link
1425 *  it to command free queue.
1426 *
1427 *  @param priv		A pointer to wlan_private structure
1428 *  @return 		0 or -1
1429 */
1430int libertas_allocate_cmd_buffer(wlan_private * priv)
1431{
1432	int ret = 0;
1433	u32 ulbufsize;
1434	u32 i;
1435	struct cmd_ctrl_node *tempcmd_array;
1436	u8 *ptempvirtualaddr;
1437	wlan_adapter *adapter = priv->adapter;
1438
1439	lbs_deb_enter(LBS_DEB_CMD);
1440
1441	/* Allocate and initialize cmdCtrlNode */
1442	ulbufsize = sizeof(struct cmd_ctrl_node) * MRVDRV_NUM_OF_CMD_BUFFER;
1443
1444	if (!(tempcmd_array = kzalloc(ulbufsize, GFP_KERNEL))) {
1445		lbs_deb_cmd(
1446		       "ALLOC_CMD_BUF: failed to allocate tempcmd_array\n");
1447		ret = -1;
1448		goto done;
1449	}
1450	adapter->cmd_array = tempcmd_array;
1451
1452	/* Allocate and initialize command buffers */
1453	ulbufsize = MRVDRV_SIZE_OF_CMD_BUFFER;
1454	for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1455		if (!(ptempvirtualaddr = kzalloc(ulbufsize, GFP_KERNEL))) {
1456			lbs_deb_cmd(
1457			       "ALLOC_CMD_BUF: ptempvirtualaddr: out of memory\n");
1458			ret = -1;
1459			goto done;
1460		}
1461
1462		/* Update command buffer virtual */
1463		tempcmd_array[i].bufvirtualaddr = ptempvirtualaddr;
1464	}
1465
1466	for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1467		init_waitqueue_head(&tempcmd_array[i].cmdwait_q);
1468		libertas_cleanup_and_insert_cmd(priv, &tempcmd_array[i]);
1469	}
1470
1471	ret = 0;
1472
1473done:
1474	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1475	return ret;
1476}
1477
1478/**
1479 *  @brief This function frees the command buffer.
1480 *
1481 *  @param priv		A pointer to wlan_private structure
1482 *  @return 		0 or -1
1483 */
1484int libertas_free_cmd_buffer(wlan_private * priv)
1485{
1486	u32 ulbufsize; /* Someone needs to die for this. Slowly and painfully */
1487	unsigned int i;
1488	struct cmd_ctrl_node *tempcmd_array;
1489	wlan_adapter *adapter = priv->adapter;
1490
1491	lbs_deb_enter(LBS_DEB_CMD);
1492
1493	/* need to check if cmd array is allocated or not */
1494	if (adapter->cmd_array == NULL) {
1495		lbs_deb_cmd("FREE_CMD_BUF: cmd_array is Null\n");
1496		goto done;
1497	}
1498
1499	tempcmd_array = adapter->cmd_array;
1500
1501	/* Release shared memory buffers */
1502	ulbufsize = MRVDRV_SIZE_OF_CMD_BUFFER;
1503	for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1504		if (tempcmd_array[i].bufvirtualaddr) {
1505			lbs_deb_cmd("Free all the array\n");
1506			kfree(tempcmd_array[i].bufvirtualaddr);
1507			tempcmd_array[i].bufvirtualaddr = NULL;
1508		}
1509	}
1510
1511	/* Release cmd_ctrl_node */
1512	if (adapter->cmd_array) {
1513		lbs_deb_cmd("Free cmd_array\n");
1514		kfree(adapter->cmd_array);
1515		adapter->cmd_array = NULL;
1516	}
1517
1518done:
1519	lbs_deb_leave(LBS_DEB_CMD);
1520	return 0;
1521}
1522
1523/**
1524 *  @brief This function gets a free command node if available in
1525 *  command free queue.
1526 *
1527 *  @param priv		A pointer to wlan_private structure
1528 *  @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1529 */
1530struct cmd_ctrl_node *libertas_get_free_cmd_ctrl_node(wlan_private * priv)
1531{
1532	struct cmd_ctrl_node *tempnode;
1533	wlan_adapter *adapter = priv->adapter;
1534	unsigned long flags;
1535
1536	if (!adapter)
1537		return NULL;
1538
1539	spin_lock_irqsave(&adapter->driver_lock, flags);
1540
1541	if (!list_empty(&adapter->cmdfreeq)) {
1542		tempnode = (struct cmd_ctrl_node *)adapter->cmdfreeq.next;
1543		list_del((struct list_head *)tempnode);
1544	} else {
1545		lbs_deb_cmd("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1546		tempnode = NULL;
1547	}
1548
1549	spin_unlock_irqrestore(&adapter->driver_lock, flags);
1550
1551	if (tempnode) {
1552		/*
1553		lbs_pr_debug(3, "GET_CMD_NODE: cmdCtrlNode available\n");
1554		lbs_pr_debug(3, "GET_CMD_NODE: cmdCtrlNode Address = %p\n",
1555		       tempnode);
1556		*/
1557		cleanup_cmdnode(tempnode);
1558	}
1559
1560	return tempnode;
1561}
1562
1563/**
1564 *  @brief This function cleans command node.
1565 *
1566 *  @param ptempnode	A pointer to cmdCtrlNode structure
1567 *  @return 		n/a
1568 */
1569static void cleanup_cmdnode(struct cmd_ctrl_node *ptempnode)
1570{
1571	if (!ptempnode)
1572		return;
1573	ptempnode->cmdwaitqwoken = 1;
1574	wake_up_interruptible(&ptempnode->cmdwait_q);
1575	ptempnode->status = 0;
1576	ptempnode->cmd_oid = (u32) 0;
1577	ptempnode->wait_option = 0;
1578	ptempnode->pdata_buf = NULL;
1579
1580	if (ptempnode->bufvirtualaddr != NULL)
1581		memset(ptempnode->bufvirtualaddr, 0, MRVDRV_SIZE_OF_CMD_BUFFER);
1582	return;
1583}
1584
1585/**
1586 *  @brief This function initializes the command node.
1587 *
1588 *  @param priv		A pointer to wlan_private structure
1589 *  @param ptempnode	A pointer to cmd_ctrl_node structure
1590 *  @param cmd_oid	cmd oid: treated as sub command
1591 *  @param wait_option	wait option: wait response or not
1592 *  @param pdata_buf	A pointer to informaion buffer
1593 *  @return 		0 or -1
1594 */
1595void libertas_set_cmd_ctrl_node(wlan_private * priv,
1596		    struct cmd_ctrl_node *ptempnode,
1597		    u32 cmd_oid, u16 wait_option, void *pdata_buf)
1598{
1599	lbs_deb_enter(LBS_DEB_CMD);
1600
1601	if (!ptempnode)
1602		return;
1603
1604	ptempnode->cmd_oid = cmd_oid;
1605	ptempnode->wait_option = wait_option;
1606	ptempnode->pdata_buf = pdata_buf;
1607
1608	lbs_deb_leave(LBS_DEB_CMD);
1609}
1610
1611/**
1612 *  @brief This function executes next command in command
1613 *  pending queue. It will put fimware back to PS mode
1614 *  if applicable.
1615 *
1616 *  @param priv     A pointer to wlan_private structure
1617 *  @return 	   0 or -1
1618 */
1619int libertas_execute_next_command(wlan_private * priv)
1620{
1621	wlan_adapter *adapter = priv->adapter;
1622	struct cmd_ctrl_node *cmdnode = NULL;
1623	struct cmd_ds_command *cmdptr;
1624	unsigned long flags;
1625	int ret = 0;
1626
1627	lbs_deb_enter(LBS_DEB_CMD);
1628
1629	spin_lock_irqsave(&adapter->driver_lock, flags);
1630
1631	if (adapter->cur_cmd) {
1632		lbs_pr_alert( "EXEC_NEXT_CMD: there is command in processing!\n");
1633		spin_unlock_irqrestore(&adapter->driver_lock, flags);
1634		ret = -1;
1635		goto done;
1636	}
1637
1638	if (!list_empty(&adapter->cmdpendingq)) {
1639		cmdnode = (struct cmd_ctrl_node *)
1640		    adapter->cmdpendingq.next;
1641	}
1642
1643	spin_unlock_irqrestore(&adapter->driver_lock, flags);
1644
1645	if (cmdnode) {
1646		lbs_deb_cmd(
1647		       "EXEC_NEXT_CMD: Got next command from cmdpendingq\n");
1648		cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
1649
1650		if (is_command_allowed_in_ps(cmdptr->command)) {
1651			if ((adapter->psstate == PS_STATE_SLEEP) ||
1652			    (adapter->psstate == PS_STATE_PRE_SLEEP)) {
1653				lbs_deb_cmd(
1654				       "EXEC_NEXT_CMD: Cannot send cmd 0x%x in psstate %d\n",
1655				       le16_to_cpu(cmdptr->command),
1656				       adapter->psstate);
1657				ret = -1;
1658				goto done;
1659			}
1660			lbs_deb_cmd("EXEC_NEXT_CMD: OK to send command "
1661			       "0x%x in psstate %d\n",
1662				    le16_to_cpu(cmdptr->command),
1663				    adapter->psstate);
1664		} else if (adapter->psstate != PS_STATE_FULL_POWER) {
1665			/*
1666			 * 1. Non-PS command:
1667			 * Queue it. set needtowakeup to TRUE if current state
1668			 * is SLEEP, otherwise call libertas_ps_wakeup to send Exit_PS.
1669			 * 2. PS command but not Exit_PS:
1670			 * Ignore it.
1671			 * 3. PS command Exit_PS:
1672			 * Set needtowakeup to TRUE if current state is SLEEP,
1673			 * otherwise send this command down to firmware
1674			 * immediately.
1675			 */
1676			if (cmdptr->command !=
1677			    cpu_to_le16(cmd_802_11_ps_mode)) {
1678				/*  Prepare to send Exit PS,
1679				 *  this non PS command will be sent later */
1680				if ((adapter->psstate == PS_STATE_SLEEP)
1681				    || (adapter->psstate == PS_STATE_PRE_SLEEP)
1682				    ) {
1683					/* w/ new scheme, it will not reach here.
1684					   since it is blocked in main_thread. */
1685					adapter->needtowakeup = 1;
1686				} else
1687					libertas_ps_wakeup(priv, 0);
1688
1689				ret = 0;
1690				goto done;
1691			} else {
1692				/*
1693				 * PS command. Ignore it if it is not Exit_PS.
1694				 * otherwise send it down immediately.
1695				 */
1696				struct cmd_ds_802_11_ps_mode *psm =
1697				    &cmdptr->params.psmode;
1698
1699				lbs_deb_cmd(
1700				       "EXEC_NEXT_CMD: PS cmd- action=0x%x\n",
1701				       psm->action);
1702				if (psm->action !=
1703				    cpu_to_le16(cmd_subcmd_exit_ps)) {
1704					lbs_deb_cmd(
1705					       "EXEC_NEXT_CMD: Ignore Enter PS cmd\n");
1706					list_del((struct list_head *)cmdnode);
1707					libertas_cleanup_and_insert_cmd(priv, cmdnode);
1708
1709					ret = 0;
1710					goto done;
1711				}
1712
1713				if ((adapter->psstate == PS_STATE_SLEEP) ||
1714				    (adapter->psstate == PS_STATE_PRE_SLEEP)) {
1715					lbs_deb_cmd(
1716					       "EXEC_NEXT_CMD: Ignore ExitPS cmd in sleep\n");
1717					list_del((struct list_head *)cmdnode);
1718					libertas_cleanup_and_insert_cmd(priv, cmdnode);
1719					adapter->needtowakeup = 1;
1720
1721					ret = 0;
1722					goto done;
1723				}
1724
1725				lbs_deb_cmd(
1726				       "EXEC_NEXT_CMD: Sending Exit_PS down...\n");
1727			}
1728		}
1729		list_del((struct list_head *)cmdnode);
1730		lbs_deb_cmd("EXEC_NEXT_CMD: Sending 0x%04X command\n",
1731			    le16_to_cpu(cmdptr->command));
1732		DownloadcommandToStation(priv, cmdnode);
1733	} else {
1734		/*
1735		 * check if in power save mode, if yes, put the device back
1736		 * to PS mode
1737		 */
1738		if ((adapter->psmode != wlan802_11powermodecam) &&
1739		    (adapter->psstate == PS_STATE_FULL_POWER) &&
1740		    (adapter->connect_status == libertas_connected)) {
1741			if (adapter->secinfo.WPAenabled ||
1742			    adapter->secinfo.WPA2enabled) {
1743				/* check for valid WPA group keys */
1744				if (adapter->wpa_mcast_key.len ||
1745				    adapter->wpa_unicast_key.len) {
1746					lbs_deb_cmd(
1747					       "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1748					       " go back to PS_SLEEP");
1749					libertas_ps_sleep(priv, 0);
1750				}
1751			} else {
1752				lbs_deb_cmd(
1753				       "EXEC_NEXT_CMD: command PendQ is empty,"
1754				       " go back to PS_SLEEP");
1755				libertas_ps_sleep(priv, 0);
1756			}
1757		}
1758	}
1759
1760	ret = 0;
1761done:
1762	lbs_deb_leave(LBS_DEB_CMD);
1763	return ret;
1764}
1765
1766void libertas_send_iwevcustom_event(wlan_private * priv, s8 * str)
1767{
1768	union iwreq_data iwrq;
1769	u8 buf[50];
1770
1771	lbs_deb_enter(LBS_DEB_CMD);
1772
1773	memset(&iwrq, 0, sizeof(union iwreq_data));
1774	memset(buf, 0, sizeof(buf));
1775
1776	snprintf(buf, sizeof(buf) - 1, "%s", str);
1777
1778	iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;
1779
1780	/* Send Event to upper layer */
1781	lbs_deb_cmd("Event Indication string = %s\n", (char *)buf);
1782	lbs_deb_cmd("Event Indication String length = %d\n", iwrq.data.length);
1783
1784	lbs_deb_cmd("Sending wireless event IWEVCUSTOM for %s\n", str);
1785	wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1786
1787	lbs_deb_leave(LBS_DEB_CMD);
1788}
1789
1790static int sendconfirmsleep(wlan_private * priv, u8 * cmdptr, u16 size)
1791{
1792	unsigned long flags;
1793	wlan_adapter *adapter = priv->adapter;
1794	int ret = 0;
1795
1796	lbs_deb_enter(LBS_DEB_CMD);
1797
1798	lbs_deb_cmd("SEND_SLEEPC_CMD: Before download, size of cmd = %d\n",
1799	       size);
1800
1801	lbs_dbg_hex("SEND_SLEEPC_CMD: Sleep confirm command", cmdptr, size);
1802
1803	ret = priv->hw_host_to_card(priv, MVMS_CMD, cmdptr, size);
1804	priv->dnld_sent = DNLD_RES_RECEIVED;
1805
1806	spin_lock_irqsave(&adapter->driver_lock, flags);
1807	if (adapter->intcounter || adapter->currenttxskb)
1808		lbs_deb_cmd("SEND_SLEEPC_CMD: intcounter=%d currenttxskb=%p\n",
1809		       adapter->intcounter, adapter->currenttxskb);
1810	spin_unlock_irqrestore(&adapter->driver_lock, flags);
1811
1812	if (ret) {
1813		lbs_pr_alert(
1814		       "SEND_SLEEPC_CMD: Host to Card failed for Confirm Sleep\n");
1815	} else {
1816		spin_lock_irqsave(&adapter->driver_lock, flags);
1817		if (!adapter->intcounter) {
1818			adapter->psstate = PS_STATE_SLEEP;
1819		} else {
1820			lbs_deb_cmd("SEND_SLEEPC_CMD: After sent,IntC=%d\n",
1821			       adapter->intcounter);
1822		}
1823		spin_unlock_irqrestore(&adapter->driver_lock, flags);
1824
1825		lbs_deb_cmd("SEND_SLEEPC_CMD: Sent Confirm Sleep command\n");
1826		lbs_deb_cmd("+");
1827	}
1828
1829	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1830	return ret;
1831}
1832
1833void libertas_ps_sleep(wlan_private * priv, int wait_option)
1834{
1835	lbs_deb_enter(LBS_DEB_CMD);
1836
1837	/*
1838	 * PS is currently supported only in Infrastructure mode
1839	 * Remove this check if it is to be supported in IBSS mode also
1840	 */
1841
1842	libertas_prepare_and_send_command(priv, cmd_802_11_ps_mode,
1843			      cmd_subcmd_enter_ps, wait_option, 0, NULL);
1844
1845	lbs_deb_leave(LBS_DEB_CMD);
1846}
1847
1848/**
1849 *  @brief This function sends Eixt_PS command to firmware.
1850 *
1851 *  @param priv    	A pointer to wlan_private structure
1852 *  @param wait_option	wait response or not
1853 *  @return 	   	n/a
1854 */
1855void libertas_ps_wakeup(wlan_private * priv, int wait_option)
1856{
1857	__le32 Localpsmode;
1858
1859	lbs_deb_enter(LBS_DEB_CMD);
1860
1861	Localpsmode = cpu_to_le32(wlan802_11powermodecam);
1862
1863	lbs_deb_cmd("Exit_PS: Localpsmode = %d\n", wlan802_11powermodecam);
1864
1865	libertas_prepare_and_send_command(priv, cmd_802_11_ps_mode,
1866			      cmd_subcmd_exit_ps,
1867			      wait_option, 0, &Localpsmode);
1868
1869	lbs_deb_leave(LBS_DEB_CMD);
1870}
1871
1872/**
1873 *  @brief This function checks condition and prepares to
1874 *  send sleep confirm command to firmware if ok.
1875 *
1876 *  @param priv    	A pointer to wlan_private structure
1877 *  @param psmode  	Power Saving mode
1878 *  @return 	   	n/a
1879 */
1880void libertas_ps_confirm_sleep(wlan_private * priv, u16 psmode)
1881{
1882	unsigned long flags =0;
1883	wlan_adapter *adapter = priv->adapter;
1884	u8 allowed = 1;
1885
1886	lbs_deb_enter(LBS_DEB_CMD);
1887
1888	if (priv->dnld_sent) {
1889		allowed = 0;
1890		lbs_deb_cmd("D");
1891	}
1892
1893	spin_lock_irqsave(&adapter->driver_lock, flags);
1894	if (adapter->cur_cmd) {
1895		allowed = 0;
1896		lbs_deb_cmd("C");
1897	}
1898	if (adapter->intcounter > 0) {
1899		allowed = 0;
1900		lbs_deb_cmd("I%d", adapter->intcounter);
1901	}
1902	spin_unlock_irqrestore(&adapter->driver_lock, flags);
1903
1904	if (allowed) {
1905		lbs_deb_cmd("Sending libertas_ps_confirm_sleep\n");
1906		sendconfirmsleep(priv, (u8 *) & adapter->libertas_ps_confirm_sleep,
1907				 sizeof(struct PS_CMD_ConfirmSleep));
1908	} else {
1909		lbs_deb_cmd("Sleep Confirm has been delayed\n");
1910	}
1911
1912	lbs_deb_leave(LBS_DEB_CMD);
1913}
1914