blob: b9bb76b22f58931abb5007454a686295c9c3272f [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 Yarivbaacb9ae2011-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 Yarivbaacb9ae2011-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{
Eliad Pellercdf09492011-10-05 11:55:44 +0200560 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300561 struct wl12xx_cmd_role_start *cmd;
562 int ret;
563
564 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
565 if (!cmd) {
566 ret = -ENOMEM;
567 goto out;
568 }
569
570 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wl->role_id);
571
572 cmd->role_id = wl->role_id;
573 if (wl->band == IEEE80211_BAND_5GHZ)
574 cmd->band = WL12XX_BAND_5GHZ;
575 cmd->channel = wl->channel;
Eliad Peller87fbcb02011-10-05 11:55:41 +0200576 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300577 cmd->sta.beacon_interval = cpu_to_le16(wl->beacon_int);
578 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
579 cmd->sta.ssid_len = wl->ssid_len;
580 memcpy(cmd->sta.ssid, wl->ssid, wl->ssid_len);
Eliad Pellercdf09492011-10-05 11:55:44 +0200581 memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200582 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300583
584 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
585 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
586 if (ret)
587 goto out_free;
588 }
589 cmd->sta.hlid = wl->sta_hlid;
Arik Nemtsov712e9bf2011-08-14 13:17:20 +0300590 cmd->sta.session = wl12xx_get_new_session_id(wl);
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200591 cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300592
593 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
594 "basic_rate_set: 0x%x, remote_rates: 0x%x",
595 wl->role_id, cmd->sta.hlid, cmd->sta.session,
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200596 wlvif->basic_rate_set, wlvif->rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300597
598 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
599 if (ret < 0) {
600 wl1271_error("failed to initiate cmd role start sta");
601 goto err_hlid;
602 }
603
604 goto out_free;
605
606err_hlid:
607 /* clear links on error. */
608 wl12xx_free_link(wl, &wl->sta_hlid);
609
610out_free:
611 kfree(cmd);
612
613out:
614 return ret;
615}
616
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300617/* use this function to stop ibss as well */
Eliad Pellerc690ec82011-08-14 13:17:07 +0300618int wl12xx_cmd_role_stop_sta(struct wl1271 *wl)
619{
620 struct wl12xx_cmd_role_stop *cmd;
621 int ret;
622
623 if (WARN_ON(wl->sta_hlid == WL12XX_INVALID_LINK_ID))
624 return -EINVAL;
625
626 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
627 if (!cmd) {
628 ret = -ENOMEM;
629 goto out;
630 }
631
632 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wl->role_id);
633
634 cmd->role_id = wl->role_id;
635 cmd->disc_type = DISCONNECT_IMMEDIATE;
636 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
637
638 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
639 if (ret < 0) {
640 wl1271_error("failed to initiate cmd role stop sta");
641 goto out_free;
642 }
643
Eliad Pellerc690ec82011-08-14 13:17:07 +0300644 wl12xx_free_link(wl, &wl->sta_hlid);
645
646out_free:
647 kfree(cmd);
648
649out:
650 return ret;
651}
652
Eliad Peller87fbcb02011-10-05 11:55:41 +0200653int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Pellerc690ec82011-08-14 13:17:07 +0300654{
655 struct wl12xx_cmd_role_start *cmd;
656 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
657 int ret;
658
659 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wl->role_id);
660
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300661 /* trying to use hidden SSID with an old hostapd version */
662 if (wl->ssid_len == 0 && !bss_conf->hidden_ssid) {
663 wl1271_error("got a null SSID from beacon/bss");
Eliad Pellerc690ec82011-08-14 13:17:07 +0300664 ret = -EINVAL;
665 goto out;
666 }
667
668 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
669 if (!cmd) {
670 ret = -ENOMEM;
671 goto out;
672 }
673
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300674 ret = wl12xx_allocate_link(wl, &wl->ap_global_hlid);
675 if (ret < 0)
676 goto out_free;
677
678 ret = wl12xx_allocate_link(wl, &wl->ap_bcast_hlid);
679 if (ret < 0)
680 goto out_free_global;
681
Eliad Pellerc690ec82011-08-14 13:17:07 +0300682 cmd->role_id = wl->role_id;
683 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
684 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300685 cmd->ap.global_hlid = wl->ap_global_hlid;
686 cmd->ap.broadcast_hlid = wl->ap_bcast_hlid;
Eliad Peller87fbcb02011-10-05 11:55:41 +0200687 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300688 cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
689 cmd->ap.dtim_interval = bss_conf->dtim_period;
690 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
691 cmd->channel = wl->channel;
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300692
693 if (!bss_conf->hidden_ssid) {
694 /* take the SSID from the beacon for backward compatibility */
695 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
696 cmd->ap.ssid_len = wl->ssid_len;
697 memcpy(cmd->ap.ssid, wl->ssid, wl->ssid_len);
698 } else {
699 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
700 cmd->ap.ssid_len = bss_conf->ssid_len;
701 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
702 }
703
Eliad Pellerc690ec82011-08-14 13:17:07 +0300704 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
705
706 switch (wl->band) {
707 case IEEE80211_BAND_2GHZ:
708 cmd->band = RADIO_BAND_2_4GHZ;
709 break;
710 case IEEE80211_BAND_5GHZ:
711 cmd->band = RADIO_BAND_5GHZ;
712 break;
713 default:
714 wl1271_warning("ap start - unknown band: %d", (int)wl->band);
715 cmd->band = RADIO_BAND_2_4GHZ;
716 break;
717 }
718
719 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
720 if (ret < 0) {
721 wl1271_error("failed to initiate cmd role start ap");
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300722 goto out_free_bcast;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300723 }
724
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300725 goto out_free;
726
727out_free_bcast:
728 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
729
730out_free_global:
731 wl12xx_free_link(wl, &wl->ap_global_hlid);
732
Eliad Pellerc690ec82011-08-14 13:17:07 +0300733out_free:
734 kfree(cmd);
735
736out:
737 return ret;
738}
739
740int wl12xx_cmd_role_stop_ap(struct wl1271 *wl)
741{
742 struct wl12xx_cmd_role_stop *cmd;
743 int ret;
744
745 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
746 if (!cmd) {
747 ret = -ENOMEM;
748 goto out;
749 }
750
751 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wl->role_id);
752
753 cmd->role_id = wl->role_id;
754
755 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
756 if (ret < 0) {
757 wl1271_error("failed to initiate cmd role stop ap");
758 goto out_free;
759 }
760
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300761 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
762 wl12xx_free_link(wl, &wl->ap_global_hlid);
763
Eliad Pellerc690ec82011-08-14 13:17:07 +0300764out_free:
765 kfree(cmd);
766
767out:
768 return ret;
769}
770
Eliad Peller87fbcb02011-10-05 11:55:41 +0200771int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300772{
Eliad Pellercdf09492011-10-05 11:55:44 +0200773 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300774 struct wl12xx_cmd_role_start *cmd;
775 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
776 int ret;
777
778 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
779 if (!cmd) {
780 ret = -ENOMEM;
781 goto out;
782 }
783
784 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wl->role_id);
785
786 cmd->role_id = wl->role_id;
787 if (wl->band == IEEE80211_BAND_5GHZ)
788 cmd->band = WL12XX_BAND_5GHZ;
789 cmd->channel = wl->channel;
Eliad Peller87fbcb02011-10-05 11:55:41 +0200790 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300791 cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
792 cmd->ibss.dtim_interval = bss_conf->dtim_period;
793 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
794 cmd->ibss.ssid_len = wl->ssid_len;
795 memcpy(cmd->ibss.ssid, wl->ssid, wl->ssid_len);
Eliad Pellercdf09492011-10-05 11:55:44 +0200796 memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200797 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300798
799 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
800 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
801 if (ret)
802 goto out_free;
803 }
804 cmd->ibss.hlid = wl->sta_hlid;
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200805 cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300806
807 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
808 "basic_rate_set: 0x%x, remote_rates: 0x%x",
809 wl->role_id, cmd->sta.hlid, cmd->sta.session,
Eliad Peller30d0c8f2011-10-05 11:55:42 +0200810 wlvif->basic_rate_set, wlvif->rate_set);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300811
Eliad Pellercdf09492011-10-05 11:55:44 +0200812 wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
813 vif->bss_conf.bssid);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300814
815 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
816 if (ret < 0) {
817 wl1271_error("failed to initiate cmd role enable");
818 goto err_hlid;
819 }
820
821 goto out_free;
822
823err_hlid:
824 /* clear links on error. */
825 wl12xx_free_link(wl, &wl->sta_hlid);
826
827out_free:
828 kfree(cmd);
829
830out:
831 return ret;
832}
833
Eliad Pellerc690ec82011-08-14 13:17:07 +0300834
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300835/**
836 * send test command to firmware
837 *
838 * @wl: wl struct
839 * @buf: buffer containing the command, with all headers, must work with dma
840 * @len: length of the buffer
841 * @answer: is answer needed
842 */
843int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
844{
845 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200846 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300847
848 wl1271_debug(DEBUG_CMD, "cmd test");
849
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200850 if (answer)
851 res_len = buf_len;
852
853 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300854
855 if (ret < 0) {
856 wl1271_warning("TEST command failed");
857 return ret;
858 }
859
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200860 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300861}
862
863/**
864 * read acx from firmware
865 *
866 * @wl: wl struct
867 * @id: acx id
868 * @buf: buffer for the response, including all headers, must work with dma
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300869 * @len: length of buf
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300870 */
871int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
872{
873 struct acx_header *acx = buf;
874 int ret;
875
876 wl1271_debug(DEBUG_CMD, "cmd interrogate");
877
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300878 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300879
880 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300881 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300882
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200883 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
884 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300885 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300886
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300887 return ret;
888}
889
890/**
891 * write acx value to firmware
892 *
893 * @wl: wl struct
894 * @id: acx id
895 * @buf: buffer containing acx, including all headers, must work with dma
896 * @len: length of buf
897 */
898int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
899{
900 struct acx_header *acx = buf;
901 int ret;
902
Eliad Pellerc91d0602011-08-25 18:10:57 +0300903 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300904
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300905 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300906
907 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300908 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300909
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200910 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300911 if (ret < 0) {
912 wl1271_warning("CONFIGURE command NOK");
913 return ret;
914 }
915
916 return 0;
917}
918
Luciano Coelho94210892009-12-11 15:40:55 +0200919int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300920{
921 struct cmd_enabledisable_path *cmd;
922 int ret;
923 u16 cmd_rx, cmd_tx;
924
925 wl1271_debug(DEBUG_CMD, "cmd data path");
926
927 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
928 if (!cmd) {
929 ret = -ENOMEM;
930 goto out;
931 }
932
Luciano Coelho94210892009-12-11 15:40:55 +0200933 /* the channel here is only used for calibration, so hardcoded to 1 */
934 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300935
936 if (enable) {
937 cmd_rx = CMD_ENABLE_RX;
938 cmd_tx = CMD_ENABLE_TX;
939 } else {
940 cmd_rx = CMD_DISABLE_RX;
941 cmd_tx = CMD_DISABLE_TX;
942 }
943
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200944 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300945 if (ret < 0) {
946 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200947 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300948 goto out;
949 }
950
951 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200952 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300953
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200954 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300955 if (ret < 0) {
956 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200957 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200958 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300959 }
960
961 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200962 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300963
964out:
965 kfree(cmd);
966 return ret;
967}
968
Eliad Pellerc8bde242011-02-02 09:59:35 +0200969int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300970{
971 struct wl1271_cmd_ps_params *ps_params = NULL;
972 int ret = 0;
973
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300974 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
975
976 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
977 if (!ps_params) {
978 ret = -ENOMEM;
979 goto out;
980 }
981
Eliad Pellerc690ec82011-08-14 13:17:07 +0300982 ps_params->role_id = wl->role_id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300983 ps_params->ps_mode = ps_mode;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300984
985 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200986 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300987 if (ret < 0) {
988 wl1271_error("cmd set_ps_mode failed");
989 goto out;
990 }
991
992out:
993 kfree(ps_params);
994 return ret;
995}
996
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300997int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300998 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300999{
1000 struct wl1271_cmd_template_set *cmd;
1001 int ret = 0;
1002
1003 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
1004
1005 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1006 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1007
1008 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1009 if (!cmd) {
1010 ret = -ENOMEM;
1011 goto out;
1012 }
1013
1014 cmd->len = cpu_to_le16(buf_len);
1015 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001016 cmd->enabled_rates = cpu_to_le32(rates);
Arik Nemtsov1e05a812010-10-16 17:44:51 +02001017 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1018 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001019 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001020
1021 if (buf)
1022 memcpy(cmd->template_data, buf, buf_len);
1023
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001024 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001025 if (ret < 0) {
1026 wl1271_warning("cmd set_template failed: %d", ret);
1027 goto out_free;
1028 }
1029
1030out_free:
1031 kfree(cmd);
1032
1033out:
1034 return ret;
1035}
1036
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001037int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001038{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001039 struct sk_buff *skb = NULL;
1040 int size;
1041 void *ptr;
1042 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001043
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001044
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001045 if (wl->bss_type == BSS_TYPE_IBSS) {
1046 size = sizeof(struct wl12xx_null_data_template);
1047 ptr = NULL;
1048 } else {
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001049 skb = ieee80211_nullfunc_get(wl->hw,
1050 wl12xx_wlvif_to_vif(wlvif));
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001051 if (!skb)
1052 goto out;
1053 size = skb->len;
1054 ptr = skb->data;
1055 }
1056
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001057 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001058 wlvif->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001059
Kalle Valo899e6e62010-03-18 12:26:34 +02001060out:
1061 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001062 if (ret)
1063 wl1271_warning("cmd buld null data failed %d", ret);
1064
Kalle Valo899e6e62010-03-18 12:26:34 +02001065 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001066
1067}
1068
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001069int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1070 struct wl12xx_vif *wlvif)
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001071{
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001072 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001073 struct sk_buff *skb = NULL;
1074 int ret = -ENOMEM;
1075
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001076 skb = ieee80211_nullfunc_get(wl->hw, vif);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001077 if (!skb)
1078 goto out;
1079
1080 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1081 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001082 CMD_TEMPL_KLV_IDX_NULL_DATA,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001083 wlvif->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001084
1085out:
1086 dev_kfree_skb(skb);
1087 if (ret)
1088 wl1271_warning("cmd build klv null data failed %d", ret);
1089
1090 return ret;
1091
1092}
1093
Eliad Peller87fbcb02011-10-05 11:55:41 +02001094int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1095 u16 aid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001096{
Kalle Valo899e6e62010-03-18 12:26:34 +02001097 struct sk_buff *skb;
1098 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001099
Kalle Valo899e6e62010-03-18 12:26:34 +02001100 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1101 if (!skb)
1102 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001103
Kalle Valo899e6e62010-03-18 12:26:34 +02001104 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Eliad Peller87fbcb02011-10-05 11:55:41 +02001105 skb->len, 0, wlvif->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001106
Kalle Valo899e6e62010-03-18 12:26:34 +02001107out:
1108 dev_kfree_skb(skb);
1109 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001110}
1111
Kalle Valo818e3062010-03-18 12:26:35 +02001112int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1113 const u8 *ssid, size_t ssid_len,
1114 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001115{
Kalle Valo818e3062010-03-18 12:26:35 +02001116 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001117 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001118 u32 rate;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001119
Kalle Valo818e3062010-03-18 12:26:35 +02001120 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1121 ie, ie_len);
1122 if (!skb) {
1123 ret = -ENOMEM;
1124 goto out;
1125 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001126
Kalle Valo818e3062010-03-18 12:26:35 +02001127 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001128
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001129 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001130 if (band == IEEE80211_BAND_2GHZ)
1131 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001132 skb->data, skb->len, 0, rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001133 else
1134 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001135 skb->data, skb->len, 0, rate);
Kalle Valo818e3062010-03-18 12:26:35 +02001136
1137out:
1138 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001139 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001140}
1141
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001142struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1143 struct sk_buff *skb)
1144{
1145 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001146 u32 rate;
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001147
1148 if (!skb)
1149 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1150 if (!skb)
1151 goto out;
1152
1153 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1154
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001155 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001156 if (wl->band == IEEE80211_BAND_2GHZ)
1157 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001158 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001159 else
1160 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001161 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001162
1163 if (ret < 0)
1164 wl1271_error("Unable to set ap probe request template.");
1165
1166out:
1167 return skb;
1168}
1169
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001170int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1171 __be32 ip_addr)
Eliad Pellerc5312772010-12-09 11:31:27 +02001172{
1173 int ret;
1174 struct wl12xx_arp_rsp_template tmpl;
1175 struct ieee80211_hdr_3addr *hdr;
1176 struct arphdr *arp_hdr;
1177
1178 memset(&tmpl, 0, sizeof(tmpl));
1179
1180 /* mac80211 header */
1181 hdr = &tmpl.hdr;
1182 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1183 IEEE80211_STYPE_DATA |
1184 IEEE80211_FCTL_TODS);
1185 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1186 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1187 memset(hdr->addr3, 0xff, ETH_ALEN);
1188
1189 /* llc layer */
1190 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +01001191 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001192
1193 /* arp header */
1194 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +01001195 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1196 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001197 arp_hdr->ar_hln = ETH_ALEN;
1198 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +01001199 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +02001200
1201 /* arp payload */
1202 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1203 tmpl.sender_ip = ip_addr;
1204
1205 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1206 &tmpl, sizeof(tmpl), 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001207 wlvif->basic_rate);
Eliad Pellerc5312772010-12-09 11:31:27 +02001208
1209 return ret;
1210}
1211
Eliad Peller784f6942011-10-05 11:55:39 +02001212int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
Kalle Valo023e0822010-03-18 12:26:36 +02001213{
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001214 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Kalle Valo023e0822010-03-18 12:26:36 +02001215 struct ieee80211_qos_hdr template;
1216
1217 memset(&template, 0, sizeof(template));
1218
Eliad Pellercdf09492011-10-05 11:55:44 +02001219 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
Eliad Peller784f6942011-10-05 11:55:39 +02001220 memcpy(template.addr2, vif->addr, ETH_ALEN);
Eliad Pellercdf09492011-10-05 11:55:44 +02001221 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
Kalle Valo023e0822010-03-18 12:26:36 +02001222
1223 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1224 IEEE80211_STYPE_QOS_NULLFUNC |
1225 IEEE80211_FCTL_TODS);
1226
1227 /* FIXME: not sure what priority to use here */
1228 template.qos_ctrl = cpu_to_le16(0);
1229
1230 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001231 sizeof(template), 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001232 wlvif->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +02001233}
1234
Eliad Pellerc690ec82011-08-14 13:17:07 +03001235int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001236{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001237 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001238 int ret = 0;
1239
1240 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1241
1242 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1243 if (!cmd) {
1244 ret = -ENOMEM;
1245 goto out;
1246 }
1247
Eliad Pellerc690ec82011-08-14 13:17:07 +03001248 cmd->hlid = hlid;
1249 cmd->key_id = id;
1250 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001251 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001252 cmd->key_type = KEY_WEP;
1253
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001254 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001255 if (ret < 0) {
1256 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1257 goto out;
1258 }
1259
1260out:
1261 kfree(cmd);
1262
1263 return ret;
1264}
1265
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001266int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001267 u8 key_size, const u8 *key, const u8 *addr,
1268 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001269{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001270 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001271 int ret = 0;
1272
Eliad Pellerb42f0682011-08-14 13:17:24 +03001273 /* hlid might have already been deleted */
1274 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID)
1275 return 0;
1276
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001277 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1278 if (!cmd) {
1279 ret = -ENOMEM;
1280 goto out;
1281 }
1282
Eliad Pellerc690ec82011-08-14 13:17:07 +03001283 cmd->hlid = wl->sta_hlid;
1284
1285 if (key_type == KEY_WEP)
1286 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1287 else if (is_broadcast_ether_addr(addr))
1288 cmd->lid_key_type = BROADCAST_LID_TYPE;
1289 else
1290 cmd->lid_key_type = UNICAST_LID_TYPE;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001291
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001292 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001293 cmd->key_size = key_size;
1294 cmd->key_type = key_type;
1295
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001296 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1297 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001298
Eliad Pellerc690ec82011-08-14 13:17:07 +03001299 cmd->key_id = id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001300
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001301 if (key_type == KEY_TKIP) {
1302 /*
1303 * We get the key in the following form:
1304 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1305 * but the target is expecting:
1306 * TKIP - RX MIC - TX MIC
1307 */
1308 memcpy(cmd->key, key, 16);
1309 memcpy(cmd->key + 16, key + 24, 8);
1310 memcpy(cmd->key + 24, key + 16, 8);
1311
1312 } else {
1313 memcpy(cmd->key, key, key_size);
1314 }
1315
1316 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1317
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001318 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001319 if (ret < 0) {
1320 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +02001321 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001322 }
1323
1324out:
1325 kfree(cmd);
1326
1327 return ret;
1328}
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001329
Eliad Pellerc690ec82011-08-14 13:17:07 +03001330/*
1331 * TODO: merge with sta/ibss into 1 set_key function.
1332 * note there are slight diffs
1333 */
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001334int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1335 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1336 u16 tx_seq_16)
1337{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001338 struct wl1271_cmd_set_keys *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001339 int ret = 0;
1340 u8 lid_type;
1341
1342 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1343 if (!cmd)
1344 return -ENOMEM;
1345
Arik Nemtsove51ae9b2011-08-14 13:17:21 +03001346 if (hlid == wl->ap_bcast_hlid) {
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001347 if (key_type == KEY_WEP)
1348 lid_type = WEP_DEFAULT_LID_TYPE;
1349 else
1350 lid_type = BROADCAST_LID_TYPE;
1351 } else {
1352 lid_type = UNICAST_LID_TYPE;
1353 }
1354
1355 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1356 " hlid: %d", (int)action, (int)id, (int)lid_type,
1357 (int)key_type, (int)hlid);
1358
1359 cmd->lid_key_type = lid_type;
1360 cmd->hlid = hlid;
1361 cmd->key_action = cpu_to_le16(action);
1362 cmd->key_size = key_size;
1363 cmd->key_type = key_type;
1364 cmd->key_id = id;
1365 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1366 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1367
1368 if (key_type == KEY_TKIP) {
1369 /*
1370 * We get the key in the following form:
1371 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1372 * but the target is expecting:
1373 * TKIP - RX MIC - TX MIC
1374 */
1375 memcpy(cmd->key, key, 16);
1376 memcpy(cmd->key + 16, key + 24, 8);
1377 memcpy(cmd->key + 24, key + 16, 8);
1378 } else {
1379 memcpy(cmd->key, key, key_size);
1380 }
1381
1382 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1383
1384 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1385 if (ret < 0) {
1386 wl1271_warning("could not set ap keys");
1387 goto out;
1388 }
1389
1390out:
1391 kfree(cmd);
1392 return ret;
1393}
1394
Eliad Pellerb67476e2011-08-14 13:17:23 +03001395int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001396{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001397 struct wl12xx_cmd_set_peer_state *cmd;
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001398 int ret = 0;
1399
Eliad Pellerb67476e2011-08-14 13:17:23 +03001400 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001401
1402 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1403 if (!cmd) {
1404 ret = -ENOMEM;
1405 goto out;
1406 }
1407
Eliad Pellerb67476e2011-08-14 13:17:23 +03001408 cmd->hlid = hlid;
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001409 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1410
Eliad Pellerc690ec82011-08-14 13:17:07 +03001411 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001412 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001413 wl1271_error("failed to send set peer state command");
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001414 goto out_free;
1415 }
1416
1417out_free:
1418 kfree(cmd);
1419
1420out:
1421 return ret;
1422}
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001423
Eliad Pellerc690ec82011-08-14 13:17:07 +03001424int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001425{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001426 struct wl12xx_cmd_add_peer *cmd;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001427 int i, ret;
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001428 u32 sta_rates;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001429
Eliad Pellerc690ec82011-08-14 13:17:07 +03001430 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001431
1432 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1433 if (!cmd) {
1434 ret = -ENOMEM;
1435 goto out;
1436 }
1437
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001438 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1439 cmd->bss_index = WL1271_AP_BSS_INDEX;
1440 cmd->aid = sta->aid;
1441 cmd->hlid = hlid;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001442 cmd->sp_len = sta->max_sp;
Arik Nemtsovb4d38db2011-06-27 23:58:46 +03001443 cmd->wmm = sta->wme ? 1 : 0;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001444
Eliad Peller1ec23f72011-08-25 14:26:54 +03001445 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1446 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1447 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1448 else
1449 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1450
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001451 sta_rates = sta->supp_rates[wl->band];
1452 if (sta->ht_cap.ht_supported)
1453 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1454
1455 cmd->supported_rates =
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001456 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1457 wl->band));
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001458
Eliad Peller1ec23f72011-08-25 14:26:54 +03001459 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1460 cmd->supported_rates, sta->uapsd_queues);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001461
Eliad Pellerc690ec82011-08-14 13:17:07 +03001462 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001463 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001464 wl1271_error("failed to initiate cmd add peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001465 goto out_free;
1466 }
1467
1468out_free:
1469 kfree(cmd);
1470
1471out:
1472 return ret;
1473}
1474
Eliad Pellerc690ec82011-08-14 13:17:07 +03001475int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001476{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001477 struct wl12xx_cmd_remove_peer *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001478 int ret;
1479
Eliad Pellerc690ec82011-08-14 13:17:07 +03001480 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001481
1482 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1483 if (!cmd) {
1484 ret = -ENOMEM;
1485 goto out;
1486 }
1487
1488 cmd->hlid = hlid;
1489 /* We never send a deauth, mac80211 is in charge of this */
1490 cmd->reason_opcode = 0;
1491 cmd->send_deauth_flag = 0;
1492
Eliad Pellerc690ec82011-08-14 13:17:07 +03001493 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001494 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001495 wl1271_error("failed to initiate cmd remove peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001496 goto out_free;
1497 }
1498
Arik Nemtsov05285cf2010-11-12 17:15:03 +02001499 /*
1500 * We are ok with a timeout here. The event is sometimes not sent
1501 * due to a firmware bug.
1502 */
Eliad Pellerc690ec82011-08-14 13:17:07 +03001503 wl1271_cmd_wait_for_event_or_timeout(wl,
1504 PEER_REMOVE_COMPLETE_EVENT_ID);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001505
1506out_free:
1507 kfree(cmd);
1508
1509out:
1510 return ret;
1511}
Ido Yariv95dac04f2011-06-06 14:57:06 +03001512
1513int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1514{
1515 struct wl12xx_cmd_config_fwlog *cmd;
1516 int ret = 0;
1517
1518 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1519
1520 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1521 if (!cmd) {
1522 ret = -ENOMEM;
1523 goto out;
1524 }
1525
1526 cmd->logger_mode = wl->conf.fwlog.mode;
1527 cmd->log_severity = wl->conf.fwlog.severity;
1528 cmd->timestamp = wl->conf.fwlog.timestamp;
1529 cmd->output = wl->conf.fwlog.output;
1530 cmd->threshold = wl->conf.fwlog.threshold;
1531
1532 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1533 if (ret < 0) {
1534 wl1271_error("failed to send config firmware logger command");
1535 goto out_free;
1536 }
1537
1538out_free:
1539 kfree(cmd);
1540
1541out:
1542 return ret;
1543}
1544
1545int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1546{
1547 struct wl12xx_cmd_start_fwlog *cmd;
1548 int ret = 0;
1549
1550 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1551
1552 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1553 if (!cmd) {
1554 ret = -ENOMEM;
1555 goto out;
1556 }
1557
1558 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1559 if (ret < 0) {
1560 wl1271_error("failed to send start firmware logger command");
1561 goto out_free;
1562 }
1563
1564out_free:
1565 kfree(cmd);
1566
1567out:
1568 return ret;
1569}
1570
1571int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1572{
1573 struct wl12xx_cmd_stop_fwlog *cmd;
1574 int ret = 0;
1575
1576 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1577
1578 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1579 if (!cmd) {
1580 ret = -ENOMEM;
1581 goto out;
1582 }
1583
1584 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1585 if (ret < 0) {
1586 wl1271_error("failed to send stop firmware logger command");
1587 goto out_free;
1588 }
1589
1590out_free:
1591 kfree(cmd);
1592
1593out:
1594 return ret;
1595}
Eliad Pellera7cba382011-08-14 13:17:16 +03001596
1597static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1598{
1599 struct wl12xx_cmd_roc *cmd;
1600 int ret = 0;
1601
1602 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1603
1604 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1605 return -EINVAL;
1606
1607 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1608 if (!cmd) {
1609 ret = -ENOMEM;
1610 goto out;
1611 }
1612
1613 cmd->role_id = role_id;
1614 cmd->channel = wl->channel;
1615 switch (wl->band) {
1616 case IEEE80211_BAND_2GHZ:
1617 cmd->band = RADIO_BAND_2_4GHZ;
1618 break;
1619 case IEEE80211_BAND_5GHZ:
1620 cmd->band = RADIO_BAND_5GHZ;
1621 break;
1622 default:
1623 wl1271_error("roc - unknown band: %d", (int)wl->band);
1624 ret = -EINVAL;
1625 goto out_free;
1626 }
1627
1628
1629 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1630 if (ret < 0) {
1631 wl1271_error("failed to send ROC command");
1632 goto out_free;
1633 }
1634
1635out_free:
1636 kfree(cmd);
1637
1638out:
1639 return ret;
1640}
1641
1642static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1643{
1644 struct wl12xx_cmd_croc *cmd;
1645 int ret = 0;
1646
1647 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1648
1649 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1650 if (!cmd) {
1651 ret = -ENOMEM;
1652 goto out;
1653 }
1654 cmd->role_id = role_id;
1655
1656 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1657 sizeof(*cmd), 0);
1658 if (ret < 0) {
1659 wl1271_error("failed to send ROC command");
1660 goto out_free;
1661 }
1662
1663out_free:
1664 kfree(cmd);
1665
1666out:
1667 return ret;
1668}
Eliad Peller251c1772011-08-14 13:17:17 +03001669
1670int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1671{
1672 int ret = 0;
1673
1674 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1675 return 0;
1676
1677 ret = wl12xx_cmd_roc(wl, role_id);
1678 if (ret < 0)
1679 goto out;
1680
1681 ret = wl1271_cmd_wait_for_event(wl,
1682 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1683 if (ret < 0) {
1684 wl1271_error("cmd roc event completion error");
1685 goto out;
1686 }
1687
1688 __set_bit(role_id, wl->roc_map);
1689out:
1690 return ret;
1691}
1692
1693int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1694{
1695 int ret = 0;
1696
1697 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1698 return 0;
1699
1700 ret = wl12xx_cmd_croc(wl, role_id);
1701 if (ret < 0)
1702 goto out;
1703
1704 __clear_bit(role_id, wl->roc_map);
1705out:
1706 return ret;
1707}
Shahar Levi6d158ff2011-09-08 13:01:33 +03001708
1709int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1710 struct ieee80211_channel_switch *ch_switch)
1711{
1712 struct wl12xx_cmd_channel_switch *cmd;
1713 int ret;
1714
1715 wl1271_debug(DEBUG_ACX, "cmd channel switch");
1716
1717 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1718 if (!cmd) {
1719 ret = -ENOMEM;
1720 goto out;
1721 }
1722
1723 cmd->channel = ch_switch->channel->hw_value;
1724 cmd->switch_time = ch_switch->count;
1725 cmd->tx_suspend = ch_switch->block_tx;
1726 cmd->flush = 0; /* this value is ignored by the FW */
1727
1728 ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1729 if (ret < 0) {
1730 wl1271_error("failed to send channel switch command");
1731 goto out_free;
1732 }
1733
1734out_free:
1735 kfree(cmd);
1736
1737out:
1738 return ret;
1739}
1740
1741int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1742{
1743 struct wl12xx_cmd_stop_channel_switch *cmd;
1744 int ret;
1745
1746 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1747
1748 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1749 if (!cmd) {
1750 ret = -ENOMEM;
1751 goto out;
1752 }
1753
1754 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1755 if (ret < 0) {
1756 wl1271_error("failed to stop channel switch command");
1757 goto out_free;
1758 }
1759
1760out_free:
1761 kfree(cmd);
1762
1763out:
1764 return ret;
1765}