blob: 990eb01b4c717f459911047f14194c8501cc1dea [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 */
Juuso Oikarinenfa867e72009-11-02 20:22:13 +020045int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
46 size_t res_len)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030047{
48 struct wl1271_cmd_header *cmd;
49 unsigned long timeout;
50 u32 intr;
51 int ret = 0;
Juuso Oikarinenad150e92009-11-02 20:22:12 +020052 u16 status;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030053
54 cmd = buf;
Luciano Coelhod0f63b22009-10-15 10:33:29 +030055 cmd->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030056 cmd->status = 0;
57
58 WARN_ON(len % 4 != 0);
59
Juuso Oikarinen74621412009-10-12 15:08:54 +030060 wl1271_spi_write(wl, wl->cmd_box_addr, buf, len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030061
Juuso Oikarinen74621412009-10-12 15:08:54 +030062 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030063
64 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
65
Juuso Oikarinen74621412009-10-12 15:08:54 +030066 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030067 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
68 if (time_after(jiffies, timeout)) {
69 wl1271_error("command complete timeout");
70 ret = -ETIMEDOUT;
71 goto out;
72 }
73
74 msleep(1);
75
Juuso Oikarinen74621412009-10-12 15:08:54 +030076 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030077 }
78
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020079 /* read back the status code of the command */
Juuso Oikarinenfa867e72009-11-02 20:22:13 +020080 if (res_len == 0)
81 res_len = sizeof(struct wl1271_cmd_header);
82 wl1271_spi_read(wl, wl->cmd_box_addr, cmd, res_len, false);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020083
Juuso Oikarinenad150e92009-11-02 20:22:12 +020084 status = le16_to_cpu(cmd->status);
85 if (status != CMD_STATUS_SUCCESS) {
86 wl1271_error("command execute failure %d", status);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020087 ret = -EIO;
88 }
89
Juuso Oikarinen74621412009-10-12 15:08:54 +030090 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030091 WL1271_ACX_INTR_CMD_COMPLETE);
92
93out:
94 return ret;
95}
96
Luciano Coelho938e30c2009-10-15 10:33:27 +030097static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030098{
99 struct wl1271_cmd_cal_channel_tune *cmd;
100 int ret = 0;
101
102 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
103 if (!cmd)
104 return -ENOMEM;
105
106 cmd->test.id = TEST_CMD_CHANNEL_TUNE;
107
108 cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
109 /* set up any channel, 7 is in the middle of the range */
110 cmd->channel = 7;
111
112 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
113 if (ret < 0)
114 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
115
116 kfree(cmd);
117 return ret;
118}
119
Luciano Coelho938e30c2009-10-15 10:33:27 +0300120static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300121{
122 struct wl1271_cmd_cal_update_ref_point *cmd;
123 int ret = 0;
124
125 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
126 if (!cmd)
127 return -ENOMEM;
128
129 cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
130
131 /* FIXME: still waiting for the correct values */
132 cmd->ref_power = 0;
133 cmd->ref_detector = 0;
134
135 cmd->sub_band = WL1271_PD_REFERENCE_POINT_BAND_B_G;
136
137 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
138 if (ret < 0)
139 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
140
141 kfree(cmd);
142 return ret;
143}
144
Luciano Coelho938e30c2009-10-15 10:33:27 +0300145static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300146{
147 struct wl1271_cmd_cal_p2g *cmd;
148 int ret = 0;
149
150 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
151 if (!cmd)
152 return -ENOMEM;
153
154 cmd->test.id = TEST_CMD_P2G_CAL;
155
156 cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
157
158 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
159 if (ret < 0)
160 wl1271_warning("TEST_CMD_P2G_CAL failed");
161
162 kfree(cmd);
163 return ret;
164}
165
Luciano Coelho938e30c2009-10-15 10:33:27 +0300166static int wl1271_cmd_cal(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300167{
168 /*
169 * FIXME: we must make sure that we're not sleeping when calibration
170 * is done
171 */
172 int ret;
173
174 wl1271_notice("performing tx calibration");
175
176 ret = wl1271_cmd_cal_channel_tune(wl);
177 if (ret < 0)
178 return ret;
179
180 ret = wl1271_cmd_cal_update_ref_point(wl);
181 if (ret < 0)
182 return ret;
183
184 ret = wl1271_cmd_cal_p2g(wl);
185 if (ret < 0)
186 return ret;
187
188 return ret;
189}
190
Juuso Oikarinend94cd292009-10-08 21:56:25 +0300191int wl1271_cmd_join(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300192{
193 static bool do_cal = true;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300194 struct wl1271_cmd_join *join;
195 int ret, i;
196 u8 *bssid;
197
198 /* FIXME: remove when we get calibration from the factory */
199 if (do_cal) {
200 ret = wl1271_cmd_cal(wl);
201 if (ret < 0)
202 wl1271_warning("couldn't calibrate");
203 else
204 do_cal = false;
205 }
206
Luciano Coelhod6e19d132009-10-12 15:08:43 +0300207 /* FIXME: This is a workaround, because with the current stack, we
208 * cannot know when we have disassociated. So, if we have already
209 * joined, we disconnect before joining again. */
210 if (wl->joined) {
211 ret = wl1271_cmd_disconnect(wl);
212 if (ret < 0) {
213 wl1271_error("failed to disconnect before rejoining");
214 goto out;
215 }
216
217 wl->joined = false;
218 }
219
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300220 join = kzalloc(sizeof(*join), GFP_KERNEL);
221 if (!join) {
222 ret = -ENOMEM;
223 goto out;
224 }
225
226 wl1271_debug(DEBUG_CMD, "cmd join");
227
228 /* Reverse order BSSID */
229 bssid = (u8 *) &join->bssid_lsb;
230 for (i = 0; i < ETH_ALEN; i++)
231 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
232
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300233 join->rx_config_options = cpu_to_le32(wl->rx_config);
234 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
Teemu Paasikivia4102642009-10-13 12:47:51 +0300235 join->bss_type = wl->bss_type;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300236
Luciano Coelho64a7f672009-10-08 21:56:28 +0300237 /*
238 * FIXME: disable temporarily all filters because after commit
239 * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
240 * association. The filter logic needs to be implemented properly
241 * and once that is done, this hack can be removed.
242 */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300243 join->rx_config_options = cpu_to_le32(0);
244 join->rx_filter_options = cpu_to_le32(WL1271_DEFAULT_RX_FILTER);
Luciano Coelho64a7f672009-10-08 21:56:28 +0300245
Teemu Paasikivia4102642009-10-13 12:47:51 +0300246 if (wl->band == IEEE80211_BAND_2GHZ)
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300247 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_1MBPS |
248 CONF_HW_BIT_RATE_2MBPS |
249 CONF_HW_BIT_RATE_5_5MBPS |
250 CONF_HW_BIT_RATE_11MBPS);
Teemu Paasikivia4102642009-10-13 12:47:51 +0300251 else {
252 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300253 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_6MBPS |
254 CONF_HW_BIT_RATE_12MBPS |
255 CONF_HW_BIT_RATE_24MBPS);
Teemu Paasikivia4102642009-10-13 12:47:51 +0300256 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300257
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300258 join->beacon_interval = cpu_to_le16(WL1271_DEFAULT_BEACON_INT);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300259 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300260
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300261 join->channel = wl->channel;
262 join->ssid_len = wl->ssid_len;
263 memcpy(join->ssid, wl->ssid, wl->ssid_len);
264 join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
265
266 /* increment the session counter */
267 wl->session_counter++;
268 if (wl->session_counter >= SESSION_COUNTER_MAX)
269 wl->session_counter = 0;
270
271 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
272
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300273 /* reset TX security counters */
274 wl->tx_security_last_seq = 0;
275 wl->tx_security_seq_16 = 0;
276 wl->tx_security_seq_32 = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300277
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200278 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300279 if (ret < 0) {
280 wl1271_error("failed to initiate cmd join");
281 goto out_free;
282 }
283
Luciano Coelhod6e19d132009-10-12 15:08:43 +0300284 wl->joined = true;
285
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300286 /*
287 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
288 * simplify locking we just sleep instead, for now
289 */
Juuso Oikarinend94cd292009-10-08 21:56:25 +0300290 msleep(10);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300291
292out_free:
293 kfree(join);
294
295out:
296 return ret;
297}
298
299/**
300 * send test command to firmware
301 *
302 * @wl: wl struct
303 * @buf: buffer containing the command, with all headers, must work with dma
304 * @len: length of the buffer
305 * @answer: is answer needed
306 */
307int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
308{
309 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200310 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300311
312 wl1271_debug(DEBUG_CMD, "cmd test");
313
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200314 if (answer)
315 res_len = buf_len;
316
317 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300318
319 if (ret < 0) {
320 wl1271_warning("TEST command failed");
321 return ret;
322 }
323
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200324 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300325}
326
327/**
328 * read acx from firmware
329 *
330 * @wl: wl struct
331 * @id: acx id
332 * @buf: buffer for the response, including all headers, must work with dma
333 * @len: lenght of buf
334 */
335int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
336{
337 struct acx_header *acx = buf;
338 int ret;
339
340 wl1271_debug(DEBUG_CMD, "cmd interrogate");
341
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300342 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300343
344 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300345 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300346
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200347 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
348 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300349 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300350
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300351 return ret;
352}
353
354/**
355 * write acx value to firmware
356 *
357 * @wl: wl struct
358 * @id: acx id
359 * @buf: buffer containing acx, including all headers, must work with dma
360 * @len: length of buf
361 */
362int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
363{
364 struct acx_header *acx = buf;
365 int ret;
366
367 wl1271_debug(DEBUG_CMD, "cmd configure");
368
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300369 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300370
371 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300372 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300373
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200374 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300375 if (ret < 0) {
376 wl1271_warning("CONFIGURE command NOK");
377 return ret;
378 }
379
380 return 0;
381}
382
383int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
384{
385 struct cmd_enabledisable_path *cmd;
386 int ret;
387 u16 cmd_rx, cmd_tx;
388
389 wl1271_debug(DEBUG_CMD, "cmd data path");
390
391 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
392 if (!cmd) {
393 ret = -ENOMEM;
394 goto out;
395 }
396
397 cmd->channel = channel;
398
399 if (enable) {
400 cmd_rx = CMD_ENABLE_RX;
401 cmd_tx = CMD_ENABLE_TX;
402 } else {
403 cmd_rx = CMD_DISABLE_RX;
404 cmd_tx = CMD_DISABLE_TX;
405 }
406
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200407 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300408 if (ret < 0) {
409 wl1271_error("rx %s cmd for channel %d failed",
410 enable ? "start" : "stop", channel);
411 goto out;
412 }
413
414 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
415 enable ? "start" : "stop", channel);
416
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200417 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300418 if (ret < 0) {
419 wl1271_error("tx %s cmd for channel %d failed",
420 enable ? "start" : "stop", channel);
421 return ret;
422 }
423
424 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
425 enable ? "start" : "stop", channel);
426
427out:
428 kfree(cmd);
429 return ret;
430}
431
432int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
433{
434 struct wl1271_cmd_ps_params *ps_params = NULL;
435 int ret = 0;
436
437 /* FIXME: this should be in ps.c */
Juuso Oikarinen51f2be22009-10-13 12:47:42 +0300438 ret = wl1271_acx_wake_up_conditions(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300439 if (ret < 0) {
440 wl1271_error("couldn't set wake up conditions");
441 goto out;
442 }
443
444 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
445
446 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
447 if (!ps_params) {
448 ret = -ENOMEM;
449 goto out;
450 }
451
452 ps_params->ps_mode = ps_mode;
453 ps_params->send_null_data = 1;
454 ps_params->retries = 5;
455 ps_params->hang_over_period = 128;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300456 ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300457
458 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200459 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300460 if (ret < 0) {
461 wl1271_error("cmd set_ps_mode failed");
462 goto out;
463 }
464
465out:
466 kfree(ps_params);
467 return ret;
468}
469
470int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
471 size_t len)
472{
473 struct cmd_read_write_memory *cmd;
474 int ret = 0;
475
476 wl1271_debug(DEBUG_CMD, "cmd read memory");
477
478 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
479 if (!cmd) {
480 ret = -ENOMEM;
481 goto out;
482 }
483
484 WARN_ON(len > MAX_READ_SIZE);
485 len = min_t(size_t, len, MAX_READ_SIZE);
486
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300487 cmd->addr = cpu_to_le32(addr);
488 cmd->size = cpu_to_le32(len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300489
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200490 ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
491 sizeof(*cmd));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300492 if (ret < 0) {
493 wl1271_error("read memory command failed: %d", ret);
494 goto out;
495 }
496
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200497 /* the read command got in */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300498 memcpy(answer, cmd->value, len);
499
500out:
501 kfree(cmd);
502 return ret;
503}
504
505int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300506 u8 active_scan, u8 high_prio, u8 band,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300507 u8 probe_requests)
508{
509
510 struct wl1271_cmd_trigger_scan_to *trigger = NULL;
511 struct wl1271_cmd_scan *params = NULL;
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300512 struct ieee80211_channel *channels;
513 int i, j, n_ch, ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300514 u16 scan_options = 0;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300515 u8 ieee_band;
516
517 if (band == WL1271_SCAN_BAND_2_4_GHZ)
518 ieee_band = IEEE80211_BAND_2GHZ;
519 else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
520 ieee_band = IEEE80211_BAND_2GHZ;
521 else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
522 ieee_band = IEEE80211_BAND_5GHZ;
523 else
524 return -EINVAL;
525
526 if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
527 return -EINVAL;
528
529 channels = wl->hw->wiphy->bands[ieee_band]->channels;
530 n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300531
532 if (wl->scanning)
533 return -EINVAL;
534
535 params = kzalloc(sizeof(*params), GFP_KERNEL);
536 if (!params)
537 return -ENOMEM;
538
539 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
540 params->params.rx_filter_options =
541 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
542
543 if (!active_scan)
544 scan_options |= WL1271_SCAN_OPT_PASSIVE;
545 if (high_prio)
546 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300547 params->params.scan_options = cpu_to_le16(scan_options);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300548
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300549 params->params.num_probe_requests = probe_requests;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300550 /* Let the fw autodetect suitable tx_rate for probes */
551 params->params.tx_rate = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300552 params->params.tid_trigger = 0;
553 params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
554
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300555 if (band == WL1271_SCAN_BAND_DUAL)
556 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
557 else
558 params->params.band = band;
559
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300560 for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
561 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
562 params->channels[j].min_duration =
563 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
564 params->channels[j].max_duration =
565 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
566 memset(&params->channels[j].bssid_lsb, 0xff, 4);
567 memset(&params->channels[j].bssid_msb, 0xff, 2);
568 params->channels[j].early_termination = 0;
569 params->channels[j].tx_power_att =
570 WL1271_SCAN_CURRENT_TX_PWR;
571 params->channels[j].channel = channels[i].hw_value;
572 j++;
573 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300574 }
575
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300576 params->params.num_channels = j;
577
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300578 if (len && ssid) {
579 params->params.ssid_len = len;
580 memcpy(params->params.ssid, ssid, len);
581 }
582
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300583 ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300584 if (ret < 0) {
585 wl1271_error("PROBE request template failed");
586 goto out;
587 }
588
589 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
590 if (!trigger) {
591 ret = -ENOMEM;
592 goto out;
593 }
594
595 /* disable the timeout */
596 trigger->timeout = 0;
597
598 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200599 sizeof(*trigger), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300600 if (ret < 0) {
601 wl1271_error("trigger scan to failed for hw scan");
602 goto out;
603 }
604
605 wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
606
607 wl->scanning = true;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300608 if (wl1271_11a_enabled()) {
609 wl->scan.state = band;
610 if (band == WL1271_SCAN_BAND_DUAL) {
611 wl->scan.active = active_scan;
612 wl->scan.high_prio = high_prio;
613 wl->scan.probe_requests = probe_requests;
614 if (len && ssid) {
615 wl->scan.ssid_len = len;
616 memcpy(wl->scan.ssid, ssid, len);
617 } else
618 wl->scan.ssid_len = 0;
619 }
620 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300621
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200622 ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300623 if (ret < 0) {
624 wl1271_error("SCAN failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300625 wl->scanning = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300626 goto out;
627 }
628
629out:
630 kfree(params);
631 return ret;
632}
633
634int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
635 void *buf, size_t buf_len)
636{
637 struct wl1271_cmd_template_set *cmd;
638 int ret = 0;
639
640 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
641
642 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
643 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
644
645 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
646 if (!cmd) {
647 ret = -ENOMEM;
648 goto out;
649 }
650
651 cmd->len = cpu_to_le16(buf_len);
652 cmd->template_type = template_id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300653 cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
Juuso Oikarinen45b531a2009-10-13 12:47:41 +0300654 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
655 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300656
657 if (buf)
658 memcpy(cmd->template_data, buf, buf_len);
659
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200660 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300661 if (ret < 0) {
662 wl1271_warning("cmd set_template failed: %d", ret);
663 goto out_free;
664 }
665
666out_free:
667 kfree(cmd);
668
669out:
670 return ret;
671}
672
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300673static int wl1271_build_basic_rates(char *rates, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300674{
675 u8 index = 0;
676
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300677 if (band == IEEE80211_BAND_2GHZ) {
678 rates[index++] =
679 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
680 rates[index++] =
681 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
682 rates[index++] =
683 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
684 rates[index++] =
685 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
686 } else if (band == IEEE80211_BAND_5GHZ) {
687 rates[index++] =
688 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
689 rates[index++] =
690 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
691 rates[index++] =
692 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
693 } else {
694 wl1271_error("build_basic_rates invalid band: %d", band);
695 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300696
697 return index;
698}
699
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300700static int wl1271_build_extended_rates(char *rates, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300701{
702 u8 index = 0;
703
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300704 if (band == IEEE80211_BAND_2GHZ) {
705 rates[index++] = IEEE80211_OFDM_RATE_6MB;
706 rates[index++] = IEEE80211_OFDM_RATE_9MB;
707 rates[index++] = IEEE80211_OFDM_RATE_12MB;
708 rates[index++] = IEEE80211_OFDM_RATE_18MB;
709 rates[index++] = IEEE80211_OFDM_RATE_24MB;
710 rates[index++] = IEEE80211_OFDM_RATE_36MB;
711 rates[index++] = IEEE80211_OFDM_RATE_48MB;
712 rates[index++] = IEEE80211_OFDM_RATE_54MB;
713 } else if (band == IEEE80211_BAND_5GHZ) {
714 rates[index++] =
715 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
716 rates[index++] =
717 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
718 rates[index++] =
719 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
720 rates[index++] =
721 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
722 rates[index++] =
723 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
724 rates[index++] =
725 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
726 } else {
727 wl1271_error("build_basic_rates invalid band: %d", band);
728 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300729
730 return index;
731}
732
733int wl1271_cmd_build_null_data(struct wl1271 *wl)
734{
735 struct wl12xx_null_data_template template;
736
737 if (!is_zero_ether_addr(wl->bssid)) {
738 memcpy(template.header.da, wl->bssid, ETH_ALEN);
739 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
740 } else {
741 memset(template.header.da, 0xff, ETH_ALEN);
742 memset(template.header.bssid, 0xff, ETH_ALEN);
743 }
744
745 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
746 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
Juuso Oikarinen7a380792009-10-15 10:33:26 +0300747 IEEE80211_STYPE_NULLFUNC |
748 IEEE80211_FCTL_TODS);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300749
750 return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
751 sizeof(template));
752
753}
754
755int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
756{
757 struct wl12xx_ps_poll_template template;
758
759 memcpy(template.bssid, wl->bssid, ETH_ALEN);
760 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300761
762 /* aid in PS-Poll has its two MSBs each set to 1 */
763 template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
764
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300765 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
766
767 return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
768 sizeof(template));
769
770}
771
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300772int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
773 u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300774{
775 struct wl12xx_probe_req_template template;
776 struct wl12xx_ie_rates *rates;
777 char *ptr;
778 u16 size;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300779 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300780
781 ptr = (char *)&template;
782 size = sizeof(struct ieee80211_header);
783
784 memset(template.header.da, 0xff, ETH_ALEN);
785 memset(template.header.bssid, 0xff, ETH_ALEN);
786 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
787 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
788
789 /* IEs */
790 /* SSID */
791 template.ssid.header.id = WLAN_EID_SSID;
792 template.ssid.header.len = ssid_len;
793 if (ssid_len && ssid)
794 memcpy(template.ssid.ssid, ssid, ssid_len);
795 size += sizeof(struct wl12xx_ie_header) + ssid_len;
796 ptr += size;
797
798 /* Basic Rates */
799 rates = (struct wl12xx_ie_rates *)ptr;
800 rates->header.id = WLAN_EID_SUPP_RATES;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300801 rates->header.len = wl1271_build_basic_rates(rates->rates, band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300802 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
803 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
804
805 /* Extended rates */
806 rates = (struct wl12xx_ie_rates *)ptr;
807 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300808 rates->header.len = wl1271_build_extended_rates(rates->rates, band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300809 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
810
811 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
812
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300813 if (band == IEEE80211_BAND_2GHZ)
814 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
815 &template, size);
816 else
817 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
818 &template, size);
819 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300820}
821
822int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
823{
824 struct wl1271_cmd_set_keys *cmd;
825 int ret = 0;
826
827 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
828
829 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
830 if (!cmd) {
831 ret = -ENOMEM;
832 goto out;
833 }
834
835 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300836 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300837 cmd->key_type = KEY_WEP;
838
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200839 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300840 if (ret < 0) {
841 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
842 goto out;
843 }
844
845out:
846 kfree(cmd);
847
848 return ret;
849}
850
851int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300852 u8 key_size, const u8 *key, const u8 *addr,
853 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300854{
855 struct wl1271_cmd_set_keys *cmd;
856 int ret = 0;
857
858 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
859 if (!cmd) {
860 ret = -ENOMEM;
861 goto out;
862 }
863
864 if (key_type != KEY_WEP)
865 memcpy(cmd->addr, addr, ETH_ALEN);
866
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300867 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300868 cmd->key_size = key_size;
869 cmd->key_type = key_type;
870
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300871 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
872 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300873
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300874 /* we have only one SSID profile */
875 cmd->ssid_profile = 0;
876
877 cmd->id = id;
878
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300879 if (key_type == KEY_TKIP) {
880 /*
881 * We get the key in the following form:
882 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
883 * but the target is expecting:
884 * TKIP - RX MIC - TX MIC
885 */
886 memcpy(cmd->key, key, 16);
887 memcpy(cmd->key + 16, key + 24, 8);
888 memcpy(cmd->key + 24, key + 16, 8);
889
890 } else {
891 memcpy(cmd->key, key, key_size);
892 }
893
894 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
895
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200896 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300897 if (ret < 0) {
898 wl1271_warning("could not set keys");
899 goto out;
900 }
901
902out:
903 kfree(cmd);
904
905 return ret;
906}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300907
908int wl1271_cmd_disconnect(struct wl1271 *wl)
909{
910 struct wl1271_cmd_disconnect *cmd;
911 int ret = 0;
912
913 wl1271_debug(DEBUG_CMD, "cmd disconnect");
914
915 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
916 if (!cmd) {
917 ret = -ENOMEM;
918 goto out;
919 }
920
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300921 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
922 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300923 /* disconnect reason is not used in immediate disconnections */
924 cmd->type = DISCONNECT_IMMEDIATE;
925
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200926 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300927 if (ret < 0) {
928 wl1271_error("failed to send disconnect command");
929 goto out_free;
930 }
931
932out_free:
933 kfree(cmd);
934
935out:
936 return ret;
937}