blob: b3a4f58249b0ddeaf7334e794ffcab0ffd9f1368 [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>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030026#include <linux/spi/spi.h>
27#include <linux/etherdevice.h>
Kalle Valo023e0822010-03-18 12:26:36 +020028#include <linux/ieee80211.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030030
Shahar Levi00d20102010-11-08 11:20:10 +000031#include "wl12xx.h"
32#include "reg.h"
33#include "io.h"
34#include "acx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030035#include "wl12xx_80211.h"
Shahar Levi00d20102010-11-08 11:20:10 +000036#include "cmd.h"
37#include "event.h"
Arik Nemtsov98bdaab2010-10-16 18:08:58 +020038#include "tx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030039
Juuso Oikarinen16092b52010-04-28 09:49:59 +030040#define WL1271_CMD_FAST_POLL_COUNT 50
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030041
42/*
43 * send command to firmware
44 *
45 * @wl: wl struct
46 * @id: command id
47 * @buf: buffer containing the command, must work with dma
48 * @len: length of the buffer
49 */
Juuso Oikarinenfa867e72009-11-02 20:22:13 +020050int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
51 size_t res_len)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030052{
53 struct wl1271_cmd_header *cmd;
54 unsigned long timeout;
55 u32 intr;
56 int ret = 0;
Juuso Oikarinenad150e92009-11-02 20:22:12 +020057 u16 status;
Saravanan Dhanabalbc0f03e2010-03-26 12:53:33 +020058 u16 poll_count = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030059
60 cmd = buf;
Luciano Coelhod0f63b22009-10-15 10:33:29 +030061 cmd->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030062 cmd->status = 0;
63
64 WARN_ON(len % 4 != 0);
Arik Nemtsov24225b32011-03-01 12:27:26 +020065 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030066
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020067 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030068
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020069 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030070
71 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
72
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020073 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030074 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
75 if (time_after(jiffies, timeout)) {
76 wl1271_error("command complete timeout");
77 ret = -ETIMEDOUT;
Arik Nemtsovf482b762011-04-18 14:15:23 +030078 goto fail;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030079 }
80
Saravanan Dhanabalbc0f03e2010-03-26 12:53:33 +020081 poll_count++;
Juuso Oikarinen16092b52010-04-28 09:49:59 +030082 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
83 udelay(10);
84 else
85 msleep(1);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030086
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020087 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030088 }
89
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020090 /* read back the status code of the command */
Juuso Oikarinenfa867e72009-11-02 20:22:13 +020091 if (res_len == 0)
92 res_len = sizeof(struct wl1271_cmd_header);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020093 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020094
Juuso Oikarinenad150e92009-11-02 20:22:12 +020095 status = le16_to_cpu(cmd->status);
96 if (status != CMD_STATUS_SUCCESS) {
97 wl1271_error("command execute failure %d", status);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020098 ret = -EIO;
Arik Nemtsovf482b762011-04-18 14:15:23 +030099 goto fail;
Juuso Oikarinen3b775b42009-11-02 20:22:10 +0200100 }
101
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200102 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
103 WL1271_ACX_INTR_CMD_COMPLETE);
Arik Nemtsovf482b762011-04-18 14:15:23 +0300104 return 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300105
Arik Nemtsovf482b762011-04-18 14:15:23 +0300106fail:
107 WARN_ON(1);
108 ieee80211_queue_work(wl->hw, &wl->recovery_work);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300109 return ret;
110}
111
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200112int wl1271_cmd_general_parms(struct wl1271 *wl)
113{
114 struct wl1271_general_parms_cmd *gen_parms;
Shahar Levi49d750ca2011-03-06 16:32:09 +0200115 struct wl1271_ini_general_params *gp =
116 &((struct wl1271_nvs_file *)wl->nvs)->general_params;
117 bool answer = false;
118 int ret;
119
120 if (!wl->nvs)
121 return -ENODEV;
122
123 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
124 if (!gen_parms)
125 return -ENOMEM;
126
127 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
128
129 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
130
131 if (gp->tx_bip_fem_auto_detect)
132 answer = true;
133
Shahar Levib03acad2011-04-03 13:54:54 +0300134 /* Override the REF CLK from the NVS with the one from platform data */
135 gen_parms->general_params.ref_clock = wl->ref_clock;
136
Shahar Levi49d750ca2011-03-06 16:32:09 +0200137 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
138 if (ret < 0) {
139 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
140 goto out;
141 }
142
143 gp->tx_bip_fem_manufacturer =
144 gen_parms->general_params.tx_bip_fem_manufacturer;
145
146 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
147 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
148
149out:
150 kfree(gen_parms);
151 return ret;
152}
153
154int wl128x_cmd_general_parms(struct wl1271 *wl)
155{
156 struct wl128x_general_parms_cmd *gen_parms;
157 struct wl128x_ini_general_params *gp =
158 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300159 bool answer = false;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200160 int ret;
161
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200162 if (!wl->nvs)
163 return -ENODEV;
164
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200165 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
166 if (!gen_parms)
167 return -ENOMEM;
168
169 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
170
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300171 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200172
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300173 if (gp->tx_bip_fem_auto_detect)
174 answer = true;
175
Shahar Levib03acad2011-04-03 13:54:54 +0300176 /* Replace REF and TCXO CLKs with the ones from platform data */
177 gen_parms->general_params.ref_clock = wl->ref_clock;
178 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
179
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300180 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
181 if (ret < 0) {
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200182 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300183 goto out;
184 }
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200185
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300186 gp->tx_bip_fem_manufacturer =
187 gen_parms->general_params.tx_bip_fem_manufacturer;
188
189 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
190 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
191
192out:
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200193 kfree(gen_parms);
194 return ret;
195}
196
197int wl1271_cmd_radio_parms(struct wl1271 *wl)
198{
Shahar Levibc765bf2011-03-06 16:32:10 +0200199 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200200 struct wl1271_radio_parms_cmd *radio_parms;
Shahar Levibc765bf2011-03-06 16:32:10 +0200201 struct wl1271_ini_general_params *gp = &nvs->general_params;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200202 int ret;
203
204 if (!wl->nvs)
205 return -ENODEV;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200206
207 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
208 if (!radio_parms)
209 return -ENOMEM;
210
211 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
212
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300213 /* 2.4GHz parameters */
Shahar Levibc765bf2011-03-06 16:32:10 +0200214 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300215 sizeof(struct wl1271_ini_band_params_2));
216 memcpy(&radio_parms->dyn_params_2,
Shahar Levibc765bf2011-03-06 16:32:10 +0200217 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300218 sizeof(struct wl1271_ini_fem_params_2));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200219
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300220 /* 5GHz parameters */
221 memcpy(&radio_parms->static_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200222 &nvs->stat_radio_params_5,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300223 sizeof(struct wl1271_ini_band_params_5));
224 memcpy(&radio_parms->dyn_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200225 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300226 sizeof(struct wl1271_ini_fem_params_5));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200227
228 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
229 radio_parms, sizeof(*radio_parms));
230
231 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
232 if (ret < 0)
233 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
234
235 kfree(radio_parms);
236 return ret;
237}
238
Shahar Levi49d750ca2011-03-06 16:32:09 +0200239int wl128x_cmd_radio_parms(struct wl1271 *wl)
240{
241 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
242 struct wl128x_radio_parms_cmd *radio_parms;
243 struct wl128x_ini_general_params *gp = &nvs->general_params;
244 int ret;
245
246 if (!wl->nvs)
247 return -ENODEV;
248
249 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
250 if (!radio_parms)
251 return -ENOMEM;
252
253 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
254
255 /* 2.4GHz parameters */
256 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
257 sizeof(struct wl128x_ini_band_params_2));
258 memcpy(&radio_parms->dyn_params_2,
259 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
260 sizeof(struct wl128x_ini_fem_params_2));
261
262 /* 5GHz parameters */
263 memcpy(&radio_parms->static_params_5,
264 &nvs->stat_radio_params_5,
265 sizeof(struct wl128x_ini_band_params_5));
266 memcpy(&radio_parms->dyn_params_5,
267 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
268 sizeof(struct wl128x_ini_fem_params_5));
269
270 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
271
272 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
273 radio_parms, sizeof(*radio_parms));
274
275 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
276 if (ret < 0)
277 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
278
279 kfree(radio_parms);
280 return ret;
281}
282
Juuso Oikarinen644a4862010-10-05 13:11:56 +0200283int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
284{
285 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
286 struct conf_rf_settings *rf = &wl->conf.rf;
287 int ret;
288
289 if (!wl->nvs)
290 return -ENODEV;
291
292 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
293 if (!ext_radio_parms)
294 return -ENOMEM;
295
296 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
297
298 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
299 rf->tx_per_channel_power_compensation_2,
300 CONF_TX_PWR_COMPENSATION_LEN_2);
301 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
302 rf->tx_per_channel_power_compensation_5,
303 CONF_TX_PWR_COMPENSATION_LEN_5);
304
305 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
306 ext_radio_parms, sizeof(*ext_radio_parms));
307
308 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
309 if (ret < 0)
310 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
311
312 kfree(ext_radio_parms);
313 return ret;
314}
315
Luciano Coelho99d84c12010-03-26 12:53:20 +0200316/*
317 * Poll the mailbox event field until any of the bits in the mask is set or a
318 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
319 */
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200320static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
Luciano Coelho99d84c12010-03-26 12:53:20 +0200321{
322 u32 events_vector, event;
323 unsigned long timeout;
324
325 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
326
327 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200328 if (time_after(jiffies, timeout)) {
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200329 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
330 (int)mask);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200331 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200332 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200333
334 msleep(1);
335
336 /* read from both event fields */
337 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
338 sizeof(events_vector), false);
339 event = events_vector & mask;
340 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
341 sizeof(events_vector), false);
342 event |= events_vector & mask;
343 } while (!event);
344
345 return 0;
346}
347
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200348static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
349{
350 int ret;
351
352 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
353 if (ret != 0) {
354 ieee80211_queue_work(wl->hw, &wl->recovery_work);
355 return ret;
356 }
357
358 return 0;
359}
360
Juuso Oikarinen15305492010-02-22 08:38:32 +0200361int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300362{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300363 struct wl1271_cmd_join *join;
364 int ret, i;
365 u8 *bssid;
366
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300367 join = kzalloc(sizeof(*join), GFP_KERNEL);
368 if (!join) {
369 ret = -ENOMEM;
370 goto out;
371 }
372
373 wl1271_debug(DEBUG_CMD, "cmd join");
374
375 /* Reverse order BSSID */
376 bssid = (u8 *) &join->bssid_lsb;
377 for (i = 0; i < ETH_ALEN; i++)
378 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
379
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300380 join->rx_config_options = cpu_to_le32(wl->rx_config);
381 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
Juuso Oikarinen15305492010-02-22 08:38:32 +0200382 join->bss_type = bss_type;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300383 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200384 join->supported_rate_set = cpu_to_le32(wl->rate_set);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300385
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300386 if (wl->band == IEEE80211_BAND_5GHZ)
Teemu Paasikivia4102642009-10-13 12:47:51 +0300387 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300388
Juuso Oikarinen60e84c22010-03-26 12:53:25 +0200389 join->beacon_interval = cpu_to_le16(wl->beacon_int);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300390 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300391
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300392 join->channel = wl->channel;
393 join->ssid_len = wl->ssid_len;
394 memcpy(join->ssid, wl->ssid, wl->ssid_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300395
396 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
397
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300398 /* reset TX security counters */
399 wl->tx_security_last_seq = 0;
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200400 wl->tx_security_seq = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300401
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200402 wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
403 join->basic_rate_set, join->supported_rate_set);
404
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 Coelho99d84c12010-03-26 12:53:20 +0200411 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
412 if (ret < 0)
413 wl1271_error("cmd join event completion error");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300414
415out_free:
416 kfree(join);
417
418out:
419 return ret;
420}
421
422/**
423 * send test command to firmware
424 *
425 * @wl: wl struct
426 * @buf: buffer containing the command, with all headers, must work with dma
427 * @len: length of the buffer
428 * @answer: is answer needed
429 */
430int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
431{
432 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200433 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300434
435 wl1271_debug(DEBUG_CMD, "cmd test");
436
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200437 if (answer)
438 res_len = buf_len;
439
440 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300441
442 if (ret < 0) {
443 wl1271_warning("TEST command failed");
444 return ret;
445 }
446
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200447 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300448}
449
450/**
451 * read acx from firmware
452 *
453 * @wl: wl struct
454 * @id: acx id
455 * @buf: buffer for the response, including all headers, must work with dma
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300456 * @len: length of buf
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300457 */
458int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
459{
460 struct acx_header *acx = buf;
461 int ret;
462
463 wl1271_debug(DEBUG_CMD, "cmd interrogate");
464
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300465 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300466
467 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300468 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300469
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200470 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
471 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300472 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300473
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300474 return ret;
475}
476
477/**
478 * write acx value to firmware
479 *
480 * @wl: wl struct
481 * @id: acx id
482 * @buf: buffer containing acx, including all headers, must work with dma
483 * @len: length of buf
484 */
485int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
486{
487 struct acx_header *acx = buf;
488 int ret;
489
490 wl1271_debug(DEBUG_CMD, "cmd configure");
491
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300492 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300493
494 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300495 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300496
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200497 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300498 if (ret < 0) {
499 wl1271_warning("CONFIGURE command NOK");
500 return ret;
501 }
502
503 return 0;
504}
505
Luciano Coelho94210892009-12-11 15:40:55 +0200506int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300507{
508 struct cmd_enabledisable_path *cmd;
509 int ret;
510 u16 cmd_rx, cmd_tx;
511
512 wl1271_debug(DEBUG_CMD, "cmd data path");
513
514 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
515 if (!cmd) {
516 ret = -ENOMEM;
517 goto out;
518 }
519
Luciano Coelho94210892009-12-11 15:40:55 +0200520 /* the channel here is only used for calibration, so hardcoded to 1 */
521 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300522
523 if (enable) {
524 cmd_rx = CMD_ENABLE_RX;
525 cmd_tx = CMD_ENABLE_TX;
526 } else {
527 cmd_rx = CMD_DISABLE_RX;
528 cmd_tx = CMD_DISABLE_TX;
529 }
530
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200531 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300532 if (ret < 0) {
533 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200534 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300535 goto out;
536 }
537
538 wl1271_debug(DEBUG_BOOT, "rx %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
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200541 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300542 if (ret < 0) {
543 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200544 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200545 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300546 }
547
548 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200549 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300550
551out:
552 kfree(cmd);
553 return ret;
554}
555
Eliad Pellerc8bde242011-02-02 09:59:35 +0200556int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300557{
558 struct wl1271_cmd_ps_params *ps_params = NULL;
559 int ret = 0;
560
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300561 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
562
563 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
564 if (!ps_params) {
565 ret = -ENOMEM;
566 goto out;
567 }
568
569 ps_params->ps_mode = ps_mode;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300570
571 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200572 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300573 if (ret < 0) {
574 wl1271_error("cmd set_ps_mode failed");
575 goto out;
576 }
577
578out:
579 kfree(ps_params);
580 return ret;
581}
582
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300583int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300584 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300585{
586 struct wl1271_cmd_template_set *cmd;
587 int ret = 0;
588
589 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
590
591 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
592 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
593
594 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
595 if (!cmd) {
596 ret = -ENOMEM;
597 goto out;
598 }
599
600 cmd->len = cpu_to_le16(buf_len);
601 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300602 cmd->enabled_rates = cpu_to_le32(rates);
Arik Nemtsov1e05a812010-10-16 17:44:51 +0200603 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
604 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200605 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300606
607 if (buf)
608 memcpy(cmd->template_data, buf, buf_len);
609
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200610 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300611 if (ret < 0) {
612 wl1271_warning("cmd set_template failed: %d", ret);
613 goto out_free;
614 }
615
616out_free:
617 kfree(cmd);
618
619out:
620 return ret;
621}
622
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300623int wl1271_cmd_build_null_data(struct wl1271 *wl)
624{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200625 struct sk_buff *skb = NULL;
626 int size;
627 void *ptr;
628 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300629
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300630
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200631 if (wl->bss_type == BSS_TYPE_IBSS) {
632 size = sizeof(struct wl12xx_null_data_template);
633 ptr = NULL;
634 } else {
635 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
636 if (!skb)
637 goto out;
638 size = skb->len;
639 ptr = skb->data;
640 }
641
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300642 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200643 wl->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300644
Kalle Valo899e6e62010-03-18 12:26:34 +0200645out:
646 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200647 if (ret)
648 wl1271_warning("cmd buld null data failed %d", ret);
649
Kalle Valo899e6e62010-03-18 12:26:34 +0200650 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300651
652}
653
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200654int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
655{
656 struct sk_buff *skb = NULL;
657 int ret = -ENOMEM;
658
659 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
660 if (!skb)
661 goto out;
662
663 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
664 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300665 CMD_TEMPL_KLV_IDX_NULL_DATA,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200666 wl->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200667
668out:
669 dev_kfree_skb(skb);
670 if (ret)
671 wl1271_warning("cmd build klv null data failed %d", ret);
672
673 return ret;
674
675}
676
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300677int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
678{
Kalle Valo899e6e62010-03-18 12:26:34 +0200679 struct sk_buff *skb;
680 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300681
Kalle Valo899e6e62010-03-18 12:26:34 +0200682 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
683 if (!skb)
684 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300685
Kalle Valo899e6e62010-03-18 12:26:34 +0200686 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Juuso Oikarinen849923f2010-07-08 17:49:59 +0300687 skb->len, 0, wl->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300688
Kalle Valo899e6e62010-03-18 12:26:34 +0200689out:
690 dev_kfree_skb(skb);
691 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300692}
693
Kalle Valo818e3062010-03-18 12:26:35 +0200694int wl1271_cmd_build_probe_req(struct wl1271 *wl,
695 const u8 *ssid, size_t ssid_len,
696 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300697{
Kalle Valo818e3062010-03-18 12:26:35 +0200698 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300699 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300700
Kalle Valo818e3062010-03-18 12:26:35 +0200701 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
702 ie, ie_len);
703 if (!skb) {
704 ret = -ENOMEM;
705 goto out;
706 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300707
Kalle Valo818e3062010-03-18 12:26:35 +0200708 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300709
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300710 if (band == IEEE80211_BAND_2GHZ)
711 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300712 skb->data, skb->len, 0,
713 wl->conf.tx.basic_rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300714 else
715 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300716 skb->data, skb->len, 0,
717 wl->conf.tx.basic_rate_5);
Kalle Valo818e3062010-03-18 12:26:35 +0200718
719out:
720 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300721 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300722}
723
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +0200724struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
725 struct sk_buff *skb)
726{
727 int ret;
728
729 if (!skb)
730 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
731 if (!skb)
732 goto out;
733
734 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
735
736 if (wl->band == IEEE80211_BAND_2GHZ)
737 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
738 skb->data, skb->len, 0,
739 wl->conf.tx.basic_rate);
740 else
741 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
742 skb->data, skb->len, 0,
743 wl->conf.tx.basic_rate_5);
744
745 if (ret < 0)
746 wl1271_error("Unable to set ap probe request template.");
747
748out:
749 return skb;
750}
751
Eliad Pellerc5312772010-12-09 11:31:27 +0200752int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
753{
754 int ret;
755 struct wl12xx_arp_rsp_template tmpl;
756 struct ieee80211_hdr_3addr *hdr;
757 struct arphdr *arp_hdr;
758
759 memset(&tmpl, 0, sizeof(tmpl));
760
761 /* mac80211 header */
762 hdr = &tmpl.hdr;
763 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
764 IEEE80211_STYPE_DATA |
765 IEEE80211_FCTL_TODS);
766 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
767 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
768 memset(hdr->addr3, 0xff, ETH_ALEN);
769
770 /* llc layer */
771 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +0100772 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200773
774 /* arp header */
775 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +0100776 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
777 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200778 arp_hdr->ar_hln = ETH_ALEN;
779 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +0100780 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +0200781
782 /* arp payload */
783 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
784 tmpl.sender_ip = ip_addr;
785
786 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
787 &tmpl, sizeof(tmpl), 0,
788 wl->basic_rate);
789
790 return ret;
791}
792
Kalle Valo023e0822010-03-18 12:26:36 +0200793int wl1271_build_qos_null_data(struct wl1271 *wl)
794{
795 struct ieee80211_qos_hdr template;
796
797 memset(&template, 0, sizeof(template));
798
799 memcpy(template.addr1, wl->bssid, ETH_ALEN);
800 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
801 memcpy(template.addr3, wl->bssid, ETH_ALEN);
802
803 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
804 IEEE80211_STYPE_QOS_NULLFUNC |
805 IEEE80211_FCTL_TODS);
806
807 /* FIXME: not sure what priority to use here */
808 template.qos_ctrl = cpu_to_le16(0);
809
810 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300811 sizeof(template), 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200812 wl->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +0200813}
814
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200815int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300816{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200817 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300818 int ret = 0;
819
820 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
821
822 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
823 if (!cmd) {
824 ret = -ENOMEM;
825 goto out;
826 }
827
828 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300829 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300830 cmd->key_type = KEY_WEP;
831
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200832 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300833 if (ret < 0) {
834 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
835 goto out;
836 }
837
838out:
839 kfree(cmd);
840
841 return ret;
842}
843
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200844int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
845{
846 struct wl1271_cmd_set_ap_keys *cmd;
847 int ret = 0;
848
849 wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
850
851 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
852 if (!cmd) {
853 ret = -ENOMEM;
854 goto out;
855 }
856
857 cmd->hlid = WL1271_AP_BROADCAST_HLID;
858 cmd->key_id = id;
859 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
860 cmd->key_action = cpu_to_le16(KEY_SET_ID);
861 cmd->key_type = KEY_WEP;
862
863 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
864 if (ret < 0) {
865 wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
866 goto out;
867 }
868
869out:
870 kfree(cmd);
871
872 return ret;
873}
874
875int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300876 u8 key_size, const u8 *key, const u8 *addr,
877 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300878{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200879 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300880 int ret = 0;
881
882 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
883 if (!cmd) {
884 ret = -ENOMEM;
885 goto out;
886 }
887
888 if (key_type != KEY_WEP)
889 memcpy(cmd->addr, addr, ETH_ALEN);
890
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300891 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300892 cmd->key_size = key_size;
893 cmd->key_type = key_type;
894
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300895 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
896 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300897
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300898 /* we have only one SSID profile */
899 cmd->ssid_profile = 0;
900
901 cmd->id = id;
902
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300903 if (key_type == KEY_TKIP) {
904 /*
905 * We get the key in the following form:
906 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
907 * but the target is expecting:
908 * TKIP - RX MIC - TX MIC
909 */
910 memcpy(cmd->key, key, 16);
911 memcpy(cmd->key + 16, key + 24, 8);
912 memcpy(cmd->key + 24, key + 16, 8);
913
914 } else {
915 memcpy(cmd->key, key, key_size);
916 }
917
918 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
919
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200920 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300921 if (ret < 0) {
922 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200923 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300924 }
925
926out:
927 kfree(cmd);
928
929 return ret;
930}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300931
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200932int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
933 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
934 u16 tx_seq_16)
935{
936 struct wl1271_cmd_set_ap_keys *cmd;
937 int ret = 0;
938 u8 lid_type;
939
940 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
941 if (!cmd)
942 return -ENOMEM;
943
944 if (hlid == WL1271_AP_BROADCAST_HLID) {
945 if (key_type == KEY_WEP)
946 lid_type = WEP_DEFAULT_LID_TYPE;
947 else
948 lid_type = BROADCAST_LID_TYPE;
949 } else {
950 lid_type = UNICAST_LID_TYPE;
951 }
952
953 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
954 " hlid: %d", (int)action, (int)id, (int)lid_type,
955 (int)key_type, (int)hlid);
956
957 cmd->lid_key_type = lid_type;
958 cmd->hlid = hlid;
959 cmd->key_action = cpu_to_le16(action);
960 cmd->key_size = key_size;
961 cmd->key_type = key_type;
962 cmd->key_id = id;
963 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
964 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
965
966 if (key_type == KEY_TKIP) {
967 /*
968 * We get the key in the following form:
969 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
970 * but the target is expecting:
971 * TKIP - RX MIC - TX MIC
972 */
973 memcpy(cmd->key, key, 16);
974 memcpy(cmd->key + 16, key + 24, 8);
975 memcpy(cmd->key + 24, key + 16, 8);
976 } else {
977 memcpy(cmd->key, key, key_size);
978 }
979
980 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
981
982 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
983 if (ret < 0) {
984 wl1271_warning("could not set ap keys");
985 goto out;
986 }
987
988out:
989 kfree(cmd);
990 return ret;
991}
992
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300993int wl1271_cmd_disconnect(struct wl1271 *wl)
994{
995 struct wl1271_cmd_disconnect *cmd;
996 int ret = 0;
997
998 wl1271_debug(DEBUG_CMD, "cmd disconnect");
999
1000 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1001 if (!cmd) {
1002 ret = -ENOMEM;
1003 goto out;
1004 }
1005
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001006 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
1007 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001008 /* disconnect reason is not used in immediate disconnections */
1009 cmd->type = DISCONNECT_IMMEDIATE;
1010
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001011 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001012 if (ret < 0) {
1013 wl1271_error("failed to send disconnect command");
1014 goto out_free;
1015 }
1016
Luciano Coelho2f826f52010-03-26 12:53:21 +02001017 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
1018 if (ret < 0)
1019 wl1271_error("cmd disconnect event completion error");
1020
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001021out_free:
1022 kfree(cmd);
1023
1024out:
1025 return ret;
1026}
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001027
1028int wl1271_cmd_set_sta_state(struct wl1271 *wl)
1029{
1030 struct wl1271_cmd_set_sta_state *cmd;
1031 int ret = 0;
1032
1033 wl1271_debug(DEBUG_CMD, "cmd set sta state");
1034
1035 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1036 if (!cmd) {
1037 ret = -ENOMEM;
1038 goto out;
1039 }
1040
1041 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1042
1043 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
1044 if (ret < 0) {
1045 wl1271_error("failed to send set STA state command");
1046 goto out_free;
1047 }
1048
1049out_free:
1050 kfree(cmd);
1051
1052out:
1053 return ret;
1054}
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001055
1056int wl1271_cmd_start_bss(struct wl1271 *wl)
1057{
1058 struct wl1271_cmd_bss_start *cmd;
1059 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
1060 int ret;
1061
1062 wl1271_debug(DEBUG_CMD, "cmd start bss");
1063
1064 /*
1065 * FIXME: We currently do not support hidden SSID. The real SSID
1066 * should be fetched from mac80211 first.
1067 */
1068 if (wl->ssid_len == 0) {
1069 wl1271_warning("Hidden SSID currently not supported for AP");
1070 ret = -EINVAL;
1071 goto out;
1072 }
1073
1074 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1075 if (!cmd) {
1076 ret = -ENOMEM;
1077 goto out;
1078 }
1079
1080 memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
1081
Luciano Coelho25eaea302011-05-02 12:37:33 +03001082 cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001083 cmd->bss_index = WL1271_AP_BSS_INDEX;
1084 cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
1085 cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
1086 cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
1087 cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
1088 cmd->dtim_interval = bss_conf->dtim_period;
1089 cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
1090 cmd->channel = wl->channel;
1091 cmd->ssid_len = wl->ssid_len;
1092 cmd->ssid_type = SSID_TYPE_PUBLIC;
1093 memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1094
1095 switch (wl->band) {
1096 case IEEE80211_BAND_2GHZ:
1097 cmd->band = RADIO_BAND_2_4GHZ;
1098 break;
1099 case IEEE80211_BAND_5GHZ:
1100 cmd->band = RADIO_BAND_5GHZ;
1101 break;
1102 default:
1103 wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1104 cmd->band = RADIO_BAND_2_4GHZ;
1105 break;
1106 }
1107
1108 ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1109 if (ret < 0) {
1110 wl1271_error("failed to initiate cmd start bss");
1111 goto out_free;
1112 }
1113
1114out_free:
1115 kfree(cmd);
1116
1117out:
1118 return ret;
1119}
1120
1121int wl1271_cmd_stop_bss(struct wl1271 *wl)
1122{
1123 struct wl1271_cmd_bss_start *cmd;
1124 int ret;
1125
1126 wl1271_debug(DEBUG_CMD, "cmd stop bss");
1127
1128 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1129 if (!cmd) {
1130 ret = -ENOMEM;
1131 goto out;
1132 }
1133
1134 cmd->bss_index = WL1271_AP_BSS_INDEX;
1135
1136 ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1137 if (ret < 0) {
1138 wl1271_error("failed to initiate cmd stop bss");
1139 goto out_free;
1140 }
1141
1142out_free:
1143 kfree(cmd);
1144
1145out:
1146 return ret;
1147}
1148
1149int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1150{
1151 struct wl1271_cmd_add_sta *cmd;
1152 int ret;
1153
1154 wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1155
1156 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1157 if (!cmd) {
1158 ret = -ENOMEM;
1159 goto out;
1160 }
1161
1162 /* currently we don't support UAPSD */
1163 cmd->sp_len = 0;
1164
1165 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1166 cmd->bss_index = WL1271_AP_BSS_INDEX;
1167 cmd->aid = sta->aid;
1168 cmd->hlid = hlid;
1169
1170 /*
1171 * FIXME: Does STA support QOS? We need to propagate this info from
1172 * hostapd. Currently not that important since this is only used for
1173 * sending the correct flavor of null-data packet in response to a
1174 * trigger.
1175 */
1176 cmd->wmm = 0;
1177
1178 cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
1179 sta->supp_rates[wl->band]));
1180
1181 wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
1182
1183 ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
1184 if (ret < 0) {
1185 wl1271_error("failed to initiate cmd add sta");
1186 goto out_free;
1187 }
1188
1189out_free:
1190 kfree(cmd);
1191
1192out:
1193 return ret;
1194}
1195
1196int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
1197{
1198 struct wl1271_cmd_remove_sta *cmd;
1199 int ret;
1200
1201 wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
1202
1203 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1204 if (!cmd) {
1205 ret = -ENOMEM;
1206 goto out;
1207 }
1208
1209 cmd->hlid = hlid;
1210 /* We never send a deauth, mac80211 is in charge of this */
1211 cmd->reason_opcode = 0;
1212 cmd->send_deauth_flag = 0;
1213
1214 ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
1215 if (ret < 0) {
1216 wl1271_error("failed to initiate cmd remove sta");
1217 goto out_free;
1218 }
1219
Arik Nemtsov05285cf2010-11-12 17:15:03 +02001220 /*
1221 * We are ok with a timeout here. The event is sometimes not sent
1222 * due to a firmware bug.
1223 */
1224 wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001225
1226out_free:
1227 kfree(cmd);
1228
1229out:
1230 return ret;
1231}