blob: 4a56ab05e736779a72abe3cd0f3dc4870493ba66 [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
Luciano Coelho99d84c12010-03-26 12:53:20 +0200174/*
175 * Poll the mailbox event field until any of the bits in the mask is set or a
176 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
177 */
178static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
179{
180 u32 events_vector, event;
181 unsigned long timeout;
182
183 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
184
185 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200186 if (time_after(jiffies, timeout)) {
187 ieee80211_queue_work(wl->hw, &wl->recovery_work);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200188 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200189 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200190
191 msleep(1);
192
193 /* read from both event fields */
194 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
195 sizeof(events_vector), false);
196 event = events_vector & mask;
197 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
198 sizeof(events_vector), false);
199 event |= events_vector & mask;
200 } while (!event);
201
202 return 0;
203}
204
Juuso Oikarinen15305492010-02-22 08:38:32 +0200205int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300206{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300207 struct wl1271_cmd_join *join;
208 int ret, i;
209 u8 *bssid;
210
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300211 join = kzalloc(sizeof(*join), GFP_KERNEL);
212 if (!join) {
213 ret = -ENOMEM;
214 goto out;
215 }
216
217 wl1271_debug(DEBUG_CMD, "cmd join");
218
219 /* Reverse order BSSID */
220 bssid = (u8 *) &join->bssid_lsb;
221 for (i = 0; i < ETH_ALEN; i++)
222 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
223
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300224 join->rx_config_options = cpu_to_le32(wl->rx_config);
225 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
Juuso Oikarinen15305492010-02-22 08:38:32 +0200226 join->bss_type = bss_type;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300227 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300228
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300229 if (wl->band == IEEE80211_BAND_5GHZ)
Teemu Paasikivia4102642009-10-13 12:47:51 +0300230 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300231
Juuso Oikarinen60e84c22010-03-26 12:53:25 +0200232 join->beacon_interval = cpu_to_le16(wl->beacon_int);
Luciano Coelhoae751ba2009-10-12 15:08:57 +0300233 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300234
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300235 join->channel = wl->channel;
236 join->ssid_len = wl->ssid_len;
237 memcpy(join->ssid, wl->ssid, wl->ssid_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300238
239 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
240
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300241 /* reset TX security counters */
242 wl->tx_security_last_seq = 0;
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200243 wl->tx_security_seq = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300244
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200245 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300246 if (ret < 0) {
247 wl1271_error("failed to initiate cmd join");
248 goto out_free;
249 }
250
Luciano Coelho99d84c12010-03-26 12:53:20 +0200251 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
252 if (ret < 0)
253 wl1271_error("cmd join event completion error");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300254
255out_free:
256 kfree(join);
257
258out:
259 return ret;
260}
261
262/**
263 * send test command to firmware
264 *
265 * @wl: wl struct
266 * @buf: buffer containing the command, with all headers, must work with dma
267 * @len: length of the buffer
268 * @answer: is answer needed
269 */
270int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
271{
272 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200273 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300274
275 wl1271_debug(DEBUG_CMD, "cmd test");
276
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200277 if (answer)
278 res_len = buf_len;
279
280 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300281
282 if (ret < 0) {
283 wl1271_warning("TEST command failed");
284 return ret;
285 }
286
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200287 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300288}
289
290/**
291 * read acx from firmware
292 *
293 * @wl: wl struct
294 * @id: acx id
295 * @buf: buffer for the response, including all headers, must work with dma
296 * @len: lenght of buf
297 */
298int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
299{
300 struct acx_header *acx = buf;
301 int ret;
302
303 wl1271_debug(DEBUG_CMD, "cmd interrogate");
304
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300305 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300306
307 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300308 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300309
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200310 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
311 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300312 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300313
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300314 return ret;
315}
316
317/**
318 * write acx value to firmware
319 *
320 * @wl: wl struct
321 * @id: acx id
322 * @buf: buffer containing acx, including all headers, must work with dma
323 * @len: length of buf
324 */
325int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
326{
327 struct acx_header *acx = buf;
328 int ret;
329
330 wl1271_debug(DEBUG_CMD, "cmd configure");
331
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300332 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300333
334 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300335 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300336
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200337 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300338 if (ret < 0) {
339 wl1271_warning("CONFIGURE command NOK");
340 return ret;
341 }
342
343 return 0;
344}
345
Luciano Coelho94210892009-12-11 15:40:55 +0200346int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300347{
348 struct cmd_enabledisable_path *cmd;
349 int ret;
350 u16 cmd_rx, cmd_tx;
351
352 wl1271_debug(DEBUG_CMD, "cmd data path");
353
354 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
355 if (!cmd) {
356 ret = -ENOMEM;
357 goto out;
358 }
359
Luciano Coelho94210892009-12-11 15:40:55 +0200360 /* the channel here is only used for calibration, so hardcoded to 1 */
361 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300362
363 if (enable) {
364 cmd_rx = CMD_ENABLE_RX;
365 cmd_tx = CMD_ENABLE_TX;
366 } else {
367 cmd_rx = CMD_DISABLE_RX;
368 cmd_tx = CMD_DISABLE_TX;
369 }
370
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200371 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300372 if (ret < 0) {
373 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200374 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300375 goto out;
376 }
377
378 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200379 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300380
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200381 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300382 if (ret < 0) {
383 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200384 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200385 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300386 }
387
388 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200389 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300390
391out:
392 kfree(cmd);
393 return ret;
394}
395
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300396int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, u32 rates, bool send)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300397{
398 struct wl1271_cmd_ps_params *ps_params = NULL;
399 int ret = 0;
400
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300401 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
402
403 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
404 if (!ps_params) {
405 ret = -ENOMEM;
406 goto out;
407 }
408
409 ps_params->ps_mode = ps_mode;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200410 ps_params->send_null_data = send;
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200411 ps_params->retries = wl->conf.conn.psm_entry_nullfunc_retries;
412 ps_params->hang_over_period = wl->conf.conn.psm_entry_hangover_period;
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300413 ps_params->null_data_rate = cpu_to_le32(rates);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300414
415 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200416 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300417 if (ret < 0) {
418 wl1271_error("cmd set_ps_mode failed");
419 goto out;
420 }
421
422out:
423 kfree(ps_params);
424 return ret;
425}
426
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300427int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300428 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300429{
430 struct wl1271_cmd_template_set *cmd;
431 int ret = 0;
432
433 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
434
435 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
436 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
437
438 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
439 if (!cmd) {
440 ret = -ENOMEM;
441 goto out;
442 }
443
444 cmd->len = cpu_to_le16(buf_len);
445 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300446 cmd->enabled_rates = cpu_to_le32(rates);
Juuso Oikarinen45b531a2009-10-13 12:47:41 +0300447 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
448 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200449 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300450
451 if (buf)
452 memcpy(cmd->template_data, buf, buf_len);
453
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200454 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300455 if (ret < 0) {
456 wl1271_warning("cmd set_template failed: %d", ret);
457 goto out_free;
458 }
459
460out_free:
461 kfree(cmd);
462
463out:
464 return ret;
465}
466
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300467int wl1271_cmd_build_null_data(struct wl1271 *wl)
468{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200469 struct sk_buff *skb = NULL;
470 int size;
471 void *ptr;
472 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300473
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300474
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200475 if (wl->bss_type == BSS_TYPE_IBSS) {
476 size = sizeof(struct wl12xx_null_data_template);
477 ptr = NULL;
478 } else {
479 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
480 if (!skb)
481 goto out;
482 size = skb->len;
483 ptr = skb->data;
484 }
485
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300486 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200487 wl->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300488
Kalle Valo899e6e62010-03-18 12:26:34 +0200489out:
490 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +0200491 if (ret)
492 wl1271_warning("cmd buld null data failed %d", ret);
493
Kalle Valo899e6e62010-03-18 12:26:34 +0200494 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300495
496}
497
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200498int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
499{
500 struct sk_buff *skb = NULL;
501 int ret = -ENOMEM;
502
503 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
504 if (!skb)
505 goto out;
506
507 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
508 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300509 CMD_TEMPL_KLV_IDX_NULL_DATA,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200510 wl->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +0200511
512out:
513 dev_kfree_skb(skb);
514 if (ret)
515 wl1271_warning("cmd build klv null data failed %d", ret);
516
517 return ret;
518
519}
520
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300521int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
522{
Kalle Valo899e6e62010-03-18 12:26:34 +0200523 struct sk_buff *skb;
524 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300525
Kalle Valo899e6e62010-03-18 12:26:34 +0200526 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
527 if (!skb)
528 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300529
Kalle Valo899e6e62010-03-18 12:26:34 +0200530 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Juuso Oikarinen849923f2010-07-08 17:49:59 +0300531 skb->len, 0, wl->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +0300532
Kalle Valo899e6e62010-03-18 12:26:34 +0200533out:
534 dev_kfree_skb(skb);
535 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300536}
537
Kalle Valo818e3062010-03-18 12:26:35 +0200538int wl1271_cmd_build_probe_req(struct wl1271 *wl,
539 const u8 *ssid, size_t ssid_len,
540 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300541{
Kalle Valo818e3062010-03-18 12:26:35 +0200542 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300543 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300544
Kalle Valo818e3062010-03-18 12:26:35 +0200545 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
546 ie, ie_len);
547 if (!skb) {
548 ret = -ENOMEM;
549 goto out;
550 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300551
Kalle Valo818e3062010-03-18 12:26:35 +0200552 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300553
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300554 if (band == IEEE80211_BAND_2GHZ)
555 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300556 skb->data, skb->len, 0,
557 wl->conf.tx.basic_rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300558 else
559 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300560 skb->data, skb->len, 0,
561 wl->conf.tx.basic_rate_5);
Kalle Valo818e3062010-03-18 12:26:35 +0200562
563out:
564 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300565 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300566}
567
Kalle Valo023e0822010-03-18 12:26:36 +0200568int wl1271_build_qos_null_data(struct wl1271 *wl)
569{
570 struct ieee80211_qos_hdr template;
571
572 memset(&template, 0, sizeof(template));
573
574 memcpy(template.addr1, wl->bssid, ETH_ALEN);
575 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
576 memcpy(template.addr3, wl->bssid, ETH_ALEN);
577
578 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
579 IEEE80211_STYPE_QOS_NULLFUNC |
580 IEEE80211_FCTL_TODS);
581
582 /* FIXME: not sure what priority to use here */
583 template.qos_ctrl = cpu_to_le16(0);
584
585 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300586 sizeof(template), 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200587 wl->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +0200588}
589
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300590int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
591{
592 struct wl1271_cmd_set_keys *cmd;
593 int ret = 0;
594
595 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
596
597 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
598 if (!cmd) {
599 ret = -ENOMEM;
600 goto out;
601 }
602
603 cmd->id = id;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300604 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300605 cmd->key_type = KEY_WEP;
606
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200607 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300608 if (ret < 0) {
609 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
610 goto out;
611 }
612
613out:
614 kfree(cmd);
615
616 return ret;
617}
618
619int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300620 u8 key_size, const u8 *key, const u8 *addr,
621 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300622{
623 struct wl1271_cmd_set_keys *cmd;
624 int ret = 0;
625
626 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
627 if (!cmd) {
628 ret = -ENOMEM;
629 goto out;
630 }
631
632 if (key_type != KEY_WEP)
633 memcpy(cmd->addr, addr, ETH_ALEN);
634
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300635 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300636 cmd->key_size = key_size;
637 cmd->key_type = key_type;
638
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300639 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
640 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300641
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300642 /* we have only one SSID profile */
643 cmd->ssid_profile = 0;
644
645 cmd->id = id;
646
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300647 if (key_type == KEY_TKIP) {
648 /*
649 * We get the key in the following form:
650 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
651 * but the target is expecting:
652 * TKIP - RX MIC - TX MIC
653 */
654 memcpy(cmd->key, key, 16);
655 memcpy(cmd->key + 16, key + 24, 8);
656 memcpy(cmd->key + 24, key + 16, 8);
657
658 } else {
659 memcpy(cmd->key, key, key_size);
660 }
661
662 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
663
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200664 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300665 if (ret < 0) {
666 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200667 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300668 }
669
670out:
671 kfree(cmd);
672
673 return ret;
674}
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300675
676int wl1271_cmd_disconnect(struct wl1271 *wl)
677{
678 struct wl1271_cmd_disconnect *cmd;
679 int ret = 0;
680
681 wl1271_debug(DEBUG_CMD, "cmd disconnect");
682
683 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
684 if (!cmd) {
685 ret = -ENOMEM;
686 goto out;
687 }
688
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300689 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
690 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300691 /* disconnect reason is not used in immediate disconnections */
692 cmd->type = DISCONNECT_IMMEDIATE;
693
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200694 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300695 if (ret < 0) {
696 wl1271_error("failed to send disconnect command");
697 goto out_free;
698 }
699
Luciano Coelho2f826f52010-03-26 12:53:21 +0200700 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
701 if (ret < 0)
702 wl1271_error("cmd disconnect event completion error");
703
Luciano Coelho25a7dc62009-10-12 15:08:42 +0300704out_free:
705 kfree(cmd);
706
707out:
708 return ret;
709}
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +0300710
711int wl1271_cmd_set_sta_state(struct wl1271 *wl)
712{
713 struct wl1271_cmd_set_sta_state *cmd;
714 int ret = 0;
715
716 wl1271_debug(DEBUG_CMD, "cmd set sta state");
717
718 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
719 if (!cmd) {
720 ret = -ENOMEM;
721 goto out;
722 }
723
724 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
725
726 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
727 if (ret < 0) {
728 wl1271_error("failed to send set STA state command");
729 goto out_free;
730 }
731
732out_free:
733 kfree(cmd);
734
735out:
736 return ret;
737}