]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/net/wireless/libertas/cmd.c
libertas: clean up MONITOR_MODE command
[mirror_ubuntu-zesty-kernel.git] / drivers / net / wireless / libertas / cmd.c
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 <linux/kfifo.h>
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/if_arp.h>
10
11 #include "decl.h"
12 #include "cfg.h"
13 #include "cmd.h"
14
15
16 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
17
18 /**
19 * @brief Simple callback that copies response back into command
20 *
21 * @param priv A pointer to struct lbs_private structure
22 * @param extra A pointer to the original command structure for which
23 * 'resp' is a response
24 * @param resp A pointer to the command response
25 *
26 * @return 0 on success, error on failure
27 */
28 int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
29 struct cmd_header *resp)
30 {
31 struct cmd_header *buf = (void *)extra;
32 uint16_t copy_len;
33
34 copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
35 memcpy(buf, resp, copy_len);
36 return 0;
37 }
38 EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
39
40 /**
41 * @brief Simple callback that ignores the result. Use this if
42 * you just want to send a command to the hardware, but don't
43 * care for the result.
44 *
45 * @param priv ignored
46 * @param extra ignored
47 * @param resp ignored
48 *
49 * @return 0 for success
50 */
51 static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
52 struct cmd_header *resp)
53 {
54 return 0;
55 }
56
57
58 /**
59 * @brief Checks whether a command is allowed in Power Save mode
60 *
61 * @param command the command ID
62 * @return 1 if allowed, 0 if not allowed
63 */
64 static u8 is_command_allowed_in_ps(u16 cmd)
65 {
66 switch (cmd) {
67 case CMD_802_11_RSSI:
68 return 1;
69 case CMD_802_11_HOST_SLEEP_CFG:
70 return 1;
71 default:
72 break;
73 }
74 return 0;
75 }
76
77 /**
78 * @brief This function checks if the command is allowed.
79 *
80 * @param priv A pointer to lbs_private structure
81 * @return allowed or not allowed.
82 */
83
84 static int lbs_is_cmd_allowed(struct lbs_private *priv)
85 {
86 int ret = 1;
87
88 lbs_deb_enter(LBS_DEB_CMD);
89
90 if (!priv->is_auto_deep_sleep_enabled) {
91 if (priv->is_deep_sleep) {
92 lbs_deb_cmd("command not allowed in deep sleep\n");
93 ret = 0;
94 }
95 }
96
97 lbs_deb_leave(LBS_DEB_CMD);
98 return ret;
99 }
100
101 /**
102 * @brief Updates the hardware details like MAC address and regulatory region
103 *
104 * @param priv A pointer to struct lbs_private structure
105 *
106 * @return 0 on success, error on failure
107 */
108 int lbs_update_hw_spec(struct lbs_private *priv)
109 {
110 struct cmd_ds_get_hw_spec cmd;
111 int ret = -1;
112 u32 i;
113
114 lbs_deb_enter(LBS_DEB_CMD);
115
116 memset(&cmd, 0, sizeof(cmd));
117 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
118 memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
119 ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
120 if (ret)
121 goto out;
122
123 priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
124
125 /* The firmware release is in an interesting format: the patch
126 * level is in the most significant nibble ... so fix that: */
127 priv->fwrelease = le32_to_cpu(cmd.fwrelease);
128 priv->fwrelease = (priv->fwrelease << 8) |
129 (priv->fwrelease >> 24 & 0xff);
130
131 /* Some firmware capabilities:
132 * CF card firmware 5.0.16p0: cap 0x00000303
133 * USB dongle firmware 5.110.17p2: cap 0x00000303
134 */
135 lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
136 cmd.permanentaddr,
137 priv->fwrelease >> 24 & 0xff,
138 priv->fwrelease >> 16 & 0xff,
139 priv->fwrelease >> 8 & 0xff,
140 priv->fwrelease & 0xff,
141 priv->fwcapinfo);
142 lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
143 cmd.hwifversion, cmd.version);
144
145 /* Clamp region code to 8-bit since FW spec indicates that it should
146 * only ever be 8-bit, even though the field size is 16-bit. Some firmware
147 * returns non-zero high 8 bits here.
148 *
149 * Firmware version 4.0.102 used in CF8381 has region code shifted. We
150 * need to check for this problem and handle it properly.
151 */
152 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
153 priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
154 else
155 priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
156
157 for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
158 /* use the region code to search for the index */
159 if (priv->regioncode == lbs_region_code_to_index[i])
160 break;
161 }
162
163 /* if it's unidentified region code, use the default (USA) */
164 if (i >= MRVDRV_MAX_REGION_CODE) {
165 priv->regioncode = 0x10;
166 lbs_pr_info("unidentified region code; using the default (USA)\n");
167 }
168
169 if (priv->current_addr[0] == 0xff)
170 memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
171
172 memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
173 if (priv->mesh_dev)
174 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
175
176 out:
177 lbs_deb_leave(LBS_DEB_CMD);
178 return ret;
179 }
180
181 static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
182 struct cmd_header *resp)
183 {
184 lbs_deb_enter(LBS_DEB_CMD);
185 if (priv->is_host_sleep_activated) {
186 priv->is_host_sleep_configured = 0;
187 if (priv->psstate == PS_STATE_FULL_POWER) {
188 priv->is_host_sleep_activated = 0;
189 wake_up_interruptible(&priv->host_sleep_q);
190 }
191 } else {
192 priv->is_host_sleep_configured = 1;
193 }
194 lbs_deb_leave(LBS_DEB_CMD);
195 return 0;
196 }
197
198 int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
199 struct wol_config *p_wol_config)
200 {
201 struct cmd_ds_host_sleep cmd_config;
202 int ret;
203
204 cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
205 cmd_config.criteria = cpu_to_le32(criteria);
206 cmd_config.gpio = priv->wol_gpio;
207 cmd_config.gap = priv->wol_gap;
208
209 if (p_wol_config != NULL)
210 memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
211 sizeof(struct wol_config));
212 else
213 cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
214
215 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
216 le16_to_cpu(cmd_config.hdr.size),
217 lbs_ret_host_sleep_cfg, 0);
218 if (!ret) {
219 if (p_wol_config)
220 memcpy((uint8_t *) p_wol_config,
221 (uint8_t *)&cmd_config.wol_conf,
222 sizeof(struct wol_config));
223 } else {
224 lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
225 }
226
227 return ret;
228 }
229 EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
230
231 static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
232 u16 cmd_action)
233 {
234 struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;
235
236 lbs_deb_enter(LBS_DEB_CMD);
237
238 cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
239 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
240 sizeof(struct cmd_header));
241 psm->action = cpu_to_le16(cmd_action);
242 psm->multipledtim = 0;
243 switch (cmd_action) {
244 case CMD_SUBCMD_ENTER_PS:
245 lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
246
247 psm->locallisteninterval = 0;
248 psm->nullpktinterval = 0;
249 psm->multipledtim =
250 cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
251 break;
252
253 case CMD_SUBCMD_EXIT_PS:
254 lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
255 break;
256
257 case CMD_SUBCMD_SLEEP_CONFIRMED:
258 lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
259 break;
260
261 default:
262 break;
263 }
264
265 lbs_deb_leave(LBS_DEB_CMD);
266 return 0;
267 }
268
269 int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
270 struct sleep_params *sp)
271 {
272 struct cmd_ds_802_11_sleep_params cmd;
273 int ret;
274
275 lbs_deb_enter(LBS_DEB_CMD);
276
277 if (cmd_action == CMD_ACT_GET) {
278 memset(&cmd, 0, sizeof(cmd));
279 } else {
280 cmd.error = cpu_to_le16(sp->sp_error);
281 cmd.offset = cpu_to_le16(sp->sp_offset);
282 cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
283 cmd.calcontrol = sp->sp_calcontrol;
284 cmd.externalsleepclk = sp->sp_extsleepclk;
285 cmd.reserved = cpu_to_le16(sp->sp_reserved);
286 }
287 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
288 cmd.action = cpu_to_le16(cmd_action);
289
290 ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
291
292 if (!ret) {
293 lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
294 "calcontrol 0x%x extsleepclk 0x%x\n",
295 le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
296 le16_to_cpu(cmd.stabletime), cmd.calcontrol,
297 cmd.externalsleepclk);
298
299 sp->sp_error = le16_to_cpu(cmd.error);
300 sp->sp_offset = le16_to_cpu(cmd.offset);
301 sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
302 sp->sp_calcontrol = cmd.calcontrol;
303 sp->sp_extsleepclk = cmd.externalsleepclk;
304 sp->sp_reserved = le16_to_cpu(cmd.reserved);
305 }
306
307 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
308 return 0;
309 }
310
311 static int lbs_wait_for_ds_awake(struct lbs_private *priv)
312 {
313 int ret = 0;
314
315 lbs_deb_enter(LBS_DEB_CMD);
316
317 if (priv->is_deep_sleep) {
318 if (!wait_event_interruptible_timeout(priv->ds_awake_q,
319 !priv->is_deep_sleep, (10 * HZ))) {
320 lbs_pr_err("ds_awake_q: timer expired\n");
321 ret = -1;
322 }
323 }
324
325 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
326 return ret;
327 }
328
329 int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
330 {
331 int ret = 0;
332
333 lbs_deb_enter(LBS_DEB_CMD);
334
335 if (deep_sleep) {
336 if (priv->is_deep_sleep != 1) {
337 lbs_deb_cmd("deep sleep: sleep\n");
338 BUG_ON(!priv->enter_deep_sleep);
339 ret = priv->enter_deep_sleep(priv);
340 if (!ret) {
341 netif_stop_queue(priv->dev);
342 netif_carrier_off(priv->dev);
343 }
344 } else {
345 lbs_pr_err("deep sleep: already enabled\n");
346 }
347 } else {
348 if (priv->is_deep_sleep) {
349 lbs_deb_cmd("deep sleep: wakeup\n");
350 BUG_ON(!priv->exit_deep_sleep);
351 ret = priv->exit_deep_sleep(priv);
352 if (!ret) {
353 ret = lbs_wait_for_ds_awake(priv);
354 if (ret)
355 lbs_pr_err("deep sleep: wakeup"
356 "failed\n");
357 }
358 }
359 }
360
361 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
362 return ret;
363 }
364
365 static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
366 unsigned long dummy,
367 struct cmd_header *cmd)
368 {
369 lbs_deb_enter(LBS_DEB_FW);
370 priv->is_host_sleep_activated = 1;
371 wake_up_interruptible(&priv->host_sleep_q);
372 lbs_deb_leave(LBS_DEB_FW);
373 return 0;
374 }
375
376 int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
377 {
378 struct cmd_header cmd;
379 int ret = 0;
380 uint32_t criteria = EHS_REMOVE_WAKEUP;
381
382 lbs_deb_enter(LBS_DEB_CMD);
383
384 if (host_sleep) {
385 if (priv->is_host_sleep_activated != 1) {
386 memset(&cmd, 0, sizeof(cmd));
387 ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
388 (struct wol_config *)NULL);
389 if (ret) {
390 lbs_pr_info("Host sleep configuration failed: "
391 "%d\n", ret);
392 return ret;
393 }
394 if (priv->psstate == PS_STATE_FULL_POWER) {
395 ret = __lbs_cmd(priv,
396 CMD_802_11_HOST_SLEEP_ACTIVATE,
397 &cmd,
398 sizeof(cmd),
399 lbs_ret_host_sleep_activate, 0);
400 if (ret)
401 lbs_pr_info("HOST_SLEEP_ACTIVATE "
402 "failed: %d\n", ret);
403 }
404
405 if (!wait_event_interruptible_timeout(
406 priv->host_sleep_q,
407 priv->is_host_sleep_activated,
408 (10 * HZ))) {
409 lbs_pr_err("host_sleep_q: timer expired\n");
410 ret = -1;
411 }
412 } else {
413 lbs_pr_err("host sleep: already enabled\n");
414 }
415 } else {
416 if (priv->is_host_sleep_activated)
417 ret = lbs_host_sleep_cfg(priv, criteria,
418 (struct wol_config *)NULL);
419 }
420
421 return ret;
422 }
423
424 /**
425 * @brief Set an SNMP MIB value
426 *
427 * @param priv A pointer to struct lbs_private structure
428 * @param oid The OID to set in the firmware
429 * @param val Value to set the OID to
430 *
431 * @return 0 on success, error on failure
432 */
433 int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
434 {
435 struct cmd_ds_802_11_snmp_mib cmd;
436 int ret;
437
438 lbs_deb_enter(LBS_DEB_CMD);
439
440 memset(&cmd, 0, sizeof (cmd));
441 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
442 cmd.action = cpu_to_le16(CMD_ACT_SET);
443 cmd.oid = cpu_to_le16((u16) oid);
444
445 switch (oid) {
446 case SNMP_MIB_OID_BSS_TYPE:
447 cmd.bufsize = cpu_to_le16(sizeof(u8));
448 cmd.value[0] = val;
449 break;
450 case SNMP_MIB_OID_11D_ENABLE:
451 case SNMP_MIB_OID_FRAG_THRESHOLD:
452 case SNMP_MIB_OID_RTS_THRESHOLD:
453 case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
454 case SNMP_MIB_OID_LONG_RETRY_LIMIT:
455 cmd.bufsize = cpu_to_le16(sizeof(u16));
456 *((__le16 *)(&cmd.value)) = cpu_to_le16(val);
457 break;
458 default:
459 lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
460 ret = -EINVAL;
461 goto out;
462 }
463
464 lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
465 le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
466
467 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
468
469 out:
470 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
471 return ret;
472 }
473
474 /**
475 * @brief Get an SNMP MIB value
476 *
477 * @param priv A pointer to struct lbs_private structure
478 * @param oid The OID to retrieve from the firmware
479 * @param out_val Location for the returned value
480 *
481 * @return 0 on success, error on failure
482 */
483 int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
484 {
485 struct cmd_ds_802_11_snmp_mib cmd;
486 int ret;
487
488 lbs_deb_enter(LBS_DEB_CMD);
489
490 memset(&cmd, 0, sizeof (cmd));
491 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
492 cmd.action = cpu_to_le16(CMD_ACT_GET);
493 cmd.oid = cpu_to_le16(oid);
494
495 ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
496 if (ret)
497 goto out;
498
499 switch (le16_to_cpu(cmd.bufsize)) {
500 case sizeof(u8):
501 *out_val = cmd.value[0];
502 break;
503 case sizeof(u16):
504 *out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
505 break;
506 default:
507 lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
508 oid, le16_to_cpu(cmd.bufsize));
509 break;
510 }
511
512 out:
513 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
514 return ret;
515 }
516
517 /**
518 * @brief Get the min, max, and current TX power
519 *
520 * @param priv A pointer to struct lbs_private structure
521 * @param curlevel Current power level in dBm
522 * @param minlevel Minimum supported power level in dBm (optional)
523 * @param maxlevel Maximum supported power level in dBm (optional)
524 *
525 * @return 0 on success, error on failure
526 */
527 int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
528 s16 *maxlevel)
529 {
530 struct cmd_ds_802_11_rf_tx_power cmd;
531 int ret;
532
533 lbs_deb_enter(LBS_DEB_CMD);
534
535 memset(&cmd, 0, sizeof(cmd));
536 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
537 cmd.action = cpu_to_le16(CMD_ACT_GET);
538
539 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
540 if (ret == 0) {
541 *curlevel = le16_to_cpu(cmd.curlevel);
542 if (minlevel)
543 *minlevel = cmd.minlevel;
544 if (maxlevel)
545 *maxlevel = cmd.maxlevel;
546 }
547
548 lbs_deb_leave(LBS_DEB_CMD);
549 return ret;
550 }
551
552 /**
553 * @brief Set the TX power
554 *
555 * @param priv A pointer to struct lbs_private structure
556 * @param dbm The desired power level in dBm
557 *
558 * @return 0 on success, error on failure
559 */
560 int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
561 {
562 struct cmd_ds_802_11_rf_tx_power cmd;
563 int ret;
564
565 lbs_deb_enter(LBS_DEB_CMD);
566
567 memset(&cmd, 0, sizeof(cmd));
568 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
569 cmd.action = cpu_to_le16(CMD_ACT_SET);
570 cmd.curlevel = cpu_to_le16(dbm);
571
572 lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);
573
574 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
575
576 lbs_deb_leave(LBS_DEB_CMD);
577 return ret;
578 }
579
580 /**
581 * @brief Enable or disable monitor mode (only implemented on OLPC usb8388 FW)
582 *
583 * @param priv A pointer to struct lbs_private structure
584 * @param enable 1 to enable monitor mode, 0 to disable
585 *
586 * @return 0 on success, error on failure
587 */
588 int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
589 {
590 struct cmd_ds_802_11_monitor_mode cmd;
591 int ret;
592
593 memset(&cmd, 0, sizeof(cmd));
594 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
595 cmd.action = cpu_to_le16(CMD_ACT_SET);
596 if (enable)
597 cmd.mode = cpu_to_le16(0x1);
598
599 lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
600
601 ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
602 if (ret == 0) {
603 priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
604 ARPHRD_ETHER;
605 }
606
607 lbs_deb_leave(LBS_DEB_CMD);
608 return ret;
609 }
610
611 /**
612 * @brief Get the radio channel
613 *
614 * @param priv A pointer to struct lbs_private structure
615 *
616 * @return The channel on success, error on failure
617 */
618 static int lbs_get_channel(struct lbs_private *priv)
619 {
620 struct cmd_ds_802_11_rf_channel cmd;
621 int ret = 0;
622
623 lbs_deb_enter(LBS_DEB_CMD);
624
625 memset(&cmd, 0, sizeof(cmd));
626 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
627 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
628
629 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
630 if (ret)
631 goto out;
632
633 ret = le16_to_cpu(cmd.channel);
634 lbs_deb_cmd("current radio channel is %d\n", ret);
635
636 out:
637 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
638 return ret;
639 }
640
641 int lbs_update_channel(struct lbs_private *priv)
642 {
643 int ret;
644
645 /* the channel in f/w could be out of sync; get the current channel */
646 lbs_deb_enter(LBS_DEB_ASSOC);
647
648 ret = lbs_get_channel(priv);
649 if (ret > 0) {
650 priv->channel = ret;
651 ret = 0;
652 }
653 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
654 return ret;
655 }
656
657 /**
658 * @brief Set the radio channel
659 *
660 * @param priv A pointer to struct lbs_private structure
661 * @param channel The desired channel, or 0 to clear a locked channel
662 *
663 * @return 0 on success, error on failure
664 */
665 int lbs_set_channel(struct lbs_private *priv, u8 channel)
666 {
667 struct cmd_ds_802_11_rf_channel cmd;
668 #ifdef DEBUG
669 u8 old_channel = priv->channel;
670 #endif
671 int ret = 0;
672
673 lbs_deb_enter(LBS_DEB_CMD);
674
675 memset(&cmd, 0, sizeof(cmd));
676 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
677 cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
678 cmd.channel = cpu_to_le16(channel);
679
680 ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
681 if (ret)
682 goto out;
683
684 priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
685 lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
686 priv->channel);
687
688 out:
689 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
690 return ret;
691 }
692
693 static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
694 u8 cmd_action, void *pdata_buf)
695 {
696 struct lbs_offset_value *offval;
697
698 lbs_deb_enter(LBS_DEB_CMD);
699
700 offval = (struct lbs_offset_value *)pdata_buf;
701
702 switch (le16_to_cpu(cmdptr->command)) {
703 case CMD_MAC_REG_ACCESS:
704 {
705 struct cmd_ds_mac_reg_access *macreg;
706
707 cmdptr->size =
708 cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
709 + sizeof(struct cmd_header));
710 macreg =
711 (struct cmd_ds_mac_reg_access *)&cmdptr->params.
712 macreg;
713
714 macreg->action = cpu_to_le16(cmd_action);
715 macreg->offset = cpu_to_le16((u16) offval->offset);
716 macreg->value = cpu_to_le32(offval->value);
717
718 break;
719 }
720
721 case CMD_BBP_REG_ACCESS:
722 {
723 struct cmd_ds_bbp_reg_access *bbpreg;
724
725 cmdptr->size =
726 cpu_to_le16(sizeof
727 (struct cmd_ds_bbp_reg_access)
728 + sizeof(struct cmd_header));
729 bbpreg =
730 (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
731 bbpreg;
732
733 bbpreg->action = cpu_to_le16(cmd_action);
734 bbpreg->offset = cpu_to_le16((u16) offval->offset);
735 bbpreg->value = (u8) offval->value;
736
737 break;
738 }
739
740 case CMD_RF_REG_ACCESS:
741 {
742 struct cmd_ds_rf_reg_access *rfreg;
743
744 cmdptr->size =
745 cpu_to_le16(sizeof
746 (struct cmd_ds_rf_reg_access) +
747 sizeof(struct cmd_header));
748 rfreg =
749 (struct cmd_ds_rf_reg_access *)&cmdptr->params.
750 rfreg;
751
752 rfreg->action = cpu_to_le16(cmd_action);
753 rfreg->offset = cpu_to_le16((u16) offval->offset);
754 rfreg->value = (u8) offval->value;
755
756 break;
757 }
758
759 default:
760 break;
761 }
762
763 lbs_deb_leave(LBS_DEB_CMD);
764 return 0;
765 }
766
767 static void lbs_queue_cmd(struct lbs_private *priv,
768 struct cmd_ctrl_node *cmdnode)
769 {
770 unsigned long flags;
771 int addtail = 1;
772
773 lbs_deb_enter(LBS_DEB_HOST);
774
775 if (!cmdnode) {
776 lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
777 goto done;
778 }
779 if (!cmdnode->cmdbuf->size) {
780 lbs_deb_host("DNLD_CMD: cmd size is zero\n");
781 goto done;
782 }
783 cmdnode->result = 0;
784
785 /* Exit_PS command needs to be queued in the header always. */
786 if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
787 struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
788
789 if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
790 if (priv->psstate != PS_STATE_FULL_POWER)
791 addtail = 0;
792 }
793 }
794
795 if (le16_to_cpu(cmdnode->cmdbuf->command) ==
796 CMD_802_11_WAKEUP_CONFIRM)
797 addtail = 0;
798
799 spin_lock_irqsave(&priv->driver_lock, flags);
800
801 if (addtail)
802 list_add_tail(&cmdnode->list, &priv->cmdpendingq);
803 else
804 list_add(&cmdnode->list, &priv->cmdpendingq);
805
806 spin_unlock_irqrestore(&priv->driver_lock, flags);
807
808 lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
809 le16_to_cpu(cmdnode->cmdbuf->command));
810
811 done:
812 lbs_deb_leave(LBS_DEB_HOST);
813 }
814
815 static void lbs_submit_command(struct lbs_private *priv,
816 struct cmd_ctrl_node *cmdnode)
817 {
818 unsigned long flags;
819 struct cmd_header *cmd;
820 uint16_t cmdsize;
821 uint16_t command;
822 int timeo = 3 * HZ;
823 int ret;
824
825 lbs_deb_enter(LBS_DEB_HOST);
826
827 cmd = cmdnode->cmdbuf;
828
829 spin_lock_irqsave(&priv->driver_lock, flags);
830 priv->cur_cmd = cmdnode;
831 priv->cur_cmd_retcode = 0;
832 spin_unlock_irqrestore(&priv->driver_lock, flags);
833
834 cmdsize = le16_to_cpu(cmd->size);
835 command = le16_to_cpu(cmd->command);
836
837 /* These commands take longer */
838 if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
839 timeo = 5 * HZ;
840
841 lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
842 command, le16_to_cpu(cmd->seqnum), cmdsize);
843 lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
844
845 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
846
847 if (ret) {
848 lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
849 /* Let the timer kick in and retry, and potentially reset
850 the whole thing if the condition persists */
851 timeo = HZ/4;
852 }
853
854 if (command == CMD_802_11_DEEP_SLEEP) {
855 if (priv->is_auto_deep_sleep_enabled) {
856 priv->wakeup_dev_required = 1;
857 priv->dnld_sent = 0;
858 }
859 priv->is_deep_sleep = 1;
860 lbs_complete_command(priv, cmdnode, 0);
861 } else {
862 /* Setup the timer after transmit command */
863 mod_timer(&priv->command_timer, jiffies + timeo);
864 }
865
866 lbs_deb_leave(LBS_DEB_HOST);
867 }
868
869 /**
870 * This function inserts command node to cmdfreeq
871 * after cleans it. Requires priv->driver_lock held.
872 */
873 static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
874 struct cmd_ctrl_node *cmdnode)
875 {
876 lbs_deb_enter(LBS_DEB_HOST);
877
878 if (!cmdnode)
879 goto out;
880
881 cmdnode->callback = NULL;
882 cmdnode->callback_arg = 0;
883
884 memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
885
886 list_add_tail(&cmdnode->list, &priv->cmdfreeq);
887 out:
888 lbs_deb_leave(LBS_DEB_HOST);
889 }
890
891 static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
892 struct cmd_ctrl_node *ptempcmd)
893 {
894 unsigned long flags;
895
896 spin_lock_irqsave(&priv->driver_lock, flags);
897 __lbs_cleanup_and_insert_cmd(priv, ptempcmd);
898 spin_unlock_irqrestore(&priv->driver_lock, flags);
899 }
900
901 void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
902 int result)
903 {
904 if (cmd == priv->cur_cmd)
905 priv->cur_cmd_retcode = result;
906
907 cmd->result = result;
908 cmd->cmdwaitqwoken = 1;
909 wake_up_interruptible(&cmd->cmdwait_q);
910
911 if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
912 __lbs_cleanup_and_insert_cmd(priv, cmd);
913 priv->cur_cmd = NULL;
914 }
915
916 int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
917 {
918 struct cmd_ds_802_11_radio_control cmd;
919 int ret = -EINVAL;
920
921 lbs_deb_enter(LBS_DEB_CMD);
922
923 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
924 cmd.action = cpu_to_le16(CMD_ACT_SET);
925
926 /* Only v8 and below support setting the preamble */
927 if (priv->fwrelease < 0x09000000) {
928 switch (preamble) {
929 case RADIO_PREAMBLE_SHORT:
930 case RADIO_PREAMBLE_AUTO:
931 case RADIO_PREAMBLE_LONG:
932 cmd.control = cpu_to_le16(preamble);
933 break;
934 default:
935 goto out;
936 }
937 }
938
939 if (radio_on)
940 cmd.control |= cpu_to_le16(0x1);
941 else {
942 cmd.control &= cpu_to_le16(~0x1);
943 priv->txpower_cur = 0;
944 }
945
946 lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
947 radio_on ? "ON" : "OFF", preamble);
948
949 priv->radio_on = radio_on;
950
951 ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
952
953 out:
954 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
955 return ret;
956 }
957
958 void lbs_set_mac_control(struct lbs_private *priv)
959 {
960 struct cmd_ds_mac_control cmd;
961
962 lbs_deb_enter(LBS_DEB_CMD);
963
964 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
965 cmd.action = cpu_to_le16(priv->mac_control);
966 cmd.reserved = 0;
967
968 lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
969
970 lbs_deb_leave(LBS_DEB_CMD);
971 }
972
973 /**
974 * @brief This function implements command CMD_802_11D_DOMAIN_INFO
975 * @param priv pointer to struct lbs_private
976 * @param cmd pointer to cmd buffer
977 * @param cmdno cmd ID
978 * @param cmdOption cmd action
979 * @return 0
980 */
981 int lbs_cmd_802_11d_domain_info(struct lbs_private *priv,
982 struct cmd_ds_command *cmd,
983 u16 cmdoption)
984 {
985 struct cmd_ds_802_11d_domain_info *pdomaininfo =
986 &cmd->params.domaininfo;
987 struct mrvl_ie_domain_param_set *domain = &pdomaininfo->domain;
988 u8 nr_triplet = priv->domain_reg.no_triplet;
989
990 lbs_deb_enter(LBS_DEB_11D);
991
992 lbs_deb_11d("nr_triplet=%x\n", nr_triplet);
993
994 pdomaininfo->action = cpu_to_le16(cmdoption);
995 if (cmdoption == CMD_ACT_GET) {
996 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
997 sizeof(struct cmd_header));
998 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
999 le16_to_cpu(cmd->size));
1000 goto done;
1001 }
1002
1003 domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
1004 memcpy(domain->countrycode, priv->domain_reg.country_code,
1005 sizeof(domain->countrycode));
1006
1007 domain->header.len = cpu_to_le16(nr_triplet
1008 * sizeof(struct ieee80211_country_ie_triplet)
1009 + sizeof(domain->countrycode));
1010
1011 if (nr_triplet) {
1012 memcpy(domain->triplet, priv->domain_reg.triplet,
1013 nr_triplet *
1014 sizeof(struct ieee80211_country_ie_triplet));
1015
1016 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1017 le16_to_cpu(domain->header.len) +
1018 sizeof(struct mrvl_ie_header) +
1019 sizeof(struct cmd_header));
1020 } else {
1021 cmd->size = cpu_to_le16(sizeof(pdomaininfo->action) +
1022 sizeof(struct cmd_header));
1023 }
1024
1025 lbs_deb_hex(LBS_DEB_11D, "802_11D_DOMAIN_INFO", (u8 *) cmd,
1026 le16_to_cpu(cmd->size));
1027
1028 done:
1029 lbs_deb_enter(LBS_DEB_11D);
1030 return 0;
1031 }
1032
1033 /**
1034 * @brief This function prepare the command before send to firmware.
1035 *
1036 * @param priv A pointer to struct lbs_private structure
1037 * @param cmd_no command number
1038 * @param cmd_action command action: GET or SET
1039 * @param wait_option wait option: wait response or not
1040 * @param cmd_oid cmd oid: treated as sub command
1041 * @param pdata_buf A pointer to informaion buffer
1042 * @return 0 or -1
1043 */
1044 int lbs_prepare_and_send_command(struct lbs_private *priv,
1045 u16 cmd_no,
1046 u16 cmd_action,
1047 u16 wait_option, u32 cmd_oid, void *pdata_buf)
1048 {
1049 int ret = 0;
1050 struct cmd_ctrl_node *cmdnode;
1051 struct cmd_ds_command *cmdptr;
1052 unsigned long flags;
1053
1054 lbs_deb_enter(LBS_DEB_HOST);
1055
1056 if (!priv) {
1057 lbs_deb_host("PREP_CMD: priv is NULL\n");
1058 ret = -1;
1059 goto done;
1060 }
1061
1062 if (priv->surpriseremoved) {
1063 lbs_deb_host("PREP_CMD: card removed\n");
1064 ret = -1;
1065 goto done;
1066 }
1067
1068 if (!lbs_is_cmd_allowed(priv)) {
1069 ret = -EBUSY;
1070 goto done;
1071 }
1072
1073 cmdnode = lbs_get_cmd_ctrl_node(priv);
1074
1075 if (cmdnode == NULL) {
1076 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1077
1078 /* Wake up main thread to execute next command */
1079 wake_up_interruptible(&priv->waitq);
1080 ret = -1;
1081 goto done;
1082 }
1083
1084 cmdnode->callback = NULL;
1085 cmdnode->callback_arg = (unsigned long)pdata_buf;
1086
1087 cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
1088
1089 lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1090
1091 /* Set sequence number, command and INT option */
1092 priv->seqnum++;
1093 cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1094
1095 cmdptr->command = cpu_to_le16(cmd_no);
1096 cmdptr->result = 0;
1097
1098 switch (cmd_no) {
1099 case CMD_802_11_PS_MODE:
1100 ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
1101 break;
1102
1103 case CMD_MAC_REG_ACCESS:
1104 case CMD_BBP_REG_ACCESS:
1105 case CMD_RF_REG_ACCESS:
1106 ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
1107 break;
1108
1109 case CMD_802_11_RSSI:
1110 ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1111 break;
1112
1113 case CMD_802_11_SET_AFC:
1114 case CMD_802_11_GET_AFC:
1115
1116 cmdptr->command = cpu_to_le16(cmd_no);
1117 cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1118 sizeof(struct cmd_header));
1119
1120 memmove(&cmdptr->params.afc,
1121 pdata_buf, sizeof(struct cmd_ds_802_11_afc));
1122
1123 ret = 0;
1124 goto done;
1125
1126 case CMD_802_11D_DOMAIN_INFO:
1127 cmdptr->command = cpu_to_le16(cmd_no);
1128 ret = lbs_cmd_802_11d_domain_info(priv, cmdptr, cmd_action);
1129 break;
1130
1131 case CMD_802_11_TPC_CFG:
1132 cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1133 cmdptr->size =
1134 cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1135 sizeof(struct cmd_header));
1136
1137 memmove(&cmdptr->params.tpccfg,
1138 pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));
1139
1140 ret = 0;
1141 break;
1142
1143 #ifdef CONFIG_LIBERTAS_MESH
1144
1145 case CMD_BT_ACCESS:
1146 ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1147 break;
1148
1149 case CMD_FWT_ACCESS:
1150 ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1151 break;
1152
1153 #endif
1154
1155 case CMD_802_11_BEACON_CTRL:
1156 ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
1157 break;
1158 case CMD_802_11_DEEP_SLEEP:
1159 cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
1160 cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
1161 break;
1162 default:
1163 lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1164 ret = -1;
1165 break;
1166 }
1167
1168 /* return error, since the command preparation failed */
1169 if (ret != 0) {
1170 lbs_deb_host("PREP_CMD: command preparation failed\n");
1171 lbs_cleanup_and_insert_cmd(priv, cmdnode);
1172 ret = -1;
1173 goto done;
1174 }
1175
1176 cmdnode->cmdwaitqwoken = 0;
1177
1178 lbs_queue_cmd(priv, cmdnode);
1179 wake_up_interruptible(&priv->waitq);
1180
1181 if (wait_option & CMD_OPTION_WAITFORRSP) {
1182 lbs_deb_host("PREP_CMD: wait for response\n");
1183 might_sleep();
1184 wait_event_interruptible(cmdnode->cmdwait_q,
1185 cmdnode->cmdwaitqwoken);
1186 }
1187
1188 spin_lock_irqsave(&priv->driver_lock, flags);
1189 if (priv->cur_cmd_retcode) {
1190 lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1191 priv->cur_cmd_retcode);
1192 priv->cur_cmd_retcode = 0;
1193 ret = -1;
1194 }
1195 spin_unlock_irqrestore(&priv->driver_lock, flags);
1196
1197 done:
1198 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1199 return ret;
1200 }
1201
1202 /**
1203 * @brief This function allocates the command buffer and link
1204 * it to command free queue.
1205 *
1206 * @param priv A pointer to struct lbs_private structure
1207 * @return 0 or -1
1208 */
1209 int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1210 {
1211 int ret = 0;
1212 u32 bufsize;
1213 u32 i;
1214 struct cmd_ctrl_node *cmdarray;
1215
1216 lbs_deb_enter(LBS_DEB_HOST);
1217
1218 /* Allocate and initialize the command array */
1219 bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1220 if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1221 lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1222 ret = -1;
1223 goto done;
1224 }
1225 priv->cmd_array = cmdarray;
1226
1227 /* Allocate and initialize each command buffer in the command array */
1228 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1229 cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1230 if (!cmdarray[i].cmdbuf) {
1231 lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1232 ret = -1;
1233 goto done;
1234 }
1235 }
1236
1237 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1238 init_waitqueue_head(&cmdarray[i].cmdwait_q);
1239 lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1240 }
1241 ret = 0;
1242
1243 done:
1244 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1245 return ret;
1246 }
1247
1248 /**
1249 * @brief This function frees the command buffer.
1250 *
1251 * @param priv A pointer to struct lbs_private structure
1252 * @return 0 or -1
1253 */
1254 int lbs_free_cmd_buffer(struct lbs_private *priv)
1255 {
1256 struct cmd_ctrl_node *cmdarray;
1257 unsigned int i;
1258
1259 lbs_deb_enter(LBS_DEB_HOST);
1260
1261 /* need to check if cmd array is allocated or not */
1262 if (priv->cmd_array == NULL) {
1263 lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1264 goto done;
1265 }
1266
1267 cmdarray = priv->cmd_array;
1268
1269 /* Release shared memory buffers */
1270 for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1271 if (cmdarray[i].cmdbuf) {
1272 kfree(cmdarray[i].cmdbuf);
1273 cmdarray[i].cmdbuf = NULL;
1274 }
1275 }
1276
1277 /* Release cmd_ctrl_node */
1278 if (priv->cmd_array) {
1279 kfree(priv->cmd_array);
1280 priv->cmd_array = NULL;
1281 }
1282
1283 done:
1284 lbs_deb_leave(LBS_DEB_HOST);
1285 return 0;
1286 }
1287
1288 /**
1289 * @brief This function gets a free command node if available in
1290 * command free queue.
1291 *
1292 * @param priv A pointer to struct lbs_private structure
1293 * @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
1294 */
1295 static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1296 {
1297 struct cmd_ctrl_node *tempnode;
1298 unsigned long flags;
1299
1300 lbs_deb_enter(LBS_DEB_HOST);
1301
1302 if (!priv)
1303 return NULL;
1304
1305 spin_lock_irqsave(&priv->driver_lock, flags);
1306
1307 if (!list_empty(&priv->cmdfreeq)) {
1308 tempnode = list_first_entry(&priv->cmdfreeq,
1309 struct cmd_ctrl_node, list);
1310 list_del(&tempnode->list);
1311 } else {
1312 lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1313 tempnode = NULL;
1314 }
1315
1316 spin_unlock_irqrestore(&priv->driver_lock, flags);
1317
1318 lbs_deb_leave(LBS_DEB_HOST);
1319 return tempnode;
1320 }
1321
1322 /**
1323 * @brief This function executes next command in command
1324 * pending queue. It will put firmware back to PS mode
1325 * if applicable.
1326 *
1327 * @param priv A pointer to struct lbs_private structure
1328 * @return 0 or -1
1329 */
1330 int lbs_execute_next_command(struct lbs_private *priv)
1331 {
1332 struct cmd_ctrl_node *cmdnode = NULL;
1333 struct cmd_header *cmd;
1334 unsigned long flags;
1335 int ret = 0;
1336
1337 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1338 * only caller to us is lbs_thread() and we get even when a
1339 * data packet is received */
1340 lbs_deb_enter(LBS_DEB_THREAD);
1341
1342 spin_lock_irqsave(&priv->driver_lock, flags);
1343
1344 if (priv->cur_cmd) {
1345 lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1346 spin_unlock_irqrestore(&priv->driver_lock, flags);
1347 ret = -1;
1348 goto done;
1349 }
1350
1351 if (!list_empty(&priv->cmdpendingq)) {
1352 cmdnode = list_first_entry(&priv->cmdpendingq,
1353 struct cmd_ctrl_node, list);
1354 }
1355
1356 spin_unlock_irqrestore(&priv->driver_lock, flags);
1357
1358 if (cmdnode) {
1359 cmd = cmdnode->cmdbuf;
1360
1361 if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1362 if ((priv->psstate == PS_STATE_SLEEP) ||
1363 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1364 lbs_deb_host(
1365 "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1366 le16_to_cpu(cmd->command),
1367 priv->psstate);
1368 ret = -1;
1369 goto done;
1370 }
1371 lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1372 "0x%04x in psstate %d\n",
1373 le16_to_cpu(cmd->command), priv->psstate);
1374 } else if (priv->psstate != PS_STATE_FULL_POWER) {
1375 /*
1376 * 1. Non-PS command:
1377 * Queue it. set needtowakeup to TRUE if current state
1378 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1379 * 2. PS command but not Exit_PS:
1380 * Ignore it.
1381 * 3. PS command Exit_PS:
1382 * Set needtowakeup to TRUE if current state is SLEEP,
1383 * otherwise send this command down to firmware
1384 * immediately.
1385 */
1386 if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1387 /* Prepare to send Exit PS,
1388 * this non PS command will be sent later */
1389 if ((priv->psstate == PS_STATE_SLEEP)
1390 || (priv->psstate == PS_STATE_PRE_SLEEP)
1391 ) {
1392 /* w/ new scheme, it will not reach here.
1393 since it is blocked in main_thread. */
1394 priv->needtowakeup = 1;
1395 } else
1396 lbs_ps_wakeup(priv, 0);
1397
1398 ret = 0;
1399 goto done;
1400 } else {
1401 /*
1402 * PS command. Ignore it if it is not Exit_PS.
1403 * otherwise send it down immediately.
1404 */
1405 struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1406
1407 lbs_deb_host(
1408 "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1409 psm->action);
1410 if (psm->action !=
1411 cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1412 lbs_deb_host(
1413 "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1414 list_del(&cmdnode->list);
1415 spin_lock_irqsave(&priv->driver_lock, flags);
1416 lbs_complete_command(priv, cmdnode, 0);
1417 spin_unlock_irqrestore(&priv->driver_lock, flags);
1418
1419 ret = 0;
1420 goto done;
1421 }
1422
1423 if ((priv->psstate == PS_STATE_SLEEP) ||
1424 (priv->psstate == PS_STATE_PRE_SLEEP)) {
1425 lbs_deb_host(
1426 "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1427 list_del(&cmdnode->list);
1428 spin_lock_irqsave(&priv->driver_lock, flags);
1429 lbs_complete_command(priv, cmdnode, 0);
1430 spin_unlock_irqrestore(&priv->driver_lock, flags);
1431 priv->needtowakeup = 1;
1432
1433 ret = 0;
1434 goto done;
1435 }
1436
1437 lbs_deb_host(
1438 "EXEC_NEXT_CMD: sending EXIT_PS\n");
1439 }
1440 }
1441 list_del(&cmdnode->list);
1442 lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1443 le16_to_cpu(cmd->command));
1444 lbs_submit_command(priv, cmdnode);
1445 } else {
1446 /*
1447 * check if in power save mode, if yes, put the device back
1448 * to PS mode
1449 */
1450 #ifdef TODO
1451 /*
1452 * This was the old code for libertas+wext. Someone that
1453 * understands this beast should re-code it in a sane way.
1454 *
1455 * I actually don't understand why this is related to WPA
1456 * and to connection status, shouldn't powering should be
1457 * independ of such things?
1458 */
1459 if ((priv->psmode != LBS802_11POWERMODECAM) &&
1460 (priv->psstate == PS_STATE_FULL_POWER) &&
1461 ((priv->connect_status == LBS_CONNECTED) ||
1462 lbs_mesh_connected(priv))) {
1463 if (priv->secinfo.WPAenabled ||
1464 priv->secinfo.WPA2enabled) {
1465 /* check for valid WPA group keys */
1466 if (priv->wpa_mcast_key.len ||
1467 priv->wpa_unicast_key.len) {
1468 lbs_deb_host(
1469 "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
1470 " go back to PS_SLEEP");
1471 lbs_ps_sleep(priv, 0);
1472 }
1473 } else {
1474 lbs_deb_host(
1475 "EXEC_NEXT_CMD: cmdpendingq empty, "
1476 "go back to PS_SLEEP");
1477 lbs_ps_sleep(priv, 0);
1478 }
1479 }
1480 #endif
1481 }
1482
1483 ret = 0;
1484 done:
1485 lbs_deb_leave(LBS_DEB_THREAD);
1486 return ret;
1487 }
1488
1489 static void lbs_send_confirmsleep(struct lbs_private *priv)
1490 {
1491 unsigned long flags;
1492 int ret;
1493
1494 lbs_deb_enter(LBS_DEB_HOST);
1495 lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1496 sizeof(confirm_sleep));
1497
1498 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1499 sizeof(confirm_sleep));
1500 if (ret) {
1501 lbs_pr_alert("confirm_sleep failed\n");
1502 goto out;
1503 }
1504
1505 spin_lock_irqsave(&priv->driver_lock, flags);
1506
1507 /* We don't get a response on the sleep-confirmation */
1508 priv->dnld_sent = DNLD_RES_RECEIVED;
1509
1510 if (priv->is_host_sleep_configured) {
1511 priv->is_host_sleep_activated = 1;
1512 wake_up_interruptible(&priv->host_sleep_q);
1513 }
1514
1515 /* If nothing to do, go back to sleep (?) */
1516 if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1517 priv->psstate = PS_STATE_SLEEP;
1518
1519 spin_unlock_irqrestore(&priv->driver_lock, flags);
1520
1521 out:
1522 lbs_deb_leave(LBS_DEB_HOST);
1523 }
1524
1525 void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1526 {
1527 lbs_deb_enter(LBS_DEB_HOST);
1528
1529 /*
1530 * PS is currently supported only in Infrastructure mode
1531 * Remove this check if it is to be supported in IBSS mode also
1532 */
1533
1534 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1535 CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1536
1537 lbs_deb_leave(LBS_DEB_HOST);
1538 }
1539
1540 /**
1541 * @brief This function sends Exit_PS command to firmware.
1542 *
1543 * @param priv A pointer to struct lbs_private structure
1544 * @param wait_option wait response or not
1545 * @return n/a
1546 */
1547 void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1548 {
1549 __le32 Localpsmode;
1550
1551 lbs_deb_enter(LBS_DEB_HOST);
1552
1553 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1554
1555 lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1556 CMD_SUBCMD_EXIT_PS,
1557 wait_option, 0, &Localpsmode);
1558
1559 lbs_deb_leave(LBS_DEB_HOST);
1560 }
1561
1562 /**
1563 * @brief This function checks condition and prepares to
1564 * send sleep confirm command to firmware if ok.
1565 *
1566 * @param priv A pointer to struct lbs_private structure
1567 * @param psmode Power Saving mode
1568 * @return n/a
1569 */
1570 void lbs_ps_confirm_sleep(struct lbs_private *priv)
1571 {
1572 unsigned long flags =0;
1573 int allowed = 1;
1574
1575 lbs_deb_enter(LBS_DEB_HOST);
1576
1577 spin_lock_irqsave(&priv->driver_lock, flags);
1578 if (priv->dnld_sent) {
1579 allowed = 0;
1580 lbs_deb_host("dnld_sent was set\n");
1581 }
1582
1583 /* In-progress command? */
1584 if (priv->cur_cmd) {
1585 allowed = 0;
1586 lbs_deb_host("cur_cmd was set\n");
1587 }
1588
1589 /* Pending events or command responses? */
1590 if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1591 allowed = 0;
1592 lbs_deb_host("pending events or command responses\n");
1593 }
1594 spin_unlock_irqrestore(&priv->driver_lock, flags);
1595
1596 if (allowed) {
1597 lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1598 lbs_send_confirmsleep(priv);
1599 } else {
1600 lbs_deb_host("sleep confirm has been delayed\n");
1601 }
1602
1603 lbs_deb_leave(LBS_DEB_HOST);
1604 }
1605
1606
1607 /**
1608 * @brief Configures the transmission power control functionality.
1609 *
1610 * @param priv A pointer to struct lbs_private structure
1611 * @param enable Transmission power control enable
1612 * @param p0 Power level when link quality is good (dBm).
1613 * @param p1 Power level when link quality is fair (dBm).
1614 * @param p2 Power level when link quality is poor (dBm).
1615 * @param usesnr Use Signal to Noise Ratio in TPC
1616 *
1617 * @return 0 on success
1618 */
1619 int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
1620 int8_t p2, int usesnr)
1621 {
1622 struct cmd_ds_802_11_tpc_cfg cmd;
1623 int ret;
1624
1625 memset(&cmd, 0, sizeof(cmd));
1626 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1627 cmd.action = cpu_to_le16(CMD_ACT_SET);
1628 cmd.enable = !!enable;
1629 cmd.usesnr = !!usesnr;
1630 cmd.P0 = p0;
1631 cmd.P1 = p1;
1632 cmd.P2 = p2;
1633
1634 ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);
1635
1636 return ret;
1637 }
1638
1639 /**
1640 * @brief Configures the power adaptation settings.
1641 *
1642 * @param priv A pointer to struct lbs_private structure
1643 * @param enable Power adaptation enable
1644 * @param p0 Power level for 1, 2, 5.5 and 11 Mbps (dBm).
1645 * @param p1 Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
1646 * @param p2 Power level for 48 and 54 Mbps (dBm).
1647 *
1648 * @return 0 on Success
1649 */
1650
1651 int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
1652 int8_t p1, int8_t p2)
1653 {
1654 struct cmd_ds_802_11_pa_cfg cmd;
1655 int ret;
1656
1657 memset(&cmd, 0, sizeof(cmd));
1658 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1659 cmd.action = cpu_to_le16(CMD_ACT_SET);
1660 cmd.enable = !!enable;
1661 cmd.P0 = p0;
1662 cmd.P1 = p1;
1663 cmd.P2 = p2;
1664
1665 ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);
1666
1667 return ret;
1668 }
1669
1670
1671 struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1672 uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1673 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1674 unsigned long callback_arg)
1675 {
1676 struct cmd_ctrl_node *cmdnode;
1677
1678 lbs_deb_enter(LBS_DEB_HOST);
1679
1680 if (priv->surpriseremoved) {
1681 lbs_deb_host("PREP_CMD: card removed\n");
1682 cmdnode = ERR_PTR(-ENOENT);
1683 goto done;
1684 }
1685
1686 if (!lbs_is_cmd_allowed(priv)) {
1687 cmdnode = ERR_PTR(-EBUSY);
1688 goto done;
1689 }
1690
1691 cmdnode = lbs_get_cmd_ctrl_node(priv);
1692 if (cmdnode == NULL) {
1693 lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1694
1695 /* Wake up main thread to execute next command */
1696 wake_up_interruptible(&priv->waitq);
1697 cmdnode = ERR_PTR(-ENOBUFS);
1698 goto done;
1699 }
1700
1701 cmdnode->callback = callback;
1702 cmdnode->callback_arg = callback_arg;
1703
1704 /* Copy the incoming command to the buffer */
1705 memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1706
1707 /* Set sequence number, clean result, move to buffer */
1708 priv->seqnum++;
1709 cmdnode->cmdbuf->command = cpu_to_le16(command);
1710 cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
1711 cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
1712 cmdnode->cmdbuf->result = 0;
1713
1714 lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1715
1716 cmdnode->cmdwaitqwoken = 0;
1717 lbs_queue_cmd(priv, cmdnode);
1718 wake_up_interruptible(&priv->waitq);
1719
1720 done:
1721 lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
1722 return cmdnode;
1723 }
1724
1725 void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1726 struct cmd_header *in_cmd, int in_cmd_size)
1727 {
1728 lbs_deb_enter(LBS_DEB_CMD);
1729 __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1730 lbs_cmd_async_callback, 0);
1731 lbs_deb_leave(LBS_DEB_CMD);
1732 }
1733
1734 int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1735 struct cmd_header *in_cmd, int in_cmd_size,
1736 int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1737 unsigned long callback_arg)
1738 {
1739 struct cmd_ctrl_node *cmdnode;
1740 unsigned long flags;
1741 int ret = 0;
1742
1743 lbs_deb_enter(LBS_DEB_HOST);
1744
1745 cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1746 callback, callback_arg);
1747 if (IS_ERR(cmdnode)) {
1748 ret = PTR_ERR(cmdnode);
1749 goto done;
1750 }
1751
1752 might_sleep();
1753 wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1754
1755 spin_lock_irqsave(&priv->driver_lock, flags);
1756 ret = cmdnode->result;
1757 if (ret)
1758 lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
1759 command, ret);
1760
1761 __lbs_cleanup_and_insert_cmd(priv, cmdnode);
1762 spin_unlock_irqrestore(&priv->driver_lock, flags);
1763
1764 done:
1765 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1766 return ret;
1767 }
1768 EXPORT_SYMBOL_GPL(__lbs_cmd);