blob: 287fe95ecb40092e826b821056ee05e093d3f3a0 [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 Levi0c005042011-06-12 10:34:43 +0300137 /* LPD mode enable (bits 6-7) in WL1271 AP mode only */
138 if (wl->quirks & WL12XX_QUIRK_LPD_MODE)
139 gen_parms->general_params.general_settings |=
140 GENERAL_SETTINGS_DRPW_LPD;
141
Shahar Levi49d750ca2011-03-06 16:32:09 +0200142 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
143 if (ret < 0) {
144 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
145 goto out;
146 }
147
148 gp->tx_bip_fem_manufacturer =
149 gen_parms->general_params.tx_bip_fem_manufacturer;
150
151 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
152 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
153
154out:
155 kfree(gen_parms);
156 return ret;
157}
158
159int wl128x_cmd_general_parms(struct wl1271 *wl)
160{
161 struct wl128x_general_parms_cmd *gen_parms;
162 struct wl128x_ini_general_params *gp =
163 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300164 bool answer = false;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200165 int ret;
166
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200167 if (!wl->nvs)
168 return -ENODEV;
169
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200170 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
171 if (!gen_parms)
172 return -ENOMEM;
173
174 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
175
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300176 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
Luciano Coelho76c0f8d2009-12-11 15:40:41 +0200177
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300178 if (gp->tx_bip_fem_auto_detect)
179 answer = true;
180
Shahar Levib03acad2011-04-03 13:54:54 +0300181 /* Replace REF and TCXO CLKs with the ones from platform data */
182 gen_parms->general_params.ref_clock = wl->ref_clock;
183 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
184
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300185 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
186 if (ret < 0) {
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200187 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300188 goto out;
189 }
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200190
Juuso Oikarinen4b34d432010-10-07 10:16:42 +0300191 gp->tx_bip_fem_manufacturer =
192 gen_parms->general_params.tx_bip_fem_manufacturer;
193
194 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
195 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
196
197out:
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200198 kfree(gen_parms);
199 return ret;
200}
201
202int wl1271_cmd_radio_parms(struct wl1271 *wl)
203{
Shahar Levibc765bf2011-03-06 16:32:10 +0200204 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200205 struct wl1271_radio_parms_cmd *radio_parms;
Shahar Levibc765bf2011-03-06 16:32:10 +0200206 struct wl1271_ini_general_params *gp = &nvs->general_params;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200207 int ret;
208
209 if (!wl->nvs)
210 return -ENODEV;
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200211
212 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
213 if (!radio_parms)
214 return -ENOMEM;
215
216 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
217
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300218 /* 2.4GHz parameters */
Shahar Levibc765bf2011-03-06 16:32:10 +0200219 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300220 sizeof(struct wl1271_ini_band_params_2));
221 memcpy(&radio_parms->dyn_params_2,
Shahar Levibc765bf2011-03-06 16:32:10 +0200222 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarineneb70eb72010-05-14 10:46:22 +0300223 sizeof(struct wl1271_ini_fem_params_2));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200224
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300225 /* 5GHz parameters */
226 memcpy(&radio_parms->static_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200227 &nvs->stat_radio_params_5,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300228 sizeof(struct wl1271_ini_band_params_5));
229 memcpy(&radio_parms->dyn_params_5,
Shahar Levibc765bf2011-03-06 16:32:10 +0200230 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
Juuso Oikarinena7da74f2010-05-14 10:46:23 +0300231 sizeof(struct wl1271_ini_fem_params_5));
Luciano Coelho98b5dd52009-11-23 23:22:17 +0200232
233 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
234 radio_parms, sizeof(*radio_parms));
235
236 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
237 if (ret < 0)
238 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
239
240 kfree(radio_parms);
241 return ret;
242}
243
Shahar Levi49d750ca2011-03-06 16:32:09 +0200244int wl128x_cmd_radio_parms(struct wl1271 *wl)
245{
246 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
247 struct wl128x_radio_parms_cmd *radio_parms;
248 struct wl128x_ini_general_params *gp = &nvs->general_params;
249 int ret;
250
251 if (!wl->nvs)
252 return -ENODEV;
253
254 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
255 if (!radio_parms)
256 return -ENOMEM;
257
258 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
259
260 /* 2.4GHz parameters */
261 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
262 sizeof(struct wl128x_ini_band_params_2));
263 memcpy(&radio_parms->dyn_params_2,
264 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
265 sizeof(struct wl128x_ini_fem_params_2));
266
267 /* 5GHz parameters */
268 memcpy(&radio_parms->static_params_5,
269 &nvs->stat_radio_params_5,
270 sizeof(struct wl128x_ini_band_params_5));
271 memcpy(&radio_parms->dyn_params_5,
272 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
273 sizeof(struct wl128x_ini_fem_params_5));
274
275 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
276
277 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
278 radio_parms, sizeof(*radio_parms));
279
280 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
281 if (ret < 0)
282 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
283
284 kfree(radio_parms);
285 return ret;
286}
287
Juuso Oikarinen644a4862010-10-05 13:11:56 +0200288int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
289{
290 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
291 struct conf_rf_settings *rf = &wl->conf.rf;
292 int ret;
293
294 if (!wl->nvs)
295 return -ENODEV;
296
297 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
298 if (!ext_radio_parms)
299 return -ENOMEM;
300
301 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
302
303 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
304 rf->tx_per_channel_power_compensation_2,
305 CONF_TX_PWR_COMPENSATION_LEN_2);
306 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
307 rf->tx_per_channel_power_compensation_5,
308 CONF_TX_PWR_COMPENSATION_LEN_5);
309
310 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
311 ext_radio_parms, sizeof(*ext_radio_parms));
312
313 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
314 if (ret < 0)
315 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
316
317 kfree(ext_radio_parms);
318 return ret;
319}
320
Luciano Coelho99d84c12010-03-26 12:53:20 +0200321/*
322 * Poll the mailbox event field until any of the bits in the mask is set or a
323 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
324 */
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200325static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
Luciano Coelho99d84c12010-03-26 12:53:20 +0200326{
327 u32 events_vector, event;
328 unsigned long timeout;
329
330 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
331
332 do {
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200333 if (time_after(jiffies, timeout)) {
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200334 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
335 (int)mask);
Luciano Coelho99d84c12010-03-26 12:53:20 +0200336 return -ETIMEDOUT;
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200337 }
Luciano Coelho99d84c12010-03-26 12:53:20 +0200338
339 msleep(1);
340
341 /* read from both event fields */
342 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
343 sizeof(events_vector), false);
344 event = events_vector & mask;
345 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
346 sizeof(events_vector), false);
347 event |= events_vector & mask;
348 } while (!event);
349
350 return 0;
351}
352
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200353static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
354{
355 int ret;
356
357 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
358 if (ret != 0) {
Ido Yarivbaacb9ae2011-06-06 14:57:05 +0300359 wl12xx_queue_recovery_work(wl);
Arik Nemtsov05285cf2010-11-12 17:15:03 +0200360 return ret;
361 }
362
363 return 0;
364}
365
Eliad Pellerc690ec82011-08-14 13:17:07 +0300366int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 role_type, u8 *role_id)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300367{
Eliad Pellerc690ec82011-08-14 13:17:07 +0300368 struct wl12xx_cmd_role_enable *cmd;
369 int ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300370
Eliad Pellerc690ec82011-08-14 13:17:07 +0300371 wl1271_debug(DEBUG_CMD, "cmd role enable");
372
373 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
374 return -EBUSY;
375
376 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
377 if (!cmd) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300378 ret = -ENOMEM;
379 goto out;
380 }
381
Eliad Pellerc690ec82011-08-14 13:17:07 +0300382 /* get role id */
383 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
384 if (cmd->role_id >= WL12XX_MAX_ROLES) {
385 ret = -EBUSY;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300386 goto out_free;
387 }
388
Eliad Pellerc690ec82011-08-14 13:17:07 +0300389 memcpy(cmd->mac_address, wl->mac_addr, ETH_ALEN);
390 cmd->role_type = role_type;
391
392 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
393 if (ret < 0) {
394 wl1271_error("failed to initiate cmd role enable");
395 goto out_free;
396 }
397
398 __set_bit(cmd->role_id, wl->roles_map);
399 *role_id = cmd->role_id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300400
401out_free:
Eliad Pellerc690ec82011-08-14 13:17:07 +0300402 kfree(cmd);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300403
404out:
405 return ret;
406}
407
Eliad Pellerc690ec82011-08-14 13:17:07 +0300408int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
409{
410 struct wl12xx_cmd_role_disable *cmd;
411 int ret;
412
413 wl1271_debug(DEBUG_CMD, "cmd role disable");
414
415 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
416 return -ENOENT;
417
418 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
419 if (!cmd) {
420 ret = -ENOMEM;
421 goto out;
422 }
423 cmd->role_id = *role_id;
424
425 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
426 if (ret < 0) {
427 wl1271_error("failed to initiate cmd role disable");
428 goto out_free;
429 }
430
431 __clear_bit(*role_id, wl->roles_map);
432 *role_id = WL12XX_INVALID_ROLE_ID;
433
434out_free:
435 kfree(cmd);
436
437out:
438 return ret;
439}
440
441static int wl12xx_allocate_link(struct wl1271 *wl, u8 *hlid)
442{
443 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
444 if (link >= WL12XX_MAX_LINKS)
445 return -EBUSY;
446
447 __set_bit(link, wl->links_map);
448 *hlid = link;
449 return 0;
450}
451
452static void wl12xx_free_link(struct wl1271 *wl, u8 *hlid)
453{
454 if (*hlid == WL12XX_INVALID_LINK_ID)
455 return;
456
457 __clear_bit(*hlid, wl->links_map);
458 *hlid = WL12XX_INVALID_LINK_ID;
459}
460
Arik Nemtsov712e9bf2011-08-14 13:17:20 +0300461static int wl12xx_get_new_session_id(struct wl1271 *wl)
462{
463 if (wl->session_counter >= SESSION_COUNTER_MAX)
464 wl->session_counter = 0;
465
466 wl->session_counter++;
467
468 return wl->session_counter;
469}
470
Eliad Peller04e80792011-08-14 13:17:09 +0300471int wl12xx_cmd_role_start_dev(struct wl1271 *wl)
472{
473 struct wl12xx_cmd_role_start *cmd;
474 int ret;
475
476 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
477 if (!cmd) {
478 ret = -ENOMEM;
479 goto out;
480 }
481
482 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wl->dev_role_id);
483
484 cmd->role_id = wl->dev_role_id;
485 if (wl->band == IEEE80211_BAND_5GHZ)
486 cmd->band = WL12XX_BAND_5GHZ;
487 cmd->channel = wl->channel;
488
489 if (wl->dev_hlid == WL12XX_INVALID_LINK_ID) {
490 ret = wl12xx_allocate_link(wl, &wl->dev_hlid);
491 if (ret)
492 goto out_free;
493 }
494 cmd->device.hlid = wl->dev_hlid;
495 cmd->device.session = wl->session_counter;
496
497 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
498 cmd->role_id, cmd->device.hlid, cmd->device.session);
499
500 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
501 if (ret < 0) {
502 wl1271_error("failed to initiate cmd role enable");
503 goto err_hlid;
504 }
505
506 goto out_free;
507
508err_hlid:
509 /* clear links on error */
510 __clear_bit(wl->dev_hlid, wl->links_map);
511 wl->dev_hlid = WL12XX_INVALID_LINK_ID;
512
513
514out_free:
515 kfree(cmd);
516
517out:
518 return ret;
519}
520
521int wl12xx_cmd_role_stop_dev(struct wl1271 *wl)
522{
523 struct wl12xx_cmd_role_stop *cmd;
524 int ret;
525
526 if (WARN_ON(wl->dev_hlid == WL12XX_INVALID_LINK_ID))
527 return -EINVAL;
528
529 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
530 if (!cmd) {
531 ret = -ENOMEM;
532 goto out;
533 }
534
535 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
536
537 cmd->role_id = wl->dev_role_id;
538 cmd->disc_type = DISCONNECT_IMMEDIATE;
539 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
540
541 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
542 if (ret < 0) {
543 wl1271_error("failed to initiate cmd role stop");
544 goto out_free;
545 }
546
547 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
548 if (ret < 0) {
549 wl1271_error("cmd role stop dev event completion error");
550 goto out_free;
551 }
552
553 wl12xx_free_link(wl, &wl->dev_hlid);
554
555out_free:
556 kfree(cmd);
557
558out:
559 return ret;
560}
561
Eliad Pellerc690ec82011-08-14 13:17:07 +0300562int wl12xx_cmd_role_start_sta(struct wl1271 *wl)
563{
564 struct wl12xx_cmd_role_start *cmd;
565 int ret;
566
567 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
568 if (!cmd) {
569 ret = -ENOMEM;
570 goto out;
571 }
572
573 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wl->role_id);
574
575 cmd->role_id = wl->role_id;
576 if (wl->band == IEEE80211_BAND_5GHZ)
577 cmd->band = WL12XX_BAND_5GHZ;
578 cmd->channel = wl->channel;
579 cmd->sta.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
580 cmd->sta.beacon_interval = cpu_to_le16(wl->beacon_int);
581 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
582 cmd->sta.ssid_len = wl->ssid_len;
583 memcpy(cmd->sta.ssid, wl->ssid, wl->ssid_len);
584 memcpy(cmd->sta.bssid, wl->bssid, ETH_ALEN);
585 cmd->sta.local_rates = cpu_to_le32(wl->rate_set);
586
587 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
588 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
589 if (ret)
590 goto out_free;
591 }
592 cmd->sta.hlid = wl->sta_hlid;
Arik Nemtsov712e9bf2011-08-14 13:17:20 +0300593 cmd->sta.session = wl12xx_get_new_session_id(wl);
Eliad Pellerc690ec82011-08-14 13:17:07 +0300594 cmd->sta.remote_rates = cpu_to_le32(wl->rate_set);
595
596 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
597 "basic_rate_set: 0x%x, remote_rates: 0x%x",
598 wl->role_id, cmd->sta.hlid, cmd->sta.session,
599 wl->basic_rate_set, wl->rate_set);
600
601 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
602 if (ret < 0) {
603 wl1271_error("failed to initiate cmd role start sta");
604 goto err_hlid;
605 }
606
607 goto out_free;
608
609err_hlid:
610 /* clear links on error. */
611 wl12xx_free_link(wl, &wl->sta_hlid);
612
613out_free:
614 kfree(cmd);
615
616out:
617 return ret;
618}
619
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300620/* use this function to stop ibss as well */
Eliad Pellerc690ec82011-08-14 13:17:07 +0300621int wl12xx_cmd_role_stop_sta(struct wl1271 *wl)
622{
623 struct wl12xx_cmd_role_stop *cmd;
624 int ret;
625
626 if (WARN_ON(wl->sta_hlid == WL12XX_INVALID_LINK_ID))
627 return -EINVAL;
628
629 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
630 if (!cmd) {
631 ret = -ENOMEM;
632 goto out;
633 }
634
635 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wl->role_id);
636
637 cmd->role_id = wl->role_id;
638 cmd->disc_type = DISCONNECT_IMMEDIATE;
639 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
640
641 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
642 if (ret < 0) {
643 wl1271_error("failed to initiate cmd role stop sta");
644 goto out_free;
645 }
646
Eliad Pellerc690ec82011-08-14 13:17:07 +0300647 wl12xx_free_link(wl, &wl->sta_hlid);
648
649out_free:
650 kfree(cmd);
651
652out:
653 return ret;
654}
655
656int wl12xx_cmd_role_start_ap(struct wl1271 *wl)
657{
658 struct wl12xx_cmd_role_start *cmd;
659 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
660 int ret;
661
662 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wl->role_id);
663
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300664 /* trying to use hidden SSID with an old hostapd version */
665 if (wl->ssid_len == 0 && !bss_conf->hidden_ssid) {
666 wl1271_error("got a null SSID from beacon/bss");
Eliad Pellerc690ec82011-08-14 13:17:07 +0300667 ret = -EINVAL;
668 goto out;
669 }
670
671 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
672 if (!cmd) {
673 ret = -ENOMEM;
674 goto out;
675 }
676
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300677 ret = wl12xx_allocate_link(wl, &wl->ap_global_hlid);
678 if (ret < 0)
679 goto out_free;
680
681 ret = wl12xx_allocate_link(wl, &wl->ap_bcast_hlid);
682 if (ret < 0)
683 goto out_free_global;
684
Eliad Pellerc690ec82011-08-14 13:17:07 +0300685 cmd->role_id = wl->role_id;
686 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
687 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300688 cmd->ap.global_hlid = wl->ap_global_hlid;
689 cmd->ap.broadcast_hlid = wl->ap_bcast_hlid;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300690 cmd->ap.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
691 cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
692 cmd->ap.dtim_interval = bss_conf->dtim_period;
693 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
694 cmd->channel = wl->channel;
Arik Nemtsov68eaaf62011-09-03 20:22:03 +0300695
696 if (!bss_conf->hidden_ssid) {
697 /* take the SSID from the beacon for backward compatibility */
698 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
699 cmd->ap.ssid_len = wl->ssid_len;
700 memcpy(cmd->ap.ssid, wl->ssid, wl->ssid_len);
701 } else {
702 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
703 cmd->ap.ssid_len = bss_conf->ssid_len;
704 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
705 }
706
Eliad Pellerc690ec82011-08-14 13:17:07 +0300707 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
708
709 switch (wl->band) {
710 case IEEE80211_BAND_2GHZ:
711 cmd->band = RADIO_BAND_2_4GHZ;
712 break;
713 case IEEE80211_BAND_5GHZ:
714 cmd->band = RADIO_BAND_5GHZ;
715 break;
716 default:
717 wl1271_warning("ap start - unknown band: %d", (int)wl->band);
718 cmd->band = RADIO_BAND_2_4GHZ;
719 break;
720 }
721
722 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
723 if (ret < 0) {
724 wl1271_error("failed to initiate cmd role start ap");
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300725 goto out_free_bcast;
Eliad Pellerc690ec82011-08-14 13:17:07 +0300726 }
727
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300728 goto out_free;
729
730out_free_bcast:
731 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
732
733out_free_global:
734 wl12xx_free_link(wl, &wl->ap_global_hlid);
735
Eliad Pellerc690ec82011-08-14 13:17:07 +0300736out_free:
737 kfree(cmd);
738
739out:
740 return ret;
741}
742
743int wl12xx_cmd_role_stop_ap(struct wl1271 *wl)
744{
745 struct wl12xx_cmd_role_stop *cmd;
746 int ret;
747
748 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
749 if (!cmd) {
750 ret = -ENOMEM;
751 goto out;
752 }
753
754 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wl->role_id);
755
756 cmd->role_id = wl->role_id;
757
758 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
759 if (ret < 0) {
760 wl1271_error("failed to initiate cmd role stop ap");
761 goto out_free;
762 }
763
Arik Nemtsove51ae9b2011-08-14 13:17:21 +0300764 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
765 wl12xx_free_link(wl, &wl->ap_global_hlid);
766
Eliad Pellerc690ec82011-08-14 13:17:07 +0300767out_free:
768 kfree(cmd);
769
770out:
771 return ret;
772}
773
Eliad Peller31cd3ae2011-08-14 13:17:25 +0300774int wl12xx_cmd_role_start_ibss(struct wl1271 *wl)
775{
776 struct wl12xx_cmd_role_start *cmd;
777 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
778 int ret;
779
780 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
781 if (!cmd) {
782 ret = -ENOMEM;
783 goto out;
784 }
785
786 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wl->role_id);
787
788 cmd->role_id = wl->role_id;
789 if (wl->band == IEEE80211_BAND_5GHZ)
790 cmd->band = WL12XX_BAND_5GHZ;
791 cmd->channel = wl->channel;
792 cmd->ibss.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
793 cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
794 cmd->ibss.dtim_interval = bss_conf->dtim_period;
795 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
796 cmd->ibss.ssid_len = wl->ssid_len;
797 memcpy(cmd->ibss.ssid, wl->ssid, wl->ssid_len);
798 memcpy(cmd->ibss.bssid, wl->bssid, ETH_ALEN);
799 cmd->sta.local_rates = cpu_to_le32(wl->rate_set);
800
801 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
802 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
803 if (ret)
804 goto out_free;
805 }
806 cmd->ibss.hlid = wl->sta_hlid;
807 cmd->ibss.remote_rates = cpu_to_le32(wl->rate_set);
808
809 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
810 "basic_rate_set: 0x%x, remote_rates: 0x%x",
811 wl->role_id, cmd->sta.hlid, cmd->sta.session,
812 wl->basic_rate_set, wl->rate_set);
813
814 wl1271_debug(DEBUG_CMD, "wl->bssid = %pM", wl->bssid);
815
816 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
817 if (ret < 0) {
818 wl1271_error("failed to initiate cmd role enable");
819 goto err_hlid;
820 }
821
822 goto out_free;
823
824err_hlid:
825 /* clear links on error. */
826 wl12xx_free_link(wl, &wl->sta_hlid);
827
828out_free:
829 kfree(cmd);
830
831out:
832 return ret;
833}
834
Eliad Pellerc690ec82011-08-14 13:17:07 +0300835
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300836/**
837 * send test command to firmware
838 *
839 * @wl: wl struct
840 * @buf: buffer containing the command, with all headers, must work with dma
841 * @len: length of the buffer
842 * @answer: is answer needed
843 */
844int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
845{
846 int ret;
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200847 size_t res_len = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300848
849 wl1271_debug(DEBUG_CMD, "cmd test");
850
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200851 if (answer)
852 res_len = buf_len;
853
854 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300855
856 if (ret < 0) {
857 wl1271_warning("TEST command failed");
858 return ret;
859 }
860
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200861 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300862}
863
864/**
865 * read acx from firmware
866 *
867 * @wl: wl struct
868 * @id: acx id
869 * @buf: buffer for the response, including all headers, must work with dma
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300870 * @len: length of buf
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300871 */
872int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
873{
874 struct acx_header *acx = buf;
875 int ret;
876
877 wl1271_debug(DEBUG_CMD, "cmd interrogate");
878
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300879 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300880
881 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300882 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300883
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200884 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
885 if (ret < 0)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300886 wl1271_error("INTERROGATE command failed");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300887
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300888 return ret;
889}
890
891/**
892 * write acx value to firmware
893 *
894 * @wl: wl struct
895 * @id: acx id
896 * @buf: buffer containing acx, including all headers, must work with dma
897 * @len: length of buf
898 */
899int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
900{
901 struct acx_header *acx = buf;
902 int ret;
903
Eliad Pellerc91d0602011-08-25 18:10:57 +0300904 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300905
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300906 acx->id = cpu_to_le16(id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300907
908 /* payload length, does not include any headers */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300909 acx->len = cpu_to_le16(len - sizeof(*acx));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300910
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200911 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300912 if (ret < 0) {
913 wl1271_warning("CONFIGURE command NOK");
914 return ret;
915 }
916
917 return 0;
918}
919
Luciano Coelho94210892009-12-11 15:40:55 +0200920int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300921{
922 struct cmd_enabledisable_path *cmd;
923 int ret;
924 u16 cmd_rx, cmd_tx;
925
926 wl1271_debug(DEBUG_CMD, "cmd data path");
927
928 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
929 if (!cmd) {
930 ret = -ENOMEM;
931 goto out;
932 }
933
Luciano Coelho94210892009-12-11 15:40:55 +0200934 /* the channel here is only used for calibration, so hardcoded to 1 */
935 cmd->channel = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300936
937 if (enable) {
938 cmd_rx = CMD_ENABLE_RX;
939 cmd_tx = CMD_ENABLE_TX;
940 } else {
941 cmd_rx = CMD_DISABLE_RX;
942 cmd_tx = CMD_DISABLE_TX;
943 }
944
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200945 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300946 if (ret < 0) {
947 wl1271_error("rx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200948 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300949 goto out;
950 }
951
952 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200953 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300954
Juuso Oikarinenfa867e72009-11-02 20:22:13 +0200955 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300956 if (ret < 0) {
957 wl1271_error("tx %s cmd for channel %d failed",
Luciano Coelho94210892009-12-11 15:40:55 +0200958 enable ? "start" : "stop", cmd->channel);
Juuso Oikarinen1b00f2b2010-03-26 12:53:17 +0200959 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300960 }
961
962 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Luciano Coelho94210892009-12-11 15:40:55 +0200963 enable ? "start" : "stop", cmd->channel);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300964
965out:
966 kfree(cmd);
967 return ret;
968}
969
Eliad Pellerc8bde242011-02-02 09:59:35 +0200970int wl1271_cmd_ps_mode(struct wl1271 *wl, 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 Pellerc690ec82011-08-14 13:17:07 +0300983 ps_params->role_id = wl->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
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001038int wl1271_cmd_build_null_data(struct wl1271 *wl)
1039{
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
Juuso Oikarinena0cb7be2010-03-18 12:26:44 +02001046 if (wl->bss_type == BSS_TYPE_IBSS) {
1047 size = sizeof(struct wl12xx_null_data_template);
1048 ptr = NULL;
1049 } else {
1050 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1051 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,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +02001058 wl->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
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001069int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
1070{
1071 struct sk_buff *skb = NULL;
1072 int ret = -ENOMEM;
1073
1074 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1075 if (!skb)
1076 goto out;
1077
1078 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1079 skb->data, skb->len,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001080 CMD_TEMPL_KLV_IDX_NULL_DATA,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +02001081 wl->basic_rate);
Juuso Oikarinenbfb24c92010-03-26 12:53:31 +02001082
1083out:
1084 dev_kfree_skb(skb);
1085 if (ret)
1086 wl1271_warning("cmd build klv null data failed %d", ret);
1087
1088 return ret;
1089
1090}
1091
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001092int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
1093{
Kalle Valo899e6e62010-03-18 12:26:34 +02001094 struct sk_buff *skb;
1095 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001096
Kalle Valo899e6e62010-03-18 12:26:34 +02001097 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1098 if (!skb)
1099 goto out;
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001100
Kalle Valo899e6e62010-03-18 12:26:34 +02001101 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
Juuso Oikarinen849923f2010-07-08 17:49:59 +03001102 skb->len, 0, wl->basic_rate_set);
Juuso Oikarinenc3fea192009-10-08 21:56:22 +03001103
Kalle Valo899e6e62010-03-18 12:26:34 +02001104out:
1105 dev_kfree_skb(skb);
1106 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001107}
1108
Kalle Valo818e3062010-03-18 12:26:35 +02001109int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1110 const u8 *ssid, size_t ssid_len,
1111 const u8 *ie, size_t ie_len, u8 band)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001112{
Kalle Valo818e3062010-03-18 12:26:35 +02001113 struct sk_buff *skb;
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001114 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001115 u32 rate;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001116
Kalle Valo818e3062010-03-18 12:26:35 +02001117 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1118 ie, ie_len);
1119 if (!skb) {
1120 ret = -ENOMEM;
1121 goto out;
1122 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001123
Kalle Valo818e3062010-03-18 12:26:35 +02001124 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001125
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001126 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001127 if (band == IEEE80211_BAND_2GHZ)
1128 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001129 skb->data, skb->len, 0, rate);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001130 else
1131 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001132 skb->data, skb->len, 0, rate);
Kalle Valo818e3062010-03-18 12:26:35 +02001133
1134out:
1135 dev_kfree_skb(skb);
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +03001136 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001137}
1138
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001139struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1140 struct sk_buff *skb)
1141{
1142 int ret;
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001143 u32 rate;
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001144
1145 if (!skb)
1146 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1147 if (!skb)
1148 goto out;
1149
1150 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1151
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001152 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001153 if (wl->band == IEEE80211_BAND_2GHZ)
1154 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001155 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001156 else
1157 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001158 skb->data, skb->len, 0, rate);
Juuso Oikarinen2f6724b2010-11-24 08:16:57 +02001159
1160 if (ret < 0)
1161 wl1271_error("Unable to set ap probe request template.");
1162
1163out:
1164 return skb;
1165}
1166
Eliad Pellerc5312772010-12-09 11:31:27 +02001167int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
1168{
1169 int ret;
1170 struct wl12xx_arp_rsp_template tmpl;
1171 struct ieee80211_hdr_3addr *hdr;
1172 struct arphdr *arp_hdr;
1173
1174 memset(&tmpl, 0, sizeof(tmpl));
1175
1176 /* mac80211 header */
1177 hdr = &tmpl.hdr;
1178 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1179 IEEE80211_STYPE_DATA |
1180 IEEE80211_FCTL_TODS);
1181 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1182 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1183 memset(hdr->addr3, 0xff, ETH_ALEN);
1184
1185 /* llc layer */
1186 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
Eliad Peller6177eae2010-12-22 12:38:52 +01001187 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001188
1189 /* arp header */
1190 arp_hdr = &tmpl.arp_hdr;
Eliad Peller6177eae2010-12-22 12:38:52 +01001191 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1192 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
Eliad Pellerc5312772010-12-09 11:31:27 +02001193 arp_hdr->ar_hln = ETH_ALEN;
1194 arp_hdr->ar_pln = 4;
Eliad Peller6177eae2010-12-22 12:38:52 +01001195 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
Eliad Pellerc5312772010-12-09 11:31:27 +02001196
1197 /* arp payload */
1198 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1199 tmpl.sender_ip = ip_addr;
1200
1201 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1202 &tmpl, sizeof(tmpl), 0,
1203 wl->basic_rate);
1204
1205 return ret;
1206}
1207
Kalle Valo023e0822010-03-18 12:26:36 +02001208int wl1271_build_qos_null_data(struct wl1271 *wl)
1209{
1210 struct ieee80211_qos_hdr template;
1211
1212 memset(&template, 0, sizeof(template));
1213
1214 memcpy(template.addr1, wl->bssid, ETH_ALEN);
1215 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
1216 memcpy(template.addr3, wl->bssid, ETH_ALEN);
1217
1218 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1219 IEEE80211_STYPE_QOS_NULLFUNC |
1220 IEEE80211_FCTL_TODS);
1221
1222 /* FIXME: not sure what priority to use here */
1223 template.qos_ctrl = cpu_to_le16(0);
1224
1225 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
Juuso Oikarinen606c1482010-04-01 11:38:21 +03001226 sizeof(template), 0,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +02001227 wl->basic_rate);
Kalle Valo023e0822010-03-18 12:26:36 +02001228}
1229
Eliad Pellerc690ec82011-08-14 13:17:07 +03001230int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001231{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001232 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001233 int ret = 0;
1234
1235 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1236
1237 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1238 if (!cmd) {
1239 ret = -ENOMEM;
1240 goto out;
1241 }
1242
Eliad Pellerc690ec82011-08-14 13:17:07 +03001243 cmd->hlid = hlid;
1244 cmd->key_id = id;
1245 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001246 cmd->key_action = cpu_to_le16(KEY_SET_ID);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001247 cmd->key_type = KEY_WEP;
1248
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001249 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001250 if (ret < 0) {
1251 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1252 goto out;
1253 }
1254
1255out:
1256 kfree(cmd);
1257
1258 return ret;
1259}
1260
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001261int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001262 u8 key_size, const u8 *key, const u8 *addr,
1263 u32 tx_seq_32, u16 tx_seq_16)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001264{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001265 struct wl1271_cmd_set_keys *cmd;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001266 int ret = 0;
1267
Eliad Pellerb42f0682011-08-14 13:17:24 +03001268 /* hlid might have already been deleted */
1269 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID)
1270 return 0;
1271
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001272 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1273 if (!cmd) {
1274 ret = -ENOMEM;
1275 goto out;
1276 }
1277
Eliad Pellerc690ec82011-08-14 13:17:07 +03001278 cmd->hlid = wl->sta_hlid;
1279
1280 if (key_type == KEY_WEP)
1281 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1282 else if (is_broadcast_ether_addr(addr))
1283 cmd->lid_key_type = BROADCAST_LID_TYPE;
1284 else
1285 cmd->lid_key_type = UNICAST_LID_TYPE;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001286
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001287 cmd->key_action = cpu_to_le16(action);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001288 cmd->key_size = key_size;
1289 cmd->key_type = key_type;
1290
Luciano Coelhod0f63b22009-10-15 10:33:29 +03001291 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1292 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +03001293
Eliad Pellerc690ec82011-08-14 13:17:07 +03001294 cmd->key_id = id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001295
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001296 if (key_type == KEY_TKIP) {
1297 /*
1298 * We get the key in the following form:
1299 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1300 * but the target is expecting:
1301 * TKIP - RX MIC - TX MIC
1302 */
1303 memcpy(cmd->key, key, 16);
1304 memcpy(cmd->key + 16, key + 24, 8);
1305 memcpy(cmd->key + 24, key + 16, 8);
1306
1307 } else {
1308 memcpy(cmd->key, key, key_size);
1309 }
1310
1311 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1312
Juuso Oikarinenfa867e72009-11-02 20:22:13 +02001313 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001314 if (ret < 0) {
1315 wl1271_warning("could not set keys");
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +02001316 goto out;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001317 }
1318
1319out:
1320 kfree(cmd);
1321
1322 return ret;
1323}
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001324
Eliad Pellerc690ec82011-08-14 13:17:07 +03001325/*
1326 * TODO: merge with sta/ibss into 1 set_key function.
1327 * note there are slight diffs
1328 */
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001329int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1330 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1331 u16 tx_seq_16)
1332{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001333 struct wl1271_cmd_set_keys *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001334 int ret = 0;
1335 u8 lid_type;
1336
1337 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1338 if (!cmd)
1339 return -ENOMEM;
1340
Arik Nemtsove51ae9b2011-08-14 13:17:21 +03001341 if (hlid == wl->ap_bcast_hlid) {
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001342 if (key_type == KEY_WEP)
1343 lid_type = WEP_DEFAULT_LID_TYPE;
1344 else
1345 lid_type = BROADCAST_LID_TYPE;
1346 } else {
1347 lid_type = UNICAST_LID_TYPE;
1348 }
1349
1350 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1351 " hlid: %d", (int)action, (int)id, (int)lid_type,
1352 (int)key_type, (int)hlid);
1353
1354 cmd->lid_key_type = lid_type;
1355 cmd->hlid = hlid;
1356 cmd->key_action = cpu_to_le16(action);
1357 cmd->key_size = key_size;
1358 cmd->key_type = key_type;
1359 cmd->key_id = id;
1360 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1361 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1362
1363 if (key_type == KEY_TKIP) {
1364 /*
1365 * We get the key in the following form:
1366 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1367 * but the target is expecting:
1368 * TKIP - RX MIC - TX MIC
1369 */
1370 memcpy(cmd->key, key, 16);
1371 memcpy(cmd->key + 16, key + 24, 8);
1372 memcpy(cmd->key + 24, key + 16, 8);
1373 } else {
1374 memcpy(cmd->key, key, key_size);
1375 }
1376
1377 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1378
1379 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1380 if (ret < 0) {
1381 wl1271_warning("could not set ap keys");
1382 goto out;
1383 }
1384
1385out:
1386 kfree(cmd);
1387 return ret;
1388}
1389
Eliad Pellerb67476e2011-08-14 13:17:23 +03001390int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001391{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001392 struct wl12xx_cmd_set_peer_state *cmd;
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001393 int ret = 0;
1394
Eliad Pellerb67476e2011-08-14 13:17:23 +03001395 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
Luciano Coelho25a7dc62009-10-12 15:08:42 +03001396
1397 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1398 if (!cmd) {
1399 ret = -ENOMEM;
1400 goto out;
1401 }
1402
Eliad Pellerb67476e2011-08-14 13:17:23 +03001403 cmd->hlid = hlid;
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001404 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1405
Eliad Pellerc690ec82011-08-14 13:17:07 +03001406 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001407 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001408 wl1271_error("failed to send set peer state command");
Juuso Oikarinenbe86cbe2010-05-27 12:53:01 +03001409 goto out_free;
1410 }
1411
1412out_free:
1413 kfree(cmd);
1414
1415out:
1416 return ret;
1417}
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001418
Eliad Pellerc690ec82011-08-14 13:17:07 +03001419int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001420{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001421 struct wl12xx_cmd_add_peer *cmd;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001422 int i, ret;
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001423 u32 sta_rates;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001424
Eliad Pellerc690ec82011-08-14 13:17:07 +03001425 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001426
1427 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1428 if (!cmd) {
1429 ret = -ENOMEM;
1430 goto out;
1431 }
1432
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001433 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1434 cmd->bss_index = WL1271_AP_BSS_INDEX;
1435 cmd->aid = sta->aid;
1436 cmd->hlid = hlid;
Eliad Peller1ec23f72011-08-25 14:26:54 +03001437 cmd->sp_len = sta->max_sp;
Arik Nemtsovb4d38db2011-06-27 23:58:46 +03001438 cmd->wmm = sta->wme ? 1 : 0;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001439
Eliad Peller1ec23f72011-08-25 14:26:54 +03001440 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1441 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1442 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1443 else
1444 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1445
Arik Nemtsov99d5ad72011-08-14 13:17:28 +03001446 sta_rates = sta->supp_rates[wl->band];
1447 if (sta->ht_cap.ht_supported)
1448 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1449
1450 cmd->supported_rates =
Eliad Pelleraf7fbb22011-09-19 13:51:42 +03001451 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1452 wl->band));
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001453
Eliad Peller1ec23f72011-08-25 14:26:54 +03001454 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1455 cmd->supported_rates, sta->uapsd_queues);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001456
Eliad Pellerc690ec82011-08-14 13:17:07 +03001457 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001458 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001459 wl1271_error("failed to initiate cmd add peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001460 goto out_free;
1461 }
1462
1463out_free:
1464 kfree(cmd);
1465
1466out:
1467 return ret;
1468}
1469
Eliad Pellerc690ec82011-08-14 13:17:07 +03001470int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001471{
Eliad Pellerc690ec82011-08-14 13:17:07 +03001472 struct wl12xx_cmd_remove_peer *cmd;
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001473 int ret;
1474
Eliad Pellerc690ec82011-08-14 13:17:07 +03001475 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001476
1477 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1478 if (!cmd) {
1479 ret = -ENOMEM;
1480 goto out;
1481 }
1482
1483 cmd->hlid = hlid;
1484 /* We never send a deauth, mac80211 is in charge of this */
1485 cmd->reason_opcode = 0;
1486 cmd->send_deauth_flag = 0;
1487
Eliad Pellerc690ec82011-08-14 13:17:07 +03001488 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001489 if (ret < 0) {
Eliad Pellerc690ec82011-08-14 13:17:07 +03001490 wl1271_error("failed to initiate cmd remove peer");
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001491 goto out_free;
1492 }
1493
Arik Nemtsov05285cf2010-11-12 17:15:03 +02001494 /*
1495 * We are ok with a timeout here. The event is sometimes not sent
1496 * due to a firmware bug.
1497 */
Eliad Pellerc690ec82011-08-14 13:17:07 +03001498 wl1271_cmd_wait_for_event_or_timeout(wl,
1499 PEER_REMOVE_COMPLETE_EVENT_ID);
Arik Nemtsov98bdaab2010-10-16 18:08:58 +02001500
1501out_free:
1502 kfree(cmd);
1503
1504out:
1505 return ret;
1506}
Ido Yariv95dac04f2011-06-06 14:57:06 +03001507
1508int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1509{
1510 struct wl12xx_cmd_config_fwlog *cmd;
1511 int ret = 0;
1512
1513 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1514
1515 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1516 if (!cmd) {
1517 ret = -ENOMEM;
1518 goto out;
1519 }
1520
1521 cmd->logger_mode = wl->conf.fwlog.mode;
1522 cmd->log_severity = wl->conf.fwlog.severity;
1523 cmd->timestamp = wl->conf.fwlog.timestamp;
1524 cmd->output = wl->conf.fwlog.output;
1525 cmd->threshold = wl->conf.fwlog.threshold;
1526
1527 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1528 if (ret < 0) {
1529 wl1271_error("failed to send config firmware logger command");
1530 goto out_free;
1531 }
1532
1533out_free:
1534 kfree(cmd);
1535
1536out:
1537 return ret;
1538}
1539
1540int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1541{
1542 struct wl12xx_cmd_start_fwlog *cmd;
1543 int ret = 0;
1544
1545 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1546
1547 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1548 if (!cmd) {
1549 ret = -ENOMEM;
1550 goto out;
1551 }
1552
1553 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1554 if (ret < 0) {
1555 wl1271_error("failed to send start firmware logger command");
1556 goto out_free;
1557 }
1558
1559out_free:
1560 kfree(cmd);
1561
1562out:
1563 return ret;
1564}
1565
1566int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1567{
1568 struct wl12xx_cmd_stop_fwlog *cmd;
1569 int ret = 0;
1570
1571 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1572
1573 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1574 if (!cmd) {
1575 ret = -ENOMEM;
1576 goto out;
1577 }
1578
1579 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1580 if (ret < 0) {
1581 wl1271_error("failed to send stop firmware logger command");
1582 goto out_free;
1583 }
1584
1585out_free:
1586 kfree(cmd);
1587
1588out:
1589 return ret;
1590}
Eliad Pellera7cba382011-08-14 13:17:16 +03001591
1592static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1593{
1594 struct wl12xx_cmd_roc *cmd;
1595 int ret = 0;
1596
1597 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1598
1599 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1600 return -EINVAL;
1601
1602 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1603 if (!cmd) {
1604 ret = -ENOMEM;
1605 goto out;
1606 }
1607
1608 cmd->role_id = role_id;
1609 cmd->channel = wl->channel;
1610 switch (wl->band) {
1611 case IEEE80211_BAND_2GHZ:
1612 cmd->band = RADIO_BAND_2_4GHZ;
1613 break;
1614 case IEEE80211_BAND_5GHZ:
1615 cmd->band = RADIO_BAND_5GHZ;
1616 break;
1617 default:
1618 wl1271_error("roc - unknown band: %d", (int)wl->band);
1619 ret = -EINVAL;
1620 goto out_free;
1621 }
1622
1623
1624 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1625 if (ret < 0) {
1626 wl1271_error("failed to send ROC command");
1627 goto out_free;
1628 }
1629
1630out_free:
1631 kfree(cmd);
1632
1633out:
1634 return ret;
1635}
1636
1637static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1638{
1639 struct wl12xx_cmd_croc *cmd;
1640 int ret = 0;
1641
1642 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1643
1644 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1645 if (!cmd) {
1646 ret = -ENOMEM;
1647 goto out;
1648 }
1649 cmd->role_id = role_id;
1650
1651 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1652 sizeof(*cmd), 0);
1653 if (ret < 0) {
1654 wl1271_error("failed to send ROC command");
1655 goto out_free;
1656 }
1657
1658out_free:
1659 kfree(cmd);
1660
1661out:
1662 return ret;
1663}
Eliad Peller251c1772011-08-14 13:17:17 +03001664
1665int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1666{
1667 int ret = 0;
1668
1669 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1670 return 0;
1671
1672 ret = wl12xx_cmd_roc(wl, role_id);
1673 if (ret < 0)
1674 goto out;
1675
1676 ret = wl1271_cmd_wait_for_event(wl,
1677 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1678 if (ret < 0) {
1679 wl1271_error("cmd roc event completion error");
1680 goto out;
1681 }
1682
1683 __set_bit(role_id, wl->roc_map);
1684out:
1685 return ret;
1686}
1687
1688int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1689{
1690 int ret = 0;
1691
1692 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1693 return 0;
1694
1695 ret = wl12xx_cmd_croc(wl, role_id);
1696 if (ret < 0)
1697 goto out;
1698
1699 __clear_bit(role_id, wl->roc_map);
1700out:
1701 return ret;
1702}