blob: fe9b187f476ecbe5c472ae477999d25b8907f892 [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
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200191int wl1271_cmd_general_parms(struct wl1271 *wl)
192{
193 struct wl1271_general_parms_cmd *gen_parms;
194 struct conf_general_parms *g = &wl->conf.init.genparam;
195 int ret;
196
197 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
198 if (!gen_parms)
199 return -ENOMEM;
200
201 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
202
203 gen_parms->ref_clk = g->ref_clk;
204 gen_parms->settling_time = g->settling_time;
205 gen_parms->clk_valid_on_wakeup = g->clk_valid_on_wakeup;
206 gen_parms->dc2dcmode = g->dc2dcmode;
207 gen_parms->single_dual_band = g->single_dual_band;
208 gen_parms->tx_bip_fem_autodetect = g->tx_bip_fem_autodetect;
209 gen_parms->tx_bip_fem_manufacturer = g->tx_bip_fem_manufacturer;
210 gen_parms->settings = g->settings;
211
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200212 gen_parms->sr_state = g->sr_state;
213
214 memcpy(gen_parms->srf1,
215 g->srf1,
216 CONF_MAX_SMART_REFLEX_PARAMS);
217 memcpy(gen_parms->srf2,
218 g->srf2,
219 CONF_MAX_SMART_REFLEX_PARAMS);
220 memcpy(gen_parms->srf3,
221 g->srf3,
222 CONF_MAX_SMART_REFLEX_PARAMS);
223 memcpy(gen_parms->sr_debug_table,
224 g->sr_debug_table,
225 CONF_MAX_SMART_REFLEX_PARAMS);
226
227 gen_parms->sr_sen_n_p = g->sr_sen_n_p;
228 gen_parms->sr_sen_n_p_gain = g->sr_sen_n_p_gain;
229 gen_parms->sr_sen_nrn = g->sr_sen_nrn;
230 gen_parms->sr_sen_prn = g->sr_sen_prn;
231
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200232 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
233 if (ret < 0)
234 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
235
236 kfree(gen_parms);
237 return ret;
238}
239
240int wl1271_cmd_radio_parms(struct wl1271 *wl)
241{
242 struct wl1271_radio_parms_cmd *radio_parms;
243 struct conf_radio_parms *r = &wl->conf.init.radioparam;
244 int i, ret;
245
246 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
247 if (!radio_parms)
248 return -ENOMEM;
249
250 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
251
252 /* Static radio parameters */
253 radio_parms->rx_trace_loss = r->rx_trace_loss;
254 radio_parms->tx_trace_loss = r->tx_trace_loss;
255 memcpy(radio_parms->rx_rssi_and_proc_compens,
256 r->rx_rssi_and_proc_compens,
257 CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
258
259 memcpy(radio_parms->rx_trace_loss_5, r->rx_trace_loss_5,
260 CONF_NUMBER_OF_SUB_BANDS_5);
261 memcpy(radio_parms->tx_trace_loss_5, r->tx_trace_loss_5,
262 CONF_NUMBER_OF_SUB_BANDS_5);
263 memcpy(radio_parms->rx_rssi_and_proc_compens_5,
264 r->rx_rssi_and_proc_compens_5,
265 CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
266
267 /* Dynamic radio parameters */
268 radio_parms->tx_ref_pd_voltage = cpu_to_le16(r->tx_ref_pd_voltage);
269 radio_parms->tx_ref_power = r->tx_ref_power;
270 radio_parms->tx_offset_db = r->tx_offset_db;
271
272 memcpy(radio_parms->tx_rate_limits_normal, r->tx_rate_limits_normal,
273 CONF_NUMBER_OF_RATE_GROUPS);
274 memcpy(radio_parms->tx_rate_limits_degraded, r->tx_rate_limits_degraded,
275 CONF_NUMBER_OF_RATE_GROUPS);
276
277 memcpy(radio_parms->tx_channel_limits_11b, r->tx_channel_limits_11b,
278 CONF_NUMBER_OF_CHANNELS_2_4);
279 memcpy(radio_parms->tx_channel_limits_ofdm, r->tx_channel_limits_ofdm,
280 CONF_NUMBER_OF_CHANNELS_2_4);
281 memcpy(radio_parms->tx_pdv_rate_offsets, r->tx_pdv_rate_offsets,
282 CONF_NUMBER_OF_RATE_GROUPS);
283 memcpy(radio_parms->tx_ibias, r->tx_ibias, CONF_NUMBER_OF_RATE_GROUPS);
284
285 radio_parms->rx_fem_insertion_loss = r->rx_fem_insertion_loss;
286
287 for (i = 0; i < CONF_NUMBER_OF_SUB_BANDS_5; i++)
288 radio_parms->tx_ref_pd_voltage_5[i] =
289 cpu_to_le16(r->tx_ref_pd_voltage_5[i]);
290 memcpy(radio_parms->tx_ref_power_5, r->tx_ref_power_5,
291 CONF_NUMBER_OF_SUB_BANDS_5);
292 memcpy(radio_parms->tx_offset_db_5, r->tx_offset_db_5,
293 CONF_NUMBER_OF_SUB_BANDS_5);
294 memcpy(radio_parms->tx_rate_limits_normal_5,
295 r->tx_rate_limits_normal_5, CONF_NUMBER_OF_RATE_GROUPS);
296 memcpy(radio_parms->tx_rate_limits_degraded_5,
297 r->tx_rate_limits_degraded_5, CONF_NUMBER_OF_RATE_GROUPS);
298 memcpy(radio_parms->tx_channel_limits_ofdm_5,
299 r->tx_channel_limits_ofdm_5, CONF_NUMBER_OF_CHANNELS_5);
300 memcpy(radio_parms->tx_pdv_rate_offsets_5, r->tx_pdv_rate_offsets_5,
301 CONF_NUMBER_OF_RATE_GROUPS);
302 memcpy(radio_parms->tx_ibias_5, r->tx_ibias_5,
303 CONF_NUMBER_OF_RATE_GROUPS);
304 memcpy(radio_parms->rx_fem_insertion_loss_5,
305 r->rx_fem_insertion_loss_5, CONF_NUMBER_OF_SUB_BANDS_5);
306
307 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
308 radio_parms, sizeof(*radio_parms));
309
310 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
311 if (ret < 0)
312 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
313
314 kfree(radio_parms);
315 return ret;
316}
317
Juuso Oikarinend94cd292009-10-08 21:56:25 +0300318int wl1271_cmd_join(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300319{
320 static bool do_cal = true;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300321 struct wl1271_cmd_join *join;
322 int ret, i;
323 u8 *bssid;
324
325 /* FIXME: remove when we get calibration from the factory */
326 if (do_cal) {
327 ret = wl1271_cmd_cal(wl);
328 if (ret < 0)
329 wl1271_warning("couldn't calibrate");
330 else
331 do_cal = false;
332 }
333
Luciano Coelhod6e19d12009-10-12 15:08:43 +0300334 /* FIXME: This is a workaround, because with the current stack, we
335 * cannot know when we have disassociated. So, if we have already
336 * joined, we disconnect before joining again. */
337 if (wl->joined) {
338 ret = wl1271_cmd_disconnect(wl);
339 if (ret < 0) {
340 wl1271_error("failed to disconnect before rejoining");
341 goto out;
342 }
343
344 wl->joined = false;
345 }
346
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300347 join = kzalloc(sizeof(*join), GFP_KERNEL);
348 if (!join) {
349 ret = -ENOMEM;
350 goto out;
351 }
352
353 wl1271_debug(DEBUG_CMD, "cmd join");
354
355 /* Reverse order BSSID */
356 bssid = (u8 *) &join->bssid_lsb;
357 for (i = 0; i < ETH_ALEN; i++)
358 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
359
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300360 join->rx_config_options = cpu_to_le32(wl->rx_config);
361 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
Teemu Paasikivia4102642009-10-13 12:47:51 +0300362 join->bss_type = wl->bss_type;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300363
Luciano Coelho64a7f672009-10-08 21:56:28 +0300364 /*
365 * FIXME: disable temporarily all filters because after commit
366 * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
367 * association. The filter logic needs to be implemented properly
368 * and once that is done, this hack can be removed.
369 */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300370 join->rx_config_options = cpu_to_le32(0);
371 join->rx_filter_options = cpu_to_le32(WL1271_DEFAULT_RX_FILTER);
Luciano Coelho64a7f672009-10-08 21:56:28 +0300372
Teemu Paasikivia4102642009-10-13 12:47:51 +0300373 if (wl->band == IEEE80211_BAND_2GHZ)
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300374 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_1MBPS |
375 CONF_HW_BIT_RATE_2MBPS |
376 CONF_HW_BIT_RATE_5_5MBPS |
377 CONF_HW_BIT_RATE_11MBPS);
Teemu Paasikivia4102642009-10-13 12:47:51 +0300378 else {
379 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300380 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_6MBPS |
381 CONF_HW_BIT_RATE_12MBPS |
382 CONF_HW_BIT_RATE_24MBPS);
Teemu Paasikivia4102642009-10-13 12:47:51 +0300383 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300384
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300385 join->beacon_interval = cpu_to_le16(WL1271_DEFAULT_BEACON_INT);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300386 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300387
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300388 join->channel = wl->channel;
389 join->ssid_len = wl->ssid_len;
390 memcpy(join->ssid, wl->ssid, wl->ssid_len);
391 join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
392
393 /* increment the session counter */
394 wl->session_counter++;
395 if (wl->session_counter >= SESSION_COUNTER_MAX)
396 wl->session_counter = 0;
397
398 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
399
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300400 /* reset TX security counters */
401 wl->tx_security_last_seq = 0;
402 wl->tx_security_seq_16 = 0;
403 wl->tx_security_seq_32 = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300404
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200405 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300406 if (ret < 0) {
407 wl1271_error("failed to initiate cmd join");
408 goto out_free;
409 }
410
Luciano Coelhod6e19d12009-10-12 15:08:43 +0300411 wl->joined = true;
412
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300413 /*
414 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
415 * simplify locking we just sleep instead, for now
416 */
Juuso Oikarinend94cd292009-10-08 21:56:25 +0300417 msleep(10);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300418
419out_free:
420 kfree(join);
421
422out:
423 return ret;
424}
425
426/**
427 * send test command to firmware
428 *
429 * @wl: wl struct
430 * @buf: buffer containing the command, with all headers, must work with dma
431 * @len: length of the buffer
432 * @answer: is answer needed
433 */
434int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
435{
436 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200437 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300438
439 wl1271_debug(DEBUG_CMD, "cmd test");
440
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200441 if (answer)
442 res_len = buf_len;
443
444 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300445
446 if (ret < 0) {
447 wl1271_warning("TEST command failed");
448 return ret;
449 }
450
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200451 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300452}
453
454/**
455 * read acx from firmware
456 *
457 * @wl: wl struct
458 * @id: acx id
459 * @buf: buffer for the response, including all headers, must work with dma
460 * @len: lenght of buf
461 */
462int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
463{
464 struct acx_header *acx = buf;
465 int ret;
466
467 wl1271_debug(DEBUG_CMD, "cmd interrogate");
468
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300469 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300470
471 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300472 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300473
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200474 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
475 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300476 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300477
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300478 return ret;
479}
480
481/**
482 * write acx value to firmware
483 *
484 * @wl: wl struct
485 * @id: acx id
486 * @buf: buffer containing acx, including all headers, must work with dma
487 * @len: length of buf
488 */
489int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
490{
491 struct acx_header *acx = buf;
492 int ret;
493
494 wl1271_debug(DEBUG_CMD, "cmd configure");
495
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300496 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300497
498 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300499 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300500
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200501 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300502 if (ret < 0) {
503 wl1271_warning("CONFIGURE command NOK");
504 return ret;
505 }
506
507 return 0;
508}
509
510int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
511{
512 struct cmd_enabledisable_path *cmd;
513 int ret;
514 u16 cmd_rx, cmd_tx;
515
516 wl1271_debug(DEBUG_CMD, "cmd data path");
517
518 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
519 if (!cmd) {
520 ret = -ENOMEM;
521 goto out;
522 }
523
524 cmd->channel = channel;
525
526 if (enable) {
527 cmd_rx = CMD_ENABLE_RX;
528 cmd_tx = CMD_ENABLE_TX;
529 } else {
530 cmd_rx = CMD_DISABLE_RX;
531 cmd_tx = CMD_DISABLE_TX;
532 }
533
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200534 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300535 if (ret < 0) {
536 wl1271_error("rx %s cmd for channel %d failed",
537 enable ? "start" : "stop", channel);
538 goto out;
539 }
540
541 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
542 enable ? "start" : "stop", channel);
543
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200544 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300545 if (ret < 0) {
546 wl1271_error("tx %s cmd for channel %d failed",
547 enable ? "start" : "stop", channel);
548 return ret;
549 }
550
551 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
552 enable ? "start" : "stop", channel);
553
554out:
555 kfree(cmd);
556 return ret;
557}
558
559int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
560{
561 struct wl1271_cmd_ps_params *ps_params = NULL;
562 int ret = 0;
563
564 /* FIXME: this should be in ps.c */
Juuso Oikarinen51f2be22009-10-13 12:47:42 +0300565 ret = wl1271_acx_wake_up_conditions(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300566 if (ret < 0) {
567 wl1271_error("couldn't set wake up conditions");
568 goto out;
569 }
570
571 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
572
573 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
574 if (!ps_params) {
575 ret = -ENOMEM;
576 goto out;
577 }
578
579 ps_params->ps_mode = ps_mode;
580 ps_params->send_null_data = 1;
581 ps_params->retries = 5;
582 ps_params->hang_over_period = 128;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300583 ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300584
585 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200586 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300587 if (ret < 0) {
588 wl1271_error("cmd set_ps_mode failed");
589 goto out;
590 }
591
592out:
593 kfree(ps_params);
594 return ret;
595}
596
597int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
598 size_t len)
599{
600 struct cmd_read_write_memory *cmd;
601 int ret = 0;
602
603 wl1271_debug(DEBUG_CMD, "cmd read memory");
604
605 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
606 if (!cmd) {
607 ret = -ENOMEM;
608 goto out;
609 }
610
611 WARN_ON(len > MAX_READ_SIZE);
612 len = min_t(size_t, len, MAX_READ_SIZE);
613
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300614 cmd->addr = cpu_to_le32(addr);
615 cmd->size = cpu_to_le32(len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300616
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200617 ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
618 sizeof(*cmd));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300619 if (ret < 0) {
620 wl1271_error("read memory command failed: %d", ret);
621 goto out;
622 }
623
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200624 /* the read command got in */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300625 memcpy(answer, cmd->value, len);
626
627out:
628 kfree(cmd);
629 return ret;
630}
631
632int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300633 u8 active_scan, u8 high_prio, u8 band,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300634 u8 probe_requests)
635{
636
637 struct wl1271_cmd_trigger_scan_to *trigger = NULL;
638 struct wl1271_cmd_scan *params = NULL;
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300639 struct ieee80211_channel *channels;
640 int i, j, n_ch, ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300641 u16 scan_options = 0;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300642 u8 ieee_band;
643
644 if (band == WL1271_SCAN_BAND_2_4_GHZ)
645 ieee_band = IEEE80211_BAND_2GHZ;
646 else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
647 ieee_band = IEEE80211_BAND_2GHZ;
648 else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
649 ieee_band = IEEE80211_BAND_5GHZ;
650 else
651 return -EINVAL;
652
653 if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
654 return -EINVAL;
655
656 channels = wl->hw->wiphy->bands[ieee_band]->channels;
657 n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300658
659 if (wl->scanning)
660 return -EINVAL;
661
662 params = kzalloc(sizeof(*params), GFP_KERNEL);
663 if (!params)
664 return -ENOMEM;
665
666 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
667 params->params.rx_filter_options =
668 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
669
670 if (!active_scan)
671 scan_options |= WL1271_SCAN_OPT_PASSIVE;
672 if (high_prio)
673 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300674 params->params.scan_options = cpu_to_le16(scan_options);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300675
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300676 params->params.num_probe_requests = probe_requests;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300677 /* Let the fw autodetect suitable tx_rate for probes */
678 params->params.tx_rate = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300679 params->params.tid_trigger = 0;
680 params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
681
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300682 if (band == WL1271_SCAN_BAND_DUAL)
683 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
684 else
685 params->params.band = band;
686
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300687 for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
688 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
689 params->channels[j].min_duration =
690 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
691 params->channels[j].max_duration =
692 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
693 memset(&params->channels[j].bssid_lsb, 0xff, 4);
694 memset(&params->channels[j].bssid_msb, 0xff, 2);
695 params->channels[j].early_termination = 0;
696 params->channels[j].tx_power_att =
697 WL1271_SCAN_CURRENT_TX_PWR;
698 params->channels[j].channel = channels[i].hw_value;
699 j++;
700 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300701 }
702
Teemu Paasikivi311494c2009-10-13 12:47:49 +0300703 params->params.num_channels = j;
704
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300705 if (len && ssid) {
706 params->params.ssid_len = len;
707 memcpy(params->params.ssid, ssid, len);
708 }
709
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300710 ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300711 if (ret < 0) {
712 wl1271_error("PROBE request template failed");
713 goto out;
714 }
715
716 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
717 if (!trigger) {
718 ret = -ENOMEM;
719 goto out;
720 }
721
722 /* disable the timeout */
723 trigger->timeout = 0;
724
725 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200726 sizeof(*trigger), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300727 if (ret < 0) {
728 wl1271_error("trigger scan to failed for hw scan");
729 goto out;
730 }
731
732 wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
733
734 wl->scanning = true;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300735 if (wl1271_11a_enabled()) {
736 wl->scan.state = band;
737 if (band == WL1271_SCAN_BAND_DUAL) {
738 wl->scan.active = active_scan;
739 wl->scan.high_prio = high_prio;
740 wl->scan.probe_requests = probe_requests;
741 if (len && ssid) {
742 wl->scan.ssid_len = len;
743 memcpy(wl->scan.ssid, ssid, len);
744 } else
745 wl->scan.ssid_len = 0;
746 }
747 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300748
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200749 ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300750 if (ret < 0) {
751 wl1271_error("SCAN failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300752 wl->scanning = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300753 goto out;
754 }
755
756out:
757 kfree(params);
758 return ret;
759}
760
761int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
762 void *buf, size_t buf_len)
763{
764 struct wl1271_cmd_template_set *cmd;
765 int ret = 0;
766
767 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
768
769 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
770 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
771
772 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
773 if (!cmd) {
774 ret = -ENOMEM;
775 goto out;
776 }
777
778 cmd->len = cpu_to_le16(buf_len);
779 cmd->template_type = template_id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300780 cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
Juuso Oikarinen45b531a2009-10-13 12:47:41 +0300781 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
782 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300783
784 if (buf)
785 memcpy(cmd->template_data, buf, buf_len);
786
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200787 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300788 if (ret < 0) {
789 wl1271_warning("cmd set_template failed: %d", ret);
790 goto out_free;
791 }
792
793out_free:
794 kfree(cmd);
795
796out:
797 return ret;
798}
799
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300800static int wl1271_build_basic_rates(char *rates, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300801{
802 u8 index = 0;
803
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300804 if (band == IEEE80211_BAND_2GHZ) {
805 rates[index++] =
806 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
807 rates[index++] =
808 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
809 rates[index++] =
810 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
811 rates[index++] =
812 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
813 } else if (band == IEEE80211_BAND_5GHZ) {
814 rates[index++] =
815 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
816 rates[index++] =
817 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
818 rates[index++] =
819 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
820 } else {
821 wl1271_error("build_basic_rates invalid band: %d", band);
822 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300823
824 return index;
825}
826
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300827static int wl1271_build_extended_rates(char *rates, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300828{
829 u8 index = 0;
830
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300831 if (band == IEEE80211_BAND_2GHZ) {
832 rates[index++] = IEEE80211_OFDM_RATE_6MB;
833 rates[index++] = IEEE80211_OFDM_RATE_9MB;
834 rates[index++] = IEEE80211_OFDM_RATE_12MB;
835 rates[index++] = IEEE80211_OFDM_RATE_18MB;
836 rates[index++] = IEEE80211_OFDM_RATE_24MB;
837 rates[index++] = IEEE80211_OFDM_RATE_36MB;
838 rates[index++] = IEEE80211_OFDM_RATE_48MB;
839 rates[index++] = IEEE80211_OFDM_RATE_54MB;
840 } else if (band == IEEE80211_BAND_5GHZ) {
841 rates[index++] =
842 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
843 rates[index++] =
844 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
845 rates[index++] =
846 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
847 rates[index++] =
848 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
849 rates[index++] =
850 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
851 rates[index++] =
852 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
853 } else {
854 wl1271_error("build_basic_rates invalid band: %d", band);
855 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300856
857 return index;
858}
859
860int wl1271_cmd_build_null_data(struct wl1271 *wl)
861{
862 struct wl12xx_null_data_template template;
863
864 if (!is_zero_ether_addr(wl->bssid)) {
865 memcpy(template.header.da, wl->bssid, ETH_ALEN);
866 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
867 } else {
868 memset(template.header.da, 0xff, ETH_ALEN);
869 memset(template.header.bssid, 0xff, ETH_ALEN);
870 }
871
872 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
873 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
Juuso Oikarinen7a380792009-10-15 10:33:26 +0300874 IEEE80211_STYPE_NULLFUNC |
875 IEEE80211_FCTL_TODS);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300876
877 return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
878 sizeof(template));
879
880}
881
882int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
883{
884 struct wl12xx_ps_poll_template template;
885
886 memcpy(template.bssid, wl->bssid, ETH_ALEN);
887 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300888
889 /* aid in PS-Poll has its two MSBs each set to 1 */
890 template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
891
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300892 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
893
894 return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
895 sizeof(template));
896
897}
898
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300899int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
900 u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300901{
902 struct wl12xx_probe_req_template template;
903 struct wl12xx_ie_rates *rates;
904 char *ptr;
905 u16 size;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300906 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300907
908 ptr = (char *)&template;
909 size = sizeof(struct ieee80211_header);
910
911 memset(template.header.da, 0xff, ETH_ALEN);
912 memset(template.header.bssid, 0xff, ETH_ALEN);
913 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
914 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
915
916 /* IEs */
917 /* SSID */
918 template.ssid.header.id = WLAN_EID_SSID;
919 template.ssid.header.len = ssid_len;
920 if (ssid_len && ssid)
921 memcpy(template.ssid.ssid, ssid, ssid_len);
922 size += sizeof(struct wl12xx_ie_header) + ssid_len;
923 ptr += size;
924
925 /* Basic Rates */
926 rates = (struct wl12xx_ie_rates *)ptr;
927 rates->header.id = WLAN_EID_SUPP_RATES;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300928 rates->header.len = wl1271_build_basic_rates(rates->rates, band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300929 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
930 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
931
932 /* Extended rates */
933 rates = (struct wl12xx_ie_rates *)ptr;
934 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300935 rates->header.len = wl1271_build_extended_rates(rates->rates, band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300936 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
937
938 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
939
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300940 if (band == IEEE80211_BAND_2GHZ)
941 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
942 &template, size);
943 else
944 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
945 &template, size);
946 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300947}
948
949int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
950{
951 struct wl1271_cmd_set_keys *cmd;
952 int ret = 0;
953
954 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
955
956 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
957 if (!cmd) {
958 ret = -ENOMEM;
959 goto out;
960 }
961
962 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300963 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300964 cmd->key_type = KEY_WEP;
965
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200966 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300967 if (ret < 0) {
968 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
969 goto out;
970 }
971
972out:
973 kfree(cmd);
974
975 return ret;
976}
977
978int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300979 u8 key_size, const u8 *key, const u8 *addr,
980 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300981{
982 struct wl1271_cmd_set_keys *cmd;
983 int ret = 0;
984
985 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
986 if (!cmd) {
987 ret = -ENOMEM;
988 goto out;
989 }
990
991 if (key_type != KEY_WEP)
992 memcpy(cmd->addr, addr, ETH_ALEN);
993
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300994 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300995 cmd->key_size = key_size;
996 cmd->key_type = key_type;
997
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300998 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
999 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001000
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001001 /* we have only one SSID profile */
1002 cmd->ssid_profile = 0;
1003
1004 cmd->id = id;
1005
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001006 if (key_type == KEY_TKIP) {
1007 /*
1008 * We get the key in the following form:
1009 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1010 * but the target is expecting:
1011 * TKIP - RX MIC - TX MIC
1012 */
1013 memcpy(cmd->key, key, 16);
1014 memcpy(cmd->key + 16, key + 24, 8);
1015 memcpy(cmd->key + 24, key + 16, 8);
1016
1017 } else {
1018 memcpy(cmd->key, key, key_size);
1019 }
1020
1021 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1022
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001023 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001024 if (ret < 0) {
1025 wl1271_warning("could not set keys");
1026 goto out;
1027 }
1028
1029out:
1030 kfree(cmd);
1031
1032 return ret;
1033}
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001034
1035int wl1271_cmd_disconnect(struct wl1271 *wl)
1036{
1037 struct wl1271_cmd_disconnect *cmd;
1038 int ret = 0;
1039
1040 wl1271_debug(DEBUG_CMD, "cmd disconnect");
1041
1042 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1043 if (!cmd) {
1044 ret = -ENOMEM;
1045 goto out;
1046 }
1047
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001048 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
1049 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001050 /* disconnect reason is not used in immediate disconnections */
1051 cmd->type = DISCONNECT_IMMEDIATE;
1052
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001053 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001054 if (ret < 0) {
1055 wl1271_error("failed to send disconnect command");
1056 goto out_free;
1057 }
1058
1059out_free:
1060 kfree(cmd);
1061
1062out:
1063 return ret;
1064}