wl12xx: move ap_global_hlid and ap_bcast_hlid into wlvif
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / wl12xx / cmd.c
blobd0124e63dad1685eaa57b56370457fbc9ab5048d
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 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
124 if (!gen_parms)
125 return -ENOMEM;
127 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
129 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
131 if (gp->tx_bip_fem_auto_detect)
132 answer = true;
134 /* Override the REF CLK from the NVS with the one from platform data */
135 gen_parms->general_params.ref_clock = wl->ref_clock;
137 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
138 if (ret < 0) {
139 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
140 goto out;
143 gp->tx_bip_fem_manufacturer =
144 gen_parms->general_params.tx_bip_fem_manufacturer;
146 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
147 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
149 out:
150 kfree(gen_parms);
151 return ret;
154 int wl128x_cmd_general_parms(struct wl1271 *wl)
156 struct wl128x_general_parms_cmd *gen_parms;
157 struct wl128x_ini_general_params *gp =
158 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
159 bool answer = false;
160 int ret;
162 if (!wl->nvs)
163 return -ENODEV;
165 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
166 if (!gen_parms)
167 return -ENOMEM;
169 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
171 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
173 if (gp->tx_bip_fem_auto_detect)
174 answer = true;
176 /* Replace REF and TCXO CLKs with the ones from platform data */
177 gen_parms->general_params.ref_clock = wl->ref_clock;
178 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
180 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
181 if (ret < 0) {
182 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
183 goto out;
186 gp->tx_bip_fem_manufacturer =
187 gen_parms->general_params.tx_bip_fem_manufacturer;
189 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
190 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
192 out:
193 kfree(gen_parms);
194 return ret;
197 int wl1271_cmd_radio_parms(struct wl1271 *wl)
199 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
200 struct wl1271_radio_parms_cmd *radio_parms;
201 struct wl1271_ini_general_params *gp = &nvs->general_params;
202 int ret;
204 if (!wl->nvs)
205 return -ENODEV;
207 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
208 if (!radio_parms)
209 return -ENOMEM;
211 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
213 /* 2.4GHz parameters */
214 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
215 sizeof(struct wl1271_ini_band_params_2));
216 memcpy(&radio_parms->dyn_params_2,
217 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
218 sizeof(struct wl1271_ini_fem_params_2));
220 /* 5GHz parameters */
221 memcpy(&radio_parms->static_params_5,
222 &nvs->stat_radio_params_5,
223 sizeof(struct wl1271_ini_band_params_5));
224 memcpy(&radio_parms->dyn_params_5,
225 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
226 sizeof(struct wl1271_ini_fem_params_5));
228 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
229 radio_parms, sizeof(*radio_parms));
231 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
232 if (ret < 0)
233 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
235 kfree(radio_parms);
236 return ret;
239 int wl128x_cmd_radio_parms(struct wl1271 *wl)
241 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
242 struct wl128x_radio_parms_cmd *radio_parms;
243 struct wl128x_ini_general_params *gp = &nvs->general_params;
244 int ret;
246 if (!wl->nvs)
247 return -ENODEV;
249 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
250 if (!radio_parms)
251 return -ENOMEM;
253 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
255 /* 2.4GHz parameters */
256 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
257 sizeof(struct wl128x_ini_band_params_2));
258 memcpy(&radio_parms->dyn_params_2,
259 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
260 sizeof(struct wl128x_ini_fem_params_2));
262 /* 5GHz parameters */
263 memcpy(&radio_parms->static_params_5,
264 &nvs->stat_radio_params_5,
265 sizeof(struct wl128x_ini_band_params_5));
266 memcpy(&radio_parms->dyn_params_5,
267 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
268 sizeof(struct wl128x_ini_fem_params_5));
270 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
272 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
273 radio_parms, sizeof(*radio_parms));
275 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
276 if (ret < 0)
277 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
279 kfree(radio_parms);
280 return ret;
283 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
285 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
286 struct conf_rf_settings *rf = &wl->conf.rf;
287 int ret;
289 if (!wl->nvs)
290 return -ENODEV;
292 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
293 if (!ext_radio_parms)
294 return -ENOMEM;
296 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
298 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
299 rf->tx_per_channel_power_compensation_2,
300 CONF_TX_PWR_COMPENSATION_LEN_2);
301 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
302 rf->tx_per_channel_power_compensation_5,
303 CONF_TX_PWR_COMPENSATION_LEN_5);
305 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
306 ext_radio_parms, sizeof(*ext_radio_parms));
308 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
309 if (ret < 0)
310 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
312 kfree(ext_radio_parms);
313 return ret;
317 * Poll the mailbox event field until any of the bits in the mask is set or a
318 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
320 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
322 u32 events_vector, event;
323 unsigned long timeout;
325 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
327 do {
328 if (time_after(jiffies, timeout)) {
329 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
330 (int)mask);
331 return -ETIMEDOUT;
334 msleep(1);
336 /* read from both event fields */
337 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
338 sizeof(events_vector), false);
339 event = events_vector & mask;
340 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
341 sizeof(events_vector), false);
342 event |= events_vector & mask;
343 } while (!event);
345 return 0;
348 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
350 int ret;
352 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
353 if (ret != 0) {
354 wl12xx_queue_recovery_work(wl);
355 return ret;
358 return 0;
361 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
362 u8 *role_id)
364 struct wl12xx_cmd_role_enable *cmd;
365 int ret;
367 wl1271_debug(DEBUG_CMD, "cmd role enable");
369 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
370 return -EBUSY;
372 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
373 if (!cmd) {
374 ret = -ENOMEM;
375 goto out;
378 /* get role id */
379 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
380 if (cmd->role_id >= WL12XX_MAX_ROLES) {
381 ret = -EBUSY;
382 goto out_free;
385 memcpy(cmd->mac_address, addr, ETH_ALEN);
386 cmd->role_type = role_type;
388 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
389 if (ret < 0) {
390 wl1271_error("failed to initiate cmd role enable");
391 goto out_free;
394 __set_bit(cmd->role_id, wl->roles_map);
395 *role_id = cmd->role_id;
397 out_free:
398 kfree(cmd);
400 out:
401 return ret;
404 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
406 struct wl12xx_cmd_role_disable *cmd;
407 int ret;
409 wl1271_debug(DEBUG_CMD, "cmd role disable");
411 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
412 return -ENOENT;
414 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
415 if (!cmd) {
416 ret = -ENOMEM;
417 goto out;
419 cmd->role_id = *role_id;
421 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
422 if (ret < 0) {
423 wl1271_error("failed to initiate cmd role disable");
424 goto out_free;
427 __clear_bit(*role_id, wl->roles_map);
428 *role_id = WL12XX_INVALID_ROLE_ID;
430 out_free:
431 kfree(cmd);
433 out:
434 return ret;
437 static int wl12xx_allocate_link(struct wl1271 *wl, u8 *hlid)
439 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
440 if (link >= WL12XX_MAX_LINKS)
441 return -EBUSY;
443 __set_bit(link, wl->links_map);
444 *hlid = link;
445 return 0;
448 static void wl12xx_free_link(struct wl1271 *wl, u8 *hlid)
450 if (*hlid == WL12XX_INVALID_LINK_ID)
451 return;
453 __clear_bit(*hlid, wl->links_map);
454 *hlid = WL12XX_INVALID_LINK_ID;
457 static int wl12xx_get_new_session_id(struct wl1271 *wl)
459 if (wl->session_counter >= SESSION_COUNTER_MAX)
460 wl->session_counter = 0;
462 wl->session_counter++;
464 return wl->session_counter;
467 int wl12xx_cmd_role_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
469 struct wl12xx_cmd_role_start *cmd;
470 int ret;
472 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
473 if (!cmd) {
474 ret = -ENOMEM;
475 goto out;
478 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
480 cmd->role_id = wlvif->dev_role_id;
481 if (wl->band == IEEE80211_BAND_5GHZ)
482 cmd->band = WL12XX_BAND_5GHZ;
483 cmd->channel = wl->channel;
485 if (wl->dev_hlid == WL12XX_INVALID_LINK_ID) {
486 ret = wl12xx_allocate_link(wl, &wl->dev_hlid);
487 if (ret)
488 goto out_free;
490 cmd->device.hlid = wl->dev_hlid;
491 cmd->device.session = wl->session_counter;
493 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
494 cmd->role_id, cmd->device.hlid, cmd->device.session);
496 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
497 if (ret < 0) {
498 wl1271_error("failed to initiate cmd role enable");
499 goto err_hlid;
502 goto out_free;
504 err_hlid:
505 /* clear links on error */
506 __clear_bit(wl->dev_hlid, wl->links_map);
507 wl->dev_hlid = WL12XX_INVALID_LINK_ID;
510 out_free:
511 kfree(cmd);
513 out:
514 return ret;
517 int wl12xx_cmd_role_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
519 struct wl12xx_cmd_role_stop *cmd;
520 int ret;
522 if (WARN_ON(wl->dev_hlid == WL12XX_INVALID_LINK_ID))
523 return -EINVAL;
525 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
526 if (!cmd) {
527 ret = -ENOMEM;
528 goto out;
531 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
533 cmd->role_id = wlvif->dev_role_id;
534 cmd->disc_type = DISCONNECT_IMMEDIATE;
535 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
537 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
538 if (ret < 0) {
539 wl1271_error("failed to initiate cmd role stop");
540 goto out_free;
543 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
544 if (ret < 0) {
545 wl1271_error("cmd role stop dev event completion error");
546 goto out_free;
549 wl12xx_free_link(wl, &wl->dev_hlid);
551 out_free:
552 kfree(cmd);
554 out:
555 return ret;
558 int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
560 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
561 struct wl12xx_cmd_role_start *cmd;
562 int ret;
564 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
565 if (!cmd) {
566 ret = -ENOMEM;
567 goto out;
570 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
572 cmd->role_id = wlvif->role_id;
573 if (wl->band == IEEE80211_BAND_5GHZ)
574 cmd->band = WL12XX_BAND_5GHZ;
575 cmd->channel = wl->channel;
576 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
577 cmd->sta.beacon_interval = cpu_to_le16(wl->beacon_int);
578 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
579 cmd->sta.ssid_len = wlvif->ssid_len;
580 memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
581 memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
582 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
584 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
585 ret = wl12xx_allocate_link(wl, &wlvif->sta.hlid);
586 if (ret)
587 goto out_free;
589 cmd->sta.hlid = wlvif->sta.hlid;
590 cmd->sta.session = wl12xx_get_new_session_id(wl);
591 cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
593 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
594 "basic_rate_set: 0x%x, remote_rates: 0x%x",
595 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
596 wlvif->basic_rate_set, wlvif->rate_set);
598 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
599 if (ret < 0) {
600 wl1271_error("failed to initiate cmd role start sta");
601 goto err_hlid;
604 goto out_free;
606 err_hlid:
607 /* clear links on error. */
608 wl12xx_free_link(wl, &wlvif->sta.hlid);
610 out_free:
611 kfree(cmd);
613 out:
614 return ret;
617 /* use this function to stop ibss as well */
618 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
620 struct wl12xx_cmd_role_stop *cmd;
621 int ret;
623 if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
624 return -EINVAL;
626 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
627 if (!cmd) {
628 ret = -ENOMEM;
629 goto out;
632 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
634 cmd->role_id = wlvif->role_id;
635 cmd->disc_type = DISCONNECT_IMMEDIATE;
636 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
638 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
639 if (ret < 0) {
640 wl1271_error("failed to initiate cmd role stop sta");
641 goto out_free;
644 wl12xx_free_link(wl, &wlvif->sta.hlid);
646 out_free:
647 kfree(cmd);
649 out:
650 return ret;
653 int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
655 struct wl12xx_cmd_role_start *cmd;
656 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
657 int ret;
659 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
661 /* trying to use hidden SSID with an old hostapd version */
662 if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
663 wl1271_error("got a null SSID from beacon/bss");
664 ret = -EINVAL;
665 goto out;
668 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
669 if (!cmd) {
670 ret = -ENOMEM;
671 goto out;
674 ret = wl12xx_allocate_link(wl, &wlvif->ap.global_hlid);
675 if (ret < 0)
676 goto out_free;
678 ret = wl12xx_allocate_link(wl, &wlvif->ap.bcast_hlid);
679 if (ret < 0)
680 goto out_free_global;
682 cmd->role_id = wlvif->role_id;
683 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
684 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
685 cmd->ap.global_hlid = wlvif->ap.global_hlid;
686 cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
687 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
688 cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
689 cmd->ap.dtim_interval = bss_conf->dtim_period;
690 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
691 cmd->channel = wl->channel;
693 if (!bss_conf->hidden_ssid) {
694 /* take the SSID from the beacon for backward compatibility */
695 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
696 cmd->ap.ssid_len = wlvif->ssid_len;
697 memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
698 } else {
699 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
700 cmd->ap.ssid_len = bss_conf->ssid_len;
701 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
704 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
706 switch (wl->band) {
707 case IEEE80211_BAND_2GHZ:
708 cmd->band = RADIO_BAND_2_4GHZ;
709 break;
710 case IEEE80211_BAND_5GHZ:
711 cmd->band = RADIO_BAND_5GHZ;
712 break;
713 default:
714 wl1271_warning("ap start - unknown band: %d", (int)wl->band);
715 cmd->band = RADIO_BAND_2_4GHZ;
716 break;
719 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
720 if (ret < 0) {
721 wl1271_error("failed to initiate cmd role start ap");
722 goto out_free_bcast;
725 goto out_free;
727 out_free_bcast:
728 wl12xx_free_link(wl, &wlvif->ap.bcast_hlid);
730 out_free_global:
731 wl12xx_free_link(wl, &wlvif->ap.global_hlid);
733 out_free:
734 kfree(cmd);
736 out:
737 return ret;
740 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
742 struct wl12xx_cmd_role_stop *cmd;
743 int ret;
745 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
746 if (!cmd) {
747 ret = -ENOMEM;
748 goto out;
751 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
753 cmd->role_id = wlvif->role_id;
755 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
756 if (ret < 0) {
757 wl1271_error("failed to initiate cmd role stop ap");
758 goto out_free;
761 wl12xx_free_link(wl, &wlvif->ap.bcast_hlid);
762 wl12xx_free_link(wl, &wlvif->ap.global_hlid);
764 out_free:
765 kfree(cmd);
767 out:
768 return ret;
771 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
773 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
774 struct wl12xx_cmd_role_start *cmd;
775 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
776 int ret;
778 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
779 if (!cmd) {
780 ret = -ENOMEM;
781 goto out;
784 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
786 cmd->role_id = wlvif->role_id;
787 if (wl->band == IEEE80211_BAND_5GHZ)
788 cmd->band = WL12XX_BAND_5GHZ;
789 cmd->channel = wl->channel;
790 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
791 cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
792 cmd->ibss.dtim_interval = bss_conf->dtim_period;
793 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
794 cmd->ibss.ssid_len = wlvif->ssid_len;
795 memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
796 memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
797 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
799 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
800 ret = wl12xx_allocate_link(wl, &wlvif->sta.hlid);
801 if (ret)
802 goto out_free;
804 cmd->ibss.hlid = wlvif->sta.hlid;
805 cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
807 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
808 "basic_rate_set: 0x%x, remote_rates: 0x%x",
809 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
810 wlvif->basic_rate_set, wlvif->rate_set);
812 wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
813 vif->bss_conf.bssid);
815 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
816 if (ret < 0) {
817 wl1271_error("failed to initiate cmd role enable");
818 goto err_hlid;
821 goto out_free;
823 err_hlid:
824 /* clear links on error. */
825 wl12xx_free_link(wl, &wlvif->sta.hlid);
827 out_free:
828 kfree(cmd);
830 out:
831 return ret;
836 * send test command to firmware
838 * @wl: wl struct
839 * @buf: buffer containing the command, with all headers, must work with dma
840 * @len: length of the buffer
841 * @answer: is answer needed
843 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
845 int ret;
846 size_t res_len = 0;
848 wl1271_debug(DEBUG_CMD, "cmd test");
850 if (answer)
851 res_len = buf_len;
853 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
855 if (ret < 0) {
856 wl1271_warning("TEST command failed");
857 return ret;
860 return ret;
864 * read acx from firmware
866 * @wl: wl struct
867 * @id: acx id
868 * @buf: buffer for the response, including all headers, must work with dma
869 * @len: length of buf
871 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
873 struct acx_header *acx = buf;
874 int ret;
876 wl1271_debug(DEBUG_CMD, "cmd interrogate");
878 acx->id = cpu_to_le16(id);
880 /* payload length, does not include any headers */
881 acx->len = cpu_to_le16(len - sizeof(*acx));
883 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
884 if (ret < 0)
885 wl1271_error("INTERROGATE command failed");
887 return ret;
891 * write acx value to firmware
893 * @wl: wl struct
894 * @id: acx id
895 * @buf: buffer containing acx, including all headers, must work with dma
896 * @len: length of buf
898 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
900 struct acx_header *acx = buf;
901 int ret;
903 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
905 acx->id = cpu_to_le16(id);
907 /* payload length, does not include any headers */
908 acx->len = cpu_to_le16(len - sizeof(*acx));
910 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
911 if (ret < 0) {
912 wl1271_warning("CONFIGURE command NOK");
913 return ret;
916 return 0;
919 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
921 struct cmd_enabledisable_path *cmd;
922 int ret;
923 u16 cmd_rx, cmd_tx;
925 wl1271_debug(DEBUG_CMD, "cmd data path");
927 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
928 if (!cmd) {
929 ret = -ENOMEM;
930 goto out;
933 /* the channel here is only used for calibration, so hardcoded to 1 */
934 cmd->channel = 1;
936 if (enable) {
937 cmd_rx = CMD_ENABLE_RX;
938 cmd_tx = CMD_ENABLE_TX;
939 } else {
940 cmd_rx = CMD_DISABLE_RX;
941 cmd_tx = CMD_DISABLE_TX;
944 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
945 if (ret < 0) {
946 wl1271_error("rx %s cmd for channel %d failed",
947 enable ? "start" : "stop", cmd->channel);
948 goto out;
951 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
952 enable ? "start" : "stop", cmd->channel);
954 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
955 if (ret < 0) {
956 wl1271_error("tx %s cmd for channel %d failed",
957 enable ? "start" : "stop", cmd->channel);
958 goto out;
961 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
962 enable ? "start" : "stop", cmd->channel);
964 out:
965 kfree(cmd);
966 return ret;
969 int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
970 u8 ps_mode)
972 struct wl1271_cmd_ps_params *ps_params = NULL;
973 int ret = 0;
975 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
977 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
978 if (!ps_params) {
979 ret = -ENOMEM;
980 goto out;
983 ps_params->role_id = wlvif->role_id;
984 ps_params->ps_mode = ps_mode;
986 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
987 sizeof(*ps_params), 0);
988 if (ret < 0) {
989 wl1271_error("cmd set_ps_mode failed");
990 goto out;
993 out:
994 kfree(ps_params);
995 return ret;
998 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
999 void *buf, size_t buf_len, int index, u32 rates)
1001 struct wl1271_cmd_template_set *cmd;
1002 int ret = 0;
1004 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
1006 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1007 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1009 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1010 if (!cmd) {
1011 ret = -ENOMEM;
1012 goto out;
1015 cmd->len = cpu_to_le16(buf_len);
1016 cmd->template_type = template_id;
1017 cmd->enabled_rates = cpu_to_le32(rates);
1018 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1019 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1020 cmd->index = index;
1022 if (buf)
1023 memcpy(cmd->template_data, buf, buf_len);
1025 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1026 if (ret < 0) {
1027 wl1271_warning("cmd set_template failed: %d", ret);
1028 goto out_free;
1031 out_free:
1032 kfree(cmd);
1034 out:
1035 return ret;
1038 int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1040 struct sk_buff *skb = NULL;
1041 int size;
1042 void *ptr;
1043 int ret = -ENOMEM;
1046 if (wlvif->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,
1051 wl12xx_wlvif_to_vif(wlvif));
1052 if (!skb)
1053 goto out;
1054 size = skb->len;
1055 ptr = skb->data;
1058 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
1059 wlvif->basic_rate);
1061 out:
1062 dev_kfree_skb(skb);
1063 if (ret)
1064 wl1271_warning("cmd buld null data failed %d", ret);
1066 return ret;
1070 int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1071 struct wl12xx_vif *wlvif)
1073 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1074 struct sk_buff *skb = NULL;
1075 int ret = -ENOMEM;
1077 skb = ieee80211_nullfunc_get(wl->hw, vif);
1078 if (!skb)
1079 goto out;
1081 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1082 skb->data, skb->len,
1083 CMD_TEMPL_KLV_IDX_NULL_DATA,
1084 wlvif->basic_rate);
1086 out:
1087 dev_kfree_skb(skb);
1088 if (ret)
1089 wl1271_warning("cmd build klv null data failed %d", ret);
1091 return ret;
1095 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1096 u16 aid)
1098 struct sk_buff *skb;
1099 int ret = 0;
1101 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1102 if (!skb)
1103 goto out;
1105 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
1106 skb->len, 0, wlvif->basic_rate_set);
1108 out:
1109 dev_kfree_skb(skb);
1110 return ret;
1113 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1114 const u8 *ssid, size_t ssid_len,
1115 const u8 *ie, size_t ie_len, u8 band)
1117 struct sk_buff *skb;
1118 int ret;
1119 u32 rate;
1121 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1122 ie, ie_len);
1123 if (!skb) {
1124 ret = -ENOMEM;
1125 goto out;
1128 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
1130 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
1131 if (band == IEEE80211_BAND_2GHZ)
1132 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1133 skb->data, skb->len, 0, rate);
1134 else
1135 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1136 skb->data, skb->len, 0, rate);
1138 out:
1139 dev_kfree_skb(skb);
1140 return ret;
1143 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1144 struct sk_buff *skb)
1146 int ret;
1147 u32 rate;
1149 if (!skb)
1150 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1151 if (!skb)
1152 goto out;
1154 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1156 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
1157 if (wl->band == IEEE80211_BAND_2GHZ)
1158 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1159 skb->data, skb->len, 0, rate);
1160 else
1161 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1162 skb->data, skb->len, 0, rate);
1164 if (ret < 0)
1165 wl1271_error("Unable to set ap probe request template.");
1167 out:
1168 return skb;
1171 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1172 __be32 ip_addr)
1174 int ret;
1175 struct wl12xx_arp_rsp_template tmpl;
1176 struct ieee80211_hdr_3addr *hdr;
1177 struct arphdr *arp_hdr;
1179 memset(&tmpl, 0, sizeof(tmpl));
1181 /* mac80211 header */
1182 hdr = &tmpl.hdr;
1183 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1184 IEEE80211_STYPE_DATA |
1185 IEEE80211_FCTL_TODS);
1186 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1187 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1188 memset(hdr->addr3, 0xff, ETH_ALEN);
1190 /* llc layer */
1191 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1192 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
1194 /* arp header */
1195 arp_hdr = &tmpl.arp_hdr;
1196 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1197 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1198 arp_hdr->ar_hln = ETH_ALEN;
1199 arp_hdr->ar_pln = 4;
1200 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1202 /* arp payload */
1203 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1204 tmpl.sender_ip = ip_addr;
1206 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1207 &tmpl, sizeof(tmpl), 0,
1208 wlvif->basic_rate);
1210 return ret;
1213 int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1215 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1216 struct ieee80211_qos_hdr template;
1218 memset(&template, 0, sizeof(template));
1220 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1221 memcpy(template.addr2, vif->addr, ETH_ALEN);
1222 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1224 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1225 IEEE80211_STYPE_QOS_NULLFUNC |
1226 IEEE80211_FCTL_TODS);
1228 /* FIXME: not sure what priority to use here */
1229 template.qos_ctrl = cpu_to_le16(0);
1231 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
1232 sizeof(template), 0,
1233 wlvif->basic_rate);
1236 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1238 struct wl1271_cmd_set_keys *cmd;
1239 int ret = 0;
1241 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1243 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1244 if (!cmd) {
1245 ret = -ENOMEM;
1246 goto out;
1249 cmd->hlid = hlid;
1250 cmd->key_id = id;
1251 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1252 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1253 cmd->key_type = KEY_WEP;
1255 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1256 if (ret < 0) {
1257 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1258 goto out;
1261 out:
1262 kfree(cmd);
1264 return ret;
1267 int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1268 u16 action, u8 id, u8 key_type,
1269 u8 key_size, const u8 *key, const u8 *addr,
1270 u32 tx_seq_32, u16 tx_seq_16)
1272 struct wl1271_cmd_set_keys *cmd;
1273 int ret = 0;
1275 /* hlid might have already been deleted */
1276 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1277 return 0;
1279 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1280 if (!cmd) {
1281 ret = -ENOMEM;
1282 goto out;
1285 cmd->hlid = wlvif->sta.hlid;
1287 if (key_type == KEY_WEP)
1288 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1289 else if (is_broadcast_ether_addr(addr))
1290 cmd->lid_key_type = BROADCAST_LID_TYPE;
1291 else
1292 cmd->lid_key_type = UNICAST_LID_TYPE;
1294 cmd->key_action = cpu_to_le16(action);
1295 cmd->key_size = key_size;
1296 cmd->key_type = key_type;
1298 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1299 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1301 cmd->key_id = id;
1303 if (key_type == KEY_TKIP) {
1305 * We get the key in the following form:
1306 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1307 * but the target is expecting:
1308 * TKIP - RX MIC - TX MIC
1310 memcpy(cmd->key, key, 16);
1311 memcpy(cmd->key + 16, key + 24, 8);
1312 memcpy(cmd->key + 24, key + 16, 8);
1314 } else {
1315 memcpy(cmd->key, key, key_size);
1318 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1320 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1321 if (ret < 0) {
1322 wl1271_warning("could not set keys");
1323 goto out;
1326 out:
1327 kfree(cmd);
1329 return ret;
1333 * TODO: merge with sta/ibss into 1 set_key function.
1334 * note there are slight diffs
1336 int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1337 u16 action, u8 id, u8 key_type,
1338 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1339 u16 tx_seq_16)
1341 struct wl1271_cmd_set_keys *cmd;
1342 int ret = 0;
1343 u8 lid_type;
1345 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1346 if (!cmd)
1347 return -ENOMEM;
1349 if (hlid == wlvif->ap.bcast_hlid) {
1350 if (key_type == KEY_WEP)
1351 lid_type = WEP_DEFAULT_LID_TYPE;
1352 else
1353 lid_type = BROADCAST_LID_TYPE;
1354 } else {
1355 lid_type = UNICAST_LID_TYPE;
1358 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1359 " hlid: %d", (int)action, (int)id, (int)lid_type,
1360 (int)key_type, (int)hlid);
1362 cmd->lid_key_type = lid_type;
1363 cmd->hlid = hlid;
1364 cmd->key_action = cpu_to_le16(action);
1365 cmd->key_size = key_size;
1366 cmd->key_type = key_type;
1367 cmd->key_id = id;
1368 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1369 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1371 if (key_type == KEY_TKIP) {
1373 * We get the key in the following form:
1374 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1375 * but the target is expecting:
1376 * TKIP - RX MIC - TX MIC
1378 memcpy(cmd->key, key, 16);
1379 memcpy(cmd->key + 16, key + 24, 8);
1380 memcpy(cmd->key + 24, key + 16, 8);
1381 } else {
1382 memcpy(cmd->key, key, key_size);
1385 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1387 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1388 if (ret < 0) {
1389 wl1271_warning("could not set ap keys");
1390 goto out;
1393 out:
1394 kfree(cmd);
1395 return ret;
1398 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
1400 struct wl12xx_cmd_set_peer_state *cmd;
1401 int ret = 0;
1403 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1405 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1406 if (!cmd) {
1407 ret = -ENOMEM;
1408 goto out;
1411 cmd->hlid = hlid;
1412 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1414 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1415 if (ret < 0) {
1416 wl1271_error("failed to send set peer state command");
1417 goto out_free;
1420 out_free:
1421 kfree(cmd);
1423 out:
1424 return ret;
1427 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1429 struct wl12xx_cmd_add_peer *cmd;
1430 int i, ret;
1431 u32 sta_rates;
1433 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1435 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1436 if (!cmd) {
1437 ret = -ENOMEM;
1438 goto out;
1441 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1442 cmd->bss_index = WL1271_AP_BSS_INDEX;
1443 cmd->aid = sta->aid;
1444 cmd->hlid = hlid;
1445 cmd->sp_len = sta->max_sp;
1446 cmd->wmm = sta->wme ? 1 : 0;
1448 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1449 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1450 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1451 else
1452 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1454 sta_rates = sta->supp_rates[wl->band];
1455 if (sta->ht_cap.ht_supported)
1456 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1458 cmd->supported_rates =
1459 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1460 wl->band));
1462 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1463 cmd->supported_rates, sta->uapsd_queues);
1465 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1466 if (ret < 0) {
1467 wl1271_error("failed to initiate cmd add peer");
1468 goto out_free;
1471 out_free:
1472 kfree(cmd);
1474 out:
1475 return ret;
1478 int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
1480 struct wl12xx_cmd_remove_peer *cmd;
1481 int ret;
1483 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1485 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1486 if (!cmd) {
1487 ret = -ENOMEM;
1488 goto out;
1491 cmd->hlid = hlid;
1492 /* We never send a deauth, mac80211 is in charge of this */
1493 cmd->reason_opcode = 0;
1494 cmd->send_deauth_flag = 0;
1496 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1497 if (ret < 0) {
1498 wl1271_error("failed to initiate cmd remove peer");
1499 goto out_free;
1503 * We are ok with a timeout here. The event is sometimes not sent
1504 * due to a firmware bug.
1506 wl1271_cmd_wait_for_event_or_timeout(wl,
1507 PEER_REMOVE_COMPLETE_EVENT_ID);
1509 out_free:
1510 kfree(cmd);
1512 out:
1513 return ret;
1516 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1518 struct wl12xx_cmd_config_fwlog *cmd;
1519 int ret = 0;
1521 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1523 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1524 if (!cmd) {
1525 ret = -ENOMEM;
1526 goto out;
1529 cmd->logger_mode = wl->conf.fwlog.mode;
1530 cmd->log_severity = wl->conf.fwlog.severity;
1531 cmd->timestamp = wl->conf.fwlog.timestamp;
1532 cmd->output = wl->conf.fwlog.output;
1533 cmd->threshold = wl->conf.fwlog.threshold;
1535 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1536 if (ret < 0) {
1537 wl1271_error("failed to send config firmware logger command");
1538 goto out_free;
1541 out_free:
1542 kfree(cmd);
1544 out:
1545 return ret;
1548 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1550 struct wl12xx_cmd_start_fwlog *cmd;
1551 int ret = 0;
1553 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1555 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1556 if (!cmd) {
1557 ret = -ENOMEM;
1558 goto out;
1561 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1562 if (ret < 0) {
1563 wl1271_error("failed to send start firmware logger command");
1564 goto out_free;
1567 out_free:
1568 kfree(cmd);
1570 out:
1571 return ret;
1574 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1576 struct wl12xx_cmd_stop_fwlog *cmd;
1577 int ret = 0;
1579 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1581 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1582 if (!cmd) {
1583 ret = -ENOMEM;
1584 goto out;
1587 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1588 if (ret < 0) {
1589 wl1271_error("failed to send stop firmware logger command");
1590 goto out_free;
1593 out_free:
1594 kfree(cmd);
1596 out:
1597 return ret;
1600 static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1602 struct wl12xx_cmd_roc *cmd;
1603 int ret = 0;
1605 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1607 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1608 return -EINVAL;
1610 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1611 if (!cmd) {
1612 ret = -ENOMEM;
1613 goto out;
1616 cmd->role_id = role_id;
1617 cmd->channel = wl->channel;
1618 switch (wl->band) {
1619 case IEEE80211_BAND_2GHZ:
1620 cmd->band = RADIO_BAND_2_4GHZ;
1621 break;
1622 case IEEE80211_BAND_5GHZ:
1623 cmd->band = RADIO_BAND_5GHZ;
1624 break;
1625 default:
1626 wl1271_error("roc - unknown band: %d", (int)wl->band);
1627 ret = -EINVAL;
1628 goto out_free;
1632 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1633 if (ret < 0) {
1634 wl1271_error("failed to send ROC command");
1635 goto out_free;
1638 out_free:
1639 kfree(cmd);
1641 out:
1642 return ret;
1645 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1647 struct wl12xx_cmd_croc *cmd;
1648 int ret = 0;
1650 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1652 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1653 if (!cmd) {
1654 ret = -ENOMEM;
1655 goto out;
1657 cmd->role_id = role_id;
1659 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1660 sizeof(*cmd), 0);
1661 if (ret < 0) {
1662 wl1271_error("failed to send ROC command");
1663 goto out_free;
1666 out_free:
1667 kfree(cmd);
1669 out:
1670 return ret;
1673 int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1675 int ret = 0;
1677 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1678 return 0;
1680 ret = wl12xx_cmd_roc(wl, role_id);
1681 if (ret < 0)
1682 goto out;
1684 ret = wl1271_cmd_wait_for_event(wl,
1685 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1686 if (ret < 0) {
1687 wl1271_error("cmd roc event completion error");
1688 goto out;
1691 __set_bit(role_id, wl->roc_map);
1692 out:
1693 return ret;
1696 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1698 int ret = 0;
1700 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1701 return 0;
1703 ret = wl12xx_cmd_croc(wl, role_id);
1704 if (ret < 0)
1705 goto out;
1707 __clear_bit(role_id, wl->roc_map);
1708 out:
1709 return ret;
1712 int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1713 struct ieee80211_channel_switch *ch_switch)
1715 struct wl12xx_cmd_channel_switch *cmd;
1716 int ret;
1718 wl1271_debug(DEBUG_ACX, "cmd channel switch");
1720 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1721 if (!cmd) {
1722 ret = -ENOMEM;
1723 goto out;
1726 cmd->channel = ch_switch->channel->hw_value;
1727 cmd->switch_time = ch_switch->count;
1728 cmd->tx_suspend = ch_switch->block_tx;
1729 cmd->flush = 0; /* this value is ignored by the FW */
1731 ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1732 if (ret < 0) {
1733 wl1271_error("failed to send channel switch command");
1734 goto out_free;
1737 out_free:
1738 kfree(cmd);
1740 out:
1741 return ret;
1744 int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1746 struct wl12xx_cmd_stop_channel_switch *cmd;
1747 int ret;
1749 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1751 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1752 if (!cmd) {
1753 ret = -ENOMEM;
1754 goto out;
1757 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1758 if (ret < 0) {
1759 wl1271_error("failed to stop channel switch command");
1760 goto out_free;
1763 out_free:
1764 kfree(cmd);
1766 out:
1767 return ret;