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