]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/wl12xx/cmd.c
wl12xx: move to new firmware (6.1.3.50.49)
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / wl12xx / cmd.c
CommitLineData
f5fc0f86
LC
1/*
2 * This file is part of wl1271
3 *
2f826f55 4 * Copyright (C) 2009-2010 Nokia Corporation
f5fc0f86
LC
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/platform_device.h>
26#include <linux/crc7.h>
27#include <linux/spi/spi.h>
28#include <linux/etherdevice.h>
023e0826 29#include <linux/ieee80211.h>
5a0e3ad6 30#include <linux/slab.h>
f5fc0f86 31
00d20100
SL
32#include "wl12xx.h"
33#include "reg.h"
34#include "io.h"
35#include "acx.h"
f5fc0f86 36#include "wl12xx_80211.h"
00d20100
SL
37#include "cmd.h"
38#include "event.h"
98bdaabb 39#include "tx.h"
f5fc0f86 40
16092b5c 41#define WL1271_CMD_FAST_POLL_COUNT 50
f5fc0f86
LC
42
43/*
44 * send command to firmware
45 *
46 * @wl: wl struct
47 * @id: command id
48 * @buf: buffer containing the command, must work with dma
49 * @len: length of the buffer
50 */
fa867e73
JO
51int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
52 size_t res_len)
f5fc0f86
LC
53{
54 struct wl1271_cmd_header *cmd;
55 unsigned long timeout;
56 u32 intr;
57 int ret = 0;
ad150e96 58 u16 status;
bc0f03ea 59 u16 poll_count = 0;
f5fc0f86
LC
60
61 cmd = buf;
d0f63b20 62 cmd->id = cpu_to_le16(id);
f5fc0f86
LC
63 cmd->status = 0;
64
65 WARN_ON(len % 4 != 0);
66
7b048c52 67 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
f5fc0f86 68
7b048c52 69 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
f5fc0f86
LC
70
71 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
72
7b048c52 73 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
f5fc0f86
LC
74 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
75 if (time_after(jiffies, timeout)) {
76 wl1271_error("command complete timeout");
77 ret = -ETIMEDOUT;
78 goto out;
79 }
80
bc0f03ea 81 poll_count++;
16092b5c
JO
82 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
83 udelay(10);
84 else
85 msleep(1);
f5fc0f86 86
7b048c52 87 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
f5fc0f86
LC
88 }
89
3b775b4b 90 /* read back the status code of the command */
fa867e73
JO
91 if (res_len == 0)
92 res_len = sizeof(struct wl1271_cmd_header);
7b048c52 93 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
3b775b4b 94
ad150e96
JO
95 status = le16_to_cpu(cmd->status);
96 if (status != CMD_STATUS_SUCCESS) {
97 wl1271_error("command execute failure %d", status);
52b0e7a6 98 ieee80211_queue_work(wl->hw, &wl->recovery_work);
3b775b4b
JO
99 ret = -EIO;
100 }
101
7b048c52
TP
102 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
103 WL1271_ACX_INTR_CMD_COMPLETE);
f5fc0f86
LC
104
105out:
106 return ret;
107}
108
98b5dd5d
LC
109int wl1271_cmd_general_parms(struct wl1271 *wl)
110{
111 struct wl1271_general_parms_cmd *gen_parms;
4b34d432
JO
112 struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
113 bool answer = false;
98b5dd5d
LC
114 int ret;
115
152ee6e0
JO
116 if (!wl->nvs)
117 return -ENODEV;
118
98b5dd5d
LC
119 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
120 if (!gen_parms)
121 return -ENOMEM;
122
123 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
124
4b34d432
JO
125 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
126
127 if (gp->tx_bip_fem_auto_detect)
128 answer = true;
76c0f8d3 129
4b34d432
JO
130 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
131 if (ret < 0) {
98b5dd5d 132 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
4b34d432
JO
133 goto out;
134 }
98b5dd5d 135
4b34d432
JO
136 gp->tx_bip_fem_manufacturer =
137 gen_parms->general_params.tx_bip_fem_manufacturer;
138
139 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
140 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
141
142out:
98b5dd5d
LC
143 kfree(gen_parms);
144 return ret;
145}
146
147int wl1271_cmd_radio_parms(struct wl1271 *wl)
148{
149 struct wl1271_radio_parms_cmd *radio_parms;
e6b190ff 150 struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
152ee6e0
JO
151 int ret;
152
153 if (!wl->nvs)
154 return -ENODEV;
98b5dd5d
LC
155
156 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
157 if (!radio_parms)
158 return -ENOMEM;
159
160 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
161
a7da74fc 162 /* 2.4GHz parameters */
eb70eb72
JO
163 memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
164 sizeof(struct wl1271_ini_band_params_2));
165 memcpy(&radio_parms->dyn_params_2,
e6b190ff 166 &wl->nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
eb70eb72 167 sizeof(struct wl1271_ini_fem_params_2));
152ee6e0 168
a7da74fc
JO
169 /* 5GHz parameters */
170 memcpy(&radio_parms->static_params_5,
171 &wl->nvs->stat_radio_params_5,
172 sizeof(struct wl1271_ini_band_params_5));
173 memcpy(&radio_parms->dyn_params_5,
e6b190ff 174 &wl->nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
a7da74fc 175 sizeof(struct wl1271_ini_fem_params_5));
98b5dd5d
LC
176
177 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
178 radio_parms, sizeof(*radio_parms));
179
180 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
181 if (ret < 0)
182 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
183
184 kfree(radio_parms);
185 return ret;
186}
187
644a4860
JO
188int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
189{
190 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
191 struct conf_rf_settings *rf = &wl->conf.rf;
192 int ret;
193
194 if (!wl->nvs)
195 return -ENODEV;
196
197 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
198 if (!ext_radio_parms)
199 return -ENOMEM;
200
201 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
202
203 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
204 rf->tx_per_channel_power_compensation_2,
205 CONF_TX_PWR_COMPENSATION_LEN_2);
206 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
207 rf->tx_per_channel_power_compensation_5,
208 CONF_TX_PWR_COMPENSATION_LEN_5);
209
210 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
211 ext_radio_parms, sizeof(*ext_radio_parms));
212
213 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
214 if (ret < 0)
215 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
216
217 kfree(ext_radio_parms);
218 return ret;
219}
220
99d84c1d
LC
221/*
222 * Poll the mailbox event field until any of the bits in the mask is set or a
223 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
224 */
05285cf9 225static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
99d84c1d
LC
226{
227 u32 events_vector, event;
228 unsigned long timeout;
229
230 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
231
232 do {
52b0e7a6 233 if (time_after(jiffies, timeout)) {
05285cf9
AN
234 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
235 (int)mask);
99d84c1d 236 return -ETIMEDOUT;
52b0e7a6 237 }
99d84c1d
LC
238
239 msleep(1);
240
241 /* read from both event fields */
242 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
243 sizeof(events_vector), false);
244 event = events_vector & mask;
245 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
246 sizeof(events_vector), false);
247 event |= events_vector & mask;
248 } while (!event);
249
250 return 0;
251}
252
05285cf9
AN
253static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
254{
255 int ret;
256
257 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
258 if (ret != 0) {
259 ieee80211_queue_work(wl->hw, &wl->recovery_work);
260 return ret;
261 }
262
263 return 0;
264}
265
15305498 266int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
f5fc0f86 267{
f5fc0f86
LC
268 struct wl1271_cmd_join *join;
269 int ret, i;
270 u8 *bssid;
271
f5fc0f86
LC
272 join = kzalloc(sizeof(*join), GFP_KERNEL);
273 if (!join) {
274 ret = -ENOMEM;
275 goto out;
276 }
277
278 wl1271_debug(DEBUG_CMD, "cmd join");
279
280 /* Reverse order BSSID */
281 bssid = (u8 *) &join->bssid_lsb;
282 for (i = 0; i < ETH_ALEN; i++)
283 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
284
d0f63b20
LC
285 join->rx_config_options = cpu_to_le32(wl->rx_config);
286 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
15305498 287 join->bss_type = bss_type;
23a7a51c 288 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
c8bde243
EP
289 /*
290 * for supported_rate_set, we should use wl->rate_set. however,
291 * it seems that acx_rate_policies doesn't affect full_rate, and
292 * since we want to avoid additional join, we'll use a 0xffffffff value,
293 * and let the fw find the actual supported rates
294 */
295 join->supported_rate_set = cpu_to_le32(0xffffffff);
f5fc0f86 296
ebba60c6 297 if (wl->band == IEEE80211_BAND_5GHZ)
a4102645 298 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
f5fc0f86 299
60e84c2e 300 join->beacon_interval = cpu_to_le16(wl->beacon_int);
ae751bab 301 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
a4102645 302
f5fc0f86
LC
303 join->channel = wl->channel;
304 join->ssid_len = wl->ssid_len;
305 memcpy(join->ssid, wl->ssid, wl->ssid_len);
f5fc0f86
LC
306
307 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
308
ac4e4ce5
JO
309 /* reset TX security counters */
310 wl->tx_security_last_seq = 0;
04e36fc5 311 wl->tx_security_seq = 0;
f5fc0f86 312
fa867e73 313 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
f5fc0f86
LC
314 if (ret < 0) {
315 wl1271_error("failed to initiate cmd join");
316 goto out_free;
317 }
318
99d84c1d
LC
319 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
320 if (ret < 0)
321 wl1271_error("cmd join event completion error");
f5fc0f86
LC
322
323out_free:
324 kfree(join);
325
326out:
327 return ret;
328}
329
330/**
331 * send test command to firmware
332 *
333 * @wl: wl struct
334 * @buf: buffer containing the command, with all headers, must work with dma
335 * @len: length of the buffer
336 * @answer: is answer needed
337 */
338int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
339{
340 int ret;
fa867e73 341 size_t res_len = 0;
f5fc0f86
LC
342
343 wl1271_debug(DEBUG_CMD, "cmd test");
344
fa867e73
JO
345 if (answer)
346 res_len = buf_len;
347
348 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
f5fc0f86
LC
349
350 if (ret < 0) {
351 wl1271_warning("TEST command failed");
352 return ret;
353 }
354
fa867e73 355 return ret;
f5fc0f86
LC
356}
357
358/**
359 * read acx from firmware
360 *
361 * @wl: wl struct
362 * @id: acx id
363 * @buf: buffer for the response, including all headers, must work with dma
364 * @len: lenght of buf
365 */
366int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
367{
368 struct acx_header *acx = buf;
369 int ret;
370
371 wl1271_debug(DEBUG_CMD, "cmd interrogate");
372
d0f63b20 373 acx->id = cpu_to_le16(id);
f5fc0f86
LC
374
375 /* payload length, does not include any headers */
d0f63b20 376 acx->len = cpu_to_le16(len - sizeof(*acx));
f5fc0f86 377
fa867e73
JO
378 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
379 if (ret < 0)
f5fc0f86 380 wl1271_error("INTERROGATE command failed");
f5fc0f86 381
f5fc0f86
LC
382 return ret;
383}
384
385/**
386 * write acx value to firmware
387 *
388 * @wl: wl struct
389 * @id: acx id
390 * @buf: buffer containing acx, including all headers, must work with dma
391 * @len: length of buf
392 */
393int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
394{
395 struct acx_header *acx = buf;
396 int ret;
397
398 wl1271_debug(DEBUG_CMD, "cmd configure");
399
d0f63b20 400 acx->id = cpu_to_le16(id);
f5fc0f86
LC
401
402 /* payload length, does not include any headers */
d0f63b20 403 acx->len = cpu_to_le16(len - sizeof(*acx));
f5fc0f86 404
fa867e73 405 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
f5fc0f86
LC
406 if (ret < 0) {
407 wl1271_warning("CONFIGURE command NOK");
408 return ret;
409 }
410
411 return 0;
412}
413
94210897 414int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
f5fc0f86
LC
415{
416 struct cmd_enabledisable_path *cmd;
417 int ret;
418 u16 cmd_rx, cmd_tx;
419
420 wl1271_debug(DEBUG_CMD, "cmd data path");
421
422 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
423 if (!cmd) {
424 ret = -ENOMEM;
425 goto out;
426 }
427
94210897
LC
428 /* the channel here is only used for calibration, so hardcoded to 1 */
429 cmd->channel = 1;
f5fc0f86
LC
430
431 if (enable) {
432 cmd_rx = CMD_ENABLE_RX;
433 cmd_tx = CMD_ENABLE_TX;
434 } else {
435 cmd_rx = CMD_DISABLE_RX;
436 cmd_tx = CMD_DISABLE_TX;
437 }
438
fa867e73 439 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
440 if (ret < 0) {
441 wl1271_error("rx %s cmd for channel %d failed",
94210897 442 enable ? "start" : "stop", cmd->channel);
f5fc0f86
LC
443 goto out;
444 }
445
446 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
94210897 447 enable ? "start" : "stop", cmd->channel);
f5fc0f86 448
fa867e73 449 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
450 if (ret < 0) {
451 wl1271_error("tx %s cmd for channel %d failed",
94210897 452 enable ? "start" : "stop", cmd->channel);
1b00f2b5 453 goto out;
f5fc0f86
LC
454 }
455
456 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
94210897 457 enable ? "start" : "stop", cmd->channel);
f5fc0f86
LC
458
459out:
460 kfree(cmd);
461 return ret;
462}
463
c8bde243 464int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
f5fc0f86
LC
465{
466 struct wl1271_cmd_ps_params *ps_params = NULL;
467 int ret = 0;
468
f5fc0f86
LC
469 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
470
471 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
472 if (!ps_params) {
473 ret = -ENOMEM;
474 goto out;
475 }
476
477 ps_params->ps_mode = ps_mode;
f5fc0f86
LC
478
479 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
fa867e73 480 sizeof(*ps_params), 0);
f5fc0f86
LC
481 if (ret < 0) {
482 wl1271_error("cmd set_ps_mode failed");
483 goto out;
484 }
485
486out:
487 kfree(ps_params);
488 return ret;
489}
490
f5fc0f86 491int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
606c1487 492 void *buf, size_t buf_len, int index, u32 rates)
f5fc0f86
LC
493{
494 struct wl1271_cmd_template_set *cmd;
495 int ret = 0;
496
497 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
498
499 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
500 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
501
502 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
503 if (!cmd) {
504 ret = -ENOMEM;
505 goto out;
506 }
507
508 cmd->len = cpu_to_le16(buf_len);
509 cmd->template_type = template_id;
606c1487 510 cmd->enabled_rates = cpu_to_le32(rates);
1e05a818
AN
511 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
512 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
bfb24c9e 513 cmd->index = index;
f5fc0f86
LC
514
515 if (buf)
516 memcpy(cmd->template_data, buf, buf_len);
517
fa867e73 518 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
519 if (ret < 0) {
520 wl1271_warning("cmd set_template failed: %d", ret);
521 goto out_free;
522 }
523
524out_free:
525 kfree(cmd);
526
527out:
528 return ret;
529}
530
f5fc0f86
LC
531int wl1271_cmd_build_null_data(struct wl1271 *wl)
532{
a0cb7be4
JO
533 struct sk_buff *skb = NULL;
534 int size;
535 void *ptr;
536 int ret = -ENOMEM;
f5fc0f86 537
f5fc0f86 538
a0cb7be4
JO
539 if (wl->bss_type == BSS_TYPE_IBSS) {
540 size = sizeof(struct wl12xx_null_data_template);
541 ptr = NULL;
542 } else {
543 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
544 if (!skb)
545 goto out;
546 size = skb->len;
547 ptr = skb->data;
548 }
549
606c1487 550 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
8eab7b47 551 wl->basic_rate);
f5fc0f86 552
899e6e65
KV
553out:
554 dev_kfree_skb(skb);
a0cb7be4
JO
555 if (ret)
556 wl1271_warning("cmd buld null data failed %d", ret);
557
899e6e65 558 return ret;
f5fc0f86
LC
559
560}
561
bfb24c9e
JO
562int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
563{
564 struct sk_buff *skb = NULL;
565 int ret = -ENOMEM;
566
567 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
568 if (!skb)
569 goto out;
570
571 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
572 skb->data, skb->len,
606c1487 573 CMD_TEMPL_KLV_IDX_NULL_DATA,
8eab7b47 574 wl->basic_rate);
bfb24c9e
JO
575
576out:
577 dev_kfree_skb(skb);
578 if (ret)
579 wl1271_warning("cmd build klv null data failed %d", ret);
580
581 return ret;
582
583}
584
f5fc0f86
LC
585int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
586{
899e6e65
KV
587 struct sk_buff *skb;
588 int ret = 0;
c3fea199 589
899e6e65
KV
590 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
591 if (!skb)
592 goto out;
f5fc0f86 593
899e6e65 594 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
849923f4 595 skb->len, 0, wl->basic_rate_set);
f5fc0f86 596
899e6e65
KV
597out:
598 dev_kfree_skb(skb);
599 return ret;
f5fc0f86
LC
600}
601
818e3063
KV
602int wl1271_cmd_build_probe_req(struct wl1271 *wl,
603 const u8 *ssid, size_t ssid_len,
604 const u8 *ie, size_t ie_len, u8 band)
f5fc0f86 605{
818e3063 606 struct sk_buff *skb;
abb0b3bf 607 int ret;
f5fc0f86 608
818e3063
KV
609 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
610 ie, ie_len);
611 if (!skb) {
612 ret = -ENOMEM;
613 goto out;
614 }
615
616 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
f5fc0f86 617
abb0b3bf
TP
618 if (band == IEEE80211_BAND_2GHZ)
619 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
606c1487
JO
620 skb->data, skb->len, 0,
621 wl->conf.tx.basic_rate);
abb0b3bf
TP
622 else
623 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
606c1487
JO
624 skb->data, skb->len, 0,
625 wl->conf.tx.basic_rate_5);
818e3063
KV
626
627out:
628 dev_kfree_skb(skb);
abb0b3bf 629 return ret;
f5fc0f86
LC
630}
631
2f6724b2
JO
632struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
633 struct sk_buff *skb)
634{
635 int ret;
636
637 if (!skb)
638 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
639 if (!skb)
640 goto out;
641
642 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
643
644 if (wl->band == IEEE80211_BAND_2GHZ)
645 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
646 skb->data, skb->len, 0,
647 wl->conf.tx.basic_rate);
648 else
649 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
650 skb->data, skb->len, 0,
651 wl->conf.tx.basic_rate_5);
652
653 if (ret < 0)
654 wl1271_error("Unable to set ap probe request template.");
655
656out:
657 return skb;
658}
659
c5312772
EP
660int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
661{
662 int ret;
663 struct wl12xx_arp_rsp_template tmpl;
664 struct ieee80211_hdr_3addr *hdr;
665 struct arphdr *arp_hdr;
666
667 memset(&tmpl, 0, sizeof(tmpl));
668
669 /* mac80211 header */
670 hdr = &tmpl.hdr;
671 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
672 IEEE80211_STYPE_DATA |
673 IEEE80211_FCTL_TODS);
674 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
675 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
676 memset(hdr->addr3, 0xff, ETH_ALEN);
677
678 /* llc layer */
679 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
6177eaea 680 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
c5312772
EP
681
682 /* arp header */
683 arp_hdr = &tmpl.arp_hdr;
6177eaea
EP
684 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
685 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
c5312772
EP
686 arp_hdr->ar_hln = ETH_ALEN;
687 arp_hdr->ar_pln = 4;
6177eaea 688 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
c5312772
EP
689
690 /* arp payload */
691 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
692 tmpl.sender_ip = ip_addr;
693
694 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
695 &tmpl, sizeof(tmpl), 0,
696 wl->basic_rate);
697
698 return ret;
699}
700
023e0826
KV
701int wl1271_build_qos_null_data(struct wl1271 *wl)
702{
703 struct ieee80211_qos_hdr template;
704
705 memset(&template, 0, sizeof(template));
706
707 memcpy(template.addr1, wl->bssid, ETH_ALEN);
708 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
709 memcpy(template.addr3, wl->bssid, ETH_ALEN);
710
711 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
712 IEEE80211_STYPE_QOS_NULLFUNC |
713 IEEE80211_FCTL_TODS);
714
715 /* FIXME: not sure what priority to use here */
716 template.qos_ctrl = cpu_to_le16(0);
717
718 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
606c1487 719 sizeof(template), 0,
8eab7b47 720 wl->basic_rate);
023e0826
KV
721}
722
98bdaabb 723int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
f5fc0f86 724{
98bdaabb 725 struct wl1271_cmd_set_sta_keys *cmd;
f5fc0f86
LC
726 int ret = 0;
727
728 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
729
730 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
731 if (!cmd) {
732 ret = -ENOMEM;
733 goto out;
734 }
735
736 cmd->id = id;
d0f63b20 737 cmd->key_action = cpu_to_le16(KEY_SET_ID);
f5fc0f86
LC
738 cmd->key_type = KEY_WEP;
739
fa867e73 740 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
741 if (ret < 0) {
742 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
743 goto out;
744 }
745
746out:
747 kfree(cmd);
748
749 return ret;
750}
751
98bdaabb
AN
752int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
753{
754 struct wl1271_cmd_set_ap_keys *cmd;
755 int ret = 0;
756
757 wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
758
759 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
760 if (!cmd) {
761 ret = -ENOMEM;
762 goto out;
763 }
764
765 cmd->hlid = WL1271_AP_BROADCAST_HLID;
766 cmd->key_id = id;
767 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
768 cmd->key_action = cpu_to_le16(KEY_SET_ID);
769 cmd->key_type = KEY_WEP;
770
771 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
772 if (ret < 0) {
773 wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
774 goto out;
775 }
776
777out:
778 kfree(cmd);
779
780 return ret;
781}
782
783int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
ac4e4ce5
JO
784 u8 key_size, const u8 *key, const u8 *addr,
785 u32 tx_seq_32, u16 tx_seq_16)
f5fc0f86 786{
98bdaabb 787 struct wl1271_cmd_set_sta_keys *cmd;
f5fc0f86
LC
788 int ret = 0;
789
790 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
791 if (!cmd) {
792 ret = -ENOMEM;
793 goto out;
794 }
795
796 if (key_type != KEY_WEP)
797 memcpy(cmd->addr, addr, ETH_ALEN);
798
d0f63b20 799 cmd->key_action = cpu_to_le16(action);
f5fc0f86
LC
800 cmd->key_size = key_size;
801 cmd->key_type = key_type;
802
d0f63b20
LC
803 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
804 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
ac4e4ce5 805
f5fc0f86
LC
806 /* we have only one SSID profile */
807 cmd->ssid_profile = 0;
808
809 cmd->id = id;
810
f5fc0f86
LC
811 if (key_type == KEY_TKIP) {
812 /*
813 * We get the key in the following form:
814 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
815 * but the target is expecting:
816 * TKIP - RX MIC - TX MIC
817 */
818 memcpy(cmd->key, key, 16);
819 memcpy(cmd->key + 16, key + 24, 8);
820 memcpy(cmd->key + 24, key + 16, 8);
821
822 } else {
823 memcpy(cmd->key, key, key_size);
824 }
825
826 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
827
fa867e73 828 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
829 if (ret < 0) {
830 wl1271_warning("could not set keys");
152ee6e0 831 goto out;
f5fc0f86
LC
832 }
833
834out:
835 kfree(cmd);
836
837 return ret;
838}
25a7dc6d 839
98bdaabb
AN
840int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
841 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
842 u16 tx_seq_16)
843{
844 struct wl1271_cmd_set_ap_keys *cmd;
845 int ret = 0;
846 u8 lid_type;
847
848 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
849 if (!cmd)
850 return -ENOMEM;
851
852 if (hlid == WL1271_AP_BROADCAST_HLID) {
853 if (key_type == KEY_WEP)
854 lid_type = WEP_DEFAULT_LID_TYPE;
855 else
856 lid_type = BROADCAST_LID_TYPE;
857 } else {
858 lid_type = UNICAST_LID_TYPE;
859 }
860
861 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
862 " hlid: %d", (int)action, (int)id, (int)lid_type,
863 (int)key_type, (int)hlid);
864
865 cmd->lid_key_type = lid_type;
866 cmd->hlid = hlid;
867 cmd->key_action = cpu_to_le16(action);
868 cmd->key_size = key_size;
869 cmd->key_type = key_type;
870 cmd->key_id = id;
871 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
872 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
873
874 if (key_type == KEY_TKIP) {
875 /*
876 * We get the key in the following form:
877 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
878 * but the target is expecting:
879 * TKIP - RX MIC - TX MIC
880 */
881 memcpy(cmd->key, key, 16);
882 memcpy(cmd->key + 16, key + 24, 8);
883 memcpy(cmd->key + 24, key + 16, 8);
884 } else {
885 memcpy(cmd->key, key, key_size);
886 }
887
888 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
889
890 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
891 if (ret < 0) {
892 wl1271_warning("could not set ap keys");
893 goto out;
894 }
895
896out:
897 kfree(cmd);
898 return ret;
899}
900
25a7dc6d
LC
901int wl1271_cmd_disconnect(struct wl1271 *wl)
902{
903 struct wl1271_cmd_disconnect *cmd;
904 int ret = 0;
905
906 wl1271_debug(DEBUG_CMD, "cmd disconnect");
907
908 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
909 if (!cmd) {
910 ret = -ENOMEM;
911 goto out;
912 }
913
d0f63b20
LC
914 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
915 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
25a7dc6d
LC
916 /* disconnect reason is not used in immediate disconnections */
917 cmd->type = DISCONNECT_IMMEDIATE;
918
fa867e73 919 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
25a7dc6d
LC
920 if (ret < 0) {
921 wl1271_error("failed to send disconnect command");
922 goto out_free;
923 }
924
2f826f55
LC
925 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
926 if (ret < 0)
927 wl1271_error("cmd disconnect event completion error");
928
25a7dc6d
LC
929out_free:
930 kfree(cmd);
931
932out:
933 return ret;
934}
be86cbea
JO
935
936int wl1271_cmd_set_sta_state(struct wl1271 *wl)
937{
938 struct wl1271_cmd_set_sta_state *cmd;
939 int ret = 0;
940
941 wl1271_debug(DEBUG_CMD, "cmd set sta state");
942
943 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
944 if (!cmd) {
945 ret = -ENOMEM;
946 goto out;
947 }
948
949 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
950
951 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
952 if (ret < 0) {
953 wl1271_error("failed to send set STA state command");
954 goto out_free;
955 }
956
957out_free:
958 kfree(cmd);
959
960out:
961 return ret;
962}
98bdaabb
AN
963
964int wl1271_cmd_start_bss(struct wl1271 *wl)
965{
966 struct wl1271_cmd_bss_start *cmd;
967 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
968 int ret;
969
970 wl1271_debug(DEBUG_CMD, "cmd start bss");
971
972 /*
973 * FIXME: We currently do not support hidden SSID. The real SSID
974 * should be fetched from mac80211 first.
975 */
976 if (wl->ssid_len == 0) {
977 wl1271_warning("Hidden SSID currently not supported for AP");
978 ret = -EINVAL;
979 goto out;
980 }
981
982 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
983 if (!cmd) {
984 ret = -ENOMEM;
985 goto out;
986 }
987
988 memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
989
1d4801f2 990 cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
98bdaabb
AN
991 cmd->bss_index = WL1271_AP_BSS_INDEX;
992 cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
993 cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
994 cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
995 cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
996 cmd->dtim_interval = bss_conf->dtim_period;
997 cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
998 cmd->channel = wl->channel;
999 cmd->ssid_len = wl->ssid_len;
1000 cmd->ssid_type = SSID_TYPE_PUBLIC;
1001 memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1002
1003 switch (wl->band) {
1004 case IEEE80211_BAND_2GHZ:
1005 cmd->band = RADIO_BAND_2_4GHZ;
1006 break;
1007 case IEEE80211_BAND_5GHZ:
1008 cmd->band = RADIO_BAND_5GHZ;
1009 break;
1010 default:
1011 wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1012 cmd->band = RADIO_BAND_2_4GHZ;
1013 break;
1014 }
1015
1016 ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1017 if (ret < 0) {
1018 wl1271_error("failed to initiate cmd start bss");
1019 goto out_free;
1020 }
1021
1022out_free:
1023 kfree(cmd);
1024
1025out:
1026 return ret;
1027}
1028
1029int wl1271_cmd_stop_bss(struct wl1271 *wl)
1030{
1031 struct wl1271_cmd_bss_start *cmd;
1032 int ret;
1033
1034 wl1271_debug(DEBUG_CMD, "cmd stop bss");
1035
1036 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1037 if (!cmd) {
1038 ret = -ENOMEM;
1039 goto out;
1040 }
1041
1042 cmd->bss_index = WL1271_AP_BSS_INDEX;
1043
1044 ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1045 if (ret < 0) {
1046 wl1271_error("failed to initiate cmd stop bss");
1047 goto out_free;
1048 }
1049
1050out_free:
1051 kfree(cmd);
1052
1053out:
1054 return ret;
1055}
1056
1057int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1058{
1059 struct wl1271_cmd_add_sta *cmd;
1060 int ret;
1061
1062 wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1063
1064 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1065 if (!cmd) {
1066 ret = -ENOMEM;
1067 goto out;
1068 }
1069
1070 /* currently we don't support UAPSD */
1071 cmd->sp_len = 0;
1072
1073 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1074 cmd->bss_index = WL1271_AP_BSS_INDEX;
1075 cmd->aid = sta->aid;
1076 cmd->hlid = hlid;
1077
1078 /*
1079 * FIXME: Does STA support QOS? We need to propagate this info from
1080 * hostapd. Currently not that important since this is only used for
1081 * sending the correct flavor of null-data packet in response to a
1082 * trigger.
1083 */
1084 cmd->wmm = 0;
1085
1086 cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
1087 sta->supp_rates[wl->band]));
1088
1089 wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
1090
1091 ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
1092 if (ret < 0) {
1093 wl1271_error("failed to initiate cmd add sta");
1094 goto out_free;
1095 }
1096
1097out_free:
1098 kfree(cmd);
1099
1100out:
1101 return ret;
1102}
1103
1104int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
1105{
1106 struct wl1271_cmd_remove_sta *cmd;
1107 int ret;
1108
1109 wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
1110
1111 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1112 if (!cmd) {
1113 ret = -ENOMEM;
1114 goto out;
1115 }
1116
1117 cmd->hlid = hlid;
1118 /* We never send a deauth, mac80211 is in charge of this */
1119 cmd->reason_opcode = 0;
1120 cmd->send_deauth_flag = 0;
1121
1122 ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
1123 if (ret < 0) {
1124 wl1271_error("failed to initiate cmd remove sta");
1125 goto out_free;
1126 }
1127
05285cf9
AN
1128 /*
1129 * We are ok with a timeout here. The event is sometimes not sent
1130 * due to a firmware bug.
1131 */
1132 wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
98bdaabb
AN
1133
1134out_free:
1135 kfree(cmd);
1136
1137out:
1138 return ret;
1139}