blob: 36544ff6a79b301b30201db43111925a1321e0ad [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 Peller7edebf52011-10-05 11:55:52 +0200467int wl12xx_cmd_role_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Peller04e80792011-08-14 13:17:09 +0300468{
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
Eliad Peller7edebf52011-10-05 11:55:52 +0200478 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
Eliad Peller04e80792011-08-14 13:17:09 +0300479
Eliad Peller7edebf52011-10-05 11:55:52 +0200480 cmd->role_id = wlvif->dev_role_id;
Eliad Peller04e80792011-08-14 13:17:09 +0300481 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
Eliad Peller7edebf52011-10-05 11:55:52 +0200517int wl12xx_cmd_role_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Peller04e80792011-08-14 13:17:09 +0300518{
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
Eliad Peller7edebf52011-10-05 11:55:52 +0200533 cmd->role_id = wlvif->dev_role_id;
Eliad Peller04e80792011-08-14 13:17:09 +0300534 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
Eliad Peller0603d892011-10-05 11:55:51 +0200570 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300571
Eliad Peller0603d892011-10-05 11:55:51 +0200572 cmd->role_id = wlvif->role_id;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300573 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;
Eliad Peller1fe9f162011-10-05 11:55:48 +0200579 cmd->sta.ssid_len = wlvif->ssid_len;
580 memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->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",
Eliad Peller0603d892011-10-05 11:55:51 +0200595 wlvif->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 Peller0603d892011-10-05 11:55:51 +0200618int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Pellerc690ec82011-08-14 13:17:07 +0300619{
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
Eliad Peller0603d892011-10-05 11:55:51 +0200632 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300633
Eliad Peller0603d892011-10-05 11:55:51 +0200634 cmd->role_id = wlvif->role_id;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300635 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
Eliad Peller0603d892011-10-05 11:55:51 +0200659 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300660
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300661 /* trying to use hidden SSID with an old hostapd version */
Eliad Peller1fe9f162011-10-05 11:55:48 +0200662 if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300663 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 Peller0603d892011-10-05 11:55:51 +0200682 cmd->role_id = wlvif->role_id;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300683 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;
Eliad Peller1fe9f162011-10-05 11:55:48 +0200696 cmd->ap.ssid_len = wlvif->ssid_len;
697 memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300698 } 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
Eliad Peller0603d892011-10-05 11:55:51 +0200740int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Eliad Pellerc690ec82011-08-14 13:17:07 +0300741{
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
Eliad Peller0603d892011-10-05 11:55:51 +0200751 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300752
Eliad Peller0603d892011-10-05 11:55:51 +0200753 cmd->role_id = wlvif->role_id;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300754
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
Eliad Peller0603d892011-10-05 11:55:51 +0200784 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300785
Eliad Peller0603d892011-10-05 11:55:51 +0200786 cmd->role_id = wlvif->role_id;
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300787 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;
Eliad Peller1fe9f162011-10-05 11:55:48 +0200794 cmd->ibss.ssid_len = wlvif->ssid_len;
795 memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->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",
Eliad Peller0603d892011-10-05 11:55:51 +0200809 wlvif->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 Peller0603d892011-10-05 11:55:51 +0200969int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
970 u8 ps_mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300971{
972 struct wl1271_cmd_ps_params *ps_params = NULL;
973 int ret = 0;
974
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300975 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
976
977 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
978 if (!ps_params) {
979 ret = -ENOMEM;
980 goto out;
981 }
982
Eliad Peller0603d892011-10-05 11:55:51 +0200983 ps_params->role_id = wlvif->role_id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300984 ps_params->ps_mode = ps_mode;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300985
986 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200987 sizeof(*ps_params), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300988 if (ret < 0) {
989 wl1271_error("cmd set_ps_mode failed");
990 goto out;
991 }
992
993out:
994 kfree(ps_params);
995 return ret;
996}
997
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300998int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
Juuso Oikarinen606c1482010-04-01 11:38:21 +0300999 void *buf, size_t buf_len, int index, u32 rates)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001000{
1001 struct wl1271_cmd_template_set *cmd;
1002 int ret = 0;
1003
1004 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
1005
1006 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1007 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1008
1009 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1010 if (!cmd) {
1011 ret = -ENOMEM;
1012 goto out;
1013 }
1014
1015 cmd->len = cpu_to_le16(buf_len);
1016 cmd->template_type = template_id;
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001017 cmd->enabled_rates = cpu_to_le32(rates);
Arik Nemtsov1e05a812010-10-16 17:44:51 +02001018 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1019 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001020 cmd->index = index;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001021
1022 if (buf)
1023 memcpy(cmd->template_data, buf, buf_len);
1024
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001025 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001026 if (ret < 0) {
1027 wl1271_warning("cmd set_template failed: %d", ret);
1028 goto out_free;
1029 }
1030
1031out_free:
1032 kfree(cmd);
1033
1034out:
1035 return ret;
1036}
1037
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001038int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001039{
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001040 struct sk_buff *skb = NULL;
1041 int size;
1042 void *ptr;
1043 int ret = -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001044
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001045
Eliad Peller536129c2011-10-05 11:55:45 +02001046 if (wlvif->bss_type == BSS_TYPE_IBSS) {
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001047 size = sizeof(struct wl12xx_null_data_template);
1048 ptr = NULL;
1049 } else {
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001050 skb = ieee80211_nullfunc_get(wl->hw,
1051 wl12xx_wlvif_to_vif(wlvif));
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001052 if (!skb)
1053 goto out;
1054 size = skb->len;
1055 ptr = skb->data;
1056 }
1057
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001058 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001059 wlvif->basic_rate);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001060
Kalle Valo899e6e62010-03-18 12:26:34 +02001061out:
1062 dev_kfree_skb(skb);
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001063 if (ret)
1064 wl1271_warning("cmd buld null data failed %d", ret);
1065
Kalle Valo899e6e62010-03-18 12:26:34 +02001066 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001067
1068}
1069
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001070int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1071 struct wl12xx_vif *wlvif)
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001072{
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001073 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001074 struct sk_buff *skb = NULL;
1075 int ret = -ENOMEM;
1076
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001077 skb = ieee80211_nullfunc_get(wl->hw, vif);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001078 if (!skb)
1079 goto out;
1080
1081 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1082 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001083 CMD_TEMPL_KLV_IDX_NULL_DATA,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001084 wlvif->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001085
1086out:
1087 dev_kfree_skb(skb);
1088 if (ret)
1089 wl1271_warning("cmd build klv null data failed %d", ret);
1090
1091 return ret;
1092
1093}
1094
Eliad Peller87fbcb02011-10-05 11:55:41 +02001095int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1096 u16 aid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001097{
Kalle Valo899e6e62010-03-18 12:26:34 +02001098 struct sk_buff *skb;
1099 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001100
Kalle Valo899e6e62010-03-18 12:26:34 +02001101 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1102 if (!skb)
1103 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001104
Kalle Valo899e6e62010-03-18 12:26:34 +02001105 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Eliad Peller87fbcb02011-10-05 11:55:41 +02001106 skb->len, 0, wlvif->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001107
Kalle Valo899e6e62010-03-18 12:26:34 +02001108out:
1109 dev_kfree_skb(skb);
1110 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001111}
1112
Kalle Valo818e3062010-03-18 12:26:35 +02001113int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1114 const u8 *ssid, size_t ssid_len,
1115 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001116{
Kalle Valo818e3062010-03-18 12:26:35 +02001117 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001118 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001119 u32 rate;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001120
Kalle Valo818e3062010-03-18 12:26:35 +02001121 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1122 ie, ie_len);
1123 if (!skb) {
1124 ret = -ENOMEM;
1125 goto out;
1126 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001127
Kalle Valo818e3062010-03-18 12:26:35 +02001128 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001129
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001130 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001131 if (band == IEEE80211_BAND_2GHZ)
1132 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001133 skb->data, skb->len, 0, rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001134 else
1135 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001136 skb->data, skb->len, 0, rate);
Kalle Valo818e3062010-03-18 12:26:35 +02001137
1138out:
1139 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001140 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001141}
1142
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001143struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1144 struct sk_buff *skb)
1145{
1146 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001147 u32 rate;
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001148
1149 if (!skb)
1150 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1151 if (!skb)
1152 goto out;
1153
1154 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1155
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001156 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001157 if (wl->band == IEEE80211_BAND_2GHZ)
1158 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001159 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001160 else
1161 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001162 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001163
1164 if (ret < 0)
1165 wl1271_error("Unable to set ap probe request template.");
1166
1167out:
1168 return skb;
1169}
1170
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001171int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1172 __be32 ip_addr)
Eliad Pellerc5312772010-12-09 11:31:27 +02001173{
1174 int ret;
1175 struct wl12xx_arp_rsp_template tmpl;
1176 struct ieee80211_hdr_3addr *hdr;
1177 struct arphdr *arp_hdr;
1178
1179 memset(&tmpl, 0, sizeof(tmpl));
1180
1181 /* mac80211 header */
1182 hdr = &tmpl.hdr;
1183 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1184 IEEE80211_STYPE_DATA |
1185 IEEE80211_FCTL_TODS);
1186 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1187 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1188 memset(hdr->addr3, 0xff, ETH_ALEN);
1189
1190 /* llc layer */
1191 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +01001192 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001193
1194 /* arp header */
1195 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +01001196 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1197 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001198 arp_hdr->ar_hln = ETH_ALEN;
1199 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +01001200 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +02001201
1202 /* arp payload */
1203 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1204 tmpl.sender_ip = ip_addr;
1205
1206 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1207 &tmpl, sizeof(tmpl), 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001208 wlvif->basic_rate);
Eliad Pellerc5312772010-12-09 11:31:27 +02001209
1210 return ret;
1211}
1212
Eliad Peller784f6942011-10-05 11:55:39 +02001213int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
Kalle Valo023e0822010-03-18 12:26:36 +02001214{
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001215 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Kalle Valo023e0822010-03-18 12:26:36 +02001216 struct ieee80211_qos_hdr template;
1217
1218 memset(&template, 0, sizeof(template));
1219
Eliad Pellercdf09492011-10-05 11:55:44 +02001220 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
Eliad Peller784f6942011-10-05 11:55:39 +02001221 memcpy(template.addr2, vif->addr, ETH_ALEN);
Eliad Pellercdf09492011-10-05 11:55:44 +02001222 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
Kalle Valo023e0822010-03-18 12:26:36 +02001223
1224 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1225 IEEE80211_STYPE_QOS_NULLFUNC |
1226 IEEE80211_FCTL_TODS);
1227
1228 /* FIXME: not sure what priority to use here */
1229 template.qos_ctrl = cpu_to_le16(0);
1230
1231 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001232 sizeof(template), 0,
Eliad Pellerd2d66c52011-10-05 11:55:43 +02001233 wlvif->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +02001234}
1235
Eliad Pellerc690ec82011-08-14 13:17:07 +03001236int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001237{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001238 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001239 int ret = 0;
1240
1241 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1242
1243 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1244 if (!cmd) {
1245 ret = -ENOMEM;
1246 goto out;
1247 }
1248
Eliad Pellerc690ec82011-08-14 13:17:07 +03001249 cmd->hlid = hlid;
1250 cmd->key_id = id;
1251 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001252 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001253 cmd->key_type = KEY_WEP;
1254
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001255 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001256 if (ret < 0) {
1257 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1258 goto out;
1259 }
1260
1261out:
1262 kfree(cmd);
1263
1264 return ret;
1265}
1266
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001267int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001268 u8 key_size, const u8 *key, const u8 *addr,
1269 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001270{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001271 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001272 int ret = 0;
1273
Eliad Pellerb42f0682011-08-14 13:17:24 +03001274 /* hlid might have already been deleted */
1275 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID)
1276 return 0;
1277
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001278 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1279 if (!cmd) {
1280 ret = -ENOMEM;
1281 goto out;
1282 }
1283
Eliad Pellerc690ec82011-08-14 13:17:07 +03001284 cmd->hlid = wl->sta_hlid;
1285
1286 if (key_type == KEY_WEP)
1287 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1288 else if (is_broadcast_ether_addr(addr))
1289 cmd->lid_key_type = BROADCAST_LID_TYPE;
1290 else
1291 cmd->lid_key_type = UNICAST_LID_TYPE;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001292
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001293 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001294 cmd->key_size = key_size;
1295 cmd->key_type = key_type;
1296
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001297 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1298 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001299
Eliad Pellerc690ec82011-08-14 13:17:07 +03001300 cmd->key_id = id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001301
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001302 if (key_type == KEY_TKIP) {
1303 /*
1304 * We get the key in the following form:
1305 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1306 * but the target is expecting:
1307 * TKIP - RX MIC - TX MIC
1308 */
1309 memcpy(cmd->key, key, 16);
1310 memcpy(cmd->key + 16, key + 24, 8);
1311 memcpy(cmd->key + 24, key + 16, 8);
1312
1313 } else {
1314 memcpy(cmd->key, key, key_size);
1315 }
1316
1317 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1318
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001319 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001320 if (ret < 0) {
1321 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +02001322 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001323 }
1324
1325out:
1326 kfree(cmd);
1327
1328 return ret;
1329}
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001330
Eliad Pellerc690ec82011-08-14 13:17:07 +03001331/*
1332 * TODO: merge with sta/ibss into 1 set_key function.
1333 * note there are slight diffs
1334 */
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001335int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1336 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1337 u16 tx_seq_16)
1338{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001339 struct wl1271_cmd_set_keys *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001340 int ret = 0;
1341 u8 lid_type;
1342
1343 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1344 if (!cmd)
1345 return -ENOMEM;
1346
Arik Nemtsove51ae9b2011-08-14 13:17:21 +03001347 if (hlid == wl->ap_bcast_hlid) {
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001348 if (key_type == KEY_WEP)
1349 lid_type = WEP_DEFAULT_LID_TYPE;
1350 else
1351 lid_type = BROADCAST_LID_TYPE;
1352 } else {
1353 lid_type = UNICAST_LID_TYPE;
1354 }
1355
1356 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1357 " hlid: %d", (int)action, (int)id, (int)lid_type,
1358 (int)key_type, (int)hlid);
1359
1360 cmd->lid_key_type = lid_type;
1361 cmd->hlid = hlid;
1362 cmd->key_action = cpu_to_le16(action);
1363 cmd->key_size = key_size;
1364 cmd->key_type = key_type;
1365 cmd->key_id = id;
1366 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1367 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1368
1369 if (key_type == KEY_TKIP) {
1370 /*
1371 * We get the key in the following form:
1372 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1373 * but the target is expecting:
1374 * TKIP - RX MIC - TX MIC
1375 */
1376 memcpy(cmd->key, key, 16);
1377 memcpy(cmd->key + 16, key + 24, 8);
1378 memcpy(cmd->key + 24, key + 16, 8);
1379 } else {
1380 memcpy(cmd->key, key, key_size);
1381 }
1382
1383 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1384
1385 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1386 if (ret < 0) {
1387 wl1271_warning("could not set ap keys");
1388 goto out;
1389 }
1390
1391out:
1392 kfree(cmd);
1393 return ret;
1394}
1395
Eliad Pellerb67476e2011-08-14 13:17:23 +03001396int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001397{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001398 struct wl12xx_cmd_set_peer_state *cmd;
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001399 int ret = 0;
1400
Eliad Pellerb67476e2011-08-14 13:17:23 +03001401 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001402
1403 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1404 if (!cmd) {
1405 ret = -ENOMEM;
1406 goto out;
1407 }
1408
Eliad Pellerb67476e2011-08-14 13:17:23 +03001409 cmd->hlid = hlid;
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001410 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1411
Eliad Pellerc690ec82011-08-14 13:17:07 +03001412 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001413 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001414 wl1271_error("failed to send set peer state command");
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001415 goto out_free;
1416 }
1417
1418out_free:
1419 kfree(cmd);
1420
1421out:
1422 return ret;
1423}
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001424
Eliad Pellerc690ec82011-08-14 13:17:07 +03001425int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001426{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001427 struct wl12xx_cmd_add_peer *cmd;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001428 int i, ret;
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001429 u32 sta_rates;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001430
Eliad Pellerc690ec82011-08-14 13:17:07 +03001431 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001432
1433 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1434 if (!cmd) {
1435 ret = -ENOMEM;
1436 goto out;
1437 }
1438
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001439 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1440 cmd->bss_index = WL1271_AP_BSS_INDEX;
1441 cmd->aid = sta->aid;
1442 cmd->hlid = hlid;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001443 cmd->sp_len = sta->max_sp;
Arik Nemtsovb4d38db2011-06-27 23:58:46 +03001444 cmd->wmm = sta->wme ? 1 : 0;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001445
Eliad Peller1ec23f72011-08-25 14:26:54 +03001446 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1447 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1448 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1449 else
1450 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1451
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001452 sta_rates = sta->supp_rates[wl->band];
1453 if (sta->ht_cap.ht_supported)
1454 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1455
1456 cmd->supported_rates =
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001457 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1458 wl->band));
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001459
Eliad Peller1ec23f72011-08-25 14:26:54 +03001460 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1461 cmd->supported_rates, sta->uapsd_queues);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001462
Eliad Pellerc690ec82011-08-14 13:17:07 +03001463 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001464 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001465 wl1271_error("failed to initiate cmd add peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001466 goto out_free;
1467 }
1468
1469out_free:
1470 kfree(cmd);
1471
1472out:
1473 return ret;
1474}
1475
Eliad Pellerc690ec82011-08-14 13:17:07 +03001476int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001477{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001478 struct wl12xx_cmd_remove_peer *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001479 int ret;
1480
Eliad Pellerc690ec82011-08-14 13:17:07 +03001481 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001482
1483 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1484 if (!cmd) {
1485 ret = -ENOMEM;
1486 goto out;
1487 }
1488
1489 cmd->hlid = hlid;
1490 /* We never send a deauth, mac80211 is in charge of this */
1491 cmd->reason_opcode = 0;
1492 cmd->send_deauth_flag = 0;
1493
Eliad Pellerc690ec82011-08-14 13:17:07 +03001494 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001495 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001496 wl1271_error("failed to initiate cmd remove peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001497 goto out_free;
1498 }
1499
Arik Nemtsov05285cf2010-11-12 17:15:03 +02001500 /*
1501 * We are ok with a timeout here. The event is sometimes not sent
1502 * due to a firmware bug.
1503 */
Eliad Pellerc690ec82011-08-14 13:17:07 +03001504 wl1271_cmd_wait_for_event_or_timeout(wl,
1505 PEER_REMOVE_COMPLETE_EVENT_ID);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001506
1507out_free:
1508 kfree(cmd);
1509
1510out:
1511 return ret;
1512}
Ido Yariv95dac04f2011-06-06 14:57:06 +03001513
1514int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1515{
1516 struct wl12xx_cmd_config_fwlog *cmd;
1517 int ret = 0;
1518
1519 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1520
1521 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1522 if (!cmd) {
1523 ret = -ENOMEM;
1524 goto out;
1525 }
1526
1527 cmd->logger_mode = wl->conf.fwlog.mode;
1528 cmd->log_severity = wl->conf.fwlog.severity;
1529 cmd->timestamp = wl->conf.fwlog.timestamp;
1530 cmd->output = wl->conf.fwlog.output;
1531 cmd->threshold = wl->conf.fwlog.threshold;
1532
1533 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1534 if (ret < 0) {
1535 wl1271_error("failed to send config firmware logger command");
1536 goto out_free;
1537 }
1538
1539out_free:
1540 kfree(cmd);
1541
1542out:
1543 return ret;
1544}
1545
1546int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1547{
1548 struct wl12xx_cmd_start_fwlog *cmd;
1549 int ret = 0;
1550
1551 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1552
1553 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1554 if (!cmd) {
1555 ret = -ENOMEM;
1556 goto out;
1557 }
1558
1559 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1560 if (ret < 0) {
1561 wl1271_error("failed to send start firmware logger command");
1562 goto out_free;
1563 }
1564
1565out_free:
1566 kfree(cmd);
1567
1568out:
1569 return ret;
1570}
1571
1572int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1573{
1574 struct wl12xx_cmd_stop_fwlog *cmd;
1575 int ret = 0;
1576
1577 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1578
1579 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1580 if (!cmd) {
1581 ret = -ENOMEM;
1582 goto out;
1583 }
1584
1585 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1586 if (ret < 0) {
1587 wl1271_error("failed to send stop firmware logger command");
1588 goto out_free;
1589 }
1590
1591out_free:
1592 kfree(cmd);
1593
1594out:
1595 return ret;
1596}
Eliad Pellera7cba382011-08-14 13:17:16 +03001597
1598static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1599{
1600 struct wl12xx_cmd_roc *cmd;
1601 int ret = 0;
1602
1603 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1604
1605 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1606 return -EINVAL;
1607
1608 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1609 if (!cmd) {
1610 ret = -ENOMEM;
1611 goto out;
1612 }
1613
1614 cmd->role_id = role_id;
1615 cmd->channel = wl->channel;
1616 switch (wl->band) {
1617 case IEEE80211_BAND_2GHZ:
1618 cmd->band = RADIO_BAND_2_4GHZ;
1619 break;
1620 case IEEE80211_BAND_5GHZ:
1621 cmd->band = RADIO_BAND_5GHZ;
1622 break;
1623 default:
1624 wl1271_error("roc - unknown band: %d", (int)wl->band);
1625 ret = -EINVAL;
1626 goto out_free;
1627 }
1628
1629
1630 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1631 if (ret < 0) {
1632 wl1271_error("failed to send ROC command");
1633 goto out_free;
1634 }
1635
1636out_free:
1637 kfree(cmd);
1638
1639out:
1640 return ret;
1641}
1642
1643static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1644{
1645 struct wl12xx_cmd_croc *cmd;
1646 int ret = 0;
1647
1648 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1649
1650 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1651 if (!cmd) {
1652 ret = -ENOMEM;
1653 goto out;
1654 }
1655 cmd->role_id = role_id;
1656
1657 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1658 sizeof(*cmd), 0);
1659 if (ret < 0) {
1660 wl1271_error("failed to send ROC command");
1661 goto out_free;
1662 }
1663
1664out_free:
1665 kfree(cmd);
1666
1667out:
1668 return ret;
1669}
Eliad Peller251c1772011-08-14 13:17:17 +03001670
1671int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1672{
1673 int ret = 0;
1674
1675 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1676 return 0;
1677
1678 ret = wl12xx_cmd_roc(wl, role_id);
1679 if (ret < 0)
1680 goto out;
1681
1682 ret = wl1271_cmd_wait_for_event(wl,
1683 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1684 if (ret < 0) {
1685 wl1271_error("cmd roc event completion error");
1686 goto out;
1687 }
1688
1689 __set_bit(role_id, wl->roc_map);
1690out:
1691 return ret;
1692}
1693
1694int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1695{
1696 int ret = 0;
1697
1698 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1699 return 0;
1700
1701 ret = wl12xx_cmd_croc(wl, role_id);
1702 if (ret < 0)
1703 goto out;
1704
1705 __clear_bit(role_id, wl->roc_map);
1706out:
1707 return ret;
1708}
Shahar Levi6d158ff2011-09-08 13:01:33 +03001709
1710int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1711 struct ieee80211_channel_switch *ch_switch)
1712{
1713 struct wl12xx_cmd_channel_switch *cmd;
1714 int ret;
1715
1716 wl1271_debug(DEBUG_ACX, "cmd channel switch");
1717
1718 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1719 if (!cmd) {
1720 ret = -ENOMEM;
1721 goto out;
1722 }
1723
1724 cmd->channel = ch_switch->channel->hw_value;
1725 cmd->switch_time = ch_switch->count;
1726 cmd->tx_suspend = ch_switch->block_tx;
1727 cmd->flush = 0; /* this value is ignored by the FW */
1728
1729 ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1730 if (ret < 0) {
1731 wl1271_error("failed to send channel switch command");
1732 goto out_free;
1733 }
1734
1735out_free:
1736 kfree(cmd);
1737
1738out:
1739 return ret;
1740}
1741
1742int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1743{
1744 struct wl12xx_cmd_stop_channel_switch *cmd;
1745 int ret;
1746
1747 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1748
1749 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1750 if (!cmd) {
1751 ret = -ENOMEM;
1752 goto out;
1753 }
1754
1755 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1756 if (ret < 0) {
1757 wl1271_error("failed to stop channel switch command");
1758 goto out_free;
1759 }
1760
1761out_free:
1762 kfree(cmd);
1763
1764out:
1765 return ret;
1766}