blob: 596e333919ae9e08f4172ff090a4c21cca8014b0 [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
32#include "wl1271.h"
33#include "wl1271_reg.h"
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020034#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030035#include "wl1271_acx.h"
36#include "wl12xx_80211.h"
37#include "wl1271_cmd.h"
Luciano Coelho99d84c12010-03-26 12:53:20 +020038#include "wl1271_event.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);
65
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020066 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030067
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020068 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030069
70 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
71
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020072 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030073 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
74 if (time_after(jiffies, timeout)) {
75 wl1271_error("command complete timeout");
76 ret = -ETIMEDOUT;
77 goto out;
78 }
79
Saravanan Dhanabalbc0f03e2010-03-26 12:53:33 +020080 poll_count++;
Juuso Oikarinen16092b52010-04-28 09:49:59 +030081 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
82 udelay(10);
83 else
84 msleep(1);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030085
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020086 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030087 }
88
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020089 /* read back the status code of the command */
Juuso Oikarinenfa867e72009-11-02 20:22:13 +020090 if (res_len == 0)
91 res_len = sizeof(struct wl1271_cmd_header);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020092 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020093
Juuso Oikarinenad150e92009-11-02 20:22:12 +020094 status = le16_to_cpu(cmd->status);
95 if (status != CMD_STATUS_SUCCESS) {
96 wl1271_error("command execute failure %d", status);
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +020097 ieee80211_queue_work(wl->hw, &wl->recovery_work);
Juuso Oikarinen3b775b42009-11-02 20:22:10 +020098 ret = -EIO;
99 }
100
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200101 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
102 WL1271_ACX_INTR_CMD_COMPLETE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300103
104out:
105 return ret;
106}
107
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200108int wl1271_cmd_general_parms(struct wl1271 *wl)
109{
110 struct wl1271_general_parms_cmd *gen_parms;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200111 int ret;
112
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200113 if (!wl->nvs)
114 return -ENODEV;
115
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200116 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
117 if (!gen_parms)
118 return -ENOMEM;
119
120 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
121
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300122 memcpy(&gen_parms->general_params, &wl->nvs->general_params,
123 sizeof(struct wl1271_ini_general_params));
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200124
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200125 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
126 if (ret < 0)
127 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
128
129 kfree(gen_parms);
130 return ret;
131}
132
133int wl1271_cmd_radio_parms(struct wl1271 *wl)
134{
135 struct wl1271_radio_parms_cmd *radio_parms;
Luciano Coelhoe6b190f2010-07-08 17:50:01 +0300136 struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200137 int ret;
138
139 if (!wl->nvs)
140 return -ENODEV;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200141
142 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
143 if (!radio_parms)
144 return -ENOMEM;
145
146 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
147
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300148 /* 2.4GHz parameters */
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300149 memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
150 sizeof(struct wl1271_ini_band_params_2));
151 memcpy(&radio_parms->dyn_params_2,
Luciano Coelhoe6b190f2010-07-08 17:50:01 +0300152 &wl->nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300153 sizeof(struct wl1271_ini_fem_params_2));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200154
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300155 /* 5GHz parameters */
156 memcpy(&radio_parms->static_params_5,
157 &wl->nvs->stat_radio_params_5,
158 sizeof(struct wl1271_ini_band_params_5));
159 memcpy(&radio_parms->dyn_params_5,
Luciano Coelhoe6b190f2010-07-08 17:50:01 +0300160 &wl->nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300161 sizeof(struct wl1271_ini_fem_params_5));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200162
163 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
164 radio_parms, sizeof(*radio_parms));
165
166 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
167 if (ret < 0)
168 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
169
170 kfree(radio_parms);
171 return ret;
172}
173
Juuso Oikarinen644a4862010-10-05 13:11:56 +0200174int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
175{
176 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
177 struct conf_rf_settings *rf = &wl->conf.rf;
178 int ret;
179
180 if (!wl->nvs)
181 return -ENODEV;
182
183 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
184 if (!ext_radio_parms)
185 return -ENOMEM;
186
187 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
188
189 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
190 rf->tx_per_channel_power_compensation_2,
191 CONF_TX_PWR_COMPENSATION_LEN_2);
192 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
193 rf->tx_per_channel_power_compensation_5,
194 CONF_TX_PWR_COMPENSATION_LEN_5);
195
196 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
197 ext_radio_parms, sizeof(*ext_radio_parms));
198
199 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
200 if (ret < 0)
201 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
202
203 kfree(ext_radio_parms);
204 return ret;
205}
206
Luciano Coelho99d84c12010-03-26 12:53:20 +0200207/*
208 * Poll the mailbox event field until any of the bits in the mask is set or a
209 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
210 */
211static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
212{
213 u32 events_vector, event;
214 unsigned long timeout;
215
216 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
217
218 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200219 if (time_after(jiffies, timeout)) {
220 ieee80211_queue_work(wl->hw, &wl->recovery_work);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200221 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200222 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200223
224 msleep(1);
225
226 /* read from both event fields */
227 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
228 sizeof(events_vector), false);
229 event = events_vector & mask;
230 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
231 sizeof(events_vector), false);
232 event |= events_vector & mask;
233 } while (!event);
234
235 return 0;
236}
237
Juuso Oikarinen15305492010-02-22 08:38:32 +0200238int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300239{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300240 struct wl1271_cmd_join *join;
241 int ret, i;
242 u8 *bssid;
243
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300244 join = kzalloc(sizeof(*join), GFP_KERNEL);
245 if (!join) {
246 ret = -ENOMEM;
247 goto out;
248 }
249
250 wl1271_debug(DEBUG_CMD, "cmd join");
251
252 /* Reverse order BSSID */
253 bssid = (u8 *) &join->bssid_lsb;
254 for (i = 0; i < ETH_ALEN; i++)
255 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
256
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300257 join->rx_config_options = cpu_to_le32(wl->rx_config);
258 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
Juuso Oikarinen15305492010-02-22 08:38:32 +0200259 join->bss_type = bss_type;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300260 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300261
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300262 if (wl->band == IEEE80211_BAND_5GHZ)
Teemu Paasikivia4102642009-10-13 12:47:51 +0300263 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300264
Juuso Oikarinen60e84c22010-03-26 12:53:25 +0200265 join->beacon_interval = cpu_to_le16(wl->beacon_int);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300266 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300267
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300268 join->channel = wl->channel;
269 join->ssid_len = wl->ssid_len;
270 memcpy(join->ssid, wl->ssid, wl->ssid_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300271
272 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
273
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300274 /* reset TX security counters */
275 wl->tx_security_last_seq = 0;
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200276 wl->tx_security_seq = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300277
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200278 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300279 if (ret < 0) {
280 wl1271_error("failed to initiate cmd join");
281 goto out_free;
282 }
283
Luciano Coelho99d84c12010-03-26 12:53:20 +0200284 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
285 if (ret < 0)
286 wl1271_error("cmd join event completion error");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300287
288out_free:
289 kfree(join);
290
291out:
292 return ret;
293}
294
295/**
296 * send test command to firmware
297 *
298 * @wl: wl struct
299 * @buf: buffer containing the command, with all headers, must work with dma
300 * @len: length of the buffer
301 * @answer: is answer needed
302 */
303int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
304{
305 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200306 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300307
308 wl1271_debug(DEBUG_CMD, "cmd test");
309
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200310 if (answer)
311 res_len = buf_len;
312
313 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300314
315 if (ret < 0) {
316 wl1271_warning("TEST command failed");
317 return ret;
318 }
319
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200320 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300321}
322
323/**
324 * read acx from firmware
325 *
326 * @wl: wl struct
327 * @id: acx id
328 * @buf: buffer for the response, including all headers, must work with dma
329 * @len: lenght of buf
330 */
331int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
332{
333 struct acx_header *acx = buf;
334 int ret;
335
336 wl1271_debug(DEBUG_CMD, "cmd interrogate");
337
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300338 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300339
340 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300341 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300342
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200343 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
344 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300345 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300346
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300347 return ret;
348}
349
350/**
351 * write acx value to firmware
352 *
353 * @wl: wl struct
354 * @id: acx id
355 * @buf: buffer containing acx, including all headers, must work with dma
356 * @len: length of buf
357 */
358int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
359{
360 struct acx_header *acx = buf;
361 int ret;
362
363 wl1271_debug(DEBUG_CMD, "cmd configure");
364
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300365 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300366
367 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300368 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300369
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200370 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300371 if (ret < 0) {
372 wl1271_warning("CONFIGURE command NOK");
373 return ret;
374 }
375
376 return 0;
377}
378
Luciano Coelho94210892009-12-11 15:40:55 +0200379int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300380{
381 struct cmd_enabledisable_path *cmd;
382 int ret;
383 u16 cmd_rx, cmd_tx;
384
385 wl1271_debug(DEBUG_CMD, "cmd data path");
386
387 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
388 if (!cmd) {
389 ret = -ENOMEM;
390 goto out;
391 }
392
Luciano Coelho94210892009-12-11 15:40:55 +0200393 /* the channel here is only used for calibration, so hardcoded to 1 */
394 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300395
396 if (enable) {
397 cmd_rx = CMD_ENABLE_RX;
398 cmd_tx = CMD_ENABLE_TX;
399 } else {
400 cmd_rx = CMD_DISABLE_RX;
401 cmd_tx = CMD_DISABLE_TX;
402 }
403
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200404 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300405 if (ret < 0) {
406 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200407 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300408 goto out;
409 }
410
411 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200412 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300413
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200414 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300415 if (ret < 0) {
416 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200417 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200418 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300419 }
420
421 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200422 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300423
424out:
425 kfree(cmd);
426 return ret;
427}
428
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300429int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, u32 rates, bool send)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300430{
431 struct wl1271_cmd_ps_params *ps_params = NULL;
432 int ret = 0;
433
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300434 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
435
436 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
437 if (!ps_params) {
438 ret = -ENOMEM;
439 goto out;
440 }
441
442 ps_params->ps_mode = ps_mode;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200443 ps_params->send_null_data = send;
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200444 ps_params->retries = wl->conf.conn.psm_entry_nullfunc_retries;
445 ps_params->hang_over_period = wl->conf.conn.psm_entry_hangover_period;
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300446 ps_params->null_data_rate = cpu_to_le32(rates);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300447
448 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200449 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300450 if (ret < 0) {
451 wl1271_error("cmd set_ps_mode failed");
452 goto out;
453 }
454
455out:
456 kfree(ps_params);
457 return ret;
458}
459
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300460int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300461 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300462{
463 struct wl1271_cmd_template_set *cmd;
464 int ret = 0;
465
466 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
467
468 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
469 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
470
471 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
472 if (!cmd) {
473 ret = -ENOMEM;
474 goto out;
475 }
476
477 cmd->len = cpu_to_le16(buf_len);
478 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300479 cmd->enabled_rates = cpu_to_le32(rates);
Juuso Oikarinen45b531a2009-10-13 12:47:41 +0300480 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
481 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200482 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300483
484 if (buf)
485 memcpy(cmd->template_data, buf, buf_len);
486
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200487 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300488 if (ret < 0) {
489 wl1271_warning("cmd set_template failed: %d", ret);
490 goto out_free;
491 }
492
493out_free:
494 kfree(cmd);
495
496out:
497 return ret;
498}
499
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300500int wl1271_cmd_build_null_data(struct wl1271 *wl)
501{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200502 struct sk_buff *skb = NULL;
503 int size;
504 void *ptr;
505 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300506
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300507
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200508 if (wl->bss_type == BSS_TYPE_IBSS) {
509 size = sizeof(struct wl12xx_null_data_template);
510 ptr = NULL;
511 } else {
512 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
513 if (!skb)
514 goto out;
515 size = skb->len;
516 ptr = skb->data;
517 }
518
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300519 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200520 wl->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300521
Kalle Valo899e6e62010-03-18 12:26:34 +0200522out:
523 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200524 if (ret)
525 wl1271_warning("cmd buld null data failed %d", ret);
526
Kalle Valo899e6e62010-03-18 12:26:34 +0200527 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300528
529}
530
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200531int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
532{
533 struct sk_buff *skb = NULL;
534 int ret = -ENOMEM;
535
536 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
537 if (!skb)
538 goto out;
539
540 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
541 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300542 CMD_TEMPL_KLV_IDX_NULL_DATA,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200543 wl->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200544
545out:
546 dev_kfree_skb(skb);
547 if (ret)
548 wl1271_warning("cmd build klv null data failed %d", ret);
549
550 return ret;
551
552}
553
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300554int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
555{
Kalle Valo899e6e62010-03-18 12:26:34 +0200556 struct sk_buff *skb;
557 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300558
Kalle Valo899e6e62010-03-18 12:26:34 +0200559 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
560 if (!skb)
561 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300562
Kalle Valo899e6e62010-03-18 12:26:34 +0200563 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Juuso Oikarinen849923f2010-07-08 17:49:59 +0300564 skb->len, 0, wl->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300565
Kalle Valo899e6e62010-03-18 12:26:34 +0200566out:
567 dev_kfree_skb(skb);
568 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300569}
570
Kalle Valo818e3062010-03-18 12:26:35 +0200571int wl1271_cmd_build_probe_req(struct wl1271 *wl,
572 const u8 *ssid, size_t ssid_len,
573 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300574{
Kalle Valo818e3062010-03-18 12:26:35 +0200575 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300576 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300577
Kalle Valo818e3062010-03-18 12:26:35 +0200578 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
579 ie, ie_len);
580 if (!skb) {
581 ret = -ENOMEM;
582 goto out;
583 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300584
Kalle Valo818e3062010-03-18 12:26:35 +0200585 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300586
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300587 if (band == IEEE80211_BAND_2GHZ)
588 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300589 skb->data, skb->len, 0,
590 wl->conf.tx.basic_rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300591 else
592 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300593 skb->data, skb->len, 0,
594 wl->conf.tx.basic_rate_5);
Kalle Valo818e3062010-03-18 12:26:35 +0200595
596out:
597 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300598 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300599}
600
Kalle Valo023e0822010-03-18 12:26:36 +0200601int wl1271_build_qos_null_data(struct wl1271 *wl)
602{
603 struct ieee80211_qos_hdr template;
604
605 memset(&template, 0, sizeof(template));
606
607 memcpy(template.addr1, wl->bssid, ETH_ALEN);
608 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
609 memcpy(template.addr3, wl->bssid, ETH_ALEN);
610
611 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
612 IEEE80211_STYPE_QOS_NULLFUNC |
613 IEEE80211_FCTL_TODS);
614
615 /* FIXME: not sure what priority to use here */
616 template.qos_ctrl = cpu_to_le16(0);
617
618 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300619 sizeof(template), 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200620 wl->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +0200621}
622
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300623int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
624{
625 struct wl1271_cmd_set_keys *cmd;
626 int ret = 0;
627
628 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
629
630 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
631 if (!cmd) {
632 ret = -ENOMEM;
633 goto out;
634 }
635
636 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300637 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300638 cmd->key_type = KEY_WEP;
639
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200640 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300641 if (ret < 0) {
642 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
643 goto out;
644 }
645
646out:
647 kfree(cmd);
648
649 return ret;
650}
651
652int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300653 u8 key_size, const u8 *key, const u8 *addr,
654 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300655{
656 struct wl1271_cmd_set_keys *cmd;
657 int ret = 0;
658
659 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
660 if (!cmd) {
661 ret = -ENOMEM;
662 goto out;
663 }
664
665 if (key_type != KEY_WEP)
666 memcpy(cmd->addr, addr, ETH_ALEN);
667
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300668 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300669 cmd->key_size = key_size;
670 cmd->key_type = key_type;
671
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300672 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
673 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300674
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300675 /* we have only one SSID profile */
676 cmd->ssid_profile = 0;
677
678 cmd->id = id;
679
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300680 if (key_type == KEY_TKIP) {
681 /*
682 * We get the key in the following form:
683 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
684 * but the target is expecting:
685 * TKIP - RX MIC - TX MIC
686 */
687 memcpy(cmd->key, key, 16);
688 memcpy(cmd->key + 16, key + 24, 8);
689 memcpy(cmd->key + 24, key + 16, 8);
690
691 } else {
692 memcpy(cmd->key, key, key_size);
693 }
694
695 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
696
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200697 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300698 if (ret < 0) {
699 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200700 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300701 }
702
703out:
704 kfree(cmd);
705
706 return ret;
707}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300708
709int wl1271_cmd_disconnect(struct wl1271 *wl)
710{
711 struct wl1271_cmd_disconnect *cmd;
712 int ret = 0;
713
714 wl1271_debug(DEBUG_CMD, "cmd disconnect");
715
716 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
717 if (!cmd) {
718 ret = -ENOMEM;
719 goto out;
720 }
721
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300722 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
723 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300724 /* disconnect reason is not used in immediate disconnections */
725 cmd->type = DISCONNECT_IMMEDIATE;
726
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200727 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300728 if (ret < 0) {
729 wl1271_error("failed to send disconnect command");
730 goto out_free;
731 }
732
Luciano Coelho2f826f52010-03-26 12:53:21 +0200733 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
734 if (ret < 0)
735 wl1271_error("cmd disconnect event completion error");
736
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300737out_free:
738 kfree(cmd);
739
740out:
741 return ret;
742}
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +0300743
744int wl1271_cmd_set_sta_state(struct wl1271 *wl)
745{
746 struct wl1271_cmd_set_sta_state *cmd;
747 int ret = 0;
748
749 wl1271_debug(DEBUG_CMD, "cmd set sta state");
750
751 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
752 if (!cmd) {
753 ret = -ENOMEM;
754 goto out;
755 }
756
757 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
758
759 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
760 if (ret < 0) {
761 wl1271_error("failed to send set STA state command");
762 goto out_free;
763 }
764
765out_free:
766 kfree(cmd);
767
768out:
769 return ret;
770}