blob: 6d7a40c004f15ff97bd8774c4f29429edfc2491b [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009 Nokia Corporation
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>
29
30#include "wl1271.h"
31#include "wl1271_reg.h"
32#include "wl1271_spi.h"
33#include "wl1271_acx.h"
34#include "wl12xx_80211.h"
35#include "wl1271_cmd.h"
36
37/*
38 * send command to firmware
39 *
40 * @wl: wl struct
41 * @id: command id
42 * @buf: buffer containing the command, must work with dma
43 * @len: length of the buffer
44 */
45int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len)
46{
47 struct wl1271_cmd_header *cmd;
48 unsigned long timeout;
49 u32 intr;
50 int ret = 0;
51
52 cmd = buf;
53 cmd->id = id;
54 cmd->status = 0;
55
56 WARN_ON(len % 4 != 0);
57
Juuso Oikarinen74621412009-10-12 15:08:54 +030058 wl1271_spi_write(wl, wl->cmd_box_addr, buf, len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030059
Juuso Oikarinen74621412009-10-12 15:08:54 +030060 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030061
62 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
63
Juuso Oikarinen74621412009-10-12 15:08:54 +030064 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030065 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
66 if (time_after(jiffies, timeout)) {
67 wl1271_error("command complete timeout");
68 ret = -ETIMEDOUT;
69 goto out;
70 }
71
72 msleep(1);
73
Juuso Oikarinen74621412009-10-12 15:08:54 +030074 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030075 }
76
Juuso Oikarinen74621412009-10-12 15:08:54 +030077 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030078 WL1271_ACX_INTR_CMD_COMPLETE);
79
80out:
81 return ret;
82}
83
84int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
85{
86 struct wl1271_cmd_cal_channel_tune *cmd;
87 int ret = 0;
88
89 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
90 if (!cmd)
91 return -ENOMEM;
92
93 cmd->test.id = TEST_CMD_CHANNEL_TUNE;
94
95 cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
96 /* set up any channel, 7 is in the middle of the range */
97 cmd->channel = 7;
98
99 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
100 if (ret < 0)
101 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
102
103 kfree(cmd);
104 return ret;
105}
106
107int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
108{
109 struct wl1271_cmd_cal_update_ref_point *cmd;
110 int ret = 0;
111
112 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
113 if (!cmd)
114 return -ENOMEM;
115
116 cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
117
118 /* FIXME: still waiting for the correct values */
119 cmd->ref_power = 0;
120 cmd->ref_detector = 0;
121
122 cmd->sub_band = WL1271_PD_REFERENCE_POINT_BAND_B_G;
123
124 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
125 if (ret < 0)
126 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
127
128 kfree(cmd);
129 return ret;
130}
131
132int wl1271_cmd_cal_p2g(struct wl1271 *wl)
133{
134 struct wl1271_cmd_cal_p2g *cmd;
135 int ret = 0;
136
137 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
138 if (!cmd)
139 return -ENOMEM;
140
141 cmd->test.id = TEST_CMD_P2G_CAL;
142
143 cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
144
145 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
146 if (ret < 0)
147 wl1271_warning("TEST_CMD_P2G_CAL failed");
148
149 kfree(cmd);
150 return ret;
151}
152
153int wl1271_cmd_cal(struct wl1271 *wl)
154{
155 /*
156 * FIXME: we must make sure that we're not sleeping when calibration
157 * is done
158 */
159 int ret;
160
161 wl1271_notice("performing tx calibration");
162
163 ret = wl1271_cmd_cal_channel_tune(wl);
164 if (ret < 0)
165 return ret;
166
167 ret = wl1271_cmd_cal_update_ref_point(wl);
168 if (ret < 0)
169 return ret;
170
171 ret = wl1271_cmd_cal_p2g(wl);
172 if (ret < 0)
173 return ret;
174
175 return ret;
176}
177
Juuso Oikarinend94cd292009-10-08 21:56:25 +0300178int wl1271_cmd_join(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300179{
180 static bool do_cal = true;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300181 struct wl1271_cmd_join *join;
182 int ret, i;
183 u8 *bssid;
184
185 /* FIXME: remove when we get calibration from the factory */
186 if (do_cal) {
187 ret = wl1271_cmd_cal(wl);
188 if (ret < 0)
189 wl1271_warning("couldn't calibrate");
190 else
191 do_cal = false;
192 }
193
Luciano Coelhod6e19d132009-10-12 15:08:43 +0300194 /* FIXME: This is a workaround, because with the current stack, we
195 * cannot know when we have disassociated. So, if we have already
196 * joined, we disconnect before joining again. */
197 if (wl->joined) {
198 ret = wl1271_cmd_disconnect(wl);
199 if (ret < 0) {
200 wl1271_error("failed to disconnect before rejoining");
201 goto out;
202 }
203
204 wl->joined = false;
205 }
206
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300207 join = kzalloc(sizeof(*join), GFP_KERNEL);
208 if (!join) {
209 ret = -ENOMEM;
210 goto out;
211 }
212
213 wl1271_debug(DEBUG_CMD, "cmd join");
214
215 /* Reverse order BSSID */
216 bssid = (u8 *) &join->bssid_lsb;
217 for (i = 0; i < ETH_ALEN; i++)
218 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
219
220 join->rx_config_options = wl->rx_config;
221 join->rx_filter_options = wl->rx_filter;
222
Luciano Coelho64a7f672009-10-08 21:56:28 +0300223 /*
224 * FIXME: disable temporarily all filters because after commit
225 * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
226 * association. The filter logic needs to be implemented properly
227 * and once that is done, this hack can be removed.
228 */
229 join->rx_config_options = 0;
230 join->rx_filter_options = WL1271_DEFAULT_RX_FILTER;
231
Juuso Oikarinen2b60100b2009-10-13 12:47:39 +0300232 join->basic_rate_set = CONF_HW_BIT_RATE_1MBPS | CONF_HW_BIT_RATE_2MBPS |
233 CONF_HW_BIT_RATE_5_5MBPS | CONF_HW_BIT_RATE_11MBPS;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300234
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300235 join->beacon_interval = WL1271_DEFAULT_BEACON_INT;
236 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Juuso Oikarinend94cd292009-10-08 21:56:25 +0300237 join->bss_type = wl->bss_type;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300238 join->channel = wl->channel;
239 join->ssid_len = wl->ssid_len;
240 memcpy(join->ssid, wl->ssid, wl->ssid_len);
241 join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
242
243 /* increment the session counter */
244 wl->session_counter++;
245 if (wl->session_counter >= SESSION_COUNTER_MAX)
246 wl->session_counter = 0;
247
248 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
249
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300250 /* reset TX security counters */
251 wl->tx_security_last_seq = 0;
252 wl->tx_security_seq_16 = 0;
253 wl->tx_security_seq_32 = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300254
255 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
256 if (ret < 0) {
257 wl1271_error("failed to initiate cmd join");
258 goto out_free;
259 }
260
Luciano Coelhod6e19d132009-10-12 15:08:43 +0300261 wl->joined = true;
262
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300263 /*
264 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
265 * simplify locking we just sleep instead, for now
266 */
Juuso Oikarinend94cd292009-10-08 21:56:25 +0300267 msleep(10);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300268
269out_free:
270 kfree(join);
271
272out:
273 return ret;
274}
275
276/**
277 * send test command to firmware
278 *
279 * @wl: wl struct
280 * @buf: buffer containing the command, with all headers, must work with dma
281 * @len: length of the buffer
282 * @answer: is answer needed
283 */
284int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
285{
286 int ret;
287
288 wl1271_debug(DEBUG_CMD, "cmd test");
289
290 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len);
291
292 if (ret < 0) {
293 wl1271_warning("TEST command failed");
294 return ret;
295 }
296
297 if (answer) {
298 struct wl1271_command *cmd_answer;
299
300 /*
301 * The test command got in, we can read the answer.
302 * The answer would be a wl1271_command, where the
303 * parameter array contains the actual answer.
304 */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300305 wl1271_spi_read(wl, wl->cmd_box_addr, buf, buf_len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300306
307 cmd_answer = buf;
308
309 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
310 wl1271_error("TEST command answer error: %d",
311 cmd_answer->header.status);
312 }
313
314 return 0;
315}
316
317/**
318 * read acx from firmware
319 *
320 * @wl: wl struct
321 * @id: acx id
322 * @buf: buffer for the response, including all headers, must work with dma
323 * @len: lenght of buf
324 */
325int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
326{
327 struct acx_header *acx = buf;
328 int ret;
329
330 wl1271_debug(DEBUG_CMD, "cmd interrogate");
331
332 acx->id = id;
333
334 /* payload length, does not include any headers */
335 acx->len = len - sizeof(*acx);
336
337 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
338 if (ret < 0) {
339 wl1271_error("INTERROGATE command failed");
340 goto out;
341 }
342
343 /* the interrogate command got in, we can read the answer */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300344 wl1271_spi_read(wl, wl->cmd_box_addr, buf, len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300345
346 acx = buf;
347 if (acx->cmd.status != CMD_STATUS_SUCCESS)
348 wl1271_error("INTERROGATE command error: %d",
349 acx->cmd.status);
350
351out:
352 return ret;
353}
354
355/**
356 * write acx value to firmware
357 *
358 * @wl: wl struct
359 * @id: acx id
360 * @buf: buffer containing acx, including all headers, must work with dma
361 * @len: length of buf
362 */
363int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
364{
365 struct acx_header *acx = buf;
366 int ret;
367
368 wl1271_debug(DEBUG_CMD, "cmd configure");
369
370 acx->id = id;
371
372 /* payload length, does not include any headers */
373 acx->len = len - sizeof(*acx);
374
375 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len);
376 if (ret < 0) {
377 wl1271_warning("CONFIGURE command NOK");
378 return ret;
379 }
380
381 return 0;
382}
383
384int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
385{
386 struct cmd_enabledisable_path *cmd;
387 int ret;
388 u16 cmd_rx, cmd_tx;
389
390 wl1271_debug(DEBUG_CMD, "cmd data path");
391
392 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
393 if (!cmd) {
394 ret = -ENOMEM;
395 goto out;
396 }
397
398 cmd->channel = channel;
399
400 if (enable) {
401 cmd_rx = CMD_ENABLE_RX;
402 cmd_tx = CMD_ENABLE_TX;
403 } else {
404 cmd_rx = CMD_DISABLE_RX;
405 cmd_tx = CMD_DISABLE_TX;
406 }
407
408 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
409 if (ret < 0) {
410 wl1271_error("rx %s cmd for channel %d failed",
411 enable ? "start" : "stop", channel);
412 goto out;
413 }
414
415 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
416 enable ? "start" : "stop", channel);
417
418 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
419 if (ret < 0) {
420 wl1271_error("tx %s cmd for channel %d failed",
421 enable ? "start" : "stop", channel);
422 return ret;
423 }
424
425 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
426 enable ? "start" : "stop", channel);
427
428out:
429 kfree(cmd);
430 return ret;
431}
432
433int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
434{
435 struct wl1271_cmd_ps_params *ps_params = NULL;
436 int ret = 0;
437
438 /* FIXME: this should be in ps.c */
Juuso Oikarinen51f2be22009-10-13 12:47:42 +0300439 ret = wl1271_acx_wake_up_conditions(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300440 if (ret < 0) {
441 wl1271_error("couldn't set wake up conditions");
442 goto out;
443 }
444
445 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
446
447 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
448 if (!ps_params) {
449 ret = -ENOMEM;
450 goto out;
451 }
452
453 ps_params->ps_mode = ps_mode;
454 ps_params->send_null_data = 1;
455 ps_params->retries = 5;
456 ps_params->hang_over_period = 128;
457 ps_params->null_data_rate = 1; /* 1 Mbps */
458
459 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
460 sizeof(*ps_params));
461 if (ret < 0) {
462 wl1271_error("cmd set_ps_mode failed");
463 goto out;
464 }
465
466out:
467 kfree(ps_params);
468 return ret;
469}
470
471int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
472 size_t len)
473{
474 struct cmd_read_write_memory *cmd;
475 int ret = 0;
476
477 wl1271_debug(DEBUG_CMD, "cmd read memory");
478
479 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
480 if (!cmd) {
481 ret = -ENOMEM;
482 goto out;
483 }
484
485 WARN_ON(len > MAX_READ_SIZE);
486 len = min_t(size_t, len, MAX_READ_SIZE);
487
488 cmd->addr = addr;
489 cmd->size = len;
490
491 ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
492 if (ret < 0) {
493 wl1271_error("read memory command failed: %d", ret);
494 goto out;
495 }
496
497 /* the read command got in, we can now read the answer */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300498 wl1271_spi_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd), false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300499
500 if (cmd->header.status != CMD_STATUS_SUCCESS)
501 wl1271_error("error in read command result: %d",
502 cmd->header.status);
503
504 memcpy(answer, cmd->value, len);
505
506out:
507 kfree(cmd);
508 return ret;
509}
510
511int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300512 u8 active_scan, u8 high_prio, u8 band,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300513 u8 probe_requests)
514{
515
516 struct wl1271_cmd_trigger_scan_to *trigger = NULL;
517 struct wl1271_cmd_scan *params = NULL;
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300518 struct ieee80211_channel *channels;
519 int i, j, n_ch, ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300520 u16 scan_options = 0;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300521 u8 ieee_band;
522
523 if (band == WL1271_SCAN_BAND_2_4_GHZ)
524 ieee_band = IEEE80211_BAND_2GHZ;
525 else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
526 ieee_band = IEEE80211_BAND_2GHZ;
527 else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
528 ieee_band = IEEE80211_BAND_5GHZ;
529 else
530 return -EINVAL;
531
532 if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
533 return -EINVAL;
534
535 channels = wl->hw->wiphy->bands[ieee_band]->channels;
536 n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300537
538 if (wl->scanning)
539 return -EINVAL;
540
541 params = kzalloc(sizeof(*params), GFP_KERNEL);
542 if (!params)
543 return -ENOMEM;
544
545 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
546 params->params.rx_filter_options =
547 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
548
549 if (!active_scan)
550 scan_options |= WL1271_SCAN_OPT_PASSIVE;
551 if (high_prio)
552 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
553 params->params.scan_options = scan_options;
554
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300555 params->params.num_probe_requests = probe_requests;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300556 /* Let the fw autodetect suitable tx_rate for probes */
557 params->params.tx_rate = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300558 params->params.tid_trigger = 0;
559 params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
560
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300561 if (band == WL1271_SCAN_BAND_DUAL)
562 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
563 else
564 params->params.band = band;
565
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300566 for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
567 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
568 params->channels[j].min_duration =
569 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
570 params->channels[j].max_duration =
571 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
572 memset(&params->channels[j].bssid_lsb, 0xff, 4);
573 memset(&params->channels[j].bssid_msb, 0xff, 2);
574 params->channels[j].early_termination = 0;
575 params->channels[j].tx_power_att =
576 WL1271_SCAN_CURRENT_TX_PWR;
577 params->channels[j].channel = channels[i].hw_value;
578 j++;
579 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300580 }
581
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300582 params->params.num_channels = j;
583
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300584 if (len && ssid) {
585 params->params.ssid_len = len;
586 memcpy(params->params.ssid, ssid, len);
587 }
588
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300589 ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300590 if (ret < 0) {
591 wl1271_error("PROBE request template failed");
592 goto out;
593 }
594
595 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
596 if (!trigger) {
597 ret = -ENOMEM;
598 goto out;
599 }
600
601 /* disable the timeout */
602 trigger->timeout = 0;
603
604 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
605 sizeof(*trigger));
606 if (ret < 0) {
607 wl1271_error("trigger scan to failed for hw scan");
608 goto out;
609 }
610
611 wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
612
613 wl->scanning = true;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300614 if (wl1271_11a_enabled()) {
615 wl->scan.state = band;
616 if (band == WL1271_SCAN_BAND_DUAL) {
617 wl->scan.active = active_scan;
618 wl->scan.high_prio = high_prio;
619 wl->scan.probe_requests = probe_requests;
620 if (len && ssid) {
621 wl->scan.ssid_len = len;
622 memcpy(wl->scan.ssid, ssid, len);
623 } else
624 wl->scan.ssid_len = 0;
625 }
626 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300627
628 ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
629 if (ret < 0) {
630 wl1271_error("SCAN failed");
631 goto out;
632 }
633
Juuso Oikarinen74621412009-10-12 15:08:54 +0300634 wl1271_spi_read(wl, wl->cmd_box_addr, params, sizeof(*params),
635 false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300636
637 if (params->header.status != CMD_STATUS_SUCCESS) {
638 wl1271_error("Scan command error: %d",
639 params->header.status);
640 wl->scanning = false;
641 ret = -EIO;
642 goto out;
643 }
644
645out:
646 kfree(params);
647 return ret;
648}
649
650int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
651 void *buf, size_t buf_len)
652{
653 struct wl1271_cmd_template_set *cmd;
654 int ret = 0;
655
656 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
657
658 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
659 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
660
661 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
662 if (!cmd) {
663 ret = -ENOMEM;
664 goto out;
665 }
666
667 cmd->len = cpu_to_le16(buf_len);
668 cmd->template_type = template_id;
Juuso Oikarinen45b531a2009-10-13 12:47:41 +0300669 cmd->enabled_rates = wl->conf.tx.rc_conf.enabled_rates;
670 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
671 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300672
673 if (buf)
674 memcpy(cmd->template_data, buf, buf_len);
675
676 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd));
677 if (ret < 0) {
678 wl1271_warning("cmd set_template failed: %d", ret);
679 goto out_free;
680 }
681
682out_free:
683 kfree(cmd);
684
685out:
686 return ret;
687}
688
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300689static int wl1271_build_basic_rates(char *rates, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300690{
691 u8 index = 0;
692
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300693 if (band == IEEE80211_BAND_2GHZ) {
694 rates[index++] =
695 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
696 rates[index++] =
697 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
698 rates[index++] =
699 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
700 rates[index++] =
701 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
702 } else if (band == IEEE80211_BAND_5GHZ) {
703 rates[index++] =
704 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
705 rates[index++] =
706 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
707 rates[index++] =
708 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
709 } else {
710 wl1271_error("build_basic_rates invalid band: %d", band);
711 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300712
713 return index;
714}
715
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300716static int wl1271_build_extended_rates(char *rates, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300717{
718 u8 index = 0;
719
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300720 if (band == IEEE80211_BAND_2GHZ) {
721 rates[index++] = IEEE80211_OFDM_RATE_6MB;
722 rates[index++] = IEEE80211_OFDM_RATE_9MB;
723 rates[index++] = IEEE80211_OFDM_RATE_12MB;
724 rates[index++] = IEEE80211_OFDM_RATE_18MB;
725 rates[index++] = IEEE80211_OFDM_RATE_24MB;
726 rates[index++] = IEEE80211_OFDM_RATE_36MB;
727 rates[index++] = IEEE80211_OFDM_RATE_48MB;
728 rates[index++] = IEEE80211_OFDM_RATE_54MB;
729 } else if (band == IEEE80211_BAND_5GHZ) {
730 rates[index++] =
731 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
732 rates[index++] =
733 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
734 rates[index++] =
735 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
736 rates[index++] =
737 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
738 rates[index++] =
739 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
740 rates[index++] =
741 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
742 } else {
743 wl1271_error("build_basic_rates invalid band: %d", band);
744 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300745
746 return index;
747}
748
749int wl1271_cmd_build_null_data(struct wl1271 *wl)
750{
751 struct wl12xx_null_data_template template;
752
753 if (!is_zero_ether_addr(wl->bssid)) {
754 memcpy(template.header.da, wl->bssid, ETH_ALEN);
755 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
756 } else {
757 memset(template.header.da, 0xff, ETH_ALEN);
758 memset(template.header.bssid, 0xff, ETH_ALEN);
759 }
760
761 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
762 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
763 IEEE80211_STYPE_NULLFUNC);
764
765 return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
766 sizeof(template));
767
768}
769
770int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
771{
772 struct wl12xx_ps_poll_template template;
773
774 memcpy(template.bssid, wl->bssid, ETH_ALEN);
775 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300776
777 /* aid in PS-Poll has its two MSBs each set to 1 */
778 template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
779
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300780 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
781
782 return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
783 sizeof(template));
784
785}
786
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300787int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
788 u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300789{
790 struct wl12xx_probe_req_template template;
791 struct wl12xx_ie_rates *rates;
792 char *ptr;
793 u16 size;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300794 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300795
796 ptr = (char *)&template;
797 size = sizeof(struct ieee80211_header);
798
799 memset(template.header.da, 0xff, ETH_ALEN);
800 memset(template.header.bssid, 0xff, ETH_ALEN);
801 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
802 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
803
804 /* IEs */
805 /* SSID */
806 template.ssid.header.id = WLAN_EID_SSID;
807 template.ssid.header.len = ssid_len;
808 if (ssid_len && ssid)
809 memcpy(template.ssid.ssid, ssid, ssid_len);
810 size += sizeof(struct wl12xx_ie_header) + ssid_len;
811 ptr += size;
812
813 /* Basic Rates */
814 rates = (struct wl12xx_ie_rates *)ptr;
815 rates->header.id = WLAN_EID_SUPP_RATES;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300816 rates->header.len = wl1271_build_basic_rates(rates->rates, band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300817 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
818 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
819
820 /* Extended rates */
821 rates = (struct wl12xx_ie_rates *)ptr;
822 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300823 rates->header.len = wl1271_build_extended_rates(rates->rates, band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300824 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
825
826 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
827
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300828 if (band == IEEE80211_BAND_2GHZ)
829 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
830 &template, size);
831 else
832 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
833 &template, size);
834 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300835}
836
837int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
838{
839 struct wl1271_cmd_set_keys *cmd;
840 int ret = 0;
841
842 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
843
844 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
845 if (!cmd) {
846 ret = -ENOMEM;
847 goto out;
848 }
849
850 cmd->id = id;
851 cmd->key_action = KEY_SET_ID;
852 cmd->key_type = KEY_WEP;
853
854 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
855 if (ret < 0) {
856 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
857 goto out;
858 }
859
860out:
861 kfree(cmd);
862
863 return ret;
864}
865
866int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300867 u8 key_size, const u8 *key, const u8 *addr,
868 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300869{
870 struct wl1271_cmd_set_keys *cmd;
871 int ret = 0;
872
873 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
874 if (!cmd) {
875 ret = -ENOMEM;
876 goto out;
877 }
878
879 if (key_type != KEY_WEP)
880 memcpy(cmd->addr, addr, ETH_ALEN);
881
882 cmd->key_action = action;
883 cmd->key_size = key_size;
884 cmd->key_type = key_type;
885
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300886 cmd->ac_seq_num16[0] = tx_seq_16;
887 cmd->ac_seq_num32[0] = tx_seq_32;
888
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300889 /* we have only one SSID profile */
890 cmd->ssid_profile = 0;
891
892 cmd->id = id;
893
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300894 if (key_type == KEY_TKIP) {
895 /*
896 * We get the key in the following form:
897 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
898 * but the target is expecting:
899 * TKIP - RX MIC - TX MIC
900 */
901 memcpy(cmd->key, key, 16);
902 memcpy(cmd->key + 16, key + 24, 8);
903 memcpy(cmd->key + 24, key + 16, 8);
904
905 } else {
906 memcpy(cmd->key, key, key_size);
907 }
908
909 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
910
911 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
912 if (ret < 0) {
913 wl1271_warning("could not set keys");
914 goto out;
915 }
916
917out:
918 kfree(cmd);
919
920 return ret;
921}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300922
923int wl1271_cmd_disconnect(struct wl1271 *wl)
924{
925 struct wl1271_cmd_disconnect *cmd;
926 int ret = 0;
927
928 wl1271_debug(DEBUG_CMD, "cmd disconnect");
929
930 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
931 if (!cmd) {
932 ret = -ENOMEM;
933 goto out;
934 }
935
936 cmd->rx_config_options = wl->rx_config;
937 cmd->rx_filter_options = wl->rx_filter;
938 /* disconnect reason is not used in immediate disconnections */
939 cmd->type = DISCONNECT_IMMEDIATE;
940
941 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd));
942 if (ret < 0) {
943 wl1271_error("failed to send disconnect command");
944 goto out_free;
945 }
946
947out_free:
948 kfree(cmd);
949
950out:
951 return ret;
952}