]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/wireless/wl12xx/cmd.c
wl12xx: don't wait for disconnection event
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / wl12xx / cmd.c
1 /*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009-2010 Nokia Corporation
5 *
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>
26 #include <linux/spi/spi.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ieee80211.h>
29 #include <linux/slab.h>
30
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"
39
40 #define WL1271_CMD_FAST_POLL_COUNT 50
41
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 */
50 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
51 size_t res_len)
52 {
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;
59
60 cmd = buf;
61 cmd->id = cpu_to_le16(id);
62 cmd->status = 0;
63
64 WARN_ON(len % 4 != 0);
65 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
66
67 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
68
69 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
70
71 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
72
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;
79 }
80
81 poll_count++;
82 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
83 udelay(10);
84 else
85 msleep(1);
86
87 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
88 }
89
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);
94
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;
100 }
101
102 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
103 WL1271_ACX_INTR_CMD_COMPLETE);
104 return 0;
105
106 fail:
107 WARN_ON(1);
108 wl12xx_queue_recovery_work(wl);
109 return ret;
110 }
111
112 int wl1271_cmd_general_parms(struct wl1271 *wl)
113 {
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;
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
134 /* Override the REF CLK from the NVS with the one from platform data */
135 gen_parms->general_params.ref_clock = wl->ref_clock;
136
137 /* 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
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;
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
154 out:
155 kfree(gen_parms);
156 return ret;
157 }
158
159 int 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;
164 bool answer = false;
165 int ret;
166
167 if (!wl->nvs)
168 return -ENODEV;
169
170 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
176 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
177
178 if (gp->tx_bip_fem_auto_detect)
179 answer = true;
180
181 /* 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
185 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
186 if (ret < 0) {
187 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
188 goto out;
189 }
190
191 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
197 out:
198 kfree(gen_parms);
199 return ret;
200 }
201
202 int wl1271_cmd_radio_parms(struct wl1271 *wl)
203 {
204 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
205 struct wl1271_radio_parms_cmd *radio_parms;
206 struct wl1271_ini_general_params *gp = &nvs->general_params;
207 int ret;
208
209 if (!wl->nvs)
210 return -ENODEV;
211
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
218 /* 2.4GHz parameters */
219 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
220 sizeof(struct wl1271_ini_band_params_2));
221 memcpy(&radio_parms->dyn_params_2,
222 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
223 sizeof(struct wl1271_ini_fem_params_2));
224
225 /* 5GHz parameters */
226 memcpy(&radio_parms->static_params_5,
227 &nvs->stat_radio_params_5,
228 sizeof(struct wl1271_ini_band_params_5));
229 memcpy(&radio_parms->dyn_params_5,
230 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
231 sizeof(struct wl1271_ini_fem_params_5));
232
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
244 int 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
288 int 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
321 /*
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 */
325 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
326 {
327 u32 events_vector, event;
328 unsigned long timeout;
329
330 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
331
332 do {
333 if (time_after(jiffies, timeout)) {
334 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
335 (int)mask);
336 return -ETIMEDOUT;
337 }
338
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
353 static 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) {
359 wl12xx_queue_recovery_work(wl);
360 return ret;
361 }
362
363 return 0;
364 }
365
366 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 role_type, u8 *role_id)
367 {
368 struct wl12xx_cmd_role_enable *cmd;
369 int ret;
370
371 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) {
378 ret = -ENOMEM;
379 goto out;
380 }
381
382 /* 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;
386 goto out_free;
387 }
388
389 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;
400
401 out_free:
402 kfree(cmd);
403
404 out:
405 return ret;
406 }
407
408 int 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
434 out_free:
435 kfree(cmd);
436
437 out:
438 return ret;
439 }
440
441 static 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
452 static 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
461 static 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
471 int 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
508 err_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
514 out_free:
515 kfree(cmd);
516
517 out:
518 return ret;
519 }
520
521 int 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
555 out_free:
556 kfree(cmd);
557
558 out:
559 return ret;
560 }
561
562 int 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;
593 cmd->sta.session = wl12xx_get_new_session_id(wl);
594 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
609 err_hlid:
610 /* clear links on error. */
611 wl12xx_free_link(wl, &wl->sta_hlid);
612
613 out_free:
614 kfree(cmd);
615
616 out:
617 return ret;
618 }
619
620 /* use this function to stop ibss as well */
621 int 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
647 wl12xx_free_link(wl, &wl->sta_hlid);
648
649 out_free:
650 kfree(cmd);
651
652 out:
653 return ret;
654 }
655
656 int 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
664 /*
665 * We currently do not support hidden SSID. The real SSID
666 * should be fetched from mac80211 first.
667 */
668 if (wl->ssid_len == 0) {
669 wl1271_warning("Hidden SSID currently not supported for AP");
670 ret = -EINVAL;
671 goto out;
672 }
673
674 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
675 if (!cmd) {
676 ret = -ENOMEM;
677 goto out;
678 }
679
680 ret = wl12xx_allocate_link(wl, &wl->ap_global_hlid);
681 if (ret < 0)
682 goto out_free;
683
684 ret = wl12xx_allocate_link(wl, &wl->ap_bcast_hlid);
685 if (ret < 0)
686 goto out_free_global;
687
688 cmd->role_id = wl->role_id;
689 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
690 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
691 cmd->ap.global_hlid = wl->ap_global_hlid;
692 cmd->ap.broadcast_hlid = wl->ap_bcast_hlid;
693 cmd->ap.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
694 cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
695 cmd->ap.dtim_interval = bss_conf->dtim_period;
696 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
697 cmd->channel = wl->channel;
698 cmd->ap.ssid_len = wl->ssid_len;
699 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
700 memcpy(cmd->ap.ssid, wl->ssid, wl->ssid_len);
701 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
702
703 switch (wl->band) {
704 case IEEE80211_BAND_2GHZ:
705 cmd->band = RADIO_BAND_2_4GHZ;
706 break;
707 case IEEE80211_BAND_5GHZ:
708 cmd->band = RADIO_BAND_5GHZ;
709 break;
710 default:
711 wl1271_warning("ap start - unknown band: %d", (int)wl->band);
712 cmd->band = RADIO_BAND_2_4GHZ;
713 break;
714 }
715
716 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
717 if (ret < 0) {
718 wl1271_error("failed to initiate cmd role start ap");
719 goto out_free_bcast;
720 }
721
722 goto out_free;
723
724 out_free_bcast:
725 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
726
727 out_free_global:
728 wl12xx_free_link(wl, &wl->ap_global_hlid);
729
730 out_free:
731 kfree(cmd);
732
733 out:
734 return ret;
735 }
736
737 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl)
738 {
739 struct wl12xx_cmd_role_stop *cmd;
740 int ret;
741
742 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
743 if (!cmd) {
744 ret = -ENOMEM;
745 goto out;
746 }
747
748 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wl->role_id);
749
750 cmd->role_id = wl->role_id;
751
752 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
753 if (ret < 0) {
754 wl1271_error("failed to initiate cmd role stop ap");
755 goto out_free;
756 }
757
758 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
759 wl12xx_free_link(wl, &wl->ap_global_hlid);
760
761 out_free:
762 kfree(cmd);
763
764 out:
765 return ret;
766 }
767
768 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl)
769 {
770 struct wl12xx_cmd_role_start *cmd;
771 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
772 int ret;
773
774 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
775 if (!cmd) {
776 ret = -ENOMEM;
777 goto out;
778 }
779
780 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wl->role_id);
781
782 cmd->role_id = wl->role_id;
783 if (wl->band == IEEE80211_BAND_5GHZ)
784 cmd->band = WL12XX_BAND_5GHZ;
785 cmd->channel = wl->channel;
786 cmd->ibss.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
787 cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
788 cmd->ibss.dtim_interval = bss_conf->dtim_period;
789 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
790 cmd->ibss.ssid_len = wl->ssid_len;
791 memcpy(cmd->ibss.ssid, wl->ssid, wl->ssid_len);
792 memcpy(cmd->ibss.bssid, wl->bssid, ETH_ALEN);
793 cmd->sta.local_rates = cpu_to_le32(wl->rate_set);
794
795 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
796 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
797 if (ret)
798 goto out_free;
799 }
800 cmd->ibss.hlid = wl->sta_hlid;
801 cmd->ibss.remote_rates = cpu_to_le32(wl->rate_set);
802
803 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
804 "basic_rate_set: 0x%x, remote_rates: 0x%x",
805 wl->role_id, cmd->sta.hlid, cmd->sta.session,
806 wl->basic_rate_set, wl->rate_set);
807
808 wl1271_debug(DEBUG_CMD, "wl->bssid = %pM", wl->bssid);
809
810 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
811 if (ret < 0) {
812 wl1271_error("failed to initiate cmd role enable");
813 goto err_hlid;
814 }
815
816 goto out_free;
817
818 err_hlid:
819 /* clear links on error. */
820 wl12xx_free_link(wl, &wl->sta_hlid);
821
822 out_free:
823 kfree(cmd);
824
825 out:
826 return ret;
827 }
828
829
830 /**
831 * send test command to firmware
832 *
833 * @wl: wl struct
834 * @buf: buffer containing the command, with all headers, must work with dma
835 * @len: length of the buffer
836 * @answer: is answer needed
837 */
838 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
839 {
840 int ret;
841 size_t res_len = 0;
842
843 wl1271_debug(DEBUG_CMD, "cmd test");
844
845 if (answer)
846 res_len = buf_len;
847
848 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
849
850 if (ret < 0) {
851 wl1271_warning("TEST command failed");
852 return ret;
853 }
854
855 return ret;
856 }
857
858 /**
859 * read acx from firmware
860 *
861 * @wl: wl struct
862 * @id: acx id
863 * @buf: buffer for the response, including all headers, must work with dma
864 * @len: length of buf
865 */
866 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
867 {
868 struct acx_header *acx = buf;
869 int ret;
870
871 wl1271_debug(DEBUG_CMD, "cmd interrogate");
872
873 acx->id = cpu_to_le16(id);
874
875 /* payload length, does not include any headers */
876 acx->len = cpu_to_le16(len - sizeof(*acx));
877
878 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
879 if (ret < 0)
880 wl1271_error("INTERROGATE command failed");
881
882 return ret;
883 }
884
885 /**
886 * write acx value to firmware
887 *
888 * @wl: wl struct
889 * @id: acx id
890 * @buf: buffer containing acx, including all headers, must work with dma
891 * @len: length of buf
892 */
893 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
894 {
895 struct acx_header *acx = buf;
896 int ret;
897
898 wl1271_debug(DEBUG_CMD, "cmd configure");
899
900 acx->id = cpu_to_le16(id);
901
902 /* payload length, does not include any headers */
903 acx->len = cpu_to_le16(len - sizeof(*acx));
904
905 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
906 if (ret < 0) {
907 wl1271_warning("CONFIGURE command NOK");
908 return ret;
909 }
910
911 return 0;
912 }
913
914 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
915 {
916 struct cmd_enabledisable_path *cmd;
917 int ret;
918 u16 cmd_rx, cmd_tx;
919
920 wl1271_debug(DEBUG_CMD, "cmd data path");
921
922 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
923 if (!cmd) {
924 ret = -ENOMEM;
925 goto out;
926 }
927
928 /* the channel here is only used for calibration, so hardcoded to 1 */
929 cmd->channel = 1;
930
931 if (enable) {
932 cmd_rx = CMD_ENABLE_RX;
933 cmd_tx = CMD_ENABLE_TX;
934 } else {
935 cmd_rx = CMD_DISABLE_RX;
936 cmd_tx = CMD_DISABLE_TX;
937 }
938
939 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
940 if (ret < 0) {
941 wl1271_error("rx %s cmd for channel %d failed",
942 enable ? "start" : "stop", cmd->channel);
943 goto out;
944 }
945
946 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
947 enable ? "start" : "stop", cmd->channel);
948
949 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
950 if (ret < 0) {
951 wl1271_error("tx %s cmd for channel %d failed",
952 enable ? "start" : "stop", cmd->channel);
953 goto out;
954 }
955
956 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
957 enable ? "start" : "stop", cmd->channel);
958
959 out:
960 kfree(cmd);
961 return ret;
962 }
963
964 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
965 {
966 struct wl1271_cmd_ps_params *ps_params = NULL;
967 int ret = 0;
968
969 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
970
971 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
972 if (!ps_params) {
973 ret = -ENOMEM;
974 goto out;
975 }
976
977 ps_params->role_id = wl->role_id;
978 ps_params->ps_mode = ps_mode;
979
980 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
981 sizeof(*ps_params), 0);
982 if (ret < 0) {
983 wl1271_error("cmd set_ps_mode failed");
984 goto out;
985 }
986
987 out:
988 kfree(ps_params);
989 return ret;
990 }
991
992 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
993 void *buf, size_t buf_len, int index, u32 rates)
994 {
995 struct wl1271_cmd_template_set *cmd;
996 int ret = 0;
997
998 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
999
1000 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1001 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1002
1003 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1004 if (!cmd) {
1005 ret = -ENOMEM;
1006 goto out;
1007 }
1008
1009 cmd->len = cpu_to_le16(buf_len);
1010 cmd->template_type = template_id;
1011 cmd->enabled_rates = cpu_to_le32(rates);
1012 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1013 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1014 cmd->index = index;
1015
1016 if (buf)
1017 memcpy(cmd->template_data, buf, buf_len);
1018
1019 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1020 if (ret < 0) {
1021 wl1271_warning("cmd set_template failed: %d", ret);
1022 goto out_free;
1023 }
1024
1025 out_free:
1026 kfree(cmd);
1027
1028 out:
1029 return ret;
1030 }
1031
1032 int wl1271_cmd_build_null_data(struct wl1271 *wl)
1033 {
1034 struct sk_buff *skb = NULL;
1035 int size;
1036 void *ptr;
1037 int ret = -ENOMEM;
1038
1039
1040 if (wl->bss_type == BSS_TYPE_IBSS) {
1041 size = sizeof(struct wl12xx_null_data_template);
1042 ptr = NULL;
1043 } else {
1044 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1045 if (!skb)
1046 goto out;
1047 size = skb->len;
1048 ptr = skb->data;
1049 }
1050
1051 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
1052 wl->basic_rate);
1053
1054 out:
1055 dev_kfree_skb(skb);
1056 if (ret)
1057 wl1271_warning("cmd buld null data failed %d", ret);
1058
1059 return ret;
1060
1061 }
1062
1063 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
1064 {
1065 struct sk_buff *skb = NULL;
1066 int ret = -ENOMEM;
1067
1068 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1069 if (!skb)
1070 goto out;
1071
1072 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1073 skb->data, skb->len,
1074 CMD_TEMPL_KLV_IDX_NULL_DATA,
1075 wl->basic_rate);
1076
1077 out:
1078 dev_kfree_skb(skb);
1079 if (ret)
1080 wl1271_warning("cmd build klv null data failed %d", ret);
1081
1082 return ret;
1083
1084 }
1085
1086 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
1087 {
1088 struct sk_buff *skb;
1089 int ret = 0;
1090
1091 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1092 if (!skb)
1093 goto out;
1094
1095 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
1096 skb->len, 0, wl->basic_rate_set);
1097
1098 out:
1099 dev_kfree_skb(skb);
1100 return ret;
1101 }
1102
1103 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1104 const u8 *ssid, size_t ssid_len,
1105 const u8 *ie, size_t ie_len, u8 band)
1106 {
1107 struct sk_buff *skb;
1108 int ret;
1109
1110 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1111 ie, ie_len);
1112 if (!skb) {
1113 ret = -ENOMEM;
1114 goto out;
1115 }
1116
1117 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
1118
1119 if (band == IEEE80211_BAND_2GHZ)
1120 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1121 skb->data, skb->len, 0,
1122 wl->conf.tx.basic_rate);
1123 else
1124 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1125 skb->data, skb->len, 0,
1126 wl->conf.tx.basic_rate_5);
1127
1128 out:
1129 dev_kfree_skb(skb);
1130 return ret;
1131 }
1132
1133 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1134 struct sk_buff *skb)
1135 {
1136 int ret;
1137
1138 if (!skb)
1139 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1140 if (!skb)
1141 goto out;
1142
1143 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1144
1145 if (wl->band == IEEE80211_BAND_2GHZ)
1146 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1147 skb->data, skb->len, 0,
1148 wl->conf.tx.basic_rate);
1149 else
1150 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1151 skb->data, skb->len, 0,
1152 wl->conf.tx.basic_rate_5);
1153
1154 if (ret < 0)
1155 wl1271_error("Unable to set ap probe request template.");
1156
1157 out:
1158 return skb;
1159 }
1160
1161 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
1162 {
1163 int ret;
1164 struct wl12xx_arp_rsp_template tmpl;
1165 struct ieee80211_hdr_3addr *hdr;
1166 struct arphdr *arp_hdr;
1167
1168 memset(&tmpl, 0, sizeof(tmpl));
1169
1170 /* mac80211 header */
1171 hdr = &tmpl.hdr;
1172 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1173 IEEE80211_STYPE_DATA |
1174 IEEE80211_FCTL_TODS);
1175 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1176 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1177 memset(hdr->addr3, 0xff, ETH_ALEN);
1178
1179 /* llc layer */
1180 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1181 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
1182
1183 /* arp header */
1184 arp_hdr = &tmpl.arp_hdr;
1185 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1186 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1187 arp_hdr->ar_hln = ETH_ALEN;
1188 arp_hdr->ar_pln = 4;
1189 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1190
1191 /* arp payload */
1192 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1193 tmpl.sender_ip = ip_addr;
1194
1195 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1196 &tmpl, sizeof(tmpl), 0,
1197 wl->basic_rate);
1198
1199 return ret;
1200 }
1201
1202 int wl1271_build_qos_null_data(struct wl1271 *wl)
1203 {
1204 struct ieee80211_qos_hdr template;
1205
1206 memset(&template, 0, sizeof(template));
1207
1208 memcpy(template.addr1, wl->bssid, ETH_ALEN);
1209 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
1210 memcpy(template.addr3, wl->bssid, ETH_ALEN);
1211
1212 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1213 IEEE80211_STYPE_QOS_NULLFUNC |
1214 IEEE80211_FCTL_TODS);
1215
1216 /* FIXME: not sure what priority to use here */
1217 template.qos_ctrl = cpu_to_le16(0);
1218
1219 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
1220 sizeof(template), 0,
1221 wl->basic_rate);
1222 }
1223
1224 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1225 {
1226 struct wl1271_cmd_set_keys *cmd;
1227 int ret = 0;
1228
1229 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1230
1231 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1232 if (!cmd) {
1233 ret = -ENOMEM;
1234 goto out;
1235 }
1236
1237 cmd->hlid = hlid;
1238 cmd->key_id = id;
1239 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1240 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1241 cmd->key_type = KEY_WEP;
1242
1243 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1244 if (ret < 0) {
1245 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1246 goto out;
1247 }
1248
1249 out:
1250 kfree(cmd);
1251
1252 return ret;
1253 }
1254
1255 int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1256 u8 key_size, const u8 *key, const u8 *addr,
1257 u32 tx_seq_32, u16 tx_seq_16)
1258 {
1259 struct wl1271_cmd_set_keys *cmd;
1260 int ret = 0;
1261
1262 /* hlid might have already been deleted */
1263 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID)
1264 return 0;
1265
1266 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1267 if (!cmd) {
1268 ret = -ENOMEM;
1269 goto out;
1270 }
1271
1272 cmd->hlid = wl->sta_hlid;
1273
1274 if (key_type == KEY_WEP)
1275 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1276 else if (is_broadcast_ether_addr(addr))
1277 cmd->lid_key_type = BROADCAST_LID_TYPE;
1278 else
1279 cmd->lid_key_type = UNICAST_LID_TYPE;
1280
1281 cmd->key_action = cpu_to_le16(action);
1282 cmd->key_size = key_size;
1283 cmd->key_type = key_type;
1284
1285 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1286 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1287
1288 cmd->key_id = id;
1289
1290 if (key_type == KEY_TKIP) {
1291 /*
1292 * We get the key in the following form:
1293 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1294 * but the target is expecting:
1295 * TKIP - RX MIC - TX MIC
1296 */
1297 memcpy(cmd->key, key, 16);
1298 memcpy(cmd->key + 16, key + 24, 8);
1299 memcpy(cmd->key + 24, key + 16, 8);
1300
1301 } else {
1302 memcpy(cmd->key, key, key_size);
1303 }
1304
1305 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1306
1307 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1308 if (ret < 0) {
1309 wl1271_warning("could not set keys");
1310 goto out;
1311 }
1312
1313 out:
1314 kfree(cmd);
1315
1316 return ret;
1317 }
1318
1319 /*
1320 * TODO: merge with sta/ibss into 1 set_key function.
1321 * note there are slight diffs
1322 */
1323 int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1324 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1325 u16 tx_seq_16)
1326 {
1327 struct wl1271_cmd_set_keys *cmd;
1328 int ret = 0;
1329 u8 lid_type;
1330
1331 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1332 if (!cmd)
1333 return -ENOMEM;
1334
1335 if (hlid == wl->ap_bcast_hlid) {
1336 if (key_type == KEY_WEP)
1337 lid_type = WEP_DEFAULT_LID_TYPE;
1338 else
1339 lid_type = BROADCAST_LID_TYPE;
1340 } else {
1341 lid_type = UNICAST_LID_TYPE;
1342 }
1343
1344 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1345 " hlid: %d", (int)action, (int)id, (int)lid_type,
1346 (int)key_type, (int)hlid);
1347
1348 cmd->lid_key_type = lid_type;
1349 cmd->hlid = hlid;
1350 cmd->key_action = cpu_to_le16(action);
1351 cmd->key_size = key_size;
1352 cmd->key_type = key_type;
1353 cmd->key_id = id;
1354 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1355 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1356
1357 if (key_type == KEY_TKIP) {
1358 /*
1359 * We get the key in the following form:
1360 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1361 * but the target is expecting:
1362 * TKIP - RX MIC - TX MIC
1363 */
1364 memcpy(cmd->key, key, 16);
1365 memcpy(cmd->key + 16, key + 24, 8);
1366 memcpy(cmd->key + 24, key + 16, 8);
1367 } else {
1368 memcpy(cmd->key, key, key_size);
1369 }
1370
1371 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1372
1373 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1374 if (ret < 0) {
1375 wl1271_warning("could not set ap keys");
1376 goto out;
1377 }
1378
1379 out:
1380 kfree(cmd);
1381 return ret;
1382 }
1383
1384 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
1385 {
1386 struct wl12xx_cmd_set_peer_state *cmd;
1387 int ret = 0;
1388
1389 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1390
1391 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1392 if (!cmd) {
1393 ret = -ENOMEM;
1394 goto out;
1395 }
1396
1397 cmd->hlid = hlid;
1398 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1399
1400 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1401 if (ret < 0) {
1402 wl1271_error("failed to send set peer state command");
1403 goto out_free;
1404 }
1405
1406 out_free:
1407 kfree(cmd);
1408
1409 out:
1410 return ret;
1411 }
1412
1413 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1414 {
1415 struct wl12xx_cmd_add_peer *cmd;
1416 int ret;
1417 u32 sta_rates;
1418
1419 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1420
1421 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1422 if (!cmd) {
1423 ret = -ENOMEM;
1424 goto out;
1425 }
1426
1427 /* currently we don't support UAPSD */
1428 cmd->sp_len = 0;
1429
1430 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1431 cmd->bss_index = WL1271_AP_BSS_INDEX;
1432 cmd->aid = sta->aid;
1433 cmd->hlid = hlid;
1434 cmd->wmm = sta->wme ? 1 : 0;
1435
1436 sta_rates = sta->supp_rates[wl->band];
1437 if (sta->ht_cap.ht_supported)
1438 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1439
1440 cmd->supported_rates =
1441 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates));
1442
1443 wl1271_debug(DEBUG_CMD, "new peer rates: 0x%x", cmd->supported_rates);
1444
1445 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1446 if (ret < 0) {
1447 wl1271_error("failed to initiate cmd add peer");
1448 goto out_free;
1449 }
1450
1451 out_free:
1452 kfree(cmd);
1453
1454 out:
1455 return ret;
1456 }
1457
1458 int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
1459 {
1460 struct wl12xx_cmd_remove_peer *cmd;
1461 int ret;
1462
1463 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1464
1465 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1466 if (!cmd) {
1467 ret = -ENOMEM;
1468 goto out;
1469 }
1470
1471 cmd->hlid = hlid;
1472 /* We never send a deauth, mac80211 is in charge of this */
1473 cmd->reason_opcode = 0;
1474 cmd->send_deauth_flag = 0;
1475
1476 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1477 if (ret < 0) {
1478 wl1271_error("failed to initiate cmd remove peer");
1479 goto out_free;
1480 }
1481
1482 /*
1483 * We are ok with a timeout here. The event is sometimes not sent
1484 * due to a firmware bug.
1485 */
1486 wl1271_cmd_wait_for_event_or_timeout(wl,
1487 PEER_REMOVE_COMPLETE_EVENT_ID);
1488
1489 out_free:
1490 kfree(cmd);
1491
1492 out:
1493 return ret;
1494 }
1495
1496 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1497 {
1498 struct wl12xx_cmd_config_fwlog *cmd;
1499 int ret = 0;
1500
1501 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1502
1503 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1504 if (!cmd) {
1505 ret = -ENOMEM;
1506 goto out;
1507 }
1508
1509 cmd->logger_mode = wl->conf.fwlog.mode;
1510 cmd->log_severity = wl->conf.fwlog.severity;
1511 cmd->timestamp = wl->conf.fwlog.timestamp;
1512 cmd->output = wl->conf.fwlog.output;
1513 cmd->threshold = wl->conf.fwlog.threshold;
1514
1515 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1516 if (ret < 0) {
1517 wl1271_error("failed to send config firmware logger command");
1518 goto out_free;
1519 }
1520
1521 out_free:
1522 kfree(cmd);
1523
1524 out:
1525 return ret;
1526 }
1527
1528 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1529 {
1530 struct wl12xx_cmd_start_fwlog *cmd;
1531 int ret = 0;
1532
1533 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1534
1535 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1536 if (!cmd) {
1537 ret = -ENOMEM;
1538 goto out;
1539 }
1540
1541 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1542 if (ret < 0) {
1543 wl1271_error("failed to send start firmware logger command");
1544 goto out_free;
1545 }
1546
1547 out_free:
1548 kfree(cmd);
1549
1550 out:
1551 return ret;
1552 }
1553
1554 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1555 {
1556 struct wl12xx_cmd_stop_fwlog *cmd;
1557 int ret = 0;
1558
1559 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1560
1561 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1562 if (!cmd) {
1563 ret = -ENOMEM;
1564 goto out;
1565 }
1566
1567 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1568 if (ret < 0) {
1569 wl1271_error("failed to send stop firmware logger command");
1570 goto out_free;
1571 }
1572
1573 out_free:
1574 kfree(cmd);
1575
1576 out:
1577 return ret;
1578 }
1579
1580 static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1581 {
1582 struct wl12xx_cmd_roc *cmd;
1583 int ret = 0;
1584
1585 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1586
1587 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1588 return -EINVAL;
1589
1590 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1591 if (!cmd) {
1592 ret = -ENOMEM;
1593 goto out;
1594 }
1595
1596 cmd->role_id = role_id;
1597 cmd->channel = wl->channel;
1598 switch (wl->band) {
1599 case IEEE80211_BAND_2GHZ:
1600 cmd->band = RADIO_BAND_2_4GHZ;
1601 break;
1602 case IEEE80211_BAND_5GHZ:
1603 cmd->band = RADIO_BAND_5GHZ;
1604 break;
1605 default:
1606 wl1271_error("roc - unknown band: %d", (int)wl->band);
1607 ret = -EINVAL;
1608 goto out_free;
1609 }
1610
1611
1612 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1613 if (ret < 0) {
1614 wl1271_error("failed to send ROC command");
1615 goto out_free;
1616 }
1617
1618 out_free:
1619 kfree(cmd);
1620
1621 out:
1622 return ret;
1623 }
1624
1625 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1626 {
1627 struct wl12xx_cmd_croc *cmd;
1628 int ret = 0;
1629
1630 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1631
1632 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1633 if (!cmd) {
1634 ret = -ENOMEM;
1635 goto out;
1636 }
1637 cmd->role_id = role_id;
1638
1639 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1640 sizeof(*cmd), 0);
1641 if (ret < 0) {
1642 wl1271_error("failed to send ROC command");
1643 goto out_free;
1644 }
1645
1646 out_free:
1647 kfree(cmd);
1648
1649 out:
1650 return ret;
1651 }
1652
1653 int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1654 {
1655 int ret = 0;
1656
1657 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1658 return 0;
1659
1660 ret = wl12xx_cmd_roc(wl, role_id);
1661 if (ret < 0)
1662 goto out;
1663
1664 ret = wl1271_cmd_wait_for_event(wl,
1665 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1666 if (ret < 0) {
1667 wl1271_error("cmd roc event completion error");
1668 goto out;
1669 }
1670
1671 __set_bit(role_id, wl->roc_map);
1672 out:
1673 return ret;
1674 }
1675
1676 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1677 {
1678 int ret = 0;
1679
1680 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1681 return 0;
1682
1683 ret = wl12xx_cmd_croc(wl, role_id);
1684 if (ret < 0)
1685 goto out;
1686
1687 __clear_bit(role_id, wl->roc_map);
1688 out:
1689 return ret;
1690 }