blob: 5d0ad2d93cb339274664fb7a4575b372e60e0fde [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;
Arik Nemtsovf482b762011-04-18 14:15:23 +030079 goto fail;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030080 }
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 Oikarinen3b775b42009-11-02 20:22:10 +020099 ret = -EIO;
Arik Nemtsovf482b762011-04-18 14:15:23 +0300100 goto fail;
Juuso Oikarinen3b775b42009-11-02 20:22:10 +0200101 }
102
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200103 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
104 WL1271_ACX_INTR_CMD_COMPLETE);
Arik Nemtsovf482b762011-04-18 14:15:23 +0300105 return 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300106
Arik Nemtsovf482b762011-04-18 14:15:23 +0300107fail:
108 WARN_ON(1);
109 ieee80211_queue_work(wl->hw, &wl->recovery_work);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300110 return ret;
111}
112
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200113int wl1271_cmd_general_parms(struct wl1271 *wl)
114{
115 struct wl1271_general_parms_cmd *gen_parms;
Shahar Levi49d750ca2011-03-06 16:32:09 +0200116 struct wl1271_ini_general_params *gp =
117 &((struct wl1271_nvs_file *)wl->nvs)->general_params;
118 bool answer = false;
119 int ret;
120
121 if (!wl->nvs)
122 return -ENODEV;
123
124 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
125 if (!gen_parms)
126 return -ENOMEM;
127
128 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
129
130 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
131
132 if (gp->tx_bip_fem_auto_detect)
133 answer = true;
134
Shahar Levib03acad2011-04-03 13:54:54 +0300135 /* Override the REF CLK from the NVS with the one from platform data */
136 gen_parms->general_params.ref_clock = wl->ref_clock;
137
Shahar Levi49d750ca2011-03-06 16:32:09 +0200138 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
139 if (ret < 0) {
140 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
141 goto out;
142 }
143
144 gp->tx_bip_fem_manufacturer =
145 gen_parms->general_params.tx_bip_fem_manufacturer;
146
147 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
148 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
149
150out:
151 kfree(gen_parms);
152 return ret;
153}
154
155int wl128x_cmd_general_parms(struct wl1271 *wl)
156{
157 struct wl128x_general_parms_cmd *gen_parms;
158 struct wl128x_ini_general_params *gp =
159 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300160 bool answer = false;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200161 int ret;
162
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200163 if (!wl->nvs)
164 return -ENODEV;
165
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200166 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
167 if (!gen_parms)
168 return -ENOMEM;
169
170 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
171
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300172 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200173
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300174 if (gp->tx_bip_fem_auto_detect)
175 answer = true;
176
Shahar Levib03acad2011-04-03 13:54:54 +0300177 /* Replace REF and TCXO CLKs with the ones from platform data */
178 gen_parms->general_params.ref_clock = wl->ref_clock;
179 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
180
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300181 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
182 if (ret < 0) {
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200183 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300184 goto out;
185 }
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200186
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300187 gp->tx_bip_fem_manufacturer =
188 gen_parms->general_params.tx_bip_fem_manufacturer;
189
190 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
191 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
192
193out:
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200194 kfree(gen_parms);
195 return ret;
196}
197
198int wl1271_cmd_radio_parms(struct wl1271 *wl)
199{
Shahar Levibc765bf2011-03-06 16:32:10 +0200200 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200201 struct wl1271_radio_parms_cmd *radio_parms;
Shahar Levibc765bf2011-03-06 16:32:10 +0200202 struct wl1271_ini_general_params *gp = &nvs->general_params;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200203 int ret;
204
205 if (!wl->nvs)
206 return -ENODEV;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200207
208 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
209 if (!radio_parms)
210 return -ENOMEM;
211
212 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
213
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300214 /* 2.4GHz parameters */
Shahar Levibc765bf2011-03-06 16:32:10 +0200215 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300216 sizeof(struct wl1271_ini_band_params_2));
217 memcpy(&radio_parms->dyn_params_2,
Shahar Levibc765bf2011-03-06 16:32:10 +0200218 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300219 sizeof(struct wl1271_ini_fem_params_2));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200220
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300221 /* 5GHz parameters */
222 memcpy(&radio_parms->static_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200223 &nvs->stat_radio_params_5,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300224 sizeof(struct wl1271_ini_band_params_5));
225 memcpy(&radio_parms->dyn_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200226 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300227 sizeof(struct wl1271_ini_fem_params_5));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200228
229 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
230 radio_parms, sizeof(*radio_parms));
231
232 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
233 if (ret < 0)
234 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
235
236 kfree(radio_parms);
237 return ret;
238}
239
Shahar Levi49d750ca2011-03-06 16:32:09 +0200240int wl128x_cmd_radio_parms(struct wl1271 *wl)
241{
242 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
243 struct wl128x_radio_parms_cmd *radio_parms;
244 struct wl128x_ini_general_params *gp = &nvs->general_params;
245 int ret;
246
247 if (!wl->nvs)
248 return -ENODEV;
249
250 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
251 if (!radio_parms)
252 return -ENOMEM;
253
254 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
255
256 /* 2.4GHz parameters */
257 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
258 sizeof(struct wl128x_ini_band_params_2));
259 memcpy(&radio_parms->dyn_params_2,
260 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
261 sizeof(struct wl128x_ini_fem_params_2));
262
263 /* 5GHz parameters */
264 memcpy(&radio_parms->static_params_5,
265 &nvs->stat_radio_params_5,
266 sizeof(struct wl128x_ini_band_params_5));
267 memcpy(&radio_parms->dyn_params_5,
268 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
269 sizeof(struct wl128x_ini_fem_params_5));
270
271 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
272
273 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
274 radio_parms, sizeof(*radio_parms));
275
276 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
277 if (ret < 0)
278 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
279
280 kfree(radio_parms);
281 return ret;
282}
283
Juuso Oikarinen644a4862010-10-05 13:11:56 +0200284int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
285{
286 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
287 struct conf_rf_settings *rf = &wl->conf.rf;
288 int ret;
289
290 if (!wl->nvs)
291 return -ENODEV;
292
293 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
294 if (!ext_radio_parms)
295 return -ENOMEM;
296
297 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
298
299 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
300 rf->tx_per_channel_power_compensation_2,
301 CONF_TX_PWR_COMPENSATION_LEN_2);
302 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
303 rf->tx_per_channel_power_compensation_5,
304 CONF_TX_PWR_COMPENSATION_LEN_5);
305
306 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
307 ext_radio_parms, sizeof(*ext_radio_parms));
308
309 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
310 if (ret < 0)
311 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
312
313 kfree(ext_radio_parms);
314 return ret;
315}
316
Luciano Coelho99d84c12010-03-26 12:53:20 +0200317/*
318 * Poll the mailbox event field until any of the bits in the mask is set or a
319 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
320 */
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200321static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
Luciano Coelho99d84c12010-03-26 12:53:20 +0200322{
323 u32 events_vector, event;
324 unsigned long timeout;
325
326 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
327
328 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200329 if (time_after(jiffies, timeout)) {
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200330 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
331 (int)mask);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200332 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200333 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200334
335 msleep(1);
336
337 /* read from both event fields */
338 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
339 sizeof(events_vector), false);
340 event = events_vector & mask;
341 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
342 sizeof(events_vector), false);
343 event |= events_vector & mask;
344 } while (!event);
345
346 return 0;
347}
348
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200349static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
350{
351 int ret;
352
353 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
354 if (ret != 0) {
355 ieee80211_queue_work(wl->hw, &wl->recovery_work);
356 return ret;
357 }
358
359 return 0;
360}
361
Juuso Oikarinen15305492010-02-22 08:38:32 +0200362int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300363{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300364 struct wl1271_cmd_join *join;
365 int ret, i;
366 u8 *bssid;
367
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300368 join = kzalloc(sizeof(*join), GFP_KERNEL);
369 if (!join) {
370 ret = -ENOMEM;
371 goto out;
372 }
373
374 wl1271_debug(DEBUG_CMD, "cmd join");
375
376 /* Reverse order BSSID */
377 bssid = (u8 *) &join->bssid_lsb;
378 for (i = 0; i < ETH_ALEN; i++)
379 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
380
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300381 join->rx_config_options = cpu_to_le32(wl->rx_config);
382 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
Juuso Oikarinen15305492010-02-22 08:38:32 +0200383 join->bss_type = bss_type;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300384 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200385 join->supported_rate_set = cpu_to_le32(wl->rate_set);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300386
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300387 if (wl->band == IEEE80211_BAND_5GHZ)
Teemu Paasikivia4102642009-10-13 12:47:51 +0300388 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300389
Juuso Oikarinen60e84c22010-03-26 12:53:25 +0200390 join->beacon_interval = cpu_to_le16(wl->beacon_int);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300391 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300392
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300393 join->channel = wl->channel;
394 join->ssid_len = wl->ssid_len;
395 memcpy(join->ssid, wl->ssid, wl->ssid_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300396
397 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
398
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300399 /* reset TX security counters */
400 wl->tx_security_last_seq = 0;
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200401 wl->tx_security_seq = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300402
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200403 wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
404 join->basic_rate_set, join->supported_rate_set);
405
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200406 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300407 if (ret < 0) {
408 wl1271_error("failed to initiate cmd join");
409 goto out_free;
410 }
411
Luciano Coelho99d84c12010-03-26 12:53:20 +0200412 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
413 if (ret < 0)
414 wl1271_error("cmd join event completion error");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300415
416out_free:
417 kfree(join);
418
419out:
420 return ret;
421}
422
423/**
424 * send test command to firmware
425 *
426 * @wl: wl struct
427 * @buf: buffer containing the command, with all headers, must work with dma
428 * @len: length of the buffer
429 * @answer: is answer needed
430 */
431int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
432{
433 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200434 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300435
436 wl1271_debug(DEBUG_CMD, "cmd test");
437
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200438 if (answer)
439 res_len = buf_len;
440
441 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300442
443 if (ret < 0) {
444 wl1271_warning("TEST command failed");
445 return ret;
446 }
447
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200448 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300449}
450
451/**
452 * read acx from firmware
453 *
454 * @wl: wl struct
455 * @id: acx id
456 * @buf: buffer for the response, including all headers, must work with dma
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300457 * @len: length of buf
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300458 */
459int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
460{
461 struct acx_header *acx = buf;
462 int ret;
463
464 wl1271_debug(DEBUG_CMD, "cmd interrogate");
465
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300466 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300467
468 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300469 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300470
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200471 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
472 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300473 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300474
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300475 return ret;
476}
477
478/**
479 * write acx value to firmware
480 *
481 * @wl: wl struct
482 * @id: acx id
483 * @buf: buffer containing acx, including all headers, must work with dma
484 * @len: length of buf
485 */
486int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
487{
488 struct acx_header *acx = buf;
489 int ret;
490
491 wl1271_debug(DEBUG_CMD, "cmd configure");
492
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300493 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300494
495 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300496 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300497
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200498 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300499 if (ret < 0) {
500 wl1271_warning("CONFIGURE command NOK");
501 return ret;
502 }
503
504 return 0;
505}
506
Luciano Coelho94210892009-12-11 15:40:55 +0200507int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300508{
509 struct cmd_enabledisable_path *cmd;
510 int ret;
511 u16 cmd_rx, cmd_tx;
512
513 wl1271_debug(DEBUG_CMD, "cmd data path");
514
515 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
516 if (!cmd) {
517 ret = -ENOMEM;
518 goto out;
519 }
520
Luciano Coelho94210892009-12-11 15:40:55 +0200521 /* the channel here is only used for calibration, so hardcoded to 1 */
522 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300523
524 if (enable) {
525 cmd_rx = CMD_ENABLE_RX;
526 cmd_tx = CMD_ENABLE_TX;
527 } else {
528 cmd_rx = CMD_DISABLE_RX;
529 cmd_tx = CMD_DISABLE_TX;
530 }
531
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200532 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300533 if (ret < 0) {
534 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200535 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300536 goto out;
537 }
538
539 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200540 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300541
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200542 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300543 if (ret < 0) {
544 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200545 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200546 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300547 }
548
549 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200550 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300551
552out:
553 kfree(cmd);
554 return ret;
555}
556
Eliad Pellerc8bde242011-02-02 09:59:35 +0200557int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300558{
559 struct wl1271_cmd_ps_params *ps_params = NULL;
560 int ret = 0;
561
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300562 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
563
564 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
565 if (!ps_params) {
566 ret = -ENOMEM;
567 goto out;
568 }
569
570 ps_params->ps_mode = ps_mode;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300571
572 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200573 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300574 if (ret < 0) {
575 wl1271_error("cmd set_ps_mode failed");
576 goto out;
577 }
578
579out:
580 kfree(ps_params);
581 return ret;
582}
583
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300584int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300585 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300586{
587 struct wl1271_cmd_template_set *cmd;
588 int ret = 0;
589
590 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
591
592 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
593 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
594
595 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
596 if (!cmd) {
597 ret = -ENOMEM;
598 goto out;
599 }
600
601 cmd->len = cpu_to_le16(buf_len);
602 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300603 cmd->enabled_rates = cpu_to_le32(rates);
Arik Nemtsov1e05a812010-10-16 17:44:51 +0200604 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
605 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200606 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300607
608 if (buf)
609 memcpy(cmd->template_data, buf, buf_len);
610
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200611 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300612 if (ret < 0) {
613 wl1271_warning("cmd set_template failed: %d", ret);
614 goto out_free;
615 }
616
617out_free:
618 kfree(cmd);
619
620out:
621 return ret;
622}
623
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300624int wl1271_cmd_build_null_data(struct wl1271 *wl)
625{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200626 struct sk_buff *skb = NULL;
627 int size;
628 void *ptr;
629 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300630
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300631
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200632 if (wl->bss_type == BSS_TYPE_IBSS) {
633 size = sizeof(struct wl12xx_null_data_template);
634 ptr = NULL;
635 } else {
636 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
637 if (!skb)
638 goto out;
639 size = skb->len;
640 ptr = skb->data;
641 }
642
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300643 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200644 wl->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300645
Kalle Valo899e6e62010-03-18 12:26:34 +0200646out:
647 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200648 if (ret)
649 wl1271_warning("cmd buld null data failed %d", ret);
650
Kalle Valo899e6e62010-03-18 12:26:34 +0200651 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300652
653}
654
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200655int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
656{
657 struct sk_buff *skb = NULL;
658 int ret = -ENOMEM;
659
660 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
661 if (!skb)
662 goto out;
663
664 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
665 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300666 CMD_TEMPL_KLV_IDX_NULL_DATA,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200667 wl->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200668
669out:
670 dev_kfree_skb(skb);
671 if (ret)
672 wl1271_warning("cmd build klv null data failed %d", ret);
673
674 return ret;
675
676}
677
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300678int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
679{
Kalle Valo899e6e62010-03-18 12:26:34 +0200680 struct sk_buff *skb;
681 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300682
Kalle Valo899e6e62010-03-18 12:26:34 +0200683 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
684 if (!skb)
685 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300686
Kalle Valo899e6e62010-03-18 12:26:34 +0200687 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Juuso Oikarinen849923f2010-07-08 17:49:59 +0300688 skb->len, 0, wl->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300689
Kalle Valo899e6e62010-03-18 12:26:34 +0200690out:
691 dev_kfree_skb(skb);
692 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300693}
694
Kalle Valo818e3062010-03-18 12:26:35 +0200695int wl1271_cmd_build_probe_req(struct wl1271 *wl,
696 const u8 *ssid, size_t ssid_len,
697 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300698{
Kalle Valo818e3062010-03-18 12:26:35 +0200699 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300700 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300701
Kalle Valo818e3062010-03-18 12:26:35 +0200702 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
703 ie, ie_len);
704 if (!skb) {
705 ret = -ENOMEM;
706 goto out;
707 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300708
Kalle Valo818e3062010-03-18 12:26:35 +0200709 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300710
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300711 if (band == IEEE80211_BAND_2GHZ)
712 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300713 skb->data, skb->len, 0,
714 wl->conf.tx.basic_rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300715 else
716 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300717 skb->data, skb->len, 0,
718 wl->conf.tx.basic_rate_5);
Kalle Valo818e3062010-03-18 12:26:35 +0200719
720out:
721 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300722 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300723}
724
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +0200725struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
726 struct sk_buff *skb)
727{
728 int ret;
729
730 if (!skb)
731 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
732 if (!skb)
733 goto out;
734
735 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
736
737 if (wl->band == IEEE80211_BAND_2GHZ)
738 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
739 skb->data, skb->len, 0,
740 wl->conf.tx.basic_rate);
741 else
742 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
743 skb->data, skb->len, 0,
744 wl->conf.tx.basic_rate_5);
745
746 if (ret < 0)
747 wl1271_error("Unable to set ap probe request template.");
748
749out:
750 return skb;
751}
752
Eliad Pellerc5312772010-12-09 11:31:27 +0200753int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
754{
755 int ret;
756 struct wl12xx_arp_rsp_template tmpl;
757 struct ieee80211_hdr_3addr *hdr;
758 struct arphdr *arp_hdr;
759
760 memset(&tmpl, 0, sizeof(tmpl));
761
762 /* mac80211 header */
763 hdr = &tmpl.hdr;
764 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
765 IEEE80211_STYPE_DATA |
766 IEEE80211_FCTL_TODS);
767 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
768 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
769 memset(hdr->addr3, 0xff, ETH_ALEN);
770
771 /* llc layer */
772 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +0100773 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200774
775 /* arp header */
776 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +0100777 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
778 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200779 arp_hdr->ar_hln = ETH_ALEN;
780 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +0100781 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +0200782
783 /* arp payload */
784 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
785 tmpl.sender_ip = ip_addr;
786
787 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
788 &tmpl, sizeof(tmpl), 0,
789 wl->basic_rate);
790
791 return ret;
792}
793
Kalle Valo023e0822010-03-18 12:26:36 +0200794int wl1271_build_qos_null_data(struct wl1271 *wl)
795{
796 struct ieee80211_qos_hdr template;
797
798 memset(&template, 0, sizeof(template));
799
800 memcpy(template.addr1, wl->bssid, ETH_ALEN);
801 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
802 memcpy(template.addr3, wl->bssid, ETH_ALEN);
803
804 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
805 IEEE80211_STYPE_QOS_NULLFUNC |
806 IEEE80211_FCTL_TODS);
807
808 /* FIXME: not sure what priority to use here */
809 template.qos_ctrl = cpu_to_le16(0);
810
811 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300812 sizeof(template), 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200813 wl->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +0200814}
815
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200816int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300817{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200818 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300819 int ret = 0;
820
821 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
822
823 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
824 if (!cmd) {
825 ret = -ENOMEM;
826 goto out;
827 }
828
829 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300830 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300831 cmd->key_type = KEY_WEP;
832
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200833 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300834 if (ret < 0) {
835 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
836 goto out;
837 }
838
839out:
840 kfree(cmd);
841
842 return ret;
843}
844
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200845int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
846{
847 struct wl1271_cmd_set_ap_keys *cmd;
848 int ret = 0;
849
850 wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
851
852 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
853 if (!cmd) {
854 ret = -ENOMEM;
855 goto out;
856 }
857
858 cmd->hlid = WL1271_AP_BROADCAST_HLID;
859 cmd->key_id = id;
860 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
861 cmd->key_action = cpu_to_le16(KEY_SET_ID);
862 cmd->key_type = KEY_WEP;
863
864 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
865 if (ret < 0) {
866 wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
867 goto out;
868 }
869
870out:
871 kfree(cmd);
872
873 return ret;
874}
875
876int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300877 u8 key_size, const u8 *key, const u8 *addr,
878 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300879{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200880 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300881 int ret = 0;
882
883 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
884 if (!cmd) {
885 ret = -ENOMEM;
886 goto out;
887 }
888
889 if (key_type != KEY_WEP)
890 memcpy(cmd->addr, addr, ETH_ALEN);
891
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300892 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300893 cmd->key_size = key_size;
894 cmd->key_type = key_type;
895
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300896 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
897 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300898
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300899 /* we have only one SSID profile */
900 cmd->ssid_profile = 0;
901
902 cmd->id = id;
903
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300904 if (key_type == KEY_TKIP) {
905 /*
906 * We get the key in the following form:
907 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
908 * but the target is expecting:
909 * TKIP - RX MIC - TX MIC
910 */
911 memcpy(cmd->key, key, 16);
912 memcpy(cmd->key + 16, key + 24, 8);
913 memcpy(cmd->key + 24, key + 16, 8);
914
915 } else {
916 memcpy(cmd->key, key, key_size);
917 }
918
919 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
920
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200921 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300922 if (ret < 0) {
923 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200924 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300925 }
926
927out:
928 kfree(cmd);
929
930 return ret;
931}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300932
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200933int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
934 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
935 u16 tx_seq_16)
936{
937 struct wl1271_cmd_set_ap_keys *cmd;
938 int ret = 0;
939 u8 lid_type;
940
941 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
942 if (!cmd)
943 return -ENOMEM;
944
945 if (hlid == WL1271_AP_BROADCAST_HLID) {
946 if (key_type == KEY_WEP)
947 lid_type = WEP_DEFAULT_LID_TYPE;
948 else
949 lid_type = BROADCAST_LID_TYPE;
950 } else {
951 lid_type = UNICAST_LID_TYPE;
952 }
953
954 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
955 " hlid: %d", (int)action, (int)id, (int)lid_type,
956 (int)key_type, (int)hlid);
957
958 cmd->lid_key_type = lid_type;
959 cmd->hlid = hlid;
960 cmd->key_action = cpu_to_le16(action);
961 cmd->key_size = key_size;
962 cmd->key_type = key_type;
963 cmd->key_id = id;
964 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
965 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
966
967 if (key_type == KEY_TKIP) {
968 /*
969 * We get the key in the following form:
970 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
971 * but the target is expecting:
972 * TKIP - RX MIC - TX MIC
973 */
974 memcpy(cmd->key, key, 16);
975 memcpy(cmd->key + 16, key + 24, 8);
976 memcpy(cmd->key + 24, key + 16, 8);
977 } else {
978 memcpy(cmd->key, key, key_size);
979 }
980
981 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
982
983 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
984 if (ret < 0) {
985 wl1271_warning("could not set ap keys");
986 goto out;
987 }
988
989out:
990 kfree(cmd);
991 return ret;
992}
993
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300994int wl1271_cmd_disconnect(struct wl1271 *wl)
995{
996 struct wl1271_cmd_disconnect *cmd;
997 int ret = 0;
998
999 wl1271_debug(DEBUG_CMD, "cmd disconnect");
1000
1001 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1002 if (!cmd) {
1003 ret = -ENOMEM;
1004 goto out;
1005 }
1006
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001007 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
1008 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001009 /* disconnect reason is not used in immediate disconnections */
1010 cmd->type = DISCONNECT_IMMEDIATE;
1011
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001012 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001013 if (ret < 0) {
1014 wl1271_error("failed to send disconnect command");
1015 goto out_free;
1016 }
1017
Luciano Coelho2f826f52010-03-26 12:53:21 +02001018 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
1019 if (ret < 0)
1020 wl1271_error("cmd disconnect event completion error");
1021
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001022out_free:
1023 kfree(cmd);
1024
1025out:
1026 return ret;
1027}
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001028
1029int wl1271_cmd_set_sta_state(struct wl1271 *wl)
1030{
1031 struct wl1271_cmd_set_sta_state *cmd;
1032 int ret = 0;
1033
1034 wl1271_debug(DEBUG_CMD, "cmd set sta state");
1035
1036 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1037 if (!cmd) {
1038 ret = -ENOMEM;
1039 goto out;
1040 }
1041
1042 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1043
1044 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
1045 if (ret < 0) {
1046 wl1271_error("failed to send set STA state command");
1047 goto out_free;
1048 }
1049
1050out_free:
1051 kfree(cmd);
1052
1053out:
1054 return ret;
1055}
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001056
1057int wl1271_cmd_start_bss(struct wl1271 *wl)
1058{
1059 struct wl1271_cmd_bss_start *cmd;
1060 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
1061 int ret;
1062
1063 wl1271_debug(DEBUG_CMD, "cmd start bss");
1064
1065 /*
1066 * FIXME: We currently do not support hidden SSID. The real SSID
1067 * should be fetched from mac80211 first.
1068 */
1069 if (wl->ssid_len == 0) {
1070 wl1271_warning("Hidden SSID currently not supported for AP");
1071 ret = -EINVAL;
1072 goto out;
1073 }
1074
1075 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1076 if (!cmd) {
1077 ret = -ENOMEM;
1078 goto out;
1079 }
1080
1081 memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
1082
Luciano Coelho25eaea302011-05-02 12:37:33 +03001083 cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001084 cmd->bss_index = WL1271_AP_BSS_INDEX;
1085 cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
1086 cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
1087 cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
1088 cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
1089 cmd->dtim_interval = bss_conf->dtim_period;
1090 cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
1091 cmd->channel = wl->channel;
1092 cmd->ssid_len = wl->ssid_len;
1093 cmd->ssid_type = SSID_TYPE_PUBLIC;
1094 memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1095
1096 switch (wl->band) {
1097 case IEEE80211_BAND_2GHZ:
1098 cmd->band = RADIO_BAND_2_4GHZ;
1099 break;
1100 case IEEE80211_BAND_5GHZ:
1101 cmd->band = RADIO_BAND_5GHZ;
1102 break;
1103 default:
1104 wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1105 cmd->band = RADIO_BAND_2_4GHZ;
1106 break;
1107 }
1108
1109 ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1110 if (ret < 0) {
1111 wl1271_error("failed to initiate cmd start bss");
1112 goto out_free;
1113 }
1114
1115out_free:
1116 kfree(cmd);
1117
1118out:
1119 return ret;
1120}
1121
1122int wl1271_cmd_stop_bss(struct wl1271 *wl)
1123{
1124 struct wl1271_cmd_bss_start *cmd;
1125 int ret;
1126
1127 wl1271_debug(DEBUG_CMD, "cmd stop bss");
1128
1129 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1130 if (!cmd) {
1131 ret = -ENOMEM;
1132 goto out;
1133 }
1134
1135 cmd->bss_index = WL1271_AP_BSS_INDEX;
1136
1137 ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1138 if (ret < 0) {
1139 wl1271_error("failed to initiate cmd stop bss");
1140 goto out_free;
1141 }
1142
1143out_free:
1144 kfree(cmd);
1145
1146out:
1147 return ret;
1148}
1149
1150int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1151{
1152 struct wl1271_cmd_add_sta *cmd;
1153 int ret;
1154
1155 wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1156
1157 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1158 if (!cmd) {
1159 ret = -ENOMEM;
1160 goto out;
1161 }
1162
1163 /* currently we don't support UAPSD */
1164 cmd->sp_len = 0;
1165
1166 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1167 cmd->bss_index = WL1271_AP_BSS_INDEX;
1168 cmd->aid = sta->aid;
1169 cmd->hlid = hlid;
Arik Nemtsovb4d38db2011-06-27 23:58:46 +03001170 cmd->wmm = sta->wme ? 1 : 0;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001171
1172 cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
1173 sta->supp_rates[wl->band]));
1174
1175 wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
1176
1177 ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
1178 if (ret < 0) {
1179 wl1271_error("failed to initiate cmd add sta");
1180 goto out_free;
1181 }
1182
1183out_free:
1184 kfree(cmd);
1185
1186out:
1187 return ret;
1188}
1189
1190int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
1191{
1192 struct wl1271_cmd_remove_sta *cmd;
1193 int ret;
1194
1195 wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
1196
1197 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1198 if (!cmd) {
1199 ret = -ENOMEM;
1200 goto out;
1201 }
1202
1203 cmd->hlid = hlid;
1204 /* We never send a deauth, mac80211 is in charge of this */
1205 cmd->reason_opcode = 0;
1206 cmd->send_deauth_flag = 0;
1207
1208 ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
1209 if (ret < 0) {
1210 wl1271_error("failed to initiate cmd remove sta");
1211 goto out_free;
1212 }
1213
Arik Nemtsov05285cf2010-11-12 17:15:03 +02001214 /*
1215 * We are ok with a timeout here. The event is sometimes not sent
1216 * due to a firmware bug.
1217 */
1218 wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001219
1220out_free:
1221 kfree(cmd);
1222
1223out:
1224 return ret;
1225}