blob: ce734157980a9bce2c63076ee3469f87bae9bf80 [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 Levi49d750ca2011-03-06 16:32:09 +0200137 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
138 if (ret < 0) {
139 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
140 goto out;
141 }
142
143 gp->tx_bip_fem_manufacturer =
144 gen_parms->general_params.tx_bip_fem_manufacturer;
145
146 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
147 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
148
149out:
150 kfree(gen_parms);
151 return ret;
152}
153
154int wl128x_cmd_general_parms(struct wl1271 *wl)
155{
156 struct wl128x_general_parms_cmd *gen_parms;
157 struct wl128x_ini_general_params *gp =
158 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300159 bool answer = false;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200160 int ret;
161
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200162 if (!wl->nvs)
163 return -ENODEV;
164
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200165 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
166 if (!gen_parms)
167 return -ENOMEM;
168
169 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
170
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300171 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200172
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300173 if (gp->tx_bip_fem_auto_detect)
174 answer = true;
175
Shahar Levib03acad2011-04-03 13:54:54 +0300176 /* Replace REF and TCXO CLKs with the ones from platform data */
177 gen_parms->general_params.ref_clock = wl->ref_clock;
178 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
179
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300180 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
181 if (ret < 0) {
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200182 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300183 goto out;
184 }
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200185
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300186 gp->tx_bip_fem_manufacturer =
187 gen_parms->general_params.tx_bip_fem_manufacturer;
188
189 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
190 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
191
192out:
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200193 kfree(gen_parms);
194 return ret;
195}
196
197int wl1271_cmd_radio_parms(struct wl1271 *wl)
198{
Shahar Levibc765bf2011-03-06 16:32:10 +0200199 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200200 struct wl1271_radio_parms_cmd *radio_parms;
Shahar Levibc765bf2011-03-06 16:32:10 +0200201 struct wl1271_ini_general_params *gp = &nvs->general_params;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200202 int ret;
203
204 if (!wl->nvs)
205 return -ENODEV;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200206
207 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
208 if (!radio_parms)
209 return -ENOMEM;
210
211 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
212
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300213 /* 2.4GHz parameters */
Shahar Levibc765bf2011-03-06 16:32:10 +0200214 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300215 sizeof(struct wl1271_ini_band_params_2));
216 memcpy(&radio_parms->dyn_params_2,
Shahar Levibc765bf2011-03-06 16:32:10 +0200217 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300218 sizeof(struct wl1271_ini_fem_params_2));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200219
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300220 /* 5GHz parameters */
221 memcpy(&radio_parms->static_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200222 &nvs->stat_radio_params_5,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300223 sizeof(struct wl1271_ini_band_params_5));
224 memcpy(&radio_parms->dyn_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200225 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300226 sizeof(struct wl1271_ini_fem_params_5));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200227
228 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
229 radio_parms, sizeof(*radio_parms));
230
231 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
232 if (ret < 0)
233 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
234
235 kfree(radio_parms);
236 return ret;
237}
238
Shahar Levi49d750ca2011-03-06 16:32:09 +0200239int wl128x_cmd_radio_parms(struct wl1271 *wl)
240{
241 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
242 struct wl128x_radio_parms_cmd *radio_parms;
243 struct wl128x_ini_general_params *gp = &nvs->general_params;
244 int ret;
245
246 if (!wl->nvs)
247 return -ENODEV;
248
249 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
250 if (!radio_parms)
251 return -ENOMEM;
252
253 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
254
255 /* 2.4GHz parameters */
256 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
257 sizeof(struct wl128x_ini_band_params_2));
258 memcpy(&radio_parms->dyn_params_2,
259 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
260 sizeof(struct wl128x_ini_fem_params_2));
261
262 /* 5GHz parameters */
263 memcpy(&radio_parms->static_params_5,
264 &nvs->stat_radio_params_5,
265 sizeof(struct wl128x_ini_band_params_5));
266 memcpy(&radio_parms->dyn_params_5,
267 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
268 sizeof(struct wl128x_ini_fem_params_5));
269
270 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
271
272 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
273 radio_parms, sizeof(*radio_parms));
274
275 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
276 if (ret < 0)
277 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
278
279 kfree(radio_parms);
280 return ret;
281}
282
Juuso Oikarinen644a4862010-10-05 13:11:56 +0200283int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
284{
285 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
286 struct conf_rf_settings *rf = &wl->conf.rf;
287 int ret;
288
289 if (!wl->nvs)
290 return -ENODEV;
291
292 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
293 if (!ext_radio_parms)
294 return -ENOMEM;
295
296 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
297
298 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
299 rf->tx_per_channel_power_compensation_2,
300 CONF_TX_PWR_COMPENSATION_LEN_2);
301 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
302 rf->tx_per_channel_power_compensation_5,
303 CONF_TX_PWR_COMPENSATION_LEN_5);
304
305 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
306 ext_radio_parms, sizeof(*ext_radio_parms));
307
308 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
309 if (ret < 0)
310 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
311
312 kfree(ext_radio_parms);
313 return ret;
314}
315
Luciano Coelho99d84c12010-03-26 12:53:20 +0200316/*
317 * Poll the mailbox event field until any of the bits in the mask is set or a
318 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
319 */
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200320static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
Luciano Coelho99d84c12010-03-26 12:53:20 +0200321{
322 u32 events_vector, event;
323 unsigned long timeout;
324
325 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
326
327 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200328 if (time_after(jiffies, timeout)) {
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200329 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
330 (int)mask);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200331 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200332 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200333
334 msleep(1);
335
336 /* read from both event fields */
337 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
338 sizeof(events_vector), false);
339 event = events_vector & mask;
340 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
341 sizeof(events_vector), false);
342 event |= events_vector & mask;
343 } while (!event);
344
345 return 0;
346}
347
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200348static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
349{
350 int ret;
351
352 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
353 if (ret != 0) {
Ido Yarivbaacb9a2011-06-06 14:57:05 +0300354 wl12xx_queue_recovery_work(wl);
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200355 return ret;
356 }
357
358 return 0;
359}
360
Eliad Peller784f6942011-10-05 11:55:39 +0200361int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
362 u8 *role_id)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300363{
Eliad Pellerc690ec82011-08-14 13:17:07 +0300364 struct wl12xx_cmd_role_enable *cmd;
365 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300366
Eliad Pellerc690ec82011-08-14 13:17:07 +0300367 wl1271_debug(DEBUG_CMD, "cmd role enable");
368
369 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
370 return -EBUSY;
371
372 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
373 if (!cmd) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300374 ret = -ENOMEM;
375 goto out;
376 }
377
Eliad Pellerc690ec82011-08-14 13:17:07 +0300378 /* get role id */
379 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
380 if (cmd->role_id >= WL12XX_MAX_ROLES) {
381 ret = -EBUSY;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300382 goto out_free;
383 }
384
Eliad Peller784f6942011-10-05 11:55:39 +0200385 memcpy(cmd->mac_address, addr, ETH_ALEN);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300386 cmd->role_type = role_type;
387
388 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
389 if (ret < 0) {
390 wl1271_error("failed to initiate cmd role enable");
391 goto out_free;
392 }
393
394 __set_bit(cmd->role_id, wl->roles_map);
395 *role_id = cmd->role_id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300396
397out_free:
Eliad Pellerc690ec82011-08-14 13:17:07 +0300398 kfree(cmd);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300399
400out:
401 return ret;
402}
403
Eliad Pellerc690ec82011-08-14 13:17:07 +0300404int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
405{
406 struct wl12xx_cmd_role_disable *cmd;
407 int ret;
408
409 wl1271_debug(DEBUG_CMD, "cmd role disable");
410
411 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
412 return -ENOENT;
413
414 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
415 if (!cmd) {
416 ret = -ENOMEM;
417 goto out;
418 }
419 cmd->role_id = *role_id;
420
421 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
422 if (ret < 0) {
423 wl1271_error("failed to initiate cmd role disable");
424 goto out_free;
425 }
426
427 __clear_bit(*role_id, wl->roles_map);
428 *role_id = WL12XX_INVALID_ROLE_ID;
429
430out_free:
431 kfree(cmd);
432
433out:
434 return ret;
435}
436
437static int wl12xx_allocate_link(struct wl1271 *wl, u8 *hlid)
438{
439 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
440 if (link >= WL12XX_MAX_LINKS)
441 return -EBUSY;
442
443 __set_bit(link, wl->links_map);
444 *hlid = link;
445 return 0;
446}
447
448static void wl12xx_free_link(struct wl1271 *wl, u8 *hlid)
449{
450 if (*hlid == WL12XX_INVALID_LINK_ID)
451 return;
452
453 __clear_bit(*hlid, wl->links_map);
454 *hlid = WL12XX_INVALID_LINK_ID;
455}
456
Arik Nemtsov712e9bf2011-08-14 13:17:20 +0300457static int wl12xx_get_new_session_id(struct wl1271 *wl)
458{
459 if (wl->session_counter >= SESSION_COUNTER_MAX)
460 wl->session_counter = 0;
461
462 wl->session_counter++;
463
464 return wl->session_counter;
465}
466
Eliad Peller04e80792011-08-14 13:17:09 +0300467int wl12xx_cmd_role_start_dev(struct wl1271 *wl)
468{
469 struct wl12xx_cmd_role_start *cmd;
470 int ret;
471
472 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
473 if (!cmd) {
474 ret = -ENOMEM;
475 goto out;
476 }
477
478 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wl->dev_role_id);
479
480 cmd->role_id = wl->dev_role_id;
481 if (wl->band == IEEE80211_BAND_5GHZ)
482 cmd->band = WL12XX_BAND_5GHZ;
483 cmd->channel = wl->channel;
484
485 if (wl->dev_hlid == WL12XX_INVALID_LINK_ID) {
486 ret = wl12xx_allocate_link(wl, &wl->dev_hlid);
487 if (ret)
488 goto out_free;
489 }
490 cmd->device.hlid = wl->dev_hlid;
491 cmd->device.session = wl->session_counter;
492
493 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
494 cmd->role_id, cmd->device.hlid, cmd->device.session);
495
496 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
497 if (ret < 0) {
498 wl1271_error("failed to initiate cmd role enable");
499 goto err_hlid;
500 }
501
502 goto out_free;
503
504err_hlid:
505 /* clear links on error */
506 __clear_bit(wl->dev_hlid, wl->links_map);
507 wl->dev_hlid = WL12XX_INVALID_LINK_ID;
508
509
510out_free:
511 kfree(cmd);
512
513out:
514 return ret;
515}
516
517int wl12xx_cmd_role_stop_dev(struct wl1271 *wl)
518{
519 struct wl12xx_cmd_role_stop *cmd;
520 int ret;
521
522 if (WARN_ON(wl->dev_hlid == WL12XX_INVALID_LINK_ID))
523 return -EINVAL;
524
525 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
526 if (!cmd) {
527 ret = -ENOMEM;
528 goto out;
529 }
530
531 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
532
533 cmd->role_id = wl->dev_role_id;
534 cmd->disc_type = DISCONNECT_IMMEDIATE;
535 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
536
537 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
538 if (ret < 0) {
539 wl1271_error("failed to initiate cmd role stop");
540 goto out_free;
541 }
542
543 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
544 if (ret < 0) {
545 wl1271_error("cmd role stop dev event completion error");
546 goto out_free;
547 }
548
549 wl12xx_free_link(wl, &wl->dev_hlid);
550
551out_free:
552 kfree(cmd);
553
554out:
555 return ret;
556}
557
Eliad Peller87fbcb02011-10-05 11:55:41 +0200558int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Pellerc690ec82011-08-14 13:17:07 +0300559{
560 struct wl12xx_cmd_role_start *cmd;
561 int ret;
562
563 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
564 if (!cmd) {
565 ret = -ENOMEM;
566 goto out;
567 }
568
569 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wl->role_id);
570
571 cmd->role_id = wl->role_id;
572 if (wl->band == IEEE80211_BAND_5GHZ)
573 cmd->band = WL12XX_BAND_5GHZ;
574 cmd->channel = wl->channel;
Eliad Peller87fbcb02011-10-05 11:55:41 +0200575 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300576 cmd->sta.beacon_interval = cpu_to_le16(wl->beacon_int);
577 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
578 cmd->sta.ssid_len = wl->ssid_len;
579 memcpy(cmd->sta.ssid, wl->ssid, wl->ssid_len);
580 memcpy(cmd->sta.bssid, wl->bssid, ETH_ALEN);
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200581 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300582
583 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
584 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
585 if (ret)
586 goto out_free;
587 }
588 cmd->sta.hlid = wl->sta_hlid;
Arik Nemtsov712e9bf2011-08-14 13:17:20 +0300589 cmd->sta.session = wl12xx_get_new_session_id(wl);
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200590 cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300591
592 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
593 "basic_rate_set: 0x%x, remote_rates: 0x%x",
594 wl->role_id, cmd->sta.hlid, cmd->sta.session,
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200595 wlvif->basic_rate_set, wlvif->rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300596
597 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
598 if (ret < 0) {
599 wl1271_error("failed to initiate cmd role start sta");
600 goto err_hlid;
601 }
602
603 goto out_free;
604
605err_hlid:
606 /* clear links on error. */
607 wl12xx_free_link(wl, &wl->sta_hlid);
608
609out_free:
610 kfree(cmd);
611
612out:
613 return ret;
614}
615
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300616/* use this function to stop ibss as well */
Eliad Pellerc690ec82011-08-14 13:17:07 +0300617int wl12xx_cmd_role_stop_sta(struct wl1271 *wl)
618{
619 struct wl12xx_cmd_role_stop *cmd;
620 int ret;
621
622 if (WARN_ON(wl->sta_hlid == WL12XX_INVALID_LINK_ID))
623 return -EINVAL;
624
625 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
626 if (!cmd) {
627 ret = -ENOMEM;
628 goto out;
629 }
630
631 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wl->role_id);
632
633 cmd->role_id = wl->role_id;
634 cmd->disc_type = DISCONNECT_IMMEDIATE;
635 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
636
637 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
638 if (ret < 0) {
639 wl1271_error("failed to initiate cmd role stop sta");
640 goto out_free;
641 }
642
Eliad Pellerc690ec82011-08-14 13:17:07 +0300643 wl12xx_free_link(wl, &wl->sta_hlid);
644
645out_free:
646 kfree(cmd);
647
648out:
649 return ret;
650}
651
Eliad Peller87fbcb02011-10-05 11:55:41 +0200652int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Pellerc690ec82011-08-14 13:17:07 +0300653{
654 struct wl12xx_cmd_role_start *cmd;
655 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
656 int ret;
657
658 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wl->role_id);
659
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300660 /* trying to use hidden SSID with an old hostapd version */
661 if (wl->ssid_len == 0 && !bss_conf->hidden_ssid) {
662 wl1271_error("got a null SSID from beacon/bss");
Eliad Pellerc690ec82011-08-14 13:17:07 +0300663 ret = -EINVAL;
664 goto out;
665 }
666
667 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
668 if (!cmd) {
669 ret = -ENOMEM;
670 goto out;
671 }
672
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300673 ret = wl12xx_allocate_link(wl, &wl->ap_global_hlid);
674 if (ret < 0)
675 goto out_free;
676
677 ret = wl12xx_allocate_link(wl, &wl->ap_bcast_hlid);
678 if (ret < 0)
679 goto out_free_global;
680
Eliad Pellerc690ec82011-08-14 13:17:07 +0300681 cmd->role_id = wl->role_id;
682 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
683 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300684 cmd->ap.global_hlid = wl->ap_global_hlid;
685 cmd->ap.broadcast_hlid = wl->ap_bcast_hlid;
Eliad Peller87fbcb02011-10-05 11:55:41 +0200686 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300687 cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
688 cmd->ap.dtim_interval = bss_conf->dtim_period;
689 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
690 cmd->channel = wl->channel;
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300691
692 if (!bss_conf->hidden_ssid) {
693 /* take the SSID from the beacon for backward compatibility */
694 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
695 cmd->ap.ssid_len = wl->ssid_len;
696 memcpy(cmd->ap.ssid, wl->ssid, wl->ssid_len);
697 } else {
698 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
699 cmd->ap.ssid_len = bss_conf->ssid_len;
700 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
701 }
702
Eliad Pellerc690ec82011-08-14 13:17:07 +0300703 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
704
705 switch (wl->band) {
706 case IEEE80211_BAND_2GHZ:
707 cmd->band = RADIO_BAND_2_4GHZ;
708 break;
709 case IEEE80211_BAND_5GHZ:
710 cmd->band = RADIO_BAND_5GHZ;
711 break;
712 default:
713 wl1271_warning("ap start - unknown band: %d", (int)wl->band);
714 cmd->band = RADIO_BAND_2_4GHZ;
715 break;
716 }
717
718 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
719 if (ret < 0) {
720 wl1271_error("failed to initiate cmd role start ap");
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300721 goto out_free_bcast;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300722 }
723
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300724 goto out_free;
725
726out_free_bcast:
727 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
728
729out_free_global:
730 wl12xx_free_link(wl, &wl->ap_global_hlid);
731
Eliad Pellerc690ec82011-08-14 13:17:07 +0300732out_free:
733 kfree(cmd);
734
735out:
736 return ret;
737}
738
739int wl12xx_cmd_role_stop_ap(struct wl1271 *wl)
740{
741 struct wl12xx_cmd_role_stop *cmd;
742 int ret;
743
744 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
745 if (!cmd) {
746 ret = -ENOMEM;
747 goto out;
748 }
749
750 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wl->role_id);
751
752 cmd->role_id = wl->role_id;
753
754 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
755 if (ret < 0) {
756 wl1271_error("failed to initiate cmd role stop ap");
757 goto out_free;
758 }
759
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300760 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
761 wl12xx_free_link(wl, &wl->ap_global_hlid);
762
Eliad Pellerc690ec82011-08-14 13:17:07 +0300763out_free:
764 kfree(cmd);
765
766out:
767 return ret;
768}
769
Eliad Peller87fbcb02011-10-05 11:55:41 +0200770int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300771{
772 struct wl12xx_cmd_role_start *cmd;
773 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
774 int ret;
775
776 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
777 if (!cmd) {
778 ret = -ENOMEM;
779 goto out;
780 }
781
782 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wl->role_id);
783
784 cmd->role_id = wl->role_id;
785 if (wl->band == IEEE80211_BAND_5GHZ)
786 cmd->band = WL12XX_BAND_5GHZ;
787 cmd->channel = wl->channel;
Eliad Peller87fbcb02011-10-05 11:55:41 +0200788 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300789 cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
790 cmd->ibss.dtim_interval = bss_conf->dtim_period;
791 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
792 cmd->ibss.ssid_len = wl->ssid_len;
793 memcpy(cmd->ibss.ssid, wl->ssid, wl->ssid_len);
794 memcpy(cmd->ibss.bssid, wl->bssid, ETH_ALEN);
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200795 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300796
797 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
798 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
799 if (ret)
800 goto out_free;
801 }
802 cmd->ibss.hlid = wl->sta_hlid;
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200803 cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300804
805 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
806 "basic_rate_set: 0x%x, remote_rates: 0x%x",
807 wl->role_id, cmd->sta.hlid, cmd->sta.session,
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200808 wlvif->basic_rate_set, wlvif->rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300809
810 wl1271_debug(DEBUG_CMD, "wl->bssid = %pM", wl->bssid);
811
812 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
813 if (ret < 0) {
814 wl1271_error("failed to initiate cmd role enable");
815 goto err_hlid;
816 }
817
818 goto out_free;
819
820err_hlid:
821 /* clear links on error. */
822 wl12xx_free_link(wl, &wl->sta_hlid);
823
824out_free:
825 kfree(cmd);
826
827out:
828 return ret;
829}
830
Eliad Pellerc690ec82011-08-14 13:17:07 +0300831
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300832/**
833 * send test command to firmware
834 *
835 * @wl: wl struct
836 * @buf: buffer containing the command, with all headers, must work with dma
837 * @len: length of the buffer
838 * @answer: is answer needed
839 */
840int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
841{
842 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200843 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300844
845 wl1271_debug(DEBUG_CMD, "cmd test");
846
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200847 if (answer)
848 res_len = buf_len;
849
850 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300851
852 if (ret < 0) {
853 wl1271_warning("TEST command failed");
854 return ret;
855 }
856
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200857 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300858}
859
860/**
861 * read acx from firmware
862 *
863 * @wl: wl struct
864 * @id: acx id
865 * @buf: buffer for the response, including all headers, must work with dma
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300866 * @len: length of buf
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300867 */
868int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
869{
870 struct acx_header *acx = buf;
871 int ret;
872
873 wl1271_debug(DEBUG_CMD, "cmd interrogate");
874
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300875 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300876
877 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300878 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300879
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200880 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
881 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300882 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300883
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300884 return ret;
885}
886
887/**
888 * write acx value to firmware
889 *
890 * @wl: wl struct
891 * @id: acx id
892 * @buf: buffer containing acx, including all headers, must work with dma
893 * @len: length of buf
894 */
895int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
896{
897 struct acx_header *acx = buf;
898 int ret;
899
Eliad Pellerc91d0602011-08-25 18:10:57 +0300900 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300901
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300902 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300903
904 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300905 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300906
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200907 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300908 if (ret < 0) {
909 wl1271_warning("CONFIGURE command NOK");
910 return ret;
911 }
912
913 return 0;
914}
915
Luciano Coelho94210892009-12-11 15:40:55 +0200916int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300917{
918 struct cmd_enabledisable_path *cmd;
919 int ret;
920 u16 cmd_rx, cmd_tx;
921
922 wl1271_debug(DEBUG_CMD, "cmd data path");
923
924 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
925 if (!cmd) {
926 ret = -ENOMEM;
927 goto out;
928 }
929
Luciano Coelho94210892009-12-11 15:40:55 +0200930 /* the channel here is only used for calibration, so hardcoded to 1 */
931 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300932
933 if (enable) {
934 cmd_rx = CMD_ENABLE_RX;
935 cmd_tx = CMD_ENABLE_TX;
936 } else {
937 cmd_rx = CMD_DISABLE_RX;
938 cmd_tx = CMD_DISABLE_TX;
939 }
940
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200941 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300942 if (ret < 0) {
943 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200944 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300945 goto out;
946 }
947
948 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200949 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300950
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200951 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300952 if (ret < 0) {
953 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200954 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200955 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300956 }
957
958 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200959 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300960
961out:
962 kfree(cmd);
963 return ret;
964}
965
Eliad Pellerc8bde242011-02-02 09:59:35 +0200966int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300967{
968 struct wl1271_cmd_ps_params *ps_params = NULL;
969 int ret = 0;
970
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300971 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
972
973 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
974 if (!ps_params) {
975 ret = -ENOMEM;
976 goto out;
977 }
978
Eliad Pellerc690ec82011-08-14 13:17:07 +0300979 ps_params->role_id = wl->role_id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300980 ps_params->ps_mode = ps_mode;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300981
982 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200983 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300984 if (ret < 0) {
985 wl1271_error("cmd set_ps_mode failed");
986 goto out;
987 }
988
989out:
990 kfree(ps_params);
991 return ret;
992}
993
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300994int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300995 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300996{
997 struct wl1271_cmd_template_set *cmd;
998 int ret = 0;
999
1000 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
1001
1002 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1003 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1004
1005 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1006 if (!cmd) {
1007 ret = -ENOMEM;
1008 goto out;
1009 }
1010
1011 cmd->len = cpu_to_le16(buf_len);
1012 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001013 cmd->enabled_rates = cpu_to_le32(rates);
Arik Nemtsov1e05a812010-10-16 17:44:51 +02001014 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1015 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001016 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001017
1018 if (buf)
1019 memcpy(cmd->template_data, buf, buf_len);
1020
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001021 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001022 if (ret < 0) {
1023 wl1271_warning("cmd set_template failed: %d", ret);
1024 goto out_free;
1025 }
1026
1027out_free:
1028 kfree(cmd);
1029
1030out:
1031 return ret;
1032}
1033
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001034int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001035{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001036 struct sk_buff *skb = NULL;
1037 int size;
1038 void *ptr;
1039 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001040
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001041
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001042 if (wl->bss_type == BSS_TYPE_IBSS) {
1043 size = sizeof(struct wl12xx_null_data_template);
1044 ptr = NULL;
1045 } else {
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001046 skb = ieee80211_nullfunc_get(wl->hw,
1047 wl12xx_wlvif_to_vif(wlvif));
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001048 if (!skb)
1049 goto out;
1050 size = skb->len;
1051 ptr = skb->data;
1052 }
1053
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001054 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001055 wlvif->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001056
Kalle Valo899e6e62010-03-18 12:26:34 +02001057out:
1058 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001059 if (ret)
1060 wl1271_warning("cmd buld null data failed %d", ret);
1061
Kalle Valo899e6e62010-03-18 12:26:34 +02001062 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001063
1064}
1065
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001066int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1067 struct wl12xx_vif *wlvif)
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001068{
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001069 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001070 struct sk_buff *skb = NULL;
1071 int ret = -ENOMEM;
1072
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001073 skb = ieee80211_nullfunc_get(wl->hw, vif);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001074 if (!skb)
1075 goto out;
1076
1077 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1078 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001079 CMD_TEMPL_KLV_IDX_NULL_DATA,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001080 wlvif->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001081
1082out:
1083 dev_kfree_skb(skb);
1084 if (ret)
1085 wl1271_warning("cmd build klv null data failed %d", ret);
1086
1087 return ret;
1088
1089}
1090
Eliad Peller87fbcb02011-10-05 11:55:41 +02001091int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1092 u16 aid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001093{
Kalle Valo899e6e62010-03-18 12:26:34 +02001094 struct sk_buff *skb;
1095 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001096
Kalle Valo899e6e62010-03-18 12:26:34 +02001097 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1098 if (!skb)
1099 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001100
Kalle Valo899e6e62010-03-18 12:26:34 +02001101 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Eliad Peller87fbcb02011-10-05 11:55:41 +02001102 skb->len, 0, wlvif->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001103
Kalle Valo899e6e62010-03-18 12:26:34 +02001104out:
1105 dev_kfree_skb(skb);
1106 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001107}
1108
Kalle Valo818e3062010-03-18 12:26:35 +02001109int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1110 const u8 *ssid, size_t ssid_len,
1111 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001112{
Kalle Valo818e3062010-03-18 12:26:35 +02001113 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001114 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001115 u32 rate;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001116
Kalle Valo818e3062010-03-18 12:26:35 +02001117 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1118 ie, ie_len);
1119 if (!skb) {
1120 ret = -ENOMEM;
1121 goto out;
1122 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001123
Kalle Valo818e3062010-03-18 12:26:35 +02001124 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001125
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001126 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001127 if (band == IEEE80211_BAND_2GHZ)
1128 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001129 skb->data, skb->len, 0, rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001130 else
1131 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001132 skb->data, skb->len, 0, rate);
Kalle Valo818e3062010-03-18 12:26:35 +02001133
1134out:
1135 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001136 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001137}
1138
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001139struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1140 struct sk_buff *skb)
1141{
1142 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001143 u32 rate;
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001144
1145 if (!skb)
1146 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1147 if (!skb)
1148 goto out;
1149
1150 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1151
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001152 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001153 if (wl->band == IEEE80211_BAND_2GHZ)
1154 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001155 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001156 else
1157 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001158 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001159
1160 if (ret < 0)
1161 wl1271_error("Unable to set ap probe request template.");
1162
1163out:
1164 return skb;
1165}
1166
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001167int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1168 __be32 ip_addr)
Eliad Pellerc5312772010-12-09 11:31:27 +02001169{
1170 int ret;
1171 struct wl12xx_arp_rsp_template tmpl;
1172 struct ieee80211_hdr_3addr *hdr;
1173 struct arphdr *arp_hdr;
1174
1175 memset(&tmpl, 0, sizeof(tmpl));
1176
1177 /* mac80211 header */
1178 hdr = &tmpl.hdr;
1179 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1180 IEEE80211_STYPE_DATA |
1181 IEEE80211_FCTL_TODS);
1182 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1183 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1184 memset(hdr->addr3, 0xff, ETH_ALEN);
1185
1186 /* llc layer */
1187 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +01001188 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001189
1190 /* arp header */
1191 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +01001192 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1193 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001194 arp_hdr->ar_hln = ETH_ALEN;
1195 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +01001196 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +02001197
1198 /* arp payload */
1199 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1200 tmpl.sender_ip = ip_addr;
1201
1202 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1203 &tmpl, sizeof(tmpl), 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001204 wlvif->basic_rate);
Eliad Pellerc5312772010-12-09 11:31:27 +02001205
1206 return ret;
1207}
1208
Eliad Peller784f6942011-10-05 11:55:39 +02001209int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
Kalle Valo023e0822010-03-18 12:26:36 +02001210{
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001211 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Kalle Valo023e0822010-03-18 12:26:36 +02001212 struct ieee80211_qos_hdr template;
1213
1214 memset(&template, 0, sizeof(template));
1215
1216 memcpy(template.addr1, wl->bssid, ETH_ALEN);
Eliad Peller784f6942011-10-05 11:55:39 +02001217 memcpy(template.addr2, vif->addr, ETH_ALEN);
Kalle Valo023e0822010-03-18 12:26:36 +02001218 memcpy(template.addr3, wl->bssid, ETH_ALEN);
1219
1220 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1221 IEEE80211_STYPE_QOS_NULLFUNC |
1222 IEEE80211_FCTL_TODS);
1223
1224 /* FIXME: not sure what priority to use here */
1225 template.qos_ctrl = cpu_to_le16(0);
1226
1227 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001228 sizeof(template), 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001229 wlvif->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +02001230}
1231
Eliad Pellerc690ec82011-08-14 13:17:07 +03001232int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001233{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001234 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001235 int ret = 0;
1236
1237 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1238
1239 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1240 if (!cmd) {
1241 ret = -ENOMEM;
1242 goto out;
1243 }
1244
Eliad Pellerc690ec82011-08-14 13:17:07 +03001245 cmd->hlid = hlid;
1246 cmd->key_id = id;
1247 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001248 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001249 cmd->key_type = KEY_WEP;
1250
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001251 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001252 if (ret < 0) {
1253 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1254 goto out;
1255 }
1256
1257out:
1258 kfree(cmd);
1259
1260 return ret;
1261}
1262
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001263int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001264 u8 key_size, const u8 *key, const u8 *addr,
1265 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001266{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001267 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001268 int ret = 0;
1269
Eliad Pellerb42f0682011-08-14 13:17:24 +03001270 /* hlid might have already been deleted */
1271 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID)
1272 return 0;
1273
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001274 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1275 if (!cmd) {
1276 ret = -ENOMEM;
1277 goto out;
1278 }
1279
Eliad Pellerc690ec82011-08-14 13:17:07 +03001280 cmd->hlid = wl->sta_hlid;
1281
1282 if (key_type == KEY_WEP)
1283 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1284 else if (is_broadcast_ether_addr(addr))
1285 cmd->lid_key_type = BROADCAST_LID_TYPE;
1286 else
1287 cmd->lid_key_type = UNICAST_LID_TYPE;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001288
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001289 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001290 cmd->key_size = key_size;
1291 cmd->key_type = key_type;
1292
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001293 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1294 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001295
Eliad Pellerc690ec82011-08-14 13:17:07 +03001296 cmd->key_id = id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001297
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001298 if (key_type == KEY_TKIP) {
1299 /*
1300 * We get the key in the following form:
1301 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1302 * but the target is expecting:
1303 * TKIP - RX MIC - TX MIC
1304 */
1305 memcpy(cmd->key, key, 16);
1306 memcpy(cmd->key + 16, key + 24, 8);
1307 memcpy(cmd->key + 24, key + 16, 8);
1308
1309 } else {
1310 memcpy(cmd->key, key, key_size);
1311 }
1312
1313 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1314
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001315 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001316 if (ret < 0) {
1317 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +02001318 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001319 }
1320
1321out:
1322 kfree(cmd);
1323
1324 return ret;
1325}
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001326
Eliad Pellerc690ec82011-08-14 13:17:07 +03001327/*
1328 * TODO: merge with sta/ibss into 1 set_key function.
1329 * note there are slight diffs
1330 */
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001331int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1332 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1333 u16 tx_seq_16)
1334{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001335 struct wl1271_cmd_set_keys *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001336 int ret = 0;
1337 u8 lid_type;
1338
1339 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1340 if (!cmd)
1341 return -ENOMEM;
1342
Arik Nemtsove51ae9b2011-08-14 13:17:21 +03001343 if (hlid == wl->ap_bcast_hlid) {
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001344 if (key_type == KEY_WEP)
1345 lid_type = WEP_DEFAULT_LID_TYPE;
1346 else
1347 lid_type = BROADCAST_LID_TYPE;
1348 } else {
1349 lid_type = UNICAST_LID_TYPE;
1350 }
1351
1352 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1353 " hlid: %d", (int)action, (int)id, (int)lid_type,
1354 (int)key_type, (int)hlid);
1355
1356 cmd->lid_key_type = lid_type;
1357 cmd->hlid = hlid;
1358 cmd->key_action = cpu_to_le16(action);
1359 cmd->key_size = key_size;
1360 cmd->key_type = key_type;
1361 cmd->key_id = id;
1362 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1363 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1364
1365 if (key_type == KEY_TKIP) {
1366 /*
1367 * We get the key in the following form:
1368 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1369 * but the target is expecting:
1370 * TKIP - RX MIC - TX MIC
1371 */
1372 memcpy(cmd->key, key, 16);
1373 memcpy(cmd->key + 16, key + 24, 8);
1374 memcpy(cmd->key + 24, key + 16, 8);
1375 } else {
1376 memcpy(cmd->key, key, key_size);
1377 }
1378
1379 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1380
1381 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1382 if (ret < 0) {
1383 wl1271_warning("could not set ap keys");
1384 goto out;
1385 }
1386
1387out:
1388 kfree(cmd);
1389 return ret;
1390}
1391
Eliad Pellerb67476e2011-08-14 13:17:23 +03001392int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001393{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001394 struct wl12xx_cmd_set_peer_state *cmd;
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001395 int ret = 0;
1396
Eliad Pellerb67476e2011-08-14 13:17:23 +03001397 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001398
1399 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1400 if (!cmd) {
1401 ret = -ENOMEM;
1402 goto out;
1403 }
1404
Eliad Pellerb67476e2011-08-14 13:17:23 +03001405 cmd->hlid = hlid;
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001406 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1407
Eliad Pellerc690ec82011-08-14 13:17:07 +03001408 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001409 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001410 wl1271_error("failed to send set peer state command");
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001411 goto out_free;
1412 }
1413
1414out_free:
1415 kfree(cmd);
1416
1417out:
1418 return ret;
1419}
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001420
Eliad Pellerc690ec82011-08-14 13:17:07 +03001421int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001422{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001423 struct wl12xx_cmd_add_peer *cmd;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001424 int i, ret;
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001425 u32 sta_rates;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001426
Eliad Pellerc690ec82011-08-14 13:17:07 +03001427 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001428
1429 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1430 if (!cmd) {
1431 ret = -ENOMEM;
1432 goto out;
1433 }
1434
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001435 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1436 cmd->bss_index = WL1271_AP_BSS_INDEX;
1437 cmd->aid = sta->aid;
1438 cmd->hlid = hlid;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001439 cmd->sp_len = sta->max_sp;
Arik Nemtsovb4d38db2011-06-27 23:58:46 +03001440 cmd->wmm = sta->wme ? 1 : 0;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001441
Eliad Peller1ec23f72011-08-25 14:26:54 +03001442 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1443 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1444 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1445 else
1446 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1447
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001448 sta_rates = sta->supp_rates[wl->band];
1449 if (sta->ht_cap.ht_supported)
1450 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1451
1452 cmd->supported_rates =
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001453 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1454 wl->band));
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001455
Eliad Peller1ec23f72011-08-25 14:26:54 +03001456 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1457 cmd->supported_rates, sta->uapsd_queues);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001458
Eliad Pellerc690ec82011-08-14 13:17:07 +03001459 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001460 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001461 wl1271_error("failed to initiate cmd add peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001462 goto out_free;
1463 }
1464
1465out_free:
1466 kfree(cmd);
1467
1468out:
1469 return ret;
1470}
1471
Eliad Pellerc690ec82011-08-14 13:17:07 +03001472int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001473{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001474 struct wl12xx_cmd_remove_peer *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001475 int ret;
1476
Eliad Pellerc690ec82011-08-14 13:17:07 +03001477 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001478
1479 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1480 if (!cmd) {
1481 ret = -ENOMEM;
1482 goto out;
1483 }
1484
1485 cmd->hlid = hlid;
1486 /* We never send a deauth, mac80211 is in charge of this */
1487 cmd->reason_opcode = 0;
1488 cmd->send_deauth_flag = 0;
1489
Eliad Pellerc690ec82011-08-14 13:17:07 +03001490 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001491 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001492 wl1271_error("failed to initiate cmd remove peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001493 goto out_free;
1494 }
1495
Arik Nemtsov05285cf2010-11-12 17:15:03 +02001496 /*
1497 * We are ok with a timeout here. The event is sometimes not sent
1498 * due to a firmware bug.
1499 */
Eliad Pellerc690ec82011-08-14 13:17:07 +03001500 wl1271_cmd_wait_for_event_or_timeout(wl,
1501 PEER_REMOVE_COMPLETE_EVENT_ID);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001502
1503out_free:
1504 kfree(cmd);
1505
1506out:
1507 return ret;
1508}
Ido Yariv95dac04f2011-06-06 14:57:06 +03001509
1510int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1511{
1512 struct wl12xx_cmd_config_fwlog *cmd;
1513 int ret = 0;
1514
1515 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1516
1517 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1518 if (!cmd) {
1519 ret = -ENOMEM;
1520 goto out;
1521 }
1522
1523 cmd->logger_mode = wl->conf.fwlog.mode;
1524 cmd->log_severity = wl->conf.fwlog.severity;
1525 cmd->timestamp = wl->conf.fwlog.timestamp;
1526 cmd->output = wl->conf.fwlog.output;
1527 cmd->threshold = wl->conf.fwlog.threshold;
1528
1529 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1530 if (ret < 0) {
1531 wl1271_error("failed to send config firmware logger command");
1532 goto out_free;
1533 }
1534
1535out_free:
1536 kfree(cmd);
1537
1538out:
1539 return ret;
1540}
1541
1542int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1543{
1544 struct wl12xx_cmd_start_fwlog *cmd;
1545 int ret = 0;
1546
1547 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1548
1549 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1550 if (!cmd) {
1551 ret = -ENOMEM;
1552 goto out;
1553 }
1554
1555 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1556 if (ret < 0) {
1557 wl1271_error("failed to send start firmware logger command");
1558 goto out_free;
1559 }
1560
1561out_free:
1562 kfree(cmd);
1563
1564out:
1565 return ret;
1566}
1567
1568int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1569{
1570 struct wl12xx_cmd_stop_fwlog *cmd;
1571 int ret = 0;
1572
1573 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1574
1575 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1576 if (!cmd) {
1577 ret = -ENOMEM;
1578 goto out;
1579 }
1580
1581 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1582 if (ret < 0) {
1583 wl1271_error("failed to send stop firmware logger command");
1584 goto out_free;
1585 }
1586
1587out_free:
1588 kfree(cmd);
1589
1590out:
1591 return ret;
1592}
Eliad Pellera7cba382011-08-14 13:17:16 +03001593
1594static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1595{
1596 struct wl12xx_cmd_roc *cmd;
1597 int ret = 0;
1598
1599 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1600
1601 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1602 return -EINVAL;
1603
1604 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1605 if (!cmd) {
1606 ret = -ENOMEM;
1607 goto out;
1608 }
1609
1610 cmd->role_id = role_id;
1611 cmd->channel = wl->channel;
1612 switch (wl->band) {
1613 case IEEE80211_BAND_2GHZ:
1614 cmd->band = RADIO_BAND_2_4GHZ;
1615 break;
1616 case IEEE80211_BAND_5GHZ:
1617 cmd->band = RADIO_BAND_5GHZ;
1618 break;
1619 default:
1620 wl1271_error("roc - unknown band: %d", (int)wl->band);
1621 ret = -EINVAL;
1622 goto out_free;
1623 }
1624
1625
1626 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1627 if (ret < 0) {
1628 wl1271_error("failed to send ROC command");
1629 goto out_free;
1630 }
1631
1632out_free:
1633 kfree(cmd);
1634
1635out:
1636 return ret;
1637}
1638
1639static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1640{
1641 struct wl12xx_cmd_croc *cmd;
1642 int ret = 0;
1643
1644 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1645
1646 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1647 if (!cmd) {
1648 ret = -ENOMEM;
1649 goto out;
1650 }
1651 cmd->role_id = role_id;
1652
1653 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1654 sizeof(*cmd), 0);
1655 if (ret < 0) {
1656 wl1271_error("failed to send ROC command");
1657 goto out_free;
1658 }
1659
1660out_free:
1661 kfree(cmd);
1662
1663out:
1664 return ret;
1665}
Eliad Peller251c1772011-08-14 13:17:17 +03001666
1667int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1668{
1669 int ret = 0;
1670
1671 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1672 return 0;
1673
1674 ret = wl12xx_cmd_roc(wl, role_id);
1675 if (ret < 0)
1676 goto out;
1677
1678 ret = wl1271_cmd_wait_for_event(wl,
1679 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1680 if (ret < 0) {
1681 wl1271_error("cmd roc event completion error");
1682 goto out;
1683 }
1684
1685 __set_bit(role_id, wl->roc_map);
1686out:
1687 return ret;
1688}
1689
1690int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1691{
1692 int ret = 0;
1693
1694 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1695 return 0;
1696
1697 ret = wl12xx_cmd_croc(wl, role_id);
1698 if (ret < 0)
1699 goto out;
1700
1701 __clear_bit(role_id, wl->roc_map);
1702out:
1703 return ret;
1704}
Shahar Levi6d158ff2011-09-08 13:01:33 +03001705
1706int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1707 struct ieee80211_channel_switch *ch_switch)
1708{
1709 struct wl12xx_cmd_channel_switch *cmd;
1710 int ret;
1711
1712 wl1271_debug(DEBUG_ACX, "cmd channel switch");
1713
1714 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1715 if (!cmd) {
1716 ret = -ENOMEM;
1717 goto out;
1718 }
1719
1720 cmd->channel = ch_switch->channel->hw_value;
1721 cmd->switch_time = ch_switch->count;
1722 cmd->tx_suspend = ch_switch->block_tx;
1723 cmd->flush = 0; /* this value is ignored by the FW */
1724
1725 ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1726 if (ret < 0) {
1727 wl1271_error("failed to send channel switch command");
1728 goto out_free;
1729 }
1730
1731out_free:
1732 kfree(cmd);
1733
1734out:
1735 return ret;
1736}
1737
1738int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1739{
1740 struct wl12xx_cmd_stop_channel_switch *cmd;
1741 int ret;
1742
1743 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1744
1745 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1746 if (!cmd) {
1747 ret = -ENOMEM;
1748 goto out;
1749 }
1750
1751 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1752 if (ret < 0) {
1753 wl1271_error("failed to stop channel switch command");
1754 goto out_free;
1755 }
1756
1757out_free:
1758 kfree(cmd);
1759
1760out:
1761 return ret;
1762}