wl12xx: Validate FEM index from ini file and FW
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / wl12xx / cmd.c
blob54a0d667ff4311fad05841874c3498fb13fc0782
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2009-2010 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/spi/spi.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ieee80211.h>
29 #include <linux/slab.h>
31 #include "wl12xx.h"
32 #include "reg.h"
33 #include "io.h"
34 #include "acx.h"
35 #include "wl12xx_80211.h"
36 #include "cmd.h"
37 #include "event.h"
38 #include "tx.h"
40 #define WL1271_CMD_FAST_POLL_COUNT 50
43 * send command to firmware
45 * @wl: wl struct
46 * @id: command id
47 * @buf: buffer containing the command, must work with dma
48 * @len: length of the buffer
50 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
51 size_t res_len)
53 struct wl1271_cmd_header *cmd;
54 unsigned long timeout;
55 u32 intr;
56 int ret = 0;
57 u16 status;
58 u16 poll_count = 0;
60 cmd = buf;
61 cmd->id = cpu_to_le16(id);
62 cmd->status = 0;
64 WARN_ON(len % 4 != 0);
65 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
67 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
69 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
71 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
73 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
74 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
75 if (time_after(jiffies, timeout)) {
76 wl1271_error("command complete timeout");
77 ret = -ETIMEDOUT;
78 goto fail;
81 poll_count++;
82 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
83 udelay(10);
84 else
85 msleep(1);
87 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
90 /* read back the status code of the command */
91 if (res_len == 0)
92 res_len = sizeof(struct wl1271_cmd_header);
93 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
95 status = le16_to_cpu(cmd->status);
96 if (status != CMD_STATUS_SUCCESS) {
97 wl1271_error("command execute failure %d", status);
98 ret = -EIO;
99 goto fail;
102 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
103 WL1271_ACX_INTR_CMD_COMPLETE);
104 return 0;
106 fail:
107 WARN_ON(1);
108 wl12xx_queue_recovery_work(wl);
109 return ret;
112 int wl1271_cmd_general_parms(struct wl1271 *wl)
114 struct wl1271_general_parms_cmd *gen_parms;
115 struct wl1271_ini_general_params *gp =
116 &((struct wl1271_nvs_file *)wl->nvs)->general_params;
117 bool answer = false;
118 int ret;
120 if (!wl->nvs)
121 return -ENODEV;
123 if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
124 wl1271_warning("FEM index from INI out of bounds");
125 return -EINVAL;
128 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
129 if (!gen_parms)
130 return -ENOMEM;
132 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
134 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
136 if (gp->tx_bip_fem_auto_detect)
137 answer = true;
139 /* Override the REF CLK from the NVS with the one from platform data */
140 gen_parms->general_params.ref_clock = wl->ref_clock;
142 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;
148 gp->tx_bip_fem_manufacturer =
149 gen_parms->general_params.tx_bip_fem_manufacturer;
151 if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
152 wl1271_warning("FEM index from FW out of bounds");
153 ret = -EINVAL;
154 goto out;
157 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
158 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
160 out:
161 kfree(gen_parms);
162 return ret;
165 int wl128x_cmd_general_parms(struct wl1271 *wl)
167 struct wl128x_general_parms_cmd *gen_parms;
168 struct wl128x_ini_general_params *gp =
169 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
170 bool answer = false;
171 int ret;
173 if (!wl->nvs)
174 return -ENODEV;
176 if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
177 wl1271_warning("FEM index from ini out of bounds");
178 return -EINVAL;
181 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
182 if (!gen_parms)
183 return -ENOMEM;
185 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
187 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
189 if (gp->tx_bip_fem_auto_detect)
190 answer = true;
192 /* Replace REF and TCXO CLKs with the ones from platform data */
193 gen_parms->general_params.ref_clock = wl->ref_clock;
194 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
196 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
197 if (ret < 0) {
198 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
199 goto out;
202 gp->tx_bip_fem_manufacturer =
203 gen_parms->general_params.tx_bip_fem_manufacturer;
205 if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
206 wl1271_warning("FEM index from FW out of bounds");
207 ret = -EINVAL;
208 goto out;
211 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
212 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
214 out:
215 kfree(gen_parms);
216 return ret;
219 int wl1271_cmd_radio_parms(struct wl1271 *wl)
221 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
222 struct wl1271_radio_parms_cmd *radio_parms;
223 struct wl1271_ini_general_params *gp = &nvs->general_params;
224 int ret;
226 if (!wl->nvs)
227 return -ENODEV;
229 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
230 if (!radio_parms)
231 return -ENOMEM;
233 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
235 /* 2.4GHz parameters */
236 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
237 sizeof(struct wl1271_ini_band_params_2));
238 memcpy(&radio_parms->dyn_params_2,
239 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
240 sizeof(struct wl1271_ini_fem_params_2));
242 /* 5GHz parameters */
243 memcpy(&radio_parms->static_params_5,
244 &nvs->stat_radio_params_5,
245 sizeof(struct wl1271_ini_band_params_5));
246 memcpy(&radio_parms->dyn_params_5,
247 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
248 sizeof(struct wl1271_ini_fem_params_5));
250 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
251 radio_parms, sizeof(*radio_parms));
253 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
254 if (ret < 0)
255 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
257 kfree(radio_parms);
258 return ret;
261 int wl128x_cmd_radio_parms(struct wl1271 *wl)
263 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
264 struct wl128x_radio_parms_cmd *radio_parms;
265 struct wl128x_ini_general_params *gp = &nvs->general_params;
266 int ret;
268 if (!wl->nvs)
269 return -ENODEV;
271 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
272 if (!radio_parms)
273 return -ENOMEM;
275 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
277 /* 2.4GHz parameters */
278 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
279 sizeof(struct wl128x_ini_band_params_2));
280 memcpy(&radio_parms->dyn_params_2,
281 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
282 sizeof(struct wl128x_ini_fem_params_2));
284 /* 5GHz parameters */
285 memcpy(&radio_parms->static_params_5,
286 &nvs->stat_radio_params_5,
287 sizeof(struct wl128x_ini_band_params_5));
288 memcpy(&radio_parms->dyn_params_5,
289 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
290 sizeof(struct wl128x_ini_fem_params_5));
292 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
294 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
295 radio_parms, sizeof(*radio_parms));
297 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
298 if (ret < 0)
299 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
301 kfree(radio_parms);
302 return ret;
305 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
307 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
308 struct conf_rf_settings *rf = &wl->conf.rf;
309 int ret;
311 if (!wl->nvs)
312 return -ENODEV;
314 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
315 if (!ext_radio_parms)
316 return -ENOMEM;
318 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
320 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
321 rf->tx_per_channel_power_compensation_2,
322 CONF_TX_PWR_COMPENSATION_LEN_2);
323 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
324 rf->tx_per_channel_power_compensation_5,
325 CONF_TX_PWR_COMPENSATION_LEN_5);
327 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
328 ext_radio_parms, sizeof(*ext_radio_parms));
330 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
331 if (ret < 0)
332 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
334 kfree(ext_radio_parms);
335 return ret;
339 * Poll the mailbox event field until any of the bits in the mask is set or a
340 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
342 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
344 u32 events_vector, event;
345 unsigned long timeout;
347 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
349 do {
350 if (time_after(jiffies, timeout)) {
351 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
352 (int)mask);
353 return -ETIMEDOUT;
356 msleep(1);
358 /* read from both event fields */
359 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
360 sizeof(events_vector), false);
361 event = events_vector & mask;
362 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
363 sizeof(events_vector), false);
364 event |= events_vector & mask;
365 } while (!event);
367 return 0;
370 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
372 int ret;
374 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
375 if (ret != 0) {
376 wl12xx_queue_recovery_work(wl);
377 return ret;
380 return 0;
383 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 role_type, u8 *role_id)
385 struct wl12xx_cmd_role_enable *cmd;
386 int ret;
388 wl1271_debug(DEBUG_CMD, "cmd role enable");
390 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
391 return -EBUSY;
393 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
394 if (!cmd) {
395 ret = -ENOMEM;
396 goto out;
399 /* get role id */
400 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
401 if (cmd->role_id >= WL12XX_MAX_ROLES) {
402 ret = -EBUSY;
403 goto out_free;
406 memcpy(cmd->mac_address, wl->mac_addr, ETH_ALEN);
407 cmd->role_type = role_type;
409 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
410 if (ret < 0) {
411 wl1271_error("failed to initiate cmd role enable");
412 goto out_free;
415 __set_bit(cmd->role_id, wl->roles_map);
416 *role_id = cmd->role_id;
418 out_free:
419 kfree(cmd);
421 out:
422 return ret;
425 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
427 struct wl12xx_cmd_role_disable *cmd;
428 int ret;
430 wl1271_debug(DEBUG_CMD, "cmd role disable");
432 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
433 return -ENOENT;
435 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
436 if (!cmd) {
437 ret = -ENOMEM;
438 goto out;
440 cmd->role_id = *role_id;
442 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
443 if (ret < 0) {
444 wl1271_error("failed to initiate cmd role disable");
445 goto out_free;
448 __clear_bit(*role_id, wl->roles_map);
449 *role_id = WL12XX_INVALID_ROLE_ID;
451 out_free:
452 kfree(cmd);
454 out:
455 return ret;
458 static int wl12xx_allocate_link(struct wl1271 *wl, u8 *hlid)
460 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
461 if (link >= WL12XX_MAX_LINKS)
462 return -EBUSY;
464 __set_bit(link, wl->links_map);
465 *hlid = link;
466 return 0;
469 static void wl12xx_free_link(struct wl1271 *wl, u8 *hlid)
471 if (*hlid == WL12XX_INVALID_LINK_ID)
472 return;
474 __clear_bit(*hlid, wl->links_map);
475 *hlid = WL12XX_INVALID_LINK_ID;
478 static int wl12xx_get_new_session_id(struct wl1271 *wl)
480 if (wl->session_counter >= SESSION_COUNTER_MAX)
481 wl->session_counter = 0;
483 wl->session_counter++;
485 return wl->session_counter;
488 int wl12xx_cmd_role_start_dev(struct wl1271 *wl)
490 struct wl12xx_cmd_role_start *cmd;
491 int ret;
493 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
494 if (!cmd) {
495 ret = -ENOMEM;
496 goto out;
499 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wl->dev_role_id);
501 cmd->role_id = wl->dev_role_id;
502 if (wl->band == IEEE80211_BAND_5GHZ)
503 cmd->band = WL12XX_BAND_5GHZ;
504 cmd->channel = wl->channel;
506 if (wl->dev_hlid == WL12XX_INVALID_LINK_ID) {
507 ret = wl12xx_allocate_link(wl, &wl->dev_hlid);
508 if (ret)
509 goto out_free;
511 cmd->device.hlid = wl->dev_hlid;
512 cmd->device.session = wl->session_counter;
514 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
515 cmd->role_id, cmd->device.hlid, cmd->device.session);
517 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
518 if (ret < 0) {
519 wl1271_error("failed to initiate cmd role enable");
520 goto err_hlid;
523 goto out_free;
525 err_hlid:
526 /* clear links on error */
527 __clear_bit(wl->dev_hlid, wl->links_map);
528 wl->dev_hlid = WL12XX_INVALID_LINK_ID;
531 out_free:
532 kfree(cmd);
534 out:
535 return ret;
538 int wl12xx_cmd_role_stop_dev(struct wl1271 *wl)
540 struct wl12xx_cmd_role_stop *cmd;
541 int ret;
543 if (WARN_ON(wl->dev_hlid == WL12XX_INVALID_LINK_ID))
544 return -EINVAL;
546 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
547 if (!cmd) {
548 ret = -ENOMEM;
549 goto out;
552 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
554 cmd->role_id = wl->dev_role_id;
555 cmd->disc_type = DISCONNECT_IMMEDIATE;
556 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
558 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
559 if (ret < 0) {
560 wl1271_error("failed to initiate cmd role stop");
561 goto out_free;
564 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
565 if (ret < 0) {
566 wl1271_error("cmd role stop dev event completion error");
567 goto out_free;
570 wl12xx_free_link(wl, &wl->dev_hlid);
572 out_free:
573 kfree(cmd);
575 out:
576 return ret;
579 int wl12xx_cmd_role_start_sta(struct wl1271 *wl)
581 struct wl12xx_cmd_role_start *cmd;
582 int ret;
584 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
585 if (!cmd) {
586 ret = -ENOMEM;
587 goto out;
590 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wl->role_id);
592 cmd->role_id = wl->role_id;
593 if (wl->band == IEEE80211_BAND_5GHZ)
594 cmd->band = WL12XX_BAND_5GHZ;
595 cmd->channel = wl->channel;
596 cmd->sta.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
597 cmd->sta.beacon_interval = cpu_to_le16(wl->beacon_int);
598 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
599 cmd->sta.ssid_len = wl->ssid_len;
600 memcpy(cmd->sta.ssid, wl->ssid, wl->ssid_len);
601 memcpy(cmd->sta.bssid, wl->bssid, ETH_ALEN);
602 cmd->sta.local_rates = cpu_to_le32(wl->rate_set);
604 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
605 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
606 if (ret)
607 goto out_free;
609 cmd->sta.hlid = wl->sta_hlid;
610 cmd->sta.session = wl12xx_get_new_session_id(wl);
611 cmd->sta.remote_rates = cpu_to_le32(wl->rate_set);
613 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
614 "basic_rate_set: 0x%x, remote_rates: 0x%x",
615 wl->role_id, cmd->sta.hlid, cmd->sta.session,
616 wl->basic_rate_set, wl->rate_set);
618 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
619 if (ret < 0) {
620 wl1271_error("failed to initiate cmd role start sta");
621 goto err_hlid;
624 goto out_free;
626 err_hlid:
627 /* clear links on error. */
628 wl12xx_free_link(wl, &wl->sta_hlid);
630 out_free:
631 kfree(cmd);
633 out:
634 return ret;
637 /* use this function to stop ibss as well */
638 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl)
640 struct wl12xx_cmd_role_stop *cmd;
641 int ret;
643 if (WARN_ON(wl->sta_hlid == WL12XX_INVALID_LINK_ID))
644 return -EINVAL;
646 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
647 if (!cmd) {
648 ret = -ENOMEM;
649 goto out;
652 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wl->role_id);
654 cmd->role_id = wl->role_id;
655 cmd->disc_type = DISCONNECT_IMMEDIATE;
656 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
658 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
659 if (ret < 0) {
660 wl1271_error("failed to initiate cmd role stop sta");
661 goto out_free;
664 wl12xx_free_link(wl, &wl->sta_hlid);
666 out_free:
667 kfree(cmd);
669 out:
670 return ret;
673 int wl12xx_cmd_role_start_ap(struct wl1271 *wl)
675 struct wl12xx_cmd_role_start *cmd;
676 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
677 int ret;
679 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wl->role_id);
681 /* trying to use hidden SSID with an old hostapd version */
682 if (wl->ssid_len == 0 && !bss_conf->hidden_ssid) {
683 wl1271_error("got a null SSID from beacon/bss");
684 ret = -EINVAL;
685 goto out;
688 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
689 if (!cmd) {
690 ret = -ENOMEM;
691 goto out;
694 ret = wl12xx_allocate_link(wl, &wl->ap_global_hlid);
695 if (ret < 0)
696 goto out_free;
698 ret = wl12xx_allocate_link(wl, &wl->ap_bcast_hlid);
699 if (ret < 0)
700 goto out_free_global;
702 cmd->role_id = wl->role_id;
703 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
704 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
705 cmd->ap.global_hlid = wl->ap_global_hlid;
706 cmd->ap.broadcast_hlid = wl->ap_bcast_hlid;
707 cmd->ap.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
708 cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
709 cmd->ap.dtim_interval = bss_conf->dtim_period;
710 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
711 cmd->channel = wl->channel;
713 if (!bss_conf->hidden_ssid) {
714 /* take the SSID from the beacon for backward compatibility */
715 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
716 cmd->ap.ssid_len = wl->ssid_len;
717 memcpy(cmd->ap.ssid, wl->ssid, wl->ssid_len);
718 } else {
719 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
720 cmd->ap.ssid_len = bss_conf->ssid_len;
721 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
724 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
726 switch (wl->band) {
727 case IEEE80211_BAND_2GHZ:
728 cmd->band = RADIO_BAND_2_4GHZ;
729 break;
730 case IEEE80211_BAND_5GHZ:
731 cmd->band = RADIO_BAND_5GHZ;
732 break;
733 default:
734 wl1271_warning("ap start - unknown band: %d", (int)wl->band);
735 cmd->band = RADIO_BAND_2_4GHZ;
736 break;
739 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
740 if (ret < 0) {
741 wl1271_error("failed to initiate cmd role start ap");
742 goto out_free_bcast;
745 goto out_free;
747 out_free_bcast:
748 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
750 out_free_global:
751 wl12xx_free_link(wl, &wl->ap_global_hlid);
753 out_free:
754 kfree(cmd);
756 out:
757 return ret;
760 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl)
762 struct wl12xx_cmd_role_stop *cmd;
763 int ret;
765 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
766 if (!cmd) {
767 ret = -ENOMEM;
768 goto out;
771 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wl->role_id);
773 cmd->role_id = wl->role_id;
775 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
776 if (ret < 0) {
777 wl1271_error("failed to initiate cmd role stop ap");
778 goto out_free;
781 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
782 wl12xx_free_link(wl, &wl->ap_global_hlid);
784 out_free:
785 kfree(cmd);
787 out:
788 return ret;
791 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl)
793 struct wl12xx_cmd_role_start *cmd;
794 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
795 int ret;
797 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
798 if (!cmd) {
799 ret = -ENOMEM;
800 goto out;
803 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wl->role_id);
805 cmd->role_id = wl->role_id;
806 if (wl->band == IEEE80211_BAND_5GHZ)
807 cmd->band = WL12XX_BAND_5GHZ;
808 cmd->channel = wl->channel;
809 cmd->ibss.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
810 cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
811 cmd->ibss.dtim_interval = bss_conf->dtim_period;
812 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
813 cmd->ibss.ssid_len = wl->ssid_len;
814 memcpy(cmd->ibss.ssid, wl->ssid, wl->ssid_len);
815 memcpy(cmd->ibss.bssid, wl->bssid, ETH_ALEN);
816 cmd->sta.local_rates = cpu_to_le32(wl->rate_set);
818 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
819 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
820 if (ret)
821 goto out_free;
823 cmd->ibss.hlid = wl->sta_hlid;
824 cmd->ibss.remote_rates = cpu_to_le32(wl->rate_set);
826 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
827 "basic_rate_set: 0x%x, remote_rates: 0x%x",
828 wl->role_id, cmd->sta.hlid, cmd->sta.session,
829 wl->basic_rate_set, wl->rate_set);
831 wl1271_debug(DEBUG_CMD, "wl->bssid = %pM", wl->bssid);
833 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
834 if (ret < 0) {
835 wl1271_error("failed to initiate cmd role enable");
836 goto err_hlid;
839 goto out_free;
841 err_hlid:
842 /* clear links on error. */
843 wl12xx_free_link(wl, &wl->sta_hlid);
845 out_free:
846 kfree(cmd);
848 out:
849 return ret;
854 * send test command to firmware
856 * @wl: wl struct
857 * @buf: buffer containing the command, with all headers, must work with dma
858 * @len: length of the buffer
859 * @answer: is answer needed
861 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
863 int ret;
864 size_t res_len = 0;
866 wl1271_debug(DEBUG_CMD, "cmd test");
868 if (answer)
869 res_len = buf_len;
871 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
873 if (ret < 0) {
874 wl1271_warning("TEST command failed");
875 return ret;
878 return ret;
882 * read acx from firmware
884 * @wl: wl struct
885 * @id: acx id
886 * @buf: buffer for the response, including all headers, must work with dma
887 * @len: length of buf
889 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
891 struct acx_header *acx = buf;
892 int ret;
894 wl1271_debug(DEBUG_CMD, "cmd interrogate");
896 acx->id = cpu_to_le16(id);
898 /* payload length, does not include any headers */
899 acx->len = cpu_to_le16(len - sizeof(*acx));
901 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
902 if (ret < 0)
903 wl1271_error("INTERROGATE command failed");
905 return ret;
909 * write acx value to firmware
911 * @wl: wl struct
912 * @id: acx id
913 * @buf: buffer containing acx, including all headers, must work with dma
914 * @len: length of buf
916 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
918 struct acx_header *acx = buf;
919 int ret;
921 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
923 acx->id = cpu_to_le16(id);
925 /* payload length, does not include any headers */
926 acx->len = cpu_to_le16(len - sizeof(*acx));
928 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
929 if (ret < 0) {
930 wl1271_warning("CONFIGURE command NOK");
931 return ret;
934 return 0;
937 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
939 struct cmd_enabledisable_path *cmd;
940 int ret;
941 u16 cmd_rx, cmd_tx;
943 wl1271_debug(DEBUG_CMD, "cmd data path");
945 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
946 if (!cmd) {
947 ret = -ENOMEM;
948 goto out;
951 /* the channel here is only used for calibration, so hardcoded to 1 */
952 cmd->channel = 1;
954 if (enable) {
955 cmd_rx = CMD_ENABLE_RX;
956 cmd_tx = CMD_ENABLE_TX;
957 } else {
958 cmd_rx = CMD_DISABLE_RX;
959 cmd_tx = CMD_DISABLE_TX;
962 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
963 if (ret < 0) {
964 wl1271_error("rx %s cmd for channel %d failed",
965 enable ? "start" : "stop", cmd->channel);
966 goto out;
969 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
970 enable ? "start" : "stop", cmd->channel);
972 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
973 if (ret < 0) {
974 wl1271_error("tx %s cmd for channel %d failed",
975 enable ? "start" : "stop", cmd->channel);
976 goto out;
979 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
980 enable ? "start" : "stop", cmd->channel);
982 out:
983 kfree(cmd);
984 return ret;
987 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
989 struct wl1271_cmd_ps_params *ps_params = NULL;
990 int ret = 0;
992 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
994 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
995 if (!ps_params) {
996 ret = -ENOMEM;
997 goto out;
1000 ps_params->role_id = wl->role_id;
1001 ps_params->ps_mode = ps_mode;
1003 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
1004 sizeof(*ps_params), 0);
1005 if (ret < 0) {
1006 wl1271_error("cmd set_ps_mode failed");
1007 goto out;
1010 out:
1011 kfree(ps_params);
1012 return ret;
1015 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
1016 void *buf, size_t buf_len, int index, u32 rates)
1018 struct wl1271_cmd_template_set *cmd;
1019 int ret = 0;
1021 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
1023 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1024 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1026 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1027 if (!cmd) {
1028 ret = -ENOMEM;
1029 goto out;
1032 cmd->len = cpu_to_le16(buf_len);
1033 cmd->template_type = template_id;
1034 cmd->enabled_rates = cpu_to_le32(rates);
1035 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1036 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1037 cmd->index = index;
1039 if (buf)
1040 memcpy(cmd->template_data, buf, buf_len);
1042 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1043 if (ret < 0) {
1044 wl1271_warning("cmd set_template failed: %d", ret);
1045 goto out_free;
1048 out_free:
1049 kfree(cmd);
1051 out:
1052 return ret;
1055 int wl1271_cmd_build_null_data(struct wl1271 *wl)
1057 struct sk_buff *skb = NULL;
1058 int size;
1059 void *ptr;
1060 int ret = -ENOMEM;
1063 if (wl->bss_type == BSS_TYPE_IBSS) {
1064 size = sizeof(struct wl12xx_null_data_template);
1065 ptr = NULL;
1066 } else {
1067 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1068 if (!skb)
1069 goto out;
1070 size = skb->len;
1071 ptr = skb->data;
1074 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
1075 wl->basic_rate);
1077 out:
1078 dev_kfree_skb(skb);
1079 if (ret)
1080 wl1271_warning("cmd buld null data failed %d", ret);
1082 return ret;
1086 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
1088 struct sk_buff *skb = NULL;
1089 int ret = -ENOMEM;
1091 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1092 if (!skb)
1093 goto out;
1095 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1096 skb->data, skb->len,
1097 CMD_TEMPL_KLV_IDX_NULL_DATA,
1098 wl->basic_rate);
1100 out:
1101 dev_kfree_skb(skb);
1102 if (ret)
1103 wl1271_warning("cmd build klv null data failed %d", ret);
1105 return ret;
1109 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
1111 struct sk_buff *skb;
1112 int ret = 0;
1114 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1115 if (!skb)
1116 goto out;
1118 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
1119 skb->len, 0, wl->basic_rate_set);
1121 out:
1122 dev_kfree_skb(skb);
1123 return ret;
1126 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1127 const u8 *ssid, size_t ssid_len,
1128 const u8 *ie, size_t ie_len, u8 band)
1130 struct sk_buff *skb;
1131 int ret;
1132 u32 rate;
1134 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1135 ie, ie_len);
1136 if (!skb) {
1137 ret = -ENOMEM;
1138 goto out;
1141 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
1143 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
1144 if (band == IEEE80211_BAND_2GHZ)
1145 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1146 skb->data, skb->len, 0, rate);
1147 else
1148 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1149 skb->data, skb->len, 0, rate);
1151 out:
1152 dev_kfree_skb(skb);
1153 return ret;
1156 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1157 struct sk_buff *skb)
1159 int ret;
1160 u32 rate;
1162 if (!skb)
1163 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1164 if (!skb)
1165 goto out;
1167 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1169 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
1170 if (wl->band == IEEE80211_BAND_2GHZ)
1171 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1172 skb->data, skb->len, 0, rate);
1173 else
1174 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1175 skb->data, skb->len, 0, rate);
1177 if (ret < 0)
1178 wl1271_error("Unable to set ap probe request template.");
1180 out:
1181 return skb;
1184 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
1186 int ret;
1187 struct wl12xx_arp_rsp_template tmpl;
1188 struct ieee80211_hdr_3addr *hdr;
1189 struct arphdr *arp_hdr;
1191 memset(&tmpl, 0, sizeof(tmpl));
1193 /* mac80211 header */
1194 hdr = &tmpl.hdr;
1195 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1196 IEEE80211_STYPE_DATA |
1197 IEEE80211_FCTL_TODS);
1198 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1199 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1200 memset(hdr->addr3, 0xff, ETH_ALEN);
1202 /* llc layer */
1203 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1204 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
1206 /* arp header */
1207 arp_hdr = &tmpl.arp_hdr;
1208 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1209 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1210 arp_hdr->ar_hln = ETH_ALEN;
1211 arp_hdr->ar_pln = 4;
1212 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1214 /* arp payload */
1215 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1216 tmpl.sender_ip = ip_addr;
1218 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1219 &tmpl, sizeof(tmpl), 0,
1220 wl->basic_rate);
1222 return ret;
1225 int wl1271_build_qos_null_data(struct wl1271 *wl)
1227 struct ieee80211_qos_hdr template;
1229 memset(&template, 0, sizeof(template));
1231 memcpy(template.addr1, wl->bssid, ETH_ALEN);
1232 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
1233 memcpy(template.addr3, wl->bssid, ETH_ALEN);
1235 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1236 IEEE80211_STYPE_QOS_NULLFUNC |
1237 IEEE80211_FCTL_TODS);
1239 /* FIXME: not sure what priority to use here */
1240 template.qos_ctrl = cpu_to_le16(0);
1242 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
1243 sizeof(template), 0,
1244 wl->basic_rate);
1247 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1249 struct wl1271_cmd_set_keys *cmd;
1250 int ret = 0;
1252 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1254 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1255 if (!cmd) {
1256 ret = -ENOMEM;
1257 goto out;
1260 cmd->hlid = hlid;
1261 cmd->key_id = id;
1262 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1263 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1264 cmd->key_type = KEY_WEP;
1266 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1267 if (ret < 0) {
1268 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1269 goto out;
1272 out:
1273 kfree(cmd);
1275 return ret;
1278 int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1279 u8 key_size, const u8 *key, const u8 *addr,
1280 u32 tx_seq_32, u16 tx_seq_16)
1282 struct wl1271_cmd_set_keys *cmd;
1283 int ret = 0;
1285 /* hlid might have already been deleted */
1286 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID)
1287 return 0;
1289 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1290 if (!cmd) {
1291 ret = -ENOMEM;
1292 goto out;
1295 cmd->hlid = wl->sta_hlid;
1297 if (key_type == KEY_WEP)
1298 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1299 else if (is_broadcast_ether_addr(addr))
1300 cmd->lid_key_type = BROADCAST_LID_TYPE;
1301 else
1302 cmd->lid_key_type = UNICAST_LID_TYPE;
1304 cmd->key_action = cpu_to_le16(action);
1305 cmd->key_size = key_size;
1306 cmd->key_type = key_type;
1308 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1309 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1311 cmd->key_id = id;
1313 if (key_type == KEY_TKIP) {
1315 * We get the key in the following form:
1316 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1317 * but the target is expecting:
1318 * TKIP - RX MIC - TX MIC
1320 memcpy(cmd->key, key, 16);
1321 memcpy(cmd->key + 16, key + 24, 8);
1322 memcpy(cmd->key + 24, key + 16, 8);
1324 } else {
1325 memcpy(cmd->key, key, key_size);
1328 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1330 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1331 if (ret < 0) {
1332 wl1271_warning("could not set keys");
1333 goto out;
1336 out:
1337 kfree(cmd);
1339 return ret;
1343 * TODO: merge with sta/ibss into 1 set_key function.
1344 * note there are slight diffs
1346 int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1347 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1348 u16 tx_seq_16)
1350 struct wl1271_cmd_set_keys *cmd;
1351 int ret = 0;
1352 u8 lid_type;
1354 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1355 if (!cmd)
1356 return -ENOMEM;
1358 if (hlid == wl->ap_bcast_hlid) {
1359 if (key_type == KEY_WEP)
1360 lid_type = WEP_DEFAULT_LID_TYPE;
1361 else
1362 lid_type = BROADCAST_LID_TYPE;
1363 } else {
1364 lid_type = UNICAST_LID_TYPE;
1367 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1368 " hlid: %d", (int)action, (int)id, (int)lid_type,
1369 (int)key_type, (int)hlid);
1371 cmd->lid_key_type = lid_type;
1372 cmd->hlid = hlid;
1373 cmd->key_action = cpu_to_le16(action);
1374 cmd->key_size = key_size;
1375 cmd->key_type = key_type;
1376 cmd->key_id = id;
1377 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1378 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1380 if (key_type == KEY_TKIP) {
1382 * We get the key in the following form:
1383 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1384 * but the target is expecting:
1385 * TKIP - RX MIC - TX MIC
1387 memcpy(cmd->key, key, 16);
1388 memcpy(cmd->key + 16, key + 24, 8);
1389 memcpy(cmd->key + 24, key + 16, 8);
1390 } else {
1391 memcpy(cmd->key, key, key_size);
1394 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1396 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1397 if (ret < 0) {
1398 wl1271_warning("could not set ap keys");
1399 goto out;
1402 out:
1403 kfree(cmd);
1404 return ret;
1407 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
1409 struct wl12xx_cmd_set_peer_state *cmd;
1410 int ret = 0;
1412 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1414 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1415 if (!cmd) {
1416 ret = -ENOMEM;
1417 goto out;
1420 cmd->hlid = hlid;
1421 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1423 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1424 if (ret < 0) {
1425 wl1271_error("failed to send set peer state command");
1426 goto out_free;
1429 out_free:
1430 kfree(cmd);
1432 out:
1433 return ret;
1436 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1438 struct wl12xx_cmd_add_peer *cmd;
1439 int i, ret;
1440 u32 sta_rates;
1442 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1444 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1445 if (!cmd) {
1446 ret = -ENOMEM;
1447 goto out;
1450 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1451 cmd->bss_index = WL1271_AP_BSS_INDEX;
1452 cmd->aid = sta->aid;
1453 cmd->hlid = hlid;
1454 cmd->sp_len = sta->max_sp;
1455 cmd->wmm = sta->wme ? 1 : 0;
1457 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1458 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1459 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1460 else
1461 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1463 sta_rates = sta->supp_rates[wl->band];
1464 if (sta->ht_cap.ht_supported)
1465 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1467 cmd->supported_rates =
1468 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1469 wl->band));
1471 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1472 cmd->supported_rates, sta->uapsd_queues);
1474 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1475 if (ret < 0) {
1476 wl1271_error("failed to initiate cmd add peer");
1477 goto out_free;
1480 out_free:
1481 kfree(cmd);
1483 out:
1484 return ret;
1487 int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
1489 struct wl12xx_cmd_remove_peer *cmd;
1490 int ret;
1492 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1494 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1495 if (!cmd) {
1496 ret = -ENOMEM;
1497 goto out;
1500 cmd->hlid = hlid;
1501 /* We never send a deauth, mac80211 is in charge of this */
1502 cmd->reason_opcode = 0;
1503 cmd->send_deauth_flag = 0;
1505 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1506 if (ret < 0) {
1507 wl1271_error("failed to initiate cmd remove peer");
1508 goto out_free;
1512 * We are ok with a timeout here. The event is sometimes not sent
1513 * due to a firmware bug.
1515 wl1271_cmd_wait_for_event_or_timeout(wl,
1516 PEER_REMOVE_COMPLETE_EVENT_ID);
1518 out_free:
1519 kfree(cmd);
1521 out:
1522 return ret;
1525 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1527 struct wl12xx_cmd_config_fwlog *cmd;
1528 int ret = 0;
1530 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1532 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1533 if (!cmd) {
1534 ret = -ENOMEM;
1535 goto out;
1538 cmd->logger_mode = wl->conf.fwlog.mode;
1539 cmd->log_severity = wl->conf.fwlog.severity;
1540 cmd->timestamp = wl->conf.fwlog.timestamp;
1541 cmd->output = wl->conf.fwlog.output;
1542 cmd->threshold = wl->conf.fwlog.threshold;
1544 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1545 if (ret < 0) {
1546 wl1271_error("failed to send config firmware logger command");
1547 goto out_free;
1550 out_free:
1551 kfree(cmd);
1553 out:
1554 return ret;
1557 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1559 struct wl12xx_cmd_start_fwlog *cmd;
1560 int ret = 0;
1562 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1564 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1565 if (!cmd) {
1566 ret = -ENOMEM;
1567 goto out;
1570 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1571 if (ret < 0) {
1572 wl1271_error("failed to send start firmware logger command");
1573 goto out_free;
1576 out_free:
1577 kfree(cmd);
1579 out:
1580 return ret;
1583 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1585 struct wl12xx_cmd_stop_fwlog *cmd;
1586 int ret = 0;
1588 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1590 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1591 if (!cmd) {
1592 ret = -ENOMEM;
1593 goto out;
1596 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1597 if (ret < 0) {
1598 wl1271_error("failed to send stop firmware logger command");
1599 goto out_free;
1602 out_free:
1603 kfree(cmd);
1605 out:
1606 return ret;
1609 static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1611 struct wl12xx_cmd_roc *cmd;
1612 int ret = 0;
1614 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1616 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1617 return -EINVAL;
1619 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1620 if (!cmd) {
1621 ret = -ENOMEM;
1622 goto out;
1625 cmd->role_id = role_id;
1626 cmd->channel = wl->channel;
1627 switch (wl->band) {
1628 case IEEE80211_BAND_2GHZ:
1629 cmd->band = RADIO_BAND_2_4GHZ;
1630 break;
1631 case IEEE80211_BAND_5GHZ:
1632 cmd->band = RADIO_BAND_5GHZ;
1633 break;
1634 default:
1635 wl1271_error("roc - unknown band: %d", (int)wl->band);
1636 ret = -EINVAL;
1637 goto out_free;
1641 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1642 if (ret < 0) {
1643 wl1271_error("failed to send ROC command");
1644 goto out_free;
1647 out_free:
1648 kfree(cmd);
1650 out:
1651 return ret;
1654 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1656 struct wl12xx_cmd_croc *cmd;
1657 int ret = 0;
1659 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1661 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1662 if (!cmd) {
1663 ret = -ENOMEM;
1664 goto out;
1666 cmd->role_id = role_id;
1668 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1669 sizeof(*cmd), 0);
1670 if (ret < 0) {
1671 wl1271_error("failed to send ROC command");
1672 goto out_free;
1675 out_free:
1676 kfree(cmd);
1678 out:
1679 return ret;
1682 int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1684 int ret = 0;
1686 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1687 return 0;
1689 ret = wl12xx_cmd_roc(wl, role_id);
1690 if (ret < 0)
1691 goto out;
1693 ret = wl1271_cmd_wait_for_event(wl,
1694 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1695 if (ret < 0) {
1696 wl1271_error("cmd roc event completion error");
1697 goto out;
1700 __set_bit(role_id, wl->roc_map);
1701 out:
1702 return ret;
1705 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1707 int ret = 0;
1709 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1710 return 0;
1712 ret = wl12xx_cmd_croc(wl, role_id);
1713 if (ret < 0)
1714 goto out;
1716 __clear_bit(role_id, wl->roc_map);
1717 out:
1718 return ret;
1721 int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1722 struct ieee80211_channel_switch *ch_switch)
1724 struct wl12xx_cmd_channel_switch *cmd;
1725 int ret;
1727 wl1271_debug(DEBUG_ACX, "cmd channel switch");
1729 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1730 if (!cmd) {
1731 ret = -ENOMEM;
1732 goto out;
1735 cmd->channel = ch_switch->channel->hw_value;
1736 cmd->switch_time = ch_switch->count;
1737 cmd->tx_suspend = ch_switch->block_tx;
1738 cmd->flush = 0; /* this value is ignored by the FW */
1740 ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1741 if (ret < 0) {
1742 wl1271_error("failed to send channel switch command");
1743 goto out_free;
1746 out_free:
1747 kfree(cmd);
1749 out:
1750 return ret;
1753 int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1755 struct wl12xx_cmd_stop_channel_switch *cmd;
1756 int ret;
1758 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1760 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1761 if (!cmd) {
1762 ret = -ENOMEM;
1763 goto out;
1766 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1767 if (ret < 0) {
1768 wl1271_error("failed to stop channel switch command");
1769 goto out_free;
1772 out_free:
1773 kfree(cmd);
1775 out:
1776 return ret;