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