blob: b6ef65a57b717eb18b9673008f12b9c3c4c5d628 [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);
Ido Yarivbaacb9a2011-06-06 14:57:05 +0300108 wl12xx_queue_recovery_work(wl);
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 Levi0c005042011-06-12 10:34:43 +0300137 /* LPD mode enable (bits 6-7) in WL1271 AP mode only */
138 if (wl->quirks & WL12XX_QUIRK_LPD_MODE)
139 gen_parms->general_params.general_settings |=
140 GENERAL_SETTINGS_DRPW_LPD;
141
Shahar Levi49d750ca2011-03-06 16:32:09 +0200142 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
143 if (ret < 0) {
144 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
145 goto out;
146 }
147
148 gp->tx_bip_fem_manufacturer =
149 gen_parms->general_params.tx_bip_fem_manufacturer;
150
151 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
152 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
153
154out:
155 kfree(gen_parms);
156 return ret;
157}
158
159int wl128x_cmd_general_parms(struct wl1271 *wl)
160{
161 struct wl128x_general_parms_cmd *gen_parms;
162 struct wl128x_ini_general_params *gp =
163 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300164 bool answer = false;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200165 int ret;
166
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200167 if (!wl->nvs)
168 return -ENODEV;
169
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200170 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
171 if (!gen_parms)
172 return -ENOMEM;
173
174 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
175
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300176 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200177
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300178 if (gp->tx_bip_fem_auto_detect)
179 answer = true;
180
Shahar Levib03acad2011-04-03 13:54:54 +0300181 /* Replace REF and TCXO CLKs with the ones from platform data */
182 gen_parms->general_params.ref_clock = wl->ref_clock;
183 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
184
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300185 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
186 if (ret < 0) {
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200187 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300188 goto out;
189 }
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200190
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300191 gp->tx_bip_fem_manufacturer =
192 gen_parms->general_params.tx_bip_fem_manufacturer;
193
194 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
195 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
196
197out:
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200198 kfree(gen_parms);
199 return ret;
200}
201
202int wl1271_cmd_radio_parms(struct wl1271 *wl)
203{
Shahar Levibc765bf2011-03-06 16:32:10 +0200204 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200205 struct wl1271_radio_parms_cmd *radio_parms;
Shahar Levibc765bf2011-03-06 16:32:10 +0200206 struct wl1271_ini_general_params *gp = &nvs->general_params;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200207 int ret;
208
209 if (!wl->nvs)
210 return -ENODEV;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200211
212 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
213 if (!radio_parms)
214 return -ENOMEM;
215
216 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
217
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300218 /* 2.4GHz parameters */
Shahar Levibc765bf2011-03-06 16:32:10 +0200219 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300220 sizeof(struct wl1271_ini_band_params_2));
221 memcpy(&radio_parms->dyn_params_2,
Shahar Levibc765bf2011-03-06 16:32:10 +0200222 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300223 sizeof(struct wl1271_ini_fem_params_2));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200224
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300225 /* 5GHz parameters */
226 memcpy(&radio_parms->static_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200227 &nvs->stat_radio_params_5,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300228 sizeof(struct wl1271_ini_band_params_5));
229 memcpy(&radio_parms->dyn_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200230 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300231 sizeof(struct wl1271_ini_fem_params_5));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200232
233 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
234 radio_parms, sizeof(*radio_parms));
235
236 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
237 if (ret < 0)
238 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
239
240 kfree(radio_parms);
241 return ret;
242}
243
Shahar Levi49d750ca2011-03-06 16:32:09 +0200244int wl128x_cmd_radio_parms(struct wl1271 *wl)
245{
246 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
247 struct wl128x_radio_parms_cmd *radio_parms;
248 struct wl128x_ini_general_params *gp = &nvs->general_params;
249 int ret;
250
251 if (!wl->nvs)
252 return -ENODEV;
253
254 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
255 if (!radio_parms)
256 return -ENOMEM;
257
258 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
259
260 /* 2.4GHz parameters */
261 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
262 sizeof(struct wl128x_ini_band_params_2));
263 memcpy(&radio_parms->dyn_params_2,
264 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
265 sizeof(struct wl128x_ini_fem_params_2));
266
267 /* 5GHz parameters */
268 memcpy(&radio_parms->static_params_5,
269 &nvs->stat_radio_params_5,
270 sizeof(struct wl128x_ini_band_params_5));
271 memcpy(&radio_parms->dyn_params_5,
272 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
273 sizeof(struct wl128x_ini_fem_params_5));
274
275 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
276
277 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
278 radio_parms, sizeof(*radio_parms));
279
280 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
281 if (ret < 0)
282 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
283
284 kfree(radio_parms);
285 return ret;
286}
287
Juuso Oikarinen644a4862010-10-05 13:11:56 +0200288int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
289{
290 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
291 struct conf_rf_settings *rf = &wl->conf.rf;
292 int ret;
293
294 if (!wl->nvs)
295 return -ENODEV;
296
297 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
298 if (!ext_radio_parms)
299 return -ENOMEM;
300
301 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
302
303 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
304 rf->tx_per_channel_power_compensation_2,
305 CONF_TX_PWR_COMPENSATION_LEN_2);
306 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
307 rf->tx_per_channel_power_compensation_5,
308 CONF_TX_PWR_COMPENSATION_LEN_5);
309
310 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
311 ext_radio_parms, sizeof(*ext_radio_parms));
312
313 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
314 if (ret < 0)
315 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
316
317 kfree(ext_radio_parms);
318 return ret;
319}
320
Luciano Coelho99d84c12010-03-26 12:53:20 +0200321/*
322 * Poll the mailbox event field until any of the bits in the mask is set or a
323 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
324 */
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200325static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
Luciano Coelho99d84c12010-03-26 12:53:20 +0200326{
327 u32 events_vector, event;
328 unsigned long timeout;
329
330 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
331
332 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200333 if (time_after(jiffies, timeout)) {
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200334 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
335 (int)mask);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200336 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200337 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200338
339 msleep(1);
340
341 /* read from both event fields */
342 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
343 sizeof(events_vector), false);
344 event = events_vector & mask;
345 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
346 sizeof(events_vector), false);
347 event |= events_vector & mask;
348 } while (!event);
349
350 return 0;
351}
352
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200353static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
354{
355 int ret;
356
357 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
358 if (ret != 0) {
Ido Yarivbaacb9a2011-06-06 14:57:05 +0300359 wl12xx_queue_recovery_work(wl);
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200360 return ret;
361 }
362
363 return 0;
364}
365
Juuso Oikarinen15305492010-02-22 08:38:32 +0200366int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300367{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300368 struct wl1271_cmd_join *join;
369 int ret, i;
370 u8 *bssid;
371
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300372 join = kzalloc(sizeof(*join), GFP_KERNEL);
373 if (!join) {
374 ret = -ENOMEM;
375 goto out;
376 }
377
378 wl1271_debug(DEBUG_CMD, "cmd join");
379
380 /* Reverse order BSSID */
381 bssid = (u8 *) &join->bssid_lsb;
382 for (i = 0; i < ETH_ALEN; i++)
383 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
384
Juuso Oikarinen15305492010-02-22 08:38:32 +0200385 join->bss_type = bss_type;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300386 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200387 join->supported_rate_set = cpu_to_le32(wl->rate_set);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300388
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300389 if (wl->band == IEEE80211_BAND_5GHZ)
Teemu Paasikivia4102642009-10-13 12:47:51 +0300390 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300391
Juuso Oikarinen60e84c22010-03-26 12:53:25 +0200392 join->beacon_interval = cpu_to_le16(wl->beacon_int);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300393 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300394
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300395 join->channel = wl->channel;
396 join->ssid_len = wl->ssid_len;
397 memcpy(join->ssid, wl->ssid, wl->ssid_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300398
399 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
400
Eliad Peller72c2d9e2011-02-02 09:59:37 +0200401 wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
402 join->basic_rate_set, join->supported_rate_set);
403
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200404 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300405 if (ret < 0) {
406 wl1271_error("failed to initiate cmd join");
407 goto out_free;
408 }
409
Luciano Coelho99d84c12010-03-26 12:53:20 +0200410 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
411 if (ret < 0)
412 wl1271_error("cmd join event completion error");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300413
414out_free:
415 kfree(join);
416
417out:
418 return ret;
419}
420
421/**
422 * send test command to firmware
423 *
424 * @wl: wl struct
425 * @buf: buffer containing the command, with all headers, must work with dma
426 * @len: length of the buffer
427 * @answer: is answer needed
428 */
429int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
430{
431 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200432 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300433
434 wl1271_debug(DEBUG_CMD, "cmd test");
435
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200436 if (answer)
437 res_len = buf_len;
438
439 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300440
441 if (ret < 0) {
442 wl1271_warning("TEST command failed");
443 return ret;
444 }
445
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200446 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300447}
448
449/**
450 * read acx from firmware
451 *
452 * @wl: wl struct
453 * @id: acx id
454 * @buf: buffer for the response, including all headers, must work with dma
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300455 * @len: length of buf
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300456 */
457int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
458{
459 struct acx_header *acx = buf;
460 int ret;
461
462 wl1271_debug(DEBUG_CMD, "cmd interrogate");
463
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300464 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300465
466 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300467 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300468
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200469 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
470 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300471 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300472
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300473 return ret;
474}
475
476/**
477 * write acx value to firmware
478 *
479 * @wl: wl struct
480 * @id: acx id
481 * @buf: buffer containing acx, including all headers, must work with dma
482 * @len: length of buf
483 */
484int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
485{
486 struct acx_header *acx = buf;
487 int ret;
488
489 wl1271_debug(DEBUG_CMD, "cmd configure");
490
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300491 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300492
493 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300494 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300495
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200496 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300497 if (ret < 0) {
498 wl1271_warning("CONFIGURE command NOK");
499 return ret;
500 }
501
502 return 0;
503}
504
Luciano Coelho94210892009-12-11 15:40:55 +0200505int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300506{
507 struct cmd_enabledisable_path *cmd;
508 int ret;
509 u16 cmd_rx, cmd_tx;
510
511 wl1271_debug(DEBUG_CMD, "cmd data path");
512
513 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
514 if (!cmd) {
515 ret = -ENOMEM;
516 goto out;
517 }
518
Luciano Coelho94210892009-12-11 15:40:55 +0200519 /* the channel here is only used for calibration, so hardcoded to 1 */
520 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300521
522 if (enable) {
523 cmd_rx = CMD_ENABLE_RX;
524 cmd_tx = CMD_ENABLE_TX;
525 } else {
526 cmd_rx = CMD_DISABLE_RX;
527 cmd_tx = CMD_DISABLE_TX;
528 }
529
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200530 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300531 if (ret < 0) {
532 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200533 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300534 goto out;
535 }
536
537 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200538 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300539
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200540 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300541 if (ret < 0) {
542 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200543 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200544 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300545 }
546
547 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200548 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300549
550out:
551 kfree(cmd);
552 return ret;
553}
554
Eliad Pellerc8bde242011-02-02 09:59:35 +0200555int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300556{
557 struct wl1271_cmd_ps_params *ps_params = NULL;
558 int ret = 0;
559
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300560 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
561
562 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
563 if (!ps_params) {
564 ret = -ENOMEM;
565 goto out;
566 }
567
568 ps_params->ps_mode = ps_mode;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300569
570 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200571 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300572 if (ret < 0) {
573 wl1271_error("cmd set_ps_mode failed");
574 goto out;
575 }
576
577out:
578 kfree(ps_params);
579 return ret;
580}
581
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300582int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300583 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300584{
585 struct wl1271_cmd_template_set *cmd;
586 int ret = 0;
587
588 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
589
590 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
591 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
592
593 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
594 if (!cmd) {
595 ret = -ENOMEM;
596 goto out;
597 }
598
599 cmd->len = cpu_to_le16(buf_len);
600 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300601 cmd->enabled_rates = cpu_to_le32(rates);
Arik Nemtsov1e05a812010-10-16 17:44:51 +0200602 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
603 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200604 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300605
606 if (buf)
607 memcpy(cmd->template_data, buf, buf_len);
608
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200609 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300610 if (ret < 0) {
611 wl1271_warning("cmd set_template failed: %d", ret);
612 goto out_free;
613 }
614
615out_free:
616 kfree(cmd);
617
618out:
619 return ret;
620}
621
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300622int wl1271_cmd_build_null_data(struct wl1271 *wl)
623{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200624 struct sk_buff *skb = NULL;
625 int size;
626 void *ptr;
627 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300628
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300629
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200630 if (wl->bss_type == BSS_TYPE_IBSS) {
631 size = sizeof(struct wl12xx_null_data_template);
632 ptr = NULL;
633 } else {
634 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
635 if (!skb)
636 goto out;
637 size = skb->len;
638 ptr = skb->data;
639 }
640
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300641 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200642 wl->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300643
Kalle Valo899e6e62010-03-18 12:26:34 +0200644out:
645 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200646 if (ret)
647 wl1271_warning("cmd buld null data failed %d", ret);
648
Kalle Valo899e6e62010-03-18 12:26:34 +0200649 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300650
651}
652
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200653int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
654{
655 struct sk_buff *skb = NULL;
656 int ret = -ENOMEM;
657
658 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
659 if (!skb)
660 goto out;
661
662 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
663 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300664 CMD_TEMPL_KLV_IDX_NULL_DATA,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200665 wl->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200666
667out:
668 dev_kfree_skb(skb);
669 if (ret)
670 wl1271_warning("cmd build klv null data failed %d", ret);
671
672 return ret;
673
674}
675
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300676int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
677{
Kalle Valo899e6e62010-03-18 12:26:34 +0200678 struct sk_buff *skb;
679 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300680
Kalle Valo899e6e62010-03-18 12:26:34 +0200681 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
682 if (!skb)
683 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300684
Kalle Valo899e6e62010-03-18 12:26:34 +0200685 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Juuso Oikarinen849923f2010-07-08 17:49:59 +0300686 skb->len, 0, wl->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300687
Kalle Valo899e6e62010-03-18 12:26:34 +0200688out:
689 dev_kfree_skb(skb);
690 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300691}
692
Kalle Valo818e3062010-03-18 12:26:35 +0200693int wl1271_cmd_build_probe_req(struct wl1271 *wl,
694 const u8 *ssid, size_t ssid_len,
695 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300696{
Kalle Valo818e3062010-03-18 12:26:35 +0200697 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300698 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300699
Kalle Valo818e3062010-03-18 12:26:35 +0200700 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
701 ie, ie_len);
702 if (!skb) {
703 ret = -ENOMEM;
704 goto out;
705 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300706
Kalle Valo818e3062010-03-18 12:26:35 +0200707 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300708
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300709 if (band == IEEE80211_BAND_2GHZ)
710 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300711 skb->data, skb->len, 0,
712 wl->conf.tx.basic_rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300713 else
714 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300715 skb->data, skb->len, 0,
716 wl->conf.tx.basic_rate_5);
Kalle Valo818e3062010-03-18 12:26:35 +0200717
718out:
719 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300720 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300721}
722
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +0200723struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
724 struct sk_buff *skb)
725{
726 int ret;
727
728 if (!skb)
729 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
730 if (!skb)
731 goto out;
732
733 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
734
735 if (wl->band == IEEE80211_BAND_2GHZ)
736 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
737 skb->data, skb->len, 0,
738 wl->conf.tx.basic_rate);
739 else
740 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
741 skb->data, skb->len, 0,
742 wl->conf.tx.basic_rate_5);
743
744 if (ret < 0)
745 wl1271_error("Unable to set ap probe request template.");
746
747out:
748 return skb;
749}
750
Eliad Pellerc5312772010-12-09 11:31:27 +0200751int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
752{
753 int ret;
754 struct wl12xx_arp_rsp_template tmpl;
755 struct ieee80211_hdr_3addr *hdr;
756 struct arphdr *arp_hdr;
757
758 memset(&tmpl, 0, sizeof(tmpl));
759
760 /* mac80211 header */
761 hdr = &tmpl.hdr;
762 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
763 IEEE80211_STYPE_DATA |
764 IEEE80211_FCTL_TODS);
765 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
766 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
767 memset(hdr->addr3, 0xff, ETH_ALEN);
768
769 /* llc layer */
770 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +0100771 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200772
773 /* arp header */
774 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +0100775 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
776 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +0200777 arp_hdr->ar_hln = ETH_ALEN;
778 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +0100779 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +0200780
781 /* arp payload */
782 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
783 tmpl.sender_ip = ip_addr;
784
785 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
786 &tmpl, sizeof(tmpl), 0,
787 wl->basic_rate);
788
789 return ret;
790}
791
Kalle Valo023e0822010-03-18 12:26:36 +0200792int wl1271_build_qos_null_data(struct wl1271 *wl)
793{
794 struct ieee80211_qos_hdr template;
795
796 memset(&template, 0, sizeof(template));
797
798 memcpy(template.addr1, wl->bssid, ETH_ALEN);
799 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
800 memcpy(template.addr3, wl->bssid, ETH_ALEN);
801
802 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
803 IEEE80211_STYPE_QOS_NULLFUNC |
804 IEEE80211_FCTL_TODS);
805
806 /* FIXME: not sure what priority to use here */
807 template.qos_ctrl = cpu_to_le16(0);
808
809 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300810 sizeof(template), 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200811 wl->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +0200812}
813
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200814int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300815{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200816 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300817 int ret = 0;
818
819 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
820
821 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
822 if (!cmd) {
823 ret = -ENOMEM;
824 goto out;
825 }
826
827 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300828 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300829 cmd->key_type = KEY_WEP;
830
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200831 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300832 if (ret < 0) {
833 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
834 goto out;
835 }
836
837out:
838 kfree(cmd);
839
840 return ret;
841}
842
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200843int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
844{
845 struct wl1271_cmd_set_ap_keys *cmd;
846 int ret = 0;
847
848 wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
849
850 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
851 if (!cmd) {
852 ret = -ENOMEM;
853 goto out;
854 }
855
856 cmd->hlid = WL1271_AP_BROADCAST_HLID;
857 cmd->key_id = id;
858 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
859 cmd->key_action = cpu_to_le16(KEY_SET_ID);
860 cmd->key_type = KEY_WEP;
861
862 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
863 if (ret < 0) {
864 wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
865 goto out;
866 }
867
868out:
869 kfree(cmd);
870
871 return ret;
872}
873
874int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300875 u8 key_size, const u8 *key, const u8 *addr,
876 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300877{
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200878 struct wl1271_cmd_set_sta_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300879 int ret = 0;
880
881 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
882 if (!cmd) {
883 ret = -ENOMEM;
884 goto out;
885 }
886
887 if (key_type != KEY_WEP)
888 memcpy(cmd->addr, addr, ETH_ALEN);
889
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300890 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300891 cmd->key_size = key_size;
892 cmd->key_type = key_type;
893
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300894 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
895 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300896
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300897 /* we have only one SSID profile */
898 cmd->ssid_profile = 0;
899
900 cmd->id = id;
901
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300902 if (key_type == KEY_TKIP) {
903 /*
904 * We get the key in the following form:
905 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
906 * but the target is expecting:
907 * TKIP - RX MIC - TX MIC
908 */
909 memcpy(cmd->key, key, 16);
910 memcpy(cmd->key + 16, key + 24, 8);
911 memcpy(cmd->key + 24, key + 16, 8);
912
913 } else {
914 memcpy(cmd->key, key, key_size);
915 }
916
917 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
918
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200919 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300920 if (ret < 0) {
921 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200922 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300923 }
924
925out:
926 kfree(cmd);
927
928 return ret;
929}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300930
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200931int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
932 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
933 u16 tx_seq_16)
934{
935 struct wl1271_cmd_set_ap_keys *cmd;
936 int ret = 0;
937 u8 lid_type;
938
939 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
940 if (!cmd)
941 return -ENOMEM;
942
943 if (hlid == WL1271_AP_BROADCAST_HLID) {
944 if (key_type == KEY_WEP)
945 lid_type = WEP_DEFAULT_LID_TYPE;
946 else
947 lid_type = BROADCAST_LID_TYPE;
948 } else {
949 lid_type = UNICAST_LID_TYPE;
950 }
951
952 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
953 " hlid: %d", (int)action, (int)id, (int)lid_type,
954 (int)key_type, (int)hlid);
955
956 cmd->lid_key_type = lid_type;
957 cmd->hlid = hlid;
958 cmd->key_action = cpu_to_le16(action);
959 cmd->key_size = key_size;
960 cmd->key_type = key_type;
961 cmd->key_id = id;
962 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
963 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
964
965 if (key_type == KEY_TKIP) {
966 /*
967 * We get the key in the following form:
968 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
969 * but the target is expecting:
970 * TKIP - RX MIC - TX MIC
971 */
972 memcpy(cmd->key, key, 16);
973 memcpy(cmd->key + 16, key + 24, 8);
974 memcpy(cmd->key + 24, key + 16, 8);
975 } else {
976 memcpy(cmd->key, key, key_size);
977 }
978
979 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
980
981 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
982 if (ret < 0) {
983 wl1271_warning("could not set ap keys");
984 goto out;
985 }
986
987out:
988 kfree(cmd);
989 return ret;
990}
991
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300992int wl1271_cmd_disconnect(struct wl1271 *wl)
993{
994 struct wl1271_cmd_disconnect *cmd;
995 int ret = 0;
996
997 wl1271_debug(DEBUG_CMD, "cmd disconnect");
998
999 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1000 if (!cmd) {
1001 ret = -ENOMEM;
1002 goto out;
1003 }
1004
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001005 /* disconnect reason is not used in immediate disconnections */
1006 cmd->type = DISCONNECT_IMMEDIATE;
1007
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001008 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001009 if (ret < 0) {
1010 wl1271_error("failed to send disconnect command");
1011 goto out_free;
1012 }
1013
Luciano Coelho2f826f52010-03-26 12:53:21 +02001014 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
1015 if (ret < 0)
1016 wl1271_error("cmd disconnect event completion error");
1017
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001018out_free:
1019 kfree(cmd);
1020
1021out:
1022 return ret;
1023}
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001024
1025int wl1271_cmd_set_sta_state(struct wl1271 *wl)
1026{
1027 struct wl1271_cmd_set_sta_state *cmd;
1028 int ret = 0;
1029
1030 wl1271_debug(DEBUG_CMD, "cmd set sta state");
1031
1032 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1033 if (!cmd) {
1034 ret = -ENOMEM;
1035 goto out;
1036 }
1037
1038 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1039
1040 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
1041 if (ret < 0) {
1042 wl1271_error("failed to send set STA state command");
1043 goto out_free;
1044 }
1045
1046out_free:
1047 kfree(cmd);
1048
1049out:
1050 return ret;
1051}
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001052
1053int wl1271_cmd_start_bss(struct wl1271 *wl)
1054{
1055 struct wl1271_cmd_bss_start *cmd;
1056 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
1057 int ret;
1058
1059 wl1271_debug(DEBUG_CMD, "cmd start bss");
1060
1061 /*
1062 * FIXME: We currently do not support hidden SSID. The real SSID
1063 * should be fetched from mac80211 first.
1064 */
1065 if (wl->ssid_len == 0) {
1066 wl1271_warning("Hidden SSID currently not supported for AP");
1067 ret = -EINVAL;
1068 goto out;
1069 }
1070
1071 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1072 if (!cmd) {
1073 ret = -ENOMEM;
1074 goto out;
1075 }
1076
1077 memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
1078
Arik Nemtsov3618f302011-06-26 10:36:03 +03001079 cmd->aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001080 cmd->bss_index = WL1271_AP_BSS_INDEX;
1081 cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
1082 cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
1083 cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
1084 cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
1085 cmd->dtim_interval = bss_conf->dtim_period;
1086 cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
1087 cmd->channel = wl->channel;
1088 cmd->ssid_len = wl->ssid_len;
1089 cmd->ssid_type = SSID_TYPE_PUBLIC;
1090 memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1091
1092 switch (wl->band) {
1093 case IEEE80211_BAND_2GHZ:
1094 cmd->band = RADIO_BAND_2_4GHZ;
1095 break;
1096 case IEEE80211_BAND_5GHZ:
1097 cmd->band = RADIO_BAND_5GHZ;
1098 break;
1099 default:
1100 wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1101 cmd->band = RADIO_BAND_2_4GHZ;
1102 break;
1103 }
1104
1105 ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1106 if (ret < 0) {
1107 wl1271_error("failed to initiate cmd start bss");
1108 goto out_free;
1109 }
1110
1111out_free:
1112 kfree(cmd);
1113
1114out:
1115 return ret;
1116}
1117
1118int wl1271_cmd_stop_bss(struct wl1271 *wl)
1119{
1120 struct wl1271_cmd_bss_start *cmd;
1121 int ret;
1122
1123 wl1271_debug(DEBUG_CMD, "cmd stop bss");
1124
1125 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1126 if (!cmd) {
1127 ret = -ENOMEM;
1128 goto out;
1129 }
1130
1131 cmd->bss_index = WL1271_AP_BSS_INDEX;
1132
1133 ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1134 if (ret < 0) {
1135 wl1271_error("failed to initiate cmd stop bss");
1136 goto out_free;
1137 }
1138
1139out_free:
1140 kfree(cmd);
1141
1142out:
1143 return ret;
1144}
1145
1146int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1147{
1148 struct wl1271_cmd_add_sta *cmd;
1149 int ret;
1150
1151 wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1152
1153 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1154 if (!cmd) {
1155 ret = -ENOMEM;
1156 goto out;
1157 }
1158
1159 /* currently we don't support UAPSD */
1160 cmd->sp_len = 0;
1161
1162 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1163 cmd->bss_index = WL1271_AP_BSS_INDEX;
1164 cmd->aid = sta->aid;
1165 cmd->hlid = hlid;
Arik Nemtsovb4d38db2011-06-27 23:58:46 +03001166 cmd->wmm = sta->wme ? 1 : 0;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001167
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}
Ido Yariv95dac04f2011-06-06 14:57:06 +03001222
1223int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1224{
1225 struct wl12xx_cmd_config_fwlog *cmd;
1226 int ret = 0;
1227
1228 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1229
1230 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1231 if (!cmd) {
1232 ret = -ENOMEM;
1233 goto out;
1234 }
1235
1236 cmd->logger_mode = wl->conf.fwlog.mode;
1237 cmd->log_severity = wl->conf.fwlog.severity;
1238 cmd->timestamp = wl->conf.fwlog.timestamp;
1239 cmd->output = wl->conf.fwlog.output;
1240 cmd->threshold = wl->conf.fwlog.threshold;
1241
1242 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1243 if (ret < 0) {
1244 wl1271_error("failed to send config firmware logger command");
1245 goto out_free;
1246 }
1247
1248out_free:
1249 kfree(cmd);
1250
1251out:
1252 return ret;
1253}
1254
1255int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1256{
1257 struct wl12xx_cmd_start_fwlog *cmd;
1258 int ret = 0;
1259
1260 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1261
1262 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1263 if (!cmd) {
1264 ret = -ENOMEM;
1265 goto out;
1266 }
1267
1268 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1269 if (ret < 0) {
1270 wl1271_error("failed to send start firmware logger command");
1271 goto out_free;
1272 }
1273
1274out_free:
1275 kfree(cmd);
1276
1277out:
1278 return ret;
1279}
1280
1281int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1282{
1283 struct wl12xx_cmd_stop_fwlog *cmd;
1284 int ret = 0;
1285
1286 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1287
1288 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1289 if (!cmd) {
1290 ret = -ENOMEM;
1291 goto out;
1292 }
1293
1294 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1295 if (ret < 0) {
1296 wl1271_error("failed to send stop firmware logger command");
1297 goto out_free;
1298 }
1299
1300out_free:
1301 kfree(cmd);
1302
1303out:
1304 return ret;
1305}