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