]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/wl12xx/wl1271_cmd.c
Merge branch 'wireless-2.6' into wireless-next-2.6
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / wl12xx / wl1271_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>
f5fc0f86
LC
30
31#include "wl1271.h"
32#include "wl1271_reg.h"
7b048c52 33#include "wl1271_io.h"
f5fc0f86
LC
34#include "wl1271_acx.h"
35#include "wl12xx_80211.h"
36#include "wl1271_cmd.h"
99d84c1d 37#include "wl1271_event.h"
f5fc0f86 38
16092b5c 39#define WL1271_CMD_FAST_POLL_COUNT 50
bc0f03ea 40
f5fc0f86
LC
41/*
42 * send command to firmware
43 *
44 * @wl: wl struct
45 * @id: command id
46 * @buf: buffer containing the command, must work with dma
47 * @len: length of the buffer
48 */
fa867e73
JO
49int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
50 size_t res_len)
f5fc0f86
LC
51{
52 struct wl1271_cmd_header *cmd;
53 unsigned long timeout;
54 u32 intr;
55 int ret = 0;
ad150e96 56 u16 status;
bc0f03ea 57 u16 poll_count = 0;
f5fc0f86
LC
58
59 cmd = buf;
d0f63b20 60 cmd->id = cpu_to_le16(id);
f5fc0f86
LC
61 cmd->status = 0;
62
63 WARN_ON(len % 4 != 0);
64
7b048c52 65 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
f5fc0f86 66
7b048c52 67 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
f5fc0f86
LC
68
69 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
70
7b048c52 71 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
f5fc0f86
LC
72 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
73 if (time_after(jiffies, timeout)) {
74 wl1271_error("command complete timeout");
75 ret = -ETIMEDOUT;
76 goto out;
77 }
78
bc0f03ea 79 poll_count++;
16092b5c
JO
80 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
81 udelay(10);
82 else
83 msleep(1);
f5fc0f86 84
7b048c52 85 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
f5fc0f86
LC
86 }
87
3b775b4b 88 /* read back the status code of the command */
fa867e73
JO
89 if (res_len == 0)
90 res_len = sizeof(struct wl1271_cmd_header);
7b048c52 91 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
3b775b4b 92
ad150e96
JO
93 status = le16_to_cpu(cmd->status);
94 if (status != CMD_STATUS_SUCCESS) {
95 wl1271_error("command execute failure %d", status);
3b775b4b
JO
96 ret = -EIO;
97 }
98
7b048c52
TP
99 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
100 WL1271_ACX_INTR_CMD_COMPLETE);
f5fc0f86
LC
101
102out:
103 return ret;
104}
105
938e30c9 106static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
f5fc0f86
LC
107{
108 struct wl1271_cmd_cal_channel_tune *cmd;
109 int ret = 0;
110
111 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
112 if (!cmd)
113 return -ENOMEM;
114
115 cmd->test.id = TEST_CMD_CHANNEL_TUNE;
116
117 cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
118 /* set up any channel, 7 is in the middle of the range */
119 cmd->channel = 7;
120
121 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
122 if (ret < 0)
123 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
124
125 kfree(cmd);
126 return ret;
127}
128
938e30c9 129static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
f5fc0f86
LC
130{
131 struct wl1271_cmd_cal_update_ref_point *cmd;
132 int ret = 0;
133
134 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
135 if (!cmd)
136 return -ENOMEM;
137
138 cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
139
140 /* FIXME: still waiting for the correct values */
141 cmd->ref_power = 0;
142 cmd->ref_detector = 0;
143
144 cmd->sub_band = WL1271_PD_REFERENCE_POINT_BAND_B_G;
145
146 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
147 if (ret < 0)
148 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
149
150 kfree(cmd);
151 return ret;
152}
153
938e30c9 154static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
f5fc0f86
LC
155{
156 struct wl1271_cmd_cal_p2g *cmd;
157 int ret = 0;
158
159 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
160 if (!cmd)
161 return -ENOMEM;
162
163 cmd->test.id = TEST_CMD_P2G_CAL;
164
165 cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
166
167 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
168 if (ret < 0)
169 wl1271_warning("TEST_CMD_P2G_CAL failed");
170
171 kfree(cmd);
172 return ret;
173}
174
938e30c9 175static int wl1271_cmd_cal(struct wl1271 *wl)
f5fc0f86
LC
176{
177 /*
178 * FIXME: we must make sure that we're not sleeping when calibration
179 * is done
180 */
181 int ret;
182
183 wl1271_notice("performing tx calibration");
184
185 ret = wl1271_cmd_cal_channel_tune(wl);
186 if (ret < 0)
187 return ret;
188
189 ret = wl1271_cmd_cal_update_ref_point(wl);
190 if (ret < 0)
191 return ret;
192
193 ret = wl1271_cmd_cal_p2g(wl);
194 if (ret < 0)
195 return ret;
196
197 return ret;
198}
199
98b5dd5d
LC
200int wl1271_cmd_general_parms(struct wl1271 *wl)
201{
202 struct wl1271_general_parms_cmd *gen_parms;
98b5dd5d
LC
203 int ret;
204
152ee6e0
JO
205 if (!wl->nvs)
206 return -ENODEV;
207
98b5dd5d
LC
208 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
209 if (!gen_parms)
210 return -ENOMEM;
211
212 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
213
152ee6e0
JO
214 memcpy(gen_parms->params, wl->nvs->general_params,
215 WL1271_NVS_GENERAL_PARAMS_SIZE);
76c0f8d3 216
98b5dd5d
LC
217 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
218 if (ret < 0)
219 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
220
221 kfree(gen_parms);
222 return ret;
223}
224
225int wl1271_cmd_radio_parms(struct wl1271 *wl)
226{
227 struct wl1271_radio_parms_cmd *radio_parms;
152ee6e0
JO
228 struct conf_radio_parms *rparam = &wl->conf.init.radioparam;
229 int ret;
230
231 if (!wl->nvs)
232 return -ENODEV;
98b5dd5d
LC
233
234 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
235 if (!radio_parms)
236 return -ENOMEM;
237
238 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
239
152ee6e0
JO
240 memcpy(radio_parms->stat_radio_params, wl->nvs->stat_radio_params,
241 WL1271_NVS_STAT_RADIO_PARAMS_SIZE);
242 memcpy(radio_parms->dyn_radio_params,
243 wl->nvs->dyn_radio_params[rparam->fem],
244 WL1271_NVS_DYN_RADIO_PARAMS_SIZE);
245
246 /* FIXME: current NVS is missing 5GHz parameters */
98b5dd5d
LC
247
248 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
249 radio_parms, sizeof(*radio_parms));
250
251 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
252 if (ret < 0)
253 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
254
255 kfree(radio_parms);
256 return ret;
257}
258
99d84c1d
LC
259/*
260 * Poll the mailbox event field until any of the bits in the mask is set or a
261 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
262 */
263static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
264{
265 u32 events_vector, event;
266 unsigned long timeout;
267
268 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
269
270 do {
271 if (time_after(jiffies, timeout))
272 return -ETIMEDOUT;
273
274 msleep(1);
275
276 /* read from both event fields */
277 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
278 sizeof(events_vector), false);
279 event = events_vector & mask;
280 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
281 sizeof(events_vector), false);
282 event |= events_vector & mask;
283 } while (!event);
284
285 return 0;
286}
287
15305498 288int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
f5fc0f86
LC
289{
290 static bool do_cal = true;
f5fc0f86
LC
291 struct wl1271_cmd_join *join;
292 int ret, i;
293 u8 *bssid;
294
295 /* FIXME: remove when we get calibration from the factory */
296 if (do_cal) {
297 ret = wl1271_cmd_cal(wl);
298 if (ret < 0)
299 wl1271_warning("couldn't calibrate");
300 else
301 do_cal = false;
302 }
303
f5fc0f86
LC
304 join = kzalloc(sizeof(*join), GFP_KERNEL);
305 if (!join) {
306 ret = -ENOMEM;
307 goto out;
308 }
309
310 wl1271_debug(DEBUG_CMD, "cmd join");
311
312 /* Reverse order BSSID */
313 bssid = (u8 *) &join->bssid_lsb;
314 for (i = 0; i < ETH_ALEN; i++)
315 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
316
d0f63b20
LC
317 join->rx_config_options = cpu_to_le32(wl->rx_config);
318 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
15305498 319 join->bss_type = bss_type;
23a7a51c 320 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
f5fc0f86 321
ebba60c6 322 if (wl->band == IEEE80211_BAND_5GHZ)
a4102645 323 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
f5fc0f86 324
60e84c2e 325 join->beacon_interval = cpu_to_le16(wl->beacon_int);
ae751bab 326 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
a4102645 327
f5fc0f86
LC
328 join->channel = wl->channel;
329 join->ssid_len = wl->ssid_len;
330 memcpy(join->ssid, wl->ssid, wl->ssid_len);
331 join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
332
333 /* increment the session counter */
334 wl->session_counter++;
335 if (wl->session_counter >= SESSION_COUNTER_MAX)
336 wl->session_counter = 0;
337
338 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
339
ac4e4ce5
JO
340 /* reset TX security counters */
341 wl->tx_security_last_seq = 0;
04e36fc5 342 wl->tx_security_seq = 0;
f5fc0f86 343
fa867e73 344 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
f5fc0f86
LC
345 if (ret < 0) {
346 wl1271_error("failed to initiate cmd join");
347 goto out_free;
348 }
349
99d84c1d
LC
350 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
351 if (ret < 0)
352 wl1271_error("cmd join event completion error");
f5fc0f86
LC
353
354out_free:
355 kfree(join);
356
357out:
358 return ret;
359}
360
361/**
362 * send test command to firmware
363 *
364 * @wl: wl struct
365 * @buf: buffer containing the command, with all headers, must work with dma
366 * @len: length of the buffer
367 * @answer: is answer needed
368 */
369int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
370{
371 int ret;
fa867e73 372 size_t res_len = 0;
f5fc0f86
LC
373
374 wl1271_debug(DEBUG_CMD, "cmd test");
375
fa867e73
JO
376 if (answer)
377 res_len = buf_len;
378
379 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
f5fc0f86
LC
380
381 if (ret < 0) {
382 wl1271_warning("TEST command failed");
383 return ret;
384 }
385
fa867e73 386 return ret;
f5fc0f86
LC
387}
388
389/**
390 * read acx from firmware
391 *
392 * @wl: wl struct
393 * @id: acx id
394 * @buf: buffer for the response, including all headers, must work with dma
395 * @len: lenght of buf
396 */
397int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
398{
399 struct acx_header *acx = buf;
400 int ret;
401
402 wl1271_debug(DEBUG_CMD, "cmd interrogate");
403
d0f63b20 404 acx->id = cpu_to_le16(id);
f5fc0f86
LC
405
406 /* payload length, does not include any headers */
d0f63b20 407 acx->len = cpu_to_le16(len - sizeof(*acx));
f5fc0f86 408
fa867e73
JO
409 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
410 if (ret < 0)
f5fc0f86 411 wl1271_error("INTERROGATE command failed");
f5fc0f86 412
f5fc0f86
LC
413 return ret;
414}
415
416/**
417 * write acx value to firmware
418 *
419 * @wl: wl struct
420 * @id: acx id
421 * @buf: buffer containing acx, including all headers, must work with dma
422 * @len: length of buf
423 */
424int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
425{
426 struct acx_header *acx = buf;
427 int ret;
428
429 wl1271_debug(DEBUG_CMD, "cmd configure");
430
d0f63b20 431 acx->id = cpu_to_le16(id);
f5fc0f86
LC
432
433 /* payload length, does not include any headers */
d0f63b20 434 acx->len = cpu_to_le16(len - sizeof(*acx));
f5fc0f86 435
fa867e73 436 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
f5fc0f86
LC
437 if (ret < 0) {
438 wl1271_warning("CONFIGURE command NOK");
439 return ret;
440 }
441
442 return 0;
443}
444
94210897 445int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
f5fc0f86
LC
446{
447 struct cmd_enabledisable_path *cmd;
448 int ret;
449 u16 cmd_rx, cmd_tx;
450
451 wl1271_debug(DEBUG_CMD, "cmd data path");
452
453 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
454 if (!cmd) {
455 ret = -ENOMEM;
456 goto out;
457 }
458
94210897
LC
459 /* the channel here is only used for calibration, so hardcoded to 1 */
460 cmd->channel = 1;
f5fc0f86
LC
461
462 if (enable) {
463 cmd_rx = CMD_ENABLE_RX;
464 cmd_tx = CMD_ENABLE_TX;
465 } else {
466 cmd_rx = CMD_DISABLE_RX;
467 cmd_tx = CMD_DISABLE_TX;
468 }
469
fa867e73 470 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
471 if (ret < 0) {
472 wl1271_error("rx %s cmd for channel %d failed",
94210897 473 enable ? "start" : "stop", cmd->channel);
f5fc0f86
LC
474 goto out;
475 }
476
477 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
94210897 478 enable ? "start" : "stop", cmd->channel);
f5fc0f86 479
fa867e73 480 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
481 if (ret < 0) {
482 wl1271_error("tx %s cmd for channel %d failed",
94210897 483 enable ? "start" : "stop", cmd->channel);
1b00f2b5 484 goto out;
f5fc0f86
LC
485 }
486
487 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
94210897 488 enable ? "start" : "stop", cmd->channel);
f5fc0f86
LC
489
490out:
491 kfree(cmd);
492 return ret;
493}
494
d8c42c0c 495int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
f5fc0f86
LC
496{
497 struct wl1271_cmd_ps_params *ps_params = NULL;
498 int ret = 0;
499
500 /* FIXME: this should be in ps.c */
51f2be24 501 ret = wl1271_acx_wake_up_conditions(wl);
f5fc0f86
LC
502 if (ret < 0) {
503 wl1271_error("couldn't set wake up conditions");
504 goto out;
505 }
506
507 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
508
509 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
510 if (!ps_params) {
511 ret = -ENOMEM;
512 goto out;
513 }
514
515 ps_params->ps_mode = ps_mode;
d8c42c0c 516 ps_params->send_null_data = send;
f5fc0f86 517 ps_params->retries = 5;
cbd1ea87 518 ps_params->hang_over_period = 1;
d0f63b20 519 ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
f5fc0f86
LC
520
521 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
fa867e73 522 sizeof(*ps_params), 0);
f5fc0f86
LC
523 if (ret < 0) {
524 wl1271_error("cmd set_ps_mode failed");
525 goto out;
526 }
527
528out:
529 kfree(ps_params);
530 return ret;
531}
532
533int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
534 size_t len)
535{
536 struct cmd_read_write_memory *cmd;
537 int ret = 0;
538
539 wl1271_debug(DEBUG_CMD, "cmd read memory");
540
541 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
542 if (!cmd) {
543 ret = -ENOMEM;
544 goto out;
545 }
546
547 WARN_ON(len > MAX_READ_SIZE);
548 len = min_t(size_t, len, MAX_READ_SIZE);
549
d0f63b20
LC
550 cmd->addr = cpu_to_le32(addr);
551 cmd->size = cpu_to_le32(len);
f5fc0f86 552
fa867e73
JO
553 ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
554 sizeof(*cmd));
f5fc0f86
LC
555 if (ret < 0) {
556 wl1271_error("read memory command failed: %d", ret);
557 goto out;
558 }
559
fa867e73 560 /* the read command got in */
f5fc0f86
LC
561 memcpy(answer, cmd->value, len);
562
563out:
564 kfree(cmd);
565 return ret;
566}
567
818e3063
KV
568int wl1271_cmd_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
569 const u8 *ie, size_t ie_len, u8 active_scan,
570 u8 high_prio, u8 band, u8 probe_requests)
f5fc0f86
LC
571{
572
573 struct wl1271_cmd_trigger_scan_to *trigger = NULL;
574 struct wl1271_cmd_scan *params = NULL;
311494c4 575 struct ieee80211_channel *channels;
ebba60c6 576 u32 rate;
311494c4 577 int i, j, n_ch, ret;
f5fc0f86 578 u16 scan_options = 0;
abb0b3bf
TP
579 u8 ieee_band;
580
ebba60c6 581 if (band == WL1271_SCAN_BAND_2_4_GHZ) {
abb0b3bf 582 ieee_band = IEEE80211_BAND_2GHZ;
ebba60c6
JO
583 rate = wl->conf.tx.basic_rate;
584 } else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled()) {
abb0b3bf 585 ieee_band = IEEE80211_BAND_2GHZ;
ebba60c6
JO
586 rate = wl->conf.tx.basic_rate;
587 } else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled()) {
abb0b3bf 588 ieee_band = IEEE80211_BAND_5GHZ;
ebba60c6
JO
589 rate = wl->conf.tx.basic_rate_5;
590 } else
abb0b3bf 591 return -EINVAL;
f5fc0f86 592
abb0b3bf 593 if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
f5fc0f86
LC
594 return -EINVAL;
595
abb0b3bf
TP
596 channels = wl->hw->wiphy->bands[ieee_band]->channels;
597 n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
598
71449f8d 599 if (test_bit(WL1271_FLAG_SCANNING, &wl->flags))
abb0b3bf 600 return -EINVAL;
311494c4 601
f5fc0f86
LC
602 params = kzalloc(sizeof(*params), GFP_KERNEL);
603 if (!params)
604 return -ENOMEM;
605
606 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
607 params->params.rx_filter_options =
608 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
609
610 if (!active_scan)
611 scan_options |= WL1271_SCAN_OPT_PASSIVE;
612 if (high_prio)
613 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
d0f63b20 614 params->params.scan_options = cpu_to_le16(scan_options);
f5fc0f86 615
f5fc0f86 616 params->params.num_probe_requests = probe_requests;
23a7a51c 617 params->params.tx_rate = cpu_to_le32(rate);
f5fc0f86
LC
618 params->params.tid_trigger = 0;
619 params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
620
abb0b3bf
TP
621 if (band == WL1271_SCAN_BAND_DUAL)
622 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
623 else
624 params->params.band = band;
625
311494c4
TP
626 for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
627 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
628 params->channels[j].min_duration =
629 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
630 params->channels[j].max_duration =
631 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
632 memset(&params->channels[j].bssid_lsb, 0xff, 4);
633 memset(&params->channels[j].bssid_msb, 0xff, 2);
634 params->channels[j].early_termination = 0;
635 params->channels[j].tx_power_att =
636 WL1271_SCAN_CURRENT_TX_PWR;
637 params->channels[j].channel = channels[i].hw_value;
638 j++;
639 }
f5fc0f86
LC
640 }
641
311494c4
TP
642 params->params.num_channels = j;
643
818e3063
KV
644 if (ssid_len && ssid) {
645 params->params.ssid_len = ssid_len;
646 memcpy(params->params.ssid, ssid, ssid_len);
f5fc0f86
LC
647 }
648
818e3063
KV
649 ret = wl1271_cmd_build_probe_req(wl, ssid, ssid_len,
650 ie, ie_len, ieee_band);
f5fc0f86
LC
651 if (ret < 0) {
652 wl1271_error("PROBE request template failed");
653 goto out;
654 }
655
656 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
657 if (!trigger) {
658 ret = -ENOMEM;
659 goto out;
660 }
661
662 /* disable the timeout */
663 trigger->timeout = 0;
664
665 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
fa867e73 666 sizeof(*trigger), 0);
f5fc0f86
LC
667 if (ret < 0) {
668 wl1271_error("trigger scan to failed for hw scan");
669 goto out;
670 }
671
672 wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
673
71449f8d 674 set_bit(WL1271_FLAG_SCANNING, &wl->flags);
abb0b3bf
TP
675 if (wl1271_11a_enabled()) {
676 wl->scan.state = band;
677 if (band == WL1271_SCAN_BAND_DUAL) {
678 wl->scan.active = active_scan;
679 wl->scan.high_prio = high_prio;
680 wl->scan.probe_requests = probe_requests;
818e3063
KV
681 if (ssid_len && ssid) {
682 wl->scan.ssid_len = ssid_len;
683 memcpy(wl->scan.ssid, ssid, ssid_len);
abb0b3bf
TP
684 } else
685 wl->scan.ssid_len = 0;
686 }
687 }
f5fc0f86 688
fa867e73 689 ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
f5fc0f86
LC
690 if (ret < 0) {
691 wl1271_error("SCAN failed");
71449f8d 692 clear_bit(WL1271_FLAG_SCANNING, &wl->flags);
f5fc0f86
LC
693 goto out;
694 }
695
696out:
697 kfree(params);
9cea461f 698 kfree(trigger);
f5fc0f86
LC
699 return ret;
700}
701
702int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
606c1487 703 void *buf, size_t buf_len, int index, u32 rates)
f5fc0f86
LC
704{
705 struct wl1271_cmd_template_set *cmd;
706 int ret = 0;
707
708 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
709
710 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
711 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
712
713 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
714 if (!cmd) {
715 ret = -ENOMEM;
716 goto out;
717 }
718
719 cmd->len = cpu_to_le16(buf_len);
720 cmd->template_type = template_id;
606c1487 721 cmd->enabled_rates = cpu_to_le32(rates);
45b531a8
JO
722 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
723 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
bfb24c9e 724 cmd->index = index;
f5fc0f86
LC
725
726 if (buf)
727 memcpy(cmd->template_data, buf, buf_len);
728
fa867e73 729 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
730 if (ret < 0) {
731 wl1271_warning("cmd set_template failed: %d", ret);
732 goto out_free;
733 }
734
735out_free:
736 kfree(cmd);
737
738out:
739 return ret;
740}
741
f5fc0f86
LC
742int wl1271_cmd_build_null_data(struct wl1271 *wl)
743{
a0cb7be4
JO
744 struct sk_buff *skb = NULL;
745 int size;
746 void *ptr;
747 int ret = -ENOMEM;
f5fc0f86 748
f5fc0f86 749
a0cb7be4
JO
750 if (wl->bss_type == BSS_TYPE_IBSS) {
751 size = sizeof(struct wl12xx_null_data_template);
752 ptr = NULL;
753 } else {
754 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
755 if (!skb)
756 goto out;
757 size = skb->len;
758 ptr = skb->data;
759 }
760
606c1487
JO
761 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
762 WL1271_RATE_AUTOMATIC);
f5fc0f86 763
899e6e65
KV
764out:
765 dev_kfree_skb(skb);
a0cb7be4
JO
766 if (ret)
767 wl1271_warning("cmd buld null data failed %d", ret);
768
899e6e65 769 return ret;
f5fc0f86
LC
770
771}
772
bfb24c9e
JO
773int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
774{
775 struct sk_buff *skb = NULL;
776 int ret = -ENOMEM;
777
778 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
779 if (!skb)
780 goto out;
781
782 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
783 skb->data, skb->len,
606c1487
JO
784 CMD_TEMPL_KLV_IDX_NULL_DATA,
785 WL1271_RATE_AUTOMATIC);
bfb24c9e
JO
786
787out:
788 dev_kfree_skb(skb);
789 if (ret)
790 wl1271_warning("cmd build klv null data failed %d", ret);
791
792 return ret;
793
794}
795
f5fc0f86
LC
796int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
797{
899e6e65
KV
798 struct sk_buff *skb;
799 int ret = 0;
c3fea199 800
899e6e65
KV
801 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
802 if (!skb)
803 goto out;
f5fc0f86 804
899e6e65 805 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
606c1487 806 skb->len, 0, wl->basic_rate);
f5fc0f86 807
899e6e65
KV
808out:
809 dev_kfree_skb(skb);
810 return ret;
f5fc0f86
LC
811}
812
818e3063
KV
813int wl1271_cmd_build_probe_req(struct wl1271 *wl,
814 const u8 *ssid, size_t ssid_len,
815 const u8 *ie, size_t ie_len, u8 band)
f5fc0f86 816{
818e3063 817 struct sk_buff *skb;
abb0b3bf 818 int ret;
f5fc0f86 819
818e3063
KV
820 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
821 ie, ie_len);
822 if (!skb) {
823 ret = -ENOMEM;
824 goto out;
825 }
826
827 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
f5fc0f86 828
abb0b3bf
TP
829 if (band == IEEE80211_BAND_2GHZ)
830 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
606c1487
JO
831 skb->data, skb->len, 0,
832 wl->conf.tx.basic_rate);
abb0b3bf
TP
833 else
834 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
606c1487
JO
835 skb->data, skb->len, 0,
836 wl->conf.tx.basic_rate_5);
818e3063
KV
837
838out:
839 dev_kfree_skb(skb);
abb0b3bf 840 return ret;
f5fc0f86
LC
841}
842
023e0826
KV
843int wl1271_build_qos_null_data(struct wl1271 *wl)
844{
845 struct ieee80211_qos_hdr template;
846
847 memset(&template, 0, sizeof(template));
848
849 memcpy(template.addr1, wl->bssid, ETH_ALEN);
850 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
851 memcpy(template.addr3, wl->bssid, ETH_ALEN);
852
853 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
854 IEEE80211_STYPE_QOS_NULLFUNC |
855 IEEE80211_FCTL_TODS);
856
857 /* FIXME: not sure what priority to use here */
858 template.qos_ctrl = cpu_to_le16(0);
859
860 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
606c1487
JO
861 sizeof(template), 0,
862 WL1271_RATE_AUTOMATIC);
023e0826
KV
863}
864
f5fc0f86
LC
865int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
866{
867 struct wl1271_cmd_set_keys *cmd;
868 int ret = 0;
869
870 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
871
872 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
873 if (!cmd) {
874 ret = -ENOMEM;
875 goto out;
876 }
877
878 cmd->id = id;
d0f63b20 879 cmd->key_action = cpu_to_le16(KEY_SET_ID);
f5fc0f86
LC
880 cmd->key_type = KEY_WEP;
881
fa867e73 882 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
883 if (ret < 0) {
884 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
885 goto out;
886 }
887
888out:
889 kfree(cmd);
890
891 return ret;
892}
893
894int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
ac4e4ce5
JO
895 u8 key_size, const u8 *key, const u8 *addr,
896 u32 tx_seq_32, u16 tx_seq_16)
f5fc0f86
LC
897{
898 struct wl1271_cmd_set_keys *cmd;
899 int ret = 0;
900
901 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
902 if (!cmd) {
903 ret = -ENOMEM;
904 goto out;
905 }
906
907 if (key_type != KEY_WEP)
908 memcpy(cmd->addr, addr, ETH_ALEN);
909
d0f63b20 910 cmd->key_action = cpu_to_le16(action);
f5fc0f86
LC
911 cmd->key_size = key_size;
912 cmd->key_type = key_type;
913
d0f63b20
LC
914 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
915 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
ac4e4ce5 916
f5fc0f86
LC
917 /* we have only one SSID profile */
918 cmd->ssid_profile = 0;
919
920 cmd->id = id;
921
f5fc0f86
LC
922 if (key_type == KEY_TKIP) {
923 /*
924 * We get the key in the following form:
925 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
926 * but the target is expecting:
927 * TKIP - RX MIC - TX MIC
928 */
929 memcpy(cmd->key, key, 16);
930 memcpy(cmd->key + 16, key + 24, 8);
931 memcpy(cmd->key + 24, key + 16, 8);
932
933 } else {
934 memcpy(cmd->key, key, key_size);
935 }
936
937 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
938
fa867e73 939 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
940 if (ret < 0) {
941 wl1271_warning("could not set keys");
152ee6e0 942 goto out;
f5fc0f86
LC
943 }
944
945out:
946 kfree(cmd);
947
948 return ret;
949}
25a7dc6d
LC
950
951int wl1271_cmd_disconnect(struct wl1271 *wl)
952{
953 struct wl1271_cmd_disconnect *cmd;
954 int ret = 0;
955
956 wl1271_debug(DEBUG_CMD, "cmd disconnect");
957
958 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
959 if (!cmd) {
960 ret = -ENOMEM;
961 goto out;
962 }
963
d0f63b20
LC
964 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
965 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
25a7dc6d
LC
966 /* disconnect reason is not used in immediate disconnections */
967 cmd->type = DISCONNECT_IMMEDIATE;
968
fa867e73 969 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
25a7dc6d
LC
970 if (ret < 0) {
971 wl1271_error("failed to send disconnect command");
972 goto out_free;
973 }
974
2f826f55
LC
975 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
976 if (ret < 0)
977 wl1271_error("cmd disconnect event completion error");
978
25a7dc6d
LC
979out_free:
980 kfree(cmd);
981
982out:
983 return ret;
984}