blob: 37eb9f3669425976bc29eec39dec415e8b1100f5 [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
2 * This file is part of wl1271
3 *
Luciano Coelho2f826f52010-03-26 12:53:21 +02004 * Copyright (C) 2009-2010 Nokia Corporation
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03005 *
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>
Kalle Valo023e0822010-03-18 12:26:36 +020029#include <linux/ieee80211.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030031
Shahar Levi00d20102010-11-08 11:20:10 +000032#include "wl12xx.h"
33#include "reg.h"
34#include "io.h"
35#include "acx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030036#include "wl12xx_80211.h"
Shahar Levi00d20102010-11-08 11:20:10 +000037#include "cmd.h"
38#include "event.h"
Arik Nemtsov98bdaab2010-10-16 18:08:58 +020039#include "tx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030040
Juuso Oikarinen16092b52010-04-28 09:49:59 +030041#define WL1271_CMD_FAST_POLL_COUNT 50
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030042
43/*
44 * send command to firmware
45 *
46 * @wl: wl struct
47 * @id: command id
48 * @buf: buffer containing the command, must work with dma
49 * @len: length of the buffer
50 */
Juuso Oikarinenfa867e72009-11-02 20:22:13 +020051int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
52 size_t res_len)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030053{
54 struct wl1271_cmd_header *cmd;
55 unsigned long timeout;
56 u32 intr;
57 int ret = 0;
Juuso Oikarinenad150e92009-11-02 20:22:12 +020058 u16 status;
Saravanan Dhanabalbc0f03e2010-03-26 12:53:33 +020059 u16 poll_count = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030060
61 cmd = buf;
Luciano Coelhod0f63b22009-10-15 10:33:29 +030062 cmd->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030063 cmd->status = 0;
64
65 WARN_ON(len % 4 != 0);
Arik Nemtsov24225b32011-03-01 12:27:26 +020066 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030067
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020068 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030069
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020070 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030071
72 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
73
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020074 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030075 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
76 if (time_after(jiffies, timeout)) {
77 wl1271_error("command complete timeout");
78 ret = -ETIMEDOUT;
79 goto out;
80 }
81
Saravanan Dhanabalbc0f03e2010-03-26 12:53:33 +020082 poll_count++;
Juuso Oikarinen16092b52010-04-28 09:49:59 +030083 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
84 udelay(10);
85 else
86 msleep(1);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030087
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020088 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030089 }
90
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020091 /* read back the status code of the command */
Juuso Oikarinenfa867e72009-11-02 20:22:13 +020092 if (res_len == 0)
93 res_len = sizeof(struct wl1271_cmd_header);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020094 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020095
Juuso Oikarinenad150e92009-11-02 20:22:12 +020096 status = le16_to_cpu(cmd->status);
97 if (status != CMD_STATUS_SUCCESS) {
98 wl1271_error("command execute failure %d", status);
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +020099 ieee80211_queue_work(wl->hw, &wl->recovery_work);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +0200100 ret = -EIO;
101 }
102
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200103 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
104 WL1271_ACX_INTR_CMD_COMPLETE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300105
106out:
107 return ret;
108}
109
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200110int wl1271_cmd_general_parms(struct wl1271 *wl)
111{
112 struct wl1271_general_parms_cmd *gen_parms;
Shahar Levi49d750ca2011-03-06 16:32:09 +0200113 struct wl1271_ini_general_params *gp =
114 &((struct wl1271_nvs_file *)wl->nvs)->general_params;
115 bool answer = false;
116 int ret;
117
118 if (!wl->nvs)
119 return -ENODEV;
120
121 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
122 if (!gen_parms)
123 return -ENOMEM;
124
125 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
126
127 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
128
129 if (gp->tx_bip_fem_auto_detect)
130 answer = true;
131
132 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
133 if (ret < 0) {
134 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
135 goto out;
136 }
137
138 gp->tx_bip_fem_manufacturer =
139 gen_parms->general_params.tx_bip_fem_manufacturer;
140
141 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
142 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
143
144out:
145 kfree(gen_parms);
146 return ret;
147}
148
149int wl128x_cmd_general_parms(struct wl1271 *wl)
150{
151 struct wl128x_general_parms_cmd *gen_parms;
152 struct wl128x_ini_general_params *gp =
153 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300154 bool answer = false;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200155 int ret;
156
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200157 if (!wl->nvs)
158 return -ENODEV;
159
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200160 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
161 if (!gen_parms)
162 return -ENOMEM;
163
164 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
165
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300166 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200167
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300168 if (gp->tx_bip_fem_auto_detect)
169 answer = true;
170
171 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
172 if (ret < 0) {
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200173 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300174 goto out;
175 }
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200176
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300177 gp->tx_bip_fem_manufacturer =
178 gen_parms->general_params.tx_bip_fem_manufacturer;
179
180 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
181 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
182
183out:
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200184 kfree(gen_parms);
185 return ret;
186}
187
188int wl1271_cmd_radio_parms(struct wl1271 *wl)
189{
190 struct wl1271_radio_parms_cmd *radio_parms;
Luciano Coelhoe6b190f2010-07-08 17:50:01 +0300191 struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200192 int ret;
193
194 if (!wl->nvs)
195 return -ENODEV;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200196
197 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
198 if (!radio_parms)
199 return -ENOMEM;
200
201 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
202
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300203 /* 2.4GHz parameters */
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300204 memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
205 sizeof(struct wl1271_ini_band_params_2));
206 memcpy(&radio_parms->dyn_params_2,
Luciano Coelhoe6b190f2010-07-08 17:50:01 +0300207 &wl->nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300208 sizeof(struct wl1271_ini_fem_params_2));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200209
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300210 /* 5GHz parameters */
211 memcpy(&radio_parms->static_params_5,
212 &wl->nvs->stat_radio_params_5,
213 sizeof(struct wl1271_ini_band_params_5));
214 memcpy(&radio_parms->dyn_params_5,
Luciano Coelhoe6b190f2010-07-08 17:50:01 +0300215 &wl->nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300216 sizeof(struct wl1271_ini_fem_params_5));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200217
218 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
219 radio_parms, sizeof(*radio_parms));
220
221 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
222 if (ret < 0)
223 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
224
225 kfree(radio_parms);
226 return ret;
227}
228
Shahar Levi49d750ca2011-03-06 16:32:09 +0200229int wl128x_cmd_radio_parms(struct wl1271 *wl)
230{
231 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
232 struct wl128x_radio_parms_cmd *radio_parms;
233 struct wl128x_ini_general_params *gp = &nvs->general_params;
234 int ret;
235
236 if (!wl->nvs)
237 return -ENODEV;
238
239 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
240 if (!radio_parms)
241 return -ENOMEM;
242
243 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
244
245 /* 2.4GHz parameters */
246 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
247 sizeof(struct wl128x_ini_band_params_2));
248 memcpy(&radio_parms->dyn_params_2,
249 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
250 sizeof(struct wl128x_ini_fem_params_2));
251
252 /* 5GHz parameters */
253 memcpy(&radio_parms->static_params_5,
254 &nvs->stat_radio_params_5,
255 sizeof(struct wl128x_ini_band_params_5));
256 memcpy(&radio_parms->dyn_params_5,
257 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
258 sizeof(struct wl128x_ini_fem_params_5));
259
260 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
261
262 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
263 radio_parms, sizeof(*radio_parms));
264
265 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
266 if (ret < 0)
267 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
268
269 kfree(radio_parms);
270 return ret;
271}
272
Juuso Oikarinen644a4862010-10-05 13:11:56 +0200273int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
274{
275 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
276 struct conf_rf_settings *rf = &wl->conf.rf;
277 int ret;
278
279 if (!wl->nvs)
280 return -ENODEV;
281
282 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
283 if (!ext_radio_parms)
284 return -ENOMEM;
285
286 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
287
288 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
289 rf->tx_per_channel_power_compensation_2,
290 CONF_TX_PWR_COMPENSATION_LEN_2);
291 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
292 rf->tx_per_channel_power_compensation_5,
293 CONF_TX_PWR_COMPENSATION_LEN_5);
294
295 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
296 ext_radio_parms, sizeof(*ext_radio_parms));
297
298 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
299 if (ret < 0)
300 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
301
302 kfree(ext_radio_parms);
303 return ret;
304}
305
Luciano Coelho99d84c12010-03-26 12:53:20 +0200306/*
307 * Poll the mailbox event field until any of the bits in the mask is set or a
308 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
309 */
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200310static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
Luciano Coelho99d84c12010-03-26 12:53:20 +0200311{
312 u32 events_vector, event;
313 unsigned long timeout;
314
315 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
316
317 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200318 if (time_after(jiffies, timeout)) {
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200319 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
320 (int)mask);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200321 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200322 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200323
324 msleep(1);
325
326 /* read from both event fields */
327 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
328 sizeof(events_vector), false);
329 event = events_vector & mask;
330 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
331 sizeof(events_vector), false);
332 event |= events_vector & mask;
333 } while (!event);
334
335 return 0;
336}
337
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200338static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
339{
340 int ret;
341
342 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
343 if (ret != 0) {
344 ieee80211_queue_work(wl->hw, &wl->recovery_work);
345 return ret;
346 }
347
348 return 0;
349}
350
Juuso Oikarinen15305492010-02-22 08:38:32 +0200351int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300352{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300353 struct wl1271_cmd_join *join;
354 int ret, i;
355 u8 *bssid;
356
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300357 join = kzalloc(sizeof(*join), GFP_KERNEL);
358 if (!join) {
359 ret = -ENOMEM;
360 goto out;
361 }
362
363 wl1271_debug(DEBUG_CMD, "cmd join");
364
365 /* Reverse order BSSID */
366 bssid = (u8 *) &join->bssid_lsb;
367 for (i = 0; i < ETH_ALEN; i++)
368 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
369
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300370 join->rx_config_options = cpu_to_le32(wl->rx_config);
371 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
Juuso Oikarinen15305492010-02-22 08:38:32 +0200372 join->bss_type = bss_type;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300373 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200374 join->supported_rate_set = cpu_to_le32(wl->rate_set);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300375
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300376 if (wl->band == IEEE80211_BAND_5GHZ)
Teemu Paasikivia4102642009-10-13 12:47:51 +0300377 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300378
Juuso Oikarinen60e84c22010-03-26 12:53:25 +0200379 join->beacon_interval = cpu_to_le16(wl->beacon_int);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300380 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300381
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300382 join->channel = wl->channel;
383 join->ssid_len = wl->ssid_len;
384 memcpy(join->ssid, wl->ssid, wl->ssid_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300385
386 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
387
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300388 /* reset TX security counters */
389 wl->tx_security_last_seq = 0;
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200390 wl->tx_security_seq = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300391
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200392 wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
393 join->basic_rate_set, join->supported_rate_set);
394
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200395 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300396 if (ret < 0) {
397 wl1271_error("failed to initiate cmd join");
398 goto out_free;
399 }
400
Luciano Coelho99d84c12010-03-26 12:53:20 +0200401 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
402 if (ret < 0)
403 wl1271_error("cmd join event completion error");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300404
405out_free:
406 kfree(join);
407
408out:
409 return ret;
410}
411
412/**
413 * send test command to firmware
414 *
415 * @wl: wl struct
416 * @buf: buffer containing the command, with all headers, must work with dma
417 * @len: length of the buffer
418 * @answer: is answer needed
419 */
420int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
421{
422 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200423 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300424
425 wl1271_debug(DEBUG_CMD, "cmd test");
426
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200427 if (answer)
428 res_len = buf_len;
429
430 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300431
432 if (ret < 0) {
433 wl1271_warning("TEST command failed");
434 return ret;
435 }
436
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200437 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300438}
439
440/**
441 * read acx from firmware
442 *
443 * @wl: wl struct
444 * @id: acx id
445 * @buf: buffer for the response, including all headers, must work with dma
446 * @len: lenght of buf
447 */
448int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
449{
450 struct acx_header *acx = buf;
451 int ret;
452
453 wl1271_debug(DEBUG_CMD, "cmd interrogate");
454
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300455 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300456
457 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300458 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300459
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200460 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
461 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300462 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300463
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300464 return ret;
465}
466
467/**
468 * write acx value to firmware
469 *
470 * @wl: wl struct
471 * @id: acx id
472 * @buf: buffer containing acx, including all headers, must work with dma
473 * @len: length of buf
474 */
475int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
476{
477 struct acx_header *acx = buf;
478 int ret;
479
480 wl1271_debug(DEBUG_CMD, "cmd configure");
481
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300482 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300483
484 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300485 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300486
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200487 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300488 if (ret < 0) {
489 wl1271_warning("CONFIGURE command NOK");
490 return ret;
491 }
492
493 return 0;
494}
495
Luciano Coelho94210892009-12-11 15:40:55 +0200496int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300497{
498 struct cmd_enabledisable_path *cmd;
499 int ret;
500 u16 cmd_rx, cmd_tx;
501
502 wl1271_debug(DEBUG_CMD, "cmd data path");
503
504 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
505 if (!cmd) {
506 ret = -ENOMEM;
507 goto out;
508 }
509
Luciano Coelho94210892009-12-11 15:40:55 +0200510 /* the channel here is only used for calibration, so hardcoded to 1 */
511 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300512
513 if (enable) {
514 cmd_rx = CMD_ENABLE_RX;
515 cmd_tx = CMD_ENABLE_TX;
516 } else {
517 cmd_rx = CMD_DISABLE_RX;
518 cmd_tx = CMD_DISABLE_TX;
519 }
520
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200521 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300522 if (ret < 0) {
523 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200524 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300525 goto out;
526 }
527
528 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200529 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300530
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200531 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300532 if (ret < 0) {
533 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200534 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200535 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300536 }
537
538 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200539 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300540
541out:
542 kfree(cmd);
543 return ret;
544}
545
Eliad Pellerc8bde242011-02-02 09:59:35 +0200546int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300547{
548 struct wl1271_cmd_ps_params *ps_params = NULL;
549 int ret = 0;
550
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300551 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
552
553 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
554 if (!ps_params) {
555 ret = -ENOMEM;
556 goto out;
557 }
558
559 ps_params->ps_mode = ps_mode;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300560
561 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200562 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300563 if (ret < 0) {
564 wl1271_error("cmd set_ps_mode failed");
565 goto out;
566 }
567
568out:
569 kfree(ps_params);
570 return ret;
571}
572
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300573int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300574 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300575{
576 struct wl1271_cmd_template_set *cmd;
577 int ret = 0;
578
579 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
580
581 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
582 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
583
584 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
585 if (!cmd) {
586 ret = -ENOMEM;
587 goto out;
588 }
589
590 cmd->len = cpu_to_le16(buf_len);
591 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300592 cmd->enabled_rates = cpu_to_le32(rates);
Arik Nemtsov1e05a812010-10-16 17:44:51 +0200593 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
594 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200595 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300596
597 if (buf)
598 memcpy(cmd->template_data, buf, buf_len);
599
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200600 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300601 if (ret < 0) {
602 wl1271_warning("cmd set_template failed: %d", ret);
603 goto out_free;
604 }
605
606out_free:
607 kfree(cmd);
608
609out:
610 return ret;
611}
612
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300613int wl1271_cmd_build_null_data(struct wl1271 *wl)
614{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200615 struct sk_buff *skb = NULL;
616 int size;
617 void *ptr;
618 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300619
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300620
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200621 if (wl->bss_type == BSS_TYPE_IBSS) {
622 size = sizeof(struct wl12xx_null_data_template);
623 ptr = NULL;
624 } else {
625 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
626 if (!skb)
627 goto out;
628 size = skb->len;
629 ptr = skb->data;
630 }
631
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300632 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200633 wl->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300634
Kalle Valo899e6e62010-03-18 12:26:34 +0200635out:
636 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200637 if (ret)
638 wl1271_warning("cmd buld null data failed %d", ret);
639
Kalle Valo899e6e62010-03-18 12:26:34 +0200640 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300641
642}
643
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200644int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
645{
646 struct sk_buff *skb = NULL;
647 int ret = -ENOMEM;
648
649 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
650 if (!skb)
651 goto out;
652
653 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
654 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300655 CMD_TEMPL_KLV_IDX_NULL_DATA,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200656 wl->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200657
658out:
659 dev_kfree_skb(skb);
660 if (ret)
661 wl1271_warning("cmd build klv null data failed %d", ret);
662
663 return ret;
664
665}
666
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300667int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
668{
Kalle Valo899e6e62010-03-18 12:26:34 +0200669 struct sk_buff *skb;
670 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300671
Kalle Valo899e6e62010-03-18 12:26:34 +0200672 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
673 if (!skb)
674 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300675
Kalle Valo899e6e62010-03-18 12:26:34 +0200676 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Juuso Oikarinen849923f2010-07-08 17:49:59 +0300677 skb->len, 0, wl->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300678
Kalle Valo899e6e62010-03-18 12:26:34 +0200679out:
680 dev_kfree_skb(skb);
681 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300682}
683
Kalle Valo818e3062010-03-18 12:26:35 +0200684int wl1271_cmd_build_probe_req(struct wl1271 *wl,
685 const u8 *ssid, size_t ssid_len,
686 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300687{
Kalle Valo818e3062010-03-18 12:26:35 +0200688 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300689 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300690
Kalle Valo818e3062010-03-18 12:26:35 +0200691 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
692 ie, ie_len);
693 if (!skb) {
694 ret = -ENOMEM;
695 goto out;
696 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300697
Kalle Valo818e3062010-03-18 12:26:35 +0200698 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300699
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300700 if (band == IEEE80211_BAND_2GHZ)
701 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300702 skb->data, skb->len, 0,
703 wl->conf.tx.basic_rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300704 else
705 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300706 skb->data, skb->len, 0,
707 wl->conf.tx.basic_rate_5);
Kalle Valo818e3062010-03-18 12:26:35 +0200708
709out:
710 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300711 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300712}
713
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +0200714struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
715 struct sk_buff *skb)
716{
717 int ret;
718
719 if (!skb)
720 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
721 if (!skb)
722 goto out;
723
724 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
725
726 if (wl->band == IEEE80211_BAND_2GHZ)
727 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
728 skb->data, skb->len, 0,
729 wl->conf.tx.basic_rate);
730 else
731 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
732 skb->data, skb->len, 0,
733 wl->conf.tx.basic_rate_5);
734
735 if (ret < 0)
736 wl1271_error("Unable to set ap probe request template.");
737
738out:
739 return skb;
740}
741
Eliad Pellerc5312772010-12-09 11:31:27 +0200742int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
743{
744 int ret;
745 struct wl12xx_arp_rsp_template tmpl;
746 struct ieee80211_hdr_3addr *hdr;
747 struct arphdr *arp_hdr;
748
749 memset(&tmpl, 0, sizeof(tmpl));
750
751 /* mac80211 header */
752 hdr = &tmpl.hdr;
753 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
754 IEEE80211_STYPE_DATA |
755 IEEE80211_FCTL_TODS);
756 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
757 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
758 memset(hdr->addr3, 0xff, ETH_ALEN);
759
760 /* llc layer */
761 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +0100762 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200763
764 /* arp header */
765 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +0100766 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
767 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200768 arp_hdr->ar_hln = ETH_ALEN;
769 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +0100770 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +0200771
772 /* arp payload */
773 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
774 tmpl.sender_ip = ip_addr;
775
776 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
777 &tmpl, sizeof(tmpl), 0,
778 wl->basic_rate);
779
780 return ret;
781}
782
Kalle Valo023e0822010-03-18 12:26:36 +0200783int wl1271_build_qos_null_data(struct wl1271 *wl)
784{
785 struct ieee80211_qos_hdr template;
786
787 memset(&template, 0, sizeof(template));
788
789 memcpy(template.addr1, wl->bssid, ETH_ALEN);
790 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
791 memcpy(template.addr3, wl->bssid, ETH_ALEN);
792
793 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
794 IEEE80211_STYPE_QOS_NULLFUNC |
795 IEEE80211_FCTL_TODS);
796
797 /* FIXME: not sure what priority to use here */
798 template.qos_ctrl = cpu_to_le16(0);
799
800 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300801 sizeof(template), 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200802 wl->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +0200803}
804
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200805int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300806{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200807 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300808 int ret = 0;
809
810 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
811
812 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
813 if (!cmd) {
814 ret = -ENOMEM;
815 goto out;
816 }
817
818 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300819 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300820 cmd->key_type = KEY_WEP;
821
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200822 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300823 if (ret < 0) {
824 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
825 goto out;
826 }
827
828out:
829 kfree(cmd);
830
831 return ret;
832}
833
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200834int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
835{
836 struct wl1271_cmd_set_ap_keys *cmd;
837 int ret = 0;
838
839 wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
840
841 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
842 if (!cmd) {
843 ret = -ENOMEM;
844 goto out;
845 }
846
847 cmd->hlid = WL1271_AP_BROADCAST_HLID;
848 cmd->key_id = id;
849 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
850 cmd->key_action = cpu_to_le16(KEY_SET_ID);
851 cmd->key_type = KEY_WEP;
852
853 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
854 if (ret < 0) {
855 wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
856 goto out;
857 }
858
859out:
860 kfree(cmd);
861
862 return ret;
863}
864
865int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300866 u8 key_size, const u8 *key, const u8 *addr,
867 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300868{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200869 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300870 int ret = 0;
871
872 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
873 if (!cmd) {
874 ret = -ENOMEM;
875 goto out;
876 }
877
878 if (key_type != KEY_WEP)
879 memcpy(cmd->addr, addr, ETH_ALEN);
880
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300881 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300882 cmd->key_size = key_size;
883 cmd->key_type = key_type;
884
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300885 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
886 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300887
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300888 /* we have only one SSID profile */
889 cmd->ssid_profile = 0;
890
891 cmd->id = id;
892
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300893 if (key_type == KEY_TKIP) {
894 /*
895 * We get the key in the following form:
896 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
897 * but the target is expecting:
898 * TKIP - RX MIC - TX MIC
899 */
900 memcpy(cmd->key, key, 16);
901 memcpy(cmd->key + 16, key + 24, 8);
902 memcpy(cmd->key + 24, key + 16, 8);
903
904 } else {
905 memcpy(cmd->key, key, key_size);
906 }
907
908 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
909
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200910 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300911 if (ret < 0) {
912 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200913 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300914 }
915
916out:
917 kfree(cmd);
918
919 return ret;
920}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300921
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200922int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
923 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
924 u16 tx_seq_16)
925{
926 struct wl1271_cmd_set_ap_keys *cmd;
927 int ret = 0;
928 u8 lid_type;
929
930 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
931 if (!cmd)
932 return -ENOMEM;
933
934 if (hlid == WL1271_AP_BROADCAST_HLID) {
935 if (key_type == KEY_WEP)
936 lid_type = WEP_DEFAULT_LID_TYPE;
937 else
938 lid_type = BROADCAST_LID_TYPE;
939 } else {
940 lid_type = UNICAST_LID_TYPE;
941 }
942
943 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
944 " hlid: %d", (int)action, (int)id, (int)lid_type,
945 (int)key_type, (int)hlid);
946
947 cmd->lid_key_type = lid_type;
948 cmd->hlid = hlid;
949 cmd->key_action = cpu_to_le16(action);
950 cmd->key_size = key_size;
951 cmd->key_type = key_type;
952 cmd->key_id = id;
953 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
954 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
955
956 if (key_type == KEY_TKIP) {
957 /*
958 * We get the key in the following form:
959 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
960 * but the target is expecting:
961 * TKIP - RX MIC - TX MIC
962 */
963 memcpy(cmd->key, key, 16);
964 memcpy(cmd->key + 16, key + 24, 8);
965 memcpy(cmd->key + 24, key + 16, 8);
966 } else {
967 memcpy(cmd->key, key, key_size);
968 }
969
970 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
971
972 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
973 if (ret < 0) {
974 wl1271_warning("could not set ap keys");
975 goto out;
976 }
977
978out:
979 kfree(cmd);
980 return ret;
981}
982
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300983int wl1271_cmd_disconnect(struct wl1271 *wl)
984{
985 struct wl1271_cmd_disconnect *cmd;
986 int ret = 0;
987
988 wl1271_debug(DEBUG_CMD, "cmd disconnect");
989
990 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
991 if (!cmd) {
992 ret = -ENOMEM;
993 goto out;
994 }
995
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300996 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
997 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300998 /* disconnect reason is not used in immediate disconnections */
999 cmd->type = DISCONNECT_IMMEDIATE;
1000
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001001 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001002 if (ret < 0) {
1003 wl1271_error("failed to send disconnect command");
1004 goto out_free;
1005 }
1006
Luciano Coelho2f826f52010-03-26 12:53:21 +02001007 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
1008 if (ret < 0)
1009 wl1271_error("cmd disconnect event completion error");
1010
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001011out_free:
1012 kfree(cmd);
1013
1014out:
1015 return ret;
1016}
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001017
1018int wl1271_cmd_set_sta_state(struct wl1271 *wl)
1019{
1020 struct wl1271_cmd_set_sta_state *cmd;
1021 int ret = 0;
1022
1023 wl1271_debug(DEBUG_CMD, "cmd set sta state");
1024
1025 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1026 if (!cmd) {
1027 ret = -ENOMEM;
1028 goto out;
1029 }
1030
1031 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1032
1033 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
1034 if (ret < 0) {
1035 wl1271_error("failed to send set STA state command");
1036 goto out_free;
1037 }
1038
1039out_free:
1040 kfree(cmd);
1041
1042out:
1043 return ret;
1044}
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001045
1046int wl1271_cmd_start_bss(struct wl1271 *wl)
1047{
1048 struct wl1271_cmd_bss_start *cmd;
1049 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
1050 int ret;
1051
1052 wl1271_debug(DEBUG_CMD, "cmd start bss");
1053
1054 /*
1055 * FIXME: We currently do not support hidden SSID. The real SSID
1056 * should be fetched from mac80211 first.
1057 */
1058 if (wl->ssid_len == 0) {
1059 wl1271_warning("Hidden SSID currently not supported for AP");
1060 ret = -EINVAL;
1061 goto out;
1062 }
1063
1064 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1065 if (!cmd) {
1066 ret = -ENOMEM;
1067 goto out;
1068 }
1069
1070 memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
1071
Eliad Peller1d4801f2011-01-16 10:07:10 +01001072 cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001073 cmd->bss_index = WL1271_AP_BSS_INDEX;
1074 cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
1075 cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
1076 cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
1077 cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
1078 cmd->dtim_interval = bss_conf->dtim_period;
1079 cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
1080 cmd->channel = wl->channel;
1081 cmd->ssid_len = wl->ssid_len;
1082 cmd->ssid_type = SSID_TYPE_PUBLIC;
1083 memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1084
1085 switch (wl->band) {
1086 case IEEE80211_BAND_2GHZ:
1087 cmd->band = RADIO_BAND_2_4GHZ;
1088 break;
1089 case IEEE80211_BAND_5GHZ:
1090 cmd->band = RADIO_BAND_5GHZ;
1091 break;
1092 default:
1093 wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1094 cmd->band = RADIO_BAND_2_4GHZ;
1095 break;
1096 }
1097
1098 ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1099 if (ret < 0) {
1100 wl1271_error("failed to initiate cmd start bss");
1101 goto out_free;
1102 }
1103
1104out_free:
1105 kfree(cmd);
1106
1107out:
1108 return ret;
1109}
1110
1111int wl1271_cmd_stop_bss(struct wl1271 *wl)
1112{
1113 struct wl1271_cmd_bss_start *cmd;
1114 int ret;
1115
1116 wl1271_debug(DEBUG_CMD, "cmd stop bss");
1117
1118 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1119 if (!cmd) {
1120 ret = -ENOMEM;
1121 goto out;
1122 }
1123
1124 cmd->bss_index = WL1271_AP_BSS_INDEX;
1125
1126 ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1127 if (ret < 0) {
1128 wl1271_error("failed to initiate cmd stop bss");
1129 goto out_free;
1130 }
1131
1132out_free:
1133 kfree(cmd);
1134
1135out:
1136 return ret;
1137}
1138
1139int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1140{
1141 struct wl1271_cmd_add_sta *cmd;
1142 int ret;
1143
1144 wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1145
1146 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1147 if (!cmd) {
1148 ret = -ENOMEM;
1149 goto out;
1150 }
1151
1152 /* currently we don't support UAPSD */
1153 cmd->sp_len = 0;
1154
1155 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1156 cmd->bss_index = WL1271_AP_BSS_INDEX;
1157 cmd->aid = sta->aid;
1158 cmd->hlid = hlid;
1159
1160 /*
1161 * FIXME: Does STA support QOS? We need to propagate this info from
1162 * hostapd. Currently not that important since this is only used for
1163 * sending the correct flavor of null-data packet in response to a
1164 * trigger.
1165 */
1166 cmd->wmm = 0;
1167
1168 cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
1169 sta->supp_rates[wl->band]));
1170
1171 wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
1172
1173 ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
1174 if (ret < 0) {
1175 wl1271_error("failed to initiate cmd add sta");
1176 goto out_free;
1177 }
1178
1179out_free:
1180 kfree(cmd);
1181
1182out:
1183 return ret;
1184}
1185
1186int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
1187{
1188 struct wl1271_cmd_remove_sta *cmd;
1189 int ret;
1190
1191 wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
1192
1193 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1194 if (!cmd) {
1195 ret = -ENOMEM;
1196 goto out;
1197 }
1198
1199 cmd->hlid = hlid;
1200 /* We never send a deauth, mac80211 is in charge of this */
1201 cmd->reason_opcode = 0;
1202 cmd->send_deauth_flag = 0;
1203
1204 ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
1205 if (ret < 0) {
1206 wl1271_error("failed to initiate cmd remove sta");
1207 goto out_free;
1208 }
1209
Arik Nemtsov05285cf2010-11-12 17:15:03 +02001210 /*
1211 * We are ok with a timeout here. The event is sometimes not sent
1212 * due to a firmware bug.
1213 */
1214 wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001215
1216out_free:
1217 kfree(cmd);
1218
1219out:
1220 return ret;
1221}