]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/wireless/wl12xx/cmd.c
65bf9526576d9a12195e1aa53c722747d4e8ea44
[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 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
138 if (ret < 0) {
139 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
140 goto out;
141 }
142
143 gp->tx_bip_fem_manufacturer =
144 gen_parms->general_params.tx_bip_fem_manufacturer;
145
146 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
147 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
148
149 out:
150 kfree(gen_parms);
151 return ret;
152 }
153
154 int wl128x_cmd_general_parms(struct wl1271 *wl)
155 {
156 struct wl128x_general_parms_cmd *gen_parms;
157 struct wl128x_ini_general_params *gp =
158 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
159 bool answer = false;
160 int ret;
161
162 if (!wl->nvs)
163 return -ENODEV;
164
165 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
166 if (!gen_parms)
167 return -ENOMEM;
168
169 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
170
171 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
172
173 if (gp->tx_bip_fem_auto_detect)
174 answer = true;
175
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;
179
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;
184 }
185
186 gp->tx_bip_fem_manufacturer =
187 gen_parms->general_params.tx_bip_fem_manufacturer;
188
189 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
190 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
191
192 out:
193 kfree(gen_parms);
194 return ret;
195 }
196
197 int wl1271_cmd_radio_parms(struct wl1271 *wl)
198 {
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;
203
204 if (!wl->nvs)
205 return -ENODEV;
206
207 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
208 if (!radio_parms)
209 return -ENOMEM;
210
211 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
212
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));
219
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));
227
228 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
229 radio_parms, sizeof(*radio_parms));
230
231 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
232 if (ret < 0)
233 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
234
235 kfree(radio_parms);
236 return ret;
237 }
238
239 int wl128x_cmd_radio_parms(struct wl1271 *wl)
240 {
241 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
242 struct wl128x_radio_parms_cmd *radio_parms;
243 struct wl128x_ini_general_params *gp = &nvs->general_params;
244 int ret;
245
246 if (!wl->nvs)
247 return -ENODEV;
248
249 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
250 if (!radio_parms)
251 return -ENOMEM;
252
253 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
254
255 /* 2.4GHz parameters */
256 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
257 sizeof(struct wl128x_ini_band_params_2));
258 memcpy(&radio_parms->dyn_params_2,
259 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
260 sizeof(struct wl128x_ini_fem_params_2));
261
262 /* 5GHz parameters */
263 memcpy(&radio_parms->static_params_5,
264 &nvs->stat_radio_params_5,
265 sizeof(struct wl128x_ini_band_params_5));
266 memcpy(&radio_parms->dyn_params_5,
267 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
268 sizeof(struct wl128x_ini_fem_params_5));
269
270 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
271
272 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
273 radio_parms, sizeof(*radio_parms));
274
275 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
276 if (ret < 0)
277 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
278
279 kfree(radio_parms);
280 return ret;
281 }
282
283 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
284 {
285 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
286 struct conf_rf_settings *rf = &wl->conf.rf;
287 int ret;
288
289 if (!wl->nvs)
290 return -ENODEV;
291
292 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
293 if (!ext_radio_parms)
294 return -ENOMEM;
295
296 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
297
298 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
299 rf->tx_per_channel_power_compensation_2,
300 CONF_TX_PWR_COMPENSATION_LEN_2);
301 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
302 rf->tx_per_channel_power_compensation_5,
303 CONF_TX_PWR_COMPENSATION_LEN_5);
304
305 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
306 ext_radio_parms, sizeof(*ext_radio_parms));
307
308 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
309 if (ret < 0)
310 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
311
312 kfree(ext_radio_parms);
313 return ret;
314 }
315
316 /*
317 * Poll the mailbox event field until any of the bits in the mask is set or a
318 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
319 */
320 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
321 {
322 u32 events_vector, event;
323 unsigned long timeout;
324
325 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
326
327 do {
328 if (time_after(jiffies, timeout)) {
329 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
330 (int)mask);
331 return -ETIMEDOUT;
332 }
333
334 msleep(1);
335
336 /* read from both event fields */
337 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
338 sizeof(events_vector), false);
339 event = events_vector & mask;
340 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
341 sizeof(events_vector), false);
342 event |= events_vector & mask;
343 } while (!event);
344
345 return 0;
346 }
347
348 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
349 {
350 int ret;
351
352 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
353 if (ret != 0) {
354 wl12xx_queue_recovery_work(wl);
355 return ret;
356 }
357
358 return 0;
359 }
360
361 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
362 u8 *role_id)
363 {
364 struct wl12xx_cmd_role_enable *cmd;
365 int ret;
366
367 wl1271_debug(DEBUG_CMD, "cmd role enable");
368
369 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
370 return -EBUSY;
371
372 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
373 if (!cmd) {
374 ret = -ENOMEM;
375 goto out;
376 }
377
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;
383 }
384
385 memcpy(cmd->mac_address, addr, ETH_ALEN);
386 cmd->role_type = role_type;
387
388 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
389 if (ret < 0) {
390 wl1271_error("failed to initiate cmd role enable");
391 goto out_free;
392 }
393
394 __set_bit(cmd->role_id, wl->roles_map);
395 *role_id = cmd->role_id;
396
397 out_free:
398 kfree(cmd);
399
400 out:
401 return ret;
402 }
403
404 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
405 {
406 struct wl12xx_cmd_role_disable *cmd;
407 int ret;
408
409 wl1271_debug(DEBUG_CMD, "cmd role disable");
410
411 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
412 return -ENOENT;
413
414 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
415 if (!cmd) {
416 ret = -ENOMEM;
417 goto out;
418 }
419 cmd->role_id = *role_id;
420
421 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
422 if (ret < 0) {
423 wl1271_error("failed to initiate cmd role disable");
424 goto out_free;
425 }
426
427 __clear_bit(*role_id, wl->roles_map);
428 *role_id = WL12XX_INVALID_ROLE_ID;
429
430 out_free:
431 kfree(cmd);
432
433 out:
434 return ret;
435 }
436
437 int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
438 {
439 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
440 if (link >= WL12XX_MAX_LINKS)
441 return -EBUSY;
442
443 __set_bit(link, wl->links_map);
444 __set_bit(link, wlvif->links_map);
445 *hlid = link;
446 return 0;
447 }
448
449 void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
450 {
451 if (*hlid == WL12XX_INVALID_LINK_ID)
452 return;
453
454 __clear_bit(*hlid, wl->links_map);
455 __clear_bit(*hlid, wlvif->links_map);
456 *hlid = WL12XX_INVALID_LINK_ID;
457 }
458
459 static int wl12xx_get_new_session_id(struct wl1271 *wl,
460 struct wl12xx_vif *wlvif)
461 {
462 if (wlvif->session_counter >= SESSION_COUNTER_MAX)
463 wlvif->session_counter = 0;
464
465 wlvif->session_counter++;
466
467 return wlvif->session_counter;
468 }
469
470 int wl12xx_cmd_role_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
471 {
472 struct wl12xx_cmd_role_start *cmd;
473 int ret;
474
475 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
476 if (!cmd) {
477 ret = -ENOMEM;
478 goto out;
479 }
480
481 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
482
483 cmd->role_id = wlvif->dev_role_id;
484 if (wlvif->band == IEEE80211_BAND_5GHZ)
485 cmd->band = WL12XX_BAND_5GHZ;
486 cmd->channel = wlvif->channel;
487
488 if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
489 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
490 if (ret)
491 goto out_free;
492 }
493 cmd->device.hlid = wlvif->dev_hlid;
494 cmd->device.session = wlvif->session_counter;
495
496 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
497 cmd->role_id, cmd->device.hlid, cmd->device.session);
498
499 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
500 if (ret < 0) {
501 wl1271_error("failed to initiate cmd role enable");
502 goto err_hlid;
503 }
504
505 goto out_free;
506
507 err_hlid:
508 /* clear links on error */
509 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
510
511 out_free:
512 kfree(cmd);
513
514 out:
515 return ret;
516 }
517
518 int wl12xx_cmd_role_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
519 {
520 struct wl12xx_cmd_role_stop *cmd;
521 int ret;
522
523 if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
524 return -EINVAL;
525
526 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
527 if (!cmd) {
528 ret = -ENOMEM;
529 goto out;
530 }
531
532 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
533
534 cmd->role_id = wlvif->dev_role_id;
535 cmd->disc_type = DISCONNECT_IMMEDIATE;
536 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
537
538 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
539 if (ret < 0) {
540 wl1271_error("failed to initiate cmd role stop");
541 goto out_free;
542 }
543
544 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
545 if (ret < 0) {
546 wl1271_error("cmd role stop dev event completion error");
547 goto out_free;
548 }
549
550 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
551
552 out_free:
553 kfree(cmd);
554
555 out:
556 return ret;
557 }
558
559 int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
560 {
561 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
562 struct wl12xx_cmd_role_start *cmd;
563 int ret;
564
565 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
566 if (!cmd) {
567 ret = -ENOMEM;
568 goto out;
569 }
570
571 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
572
573 cmd->role_id = wlvif->role_id;
574 if (wlvif->band == IEEE80211_BAND_5GHZ)
575 cmd->band = WL12XX_BAND_5GHZ;
576 cmd->channel = wlvif->channel;
577 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
578 cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
579 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
580 cmd->sta.ssid_len = wlvif->ssid_len;
581 memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
582 memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
583 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
584
585 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
586 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
587 if (ret)
588 goto out_free;
589 }
590 cmd->sta.hlid = wlvif->sta.hlid;
591 cmd->sta.session = wl12xx_get_new_session_id(wl, wlvif);
592 cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
593
594 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
595 "basic_rate_set: 0x%x, remote_rates: 0x%x",
596 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
597 wlvif->basic_rate_set, wlvif->rate_set);
598
599 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
600 if (ret < 0) {
601 wl1271_error("failed to initiate cmd role start sta");
602 goto err_hlid;
603 }
604
605 goto out_free;
606
607 err_hlid:
608 /* clear links on error. */
609 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
610
611 out_free:
612 kfree(cmd);
613
614 out:
615 return ret;
616 }
617
618 /* use this function to stop ibss as well */
619 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
620 {
621 struct wl12xx_cmd_role_stop *cmd;
622 int ret;
623
624 if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
625 return -EINVAL;
626
627 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
628 if (!cmd) {
629 ret = -ENOMEM;
630 goto out;
631 }
632
633 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
634
635 cmd->role_id = wlvif->role_id;
636 cmd->disc_type = DISCONNECT_IMMEDIATE;
637 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
638
639 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
640 if (ret < 0) {
641 wl1271_error("failed to initiate cmd role stop sta");
642 goto out_free;
643 }
644
645 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
646
647 out_free:
648 kfree(cmd);
649
650 out:
651 return ret;
652 }
653
654 int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
655 {
656 struct wl12xx_cmd_role_start *cmd;
657 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
658 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
659 int ret;
660
661 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
662
663 /* trying to use hidden SSID with an old hostapd version */
664 if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
665 wl1271_error("got a null SSID from beacon/bss");
666 ret = -EINVAL;
667 goto out;
668 }
669
670 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
671 if (!cmd) {
672 ret = -ENOMEM;
673 goto out;
674 }
675
676 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
677 if (ret < 0)
678 goto out_free;
679
680 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
681 if (ret < 0)
682 goto out_free_global;
683
684 cmd->role_id = wlvif->role_id;
685 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
686 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
687 cmd->ap.global_hlid = wlvif->ap.global_hlid;
688 cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
689 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
690 cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
691 cmd->ap.dtim_interval = bss_conf->dtim_period;
692 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
693 cmd->channel = wlvif->channel;
694
695 if (!bss_conf->hidden_ssid) {
696 /* take the SSID from the beacon for backward compatibility */
697 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
698 cmd->ap.ssid_len = wlvif->ssid_len;
699 memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
700 } else {
701 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
702 cmd->ap.ssid_len = bss_conf->ssid_len;
703 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
704 }
705
706 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
707
708 switch (wlvif->band) {
709 case IEEE80211_BAND_2GHZ:
710 cmd->band = RADIO_BAND_2_4GHZ;
711 break;
712 case IEEE80211_BAND_5GHZ:
713 cmd->band = RADIO_BAND_5GHZ;
714 break;
715 default:
716 wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
717 cmd->band = RADIO_BAND_2_4GHZ;
718 break;
719 }
720
721 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
722 if (ret < 0) {
723 wl1271_error("failed to initiate cmd role start ap");
724 goto out_free_bcast;
725 }
726
727 goto out_free;
728
729 out_free_bcast:
730 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
731
732 out_free_global:
733 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
734
735 out_free:
736 kfree(cmd);
737
738 out:
739 return ret;
740 }
741
742 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
743 {
744 struct wl12xx_cmd_role_stop *cmd;
745 int ret;
746
747 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
748 if (!cmd) {
749 ret = -ENOMEM;
750 goto out;
751 }
752
753 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
754
755 cmd->role_id = wlvif->role_id;
756
757 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
758 if (ret < 0) {
759 wl1271_error("failed to initiate cmd role stop ap");
760 goto out_free;
761 }
762
763 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
764 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
765
766 out_free:
767 kfree(cmd);
768
769 out:
770 return ret;
771 }
772
773 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
774 {
775 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
776 struct wl12xx_cmd_role_start *cmd;
777 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
778 int ret;
779
780 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
781 if (!cmd) {
782 ret = -ENOMEM;
783 goto out;
784 }
785
786 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
787
788 cmd->role_id = wlvif->role_id;
789 if (wlvif->band == IEEE80211_BAND_5GHZ)
790 cmd->band = WL12XX_BAND_5GHZ;
791 cmd->channel = wlvif->channel;
792 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
793 cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
794 cmd->ibss.dtim_interval = bss_conf->dtim_period;
795 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
796 cmd->ibss.ssid_len = wlvif->ssid_len;
797 memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
798 memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
799 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
800
801 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
802 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
803 if (ret)
804 goto out_free;
805 }
806 cmd->ibss.hlid = wlvif->sta.hlid;
807 cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
808
809 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
810 "basic_rate_set: 0x%x, remote_rates: 0x%x",
811 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
812 wlvif->basic_rate_set, wlvif->rate_set);
813
814 wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
815 vif->bss_conf.bssid);
816
817 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
818 if (ret < 0) {
819 wl1271_error("failed to initiate cmd role enable");
820 goto err_hlid;
821 }
822
823 goto out_free;
824
825 err_hlid:
826 /* clear links on error. */
827 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
828
829 out_free:
830 kfree(cmd);
831
832 out:
833 return ret;
834 }
835
836
837 /**
838 * send test command to firmware
839 *
840 * @wl: wl struct
841 * @buf: buffer containing the command, with all headers, must work with dma
842 * @len: length of the buffer
843 * @answer: is answer needed
844 */
845 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
846 {
847 int ret;
848 size_t res_len = 0;
849
850 wl1271_debug(DEBUG_CMD, "cmd test");
851
852 if (answer)
853 res_len = buf_len;
854
855 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
856
857 if (ret < 0) {
858 wl1271_warning("TEST command failed");
859 return ret;
860 }
861
862 return ret;
863 }
864
865 /**
866 * read acx from firmware
867 *
868 * @wl: wl struct
869 * @id: acx id
870 * @buf: buffer for the response, including all headers, must work with dma
871 * @len: length of buf
872 */
873 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
874 {
875 struct acx_header *acx = buf;
876 int ret;
877
878 wl1271_debug(DEBUG_CMD, "cmd interrogate");
879
880 acx->id = cpu_to_le16(id);
881
882 /* payload length, does not include any headers */
883 acx->len = cpu_to_le16(len - sizeof(*acx));
884
885 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
886 if (ret < 0)
887 wl1271_error("INTERROGATE command failed");
888
889 return ret;
890 }
891
892 /**
893 * write acx value to firmware
894 *
895 * @wl: wl struct
896 * @id: acx id
897 * @buf: buffer containing acx, including all headers, must work with dma
898 * @len: length of buf
899 */
900 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
901 {
902 struct acx_header *acx = buf;
903 int ret;
904
905 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
906
907 acx->id = cpu_to_le16(id);
908
909 /* payload length, does not include any headers */
910 acx->len = cpu_to_le16(len - sizeof(*acx));
911
912 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
913 if (ret < 0) {
914 wl1271_warning("CONFIGURE command NOK");
915 return ret;
916 }
917
918 return 0;
919 }
920
921 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
922 {
923 struct cmd_enabledisable_path *cmd;
924 int ret;
925 u16 cmd_rx, cmd_tx;
926
927 wl1271_debug(DEBUG_CMD, "cmd data path");
928
929 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
930 if (!cmd) {
931 ret = -ENOMEM;
932 goto out;
933 }
934
935 /* the channel here is only used for calibration, so hardcoded to 1 */
936 cmd->channel = 1;
937
938 if (enable) {
939 cmd_rx = CMD_ENABLE_RX;
940 cmd_tx = CMD_ENABLE_TX;
941 } else {
942 cmd_rx = CMD_DISABLE_RX;
943 cmd_tx = CMD_DISABLE_TX;
944 }
945
946 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
947 if (ret < 0) {
948 wl1271_error("rx %s cmd for channel %d failed",
949 enable ? "start" : "stop", cmd->channel);
950 goto out;
951 }
952
953 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
954 enable ? "start" : "stop", cmd->channel);
955
956 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
957 if (ret < 0) {
958 wl1271_error("tx %s cmd for channel %d failed",
959 enable ? "start" : "stop", cmd->channel);
960 goto out;
961 }
962
963 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
964 enable ? "start" : "stop", cmd->channel);
965
966 out:
967 kfree(cmd);
968 return ret;
969 }
970
971 int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
972 u8 ps_mode)
973 {
974 struct wl1271_cmd_ps_params *ps_params = NULL;
975 int ret = 0;
976
977 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
978
979 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
980 if (!ps_params) {
981 ret = -ENOMEM;
982 goto out;
983 }
984
985 ps_params->role_id = wlvif->role_id;
986 ps_params->ps_mode = ps_mode;
987
988 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
989 sizeof(*ps_params), 0);
990 if (ret < 0) {
991 wl1271_error("cmd set_ps_mode failed");
992 goto out;
993 }
994
995 out:
996 kfree(ps_params);
997 return ret;
998 }
999
1000 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
1001 void *buf, size_t buf_len, int index, u32 rates)
1002 {
1003 struct wl1271_cmd_template_set *cmd;
1004 int ret = 0;
1005
1006 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
1007
1008 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1009 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1010
1011 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1012 if (!cmd) {
1013 ret = -ENOMEM;
1014 goto out;
1015 }
1016
1017 cmd->len = cpu_to_le16(buf_len);
1018 cmd->template_type = template_id;
1019 cmd->enabled_rates = cpu_to_le32(rates);
1020 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1021 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1022 cmd->index = index;
1023
1024 if (buf)
1025 memcpy(cmd->template_data, buf, buf_len);
1026
1027 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1028 if (ret < 0) {
1029 wl1271_warning("cmd set_template failed: %d", ret);
1030 goto out_free;
1031 }
1032
1033 out_free:
1034 kfree(cmd);
1035
1036 out:
1037 return ret;
1038 }
1039
1040 int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1041 {
1042 struct sk_buff *skb = NULL;
1043 int size;
1044 void *ptr;
1045 int ret = -ENOMEM;
1046
1047
1048 if (wlvif->bss_type == BSS_TYPE_IBSS) {
1049 size = sizeof(struct wl12xx_null_data_template);
1050 ptr = NULL;
1051 } else {
1052 skb = ieee80211_nullfunc_get(wl->hw,
1053 wl12xx_wlvif_to_vif(wlvif));
1054 if (!skb)
1055 goto out;
1056 size = skb->len;
1057 ptr = skb->data;
1058 }
1059
1060 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
1061 wlvif->basic_rate);
1062
1063 out:
1064 dev_kfree_skb(skb);
1065 if (ret)
1066 wl1271_warning("cmd buld null data failed %d", ret);
1067
1068 return ret;
1069
1070 }
1071
1072 int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1073 struct wl12xx_vif *wlvif)
1074 {
1075 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1076 struct sk_buff *skb = NULL;
1077 int ret = -ENOMEM;
1078
1079 skb = ieee80211_nullfunc_get(wl->hw, vif);
1080 if (!skb)
1081 goto out;
1082
1083 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1084 skb->data, skb->len,
1085 CMD_TEMPL_KLV_IDX_NULL_DATA,
1086 wlvif->basic_rate);
1087
1088 out:
1089 dev_kfree_skb(skb);
1090 if (ret)
1091 wl1271_warning("cmd build klv null data failed %d", ret);
1092
1093 return ret;
1094
1095 }
1096
1097 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1098 u16 aid)
1099 {
1100 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1101 struct sk_buff *skb;
1102 int ret = 0;
1103
1104 skb = ieee80211_pspoll_get(wl->hw, vif);
1105 if (!skb)
1106 goto out;
1107
1108 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
1109 skb->len, 0, wlvif->basic_rate_set);
1110
1111 out:
1112 dev_kfree_skb(skb);
1113 return ret;
1114 }
1115
1116 int wl1271_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1117 const u8 *ssid, size_t ssid_len,
1118 const u8 *ie, size_t ie_len, u8 band)
1119 {
1120 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1121 struct sk_buff *skb;
1122 int ret;
1123 u32 rate;
1124
1125 skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
1126 ie, ie_len);
1127 if (!skb) {
1128 ret = -ENOMEM;
1129 goto out;
1130 }
1131
1132 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
1133
1134 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1135 if (band == IEEE80211_BAND_2GHZ)
1136 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1137 skb->data, skb->len, 0, rate);
1138 else
1139 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1140 skb->data, skb->len, 0, rate);
1141
1142 out:
1143 dev_kfree_skb(skb);
1144 return ret;
1145 }
1146
1147 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1148 struct wl12xx_vif *wlvif,
1149 struct sk_buff *skb)
1150 {
1151 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1152 int ret;
1153 u32 rate;
1154
1155 if (!skb)
1156 skb = ieee80211_ap_probereq_get(wl->hw, vif);
1157 if (!skb)
1158 goto out;
1159
1160 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1161
1162 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1163 if (wlvif->band == IEEE80211_BAND_2GHZ)
1164 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1165 skb->data, skb->len, 0, rate);
1166 else
1167 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1168 skb->data, skb->len, 0, rate);
1169
1170 if (ret < 0)
1171 wl1271_error("Unable to set ap probe request template.");
1172
1173 out:
1174 return skb;
1175 }
1176
1177 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1178 __be32 ip_addr)
1179 {
1180 int ret;
1181 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1182 struct wl12xx_arp_rsp_template tmpl;
1183 struct ieee80211_hdr_3addr *hdr;
1184 struct arphdr *arp_hdr;
1185
1186 memset(&tmpl, 0, sizeof(tmpl));
1187
1188 /* mac80211 header */
1189 hdr = &tmpl.hdr;
1190 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1191 IEEE80211_STYPE_DATA |
1192 IEEE80211_FCTL_TODS);
1193 memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1194 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1195 memset(hdr->addr3, 0xff, ETH_ALEN);
1196
1197 /* llc layer */
1198 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1199 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
1200
1201 /* arp header */
1202 arp_hdr = &tmpl.arp_hdr;
1203 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1204 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1205 arp_hdr->ar_hln = ETH_ALEN;
1206 arp_hdr->ar_pln = 4;
1207 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1208
1209 /* arp payload */
1210 memcpy(tmpl.sender_hw, vif->addr, ETH_ALEN);
1211 tmpl.sender_ip = ip_addr;
1212
1213 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1214 &tmpl, sizeof(tmpl), 0,
1215 wlvif->basic_rate);
1216
1217 return ret;
1218 }
1219
1220 int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1221 {
1222 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1223 struct ieee80211_qos_hdr template;
1224
1225 memset(&template, 0, sizeof(template));
1226
1227 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1228 memcpy(template.addr2, vif->addr, ETH_ALEN);
1229 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1230
1231 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1232 IEEE80211_STYPE_QOS_NULLFUNC |
1233 IEEE80211_FCTL_TODS);
1234
1235 /* FIXME: not sure what priority to use here */
1236 template.qos_ctrl = cpu_to_le16(0);
1237
1238 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
1239 sizeof(template), 0,
1240 wlvif->basic_rate);
1241 }
1242
1243 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1244 {
1245 struct wl1271_cmd_set_keys *cmd;
1246 int ret = 0;
1247
1248 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1249
1250 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1251 if (!cmd) {
1252 ret = -ENOMEM;
1253 goto out;
1254 }
1255
1256 cmd->hlid = hlid;
1257 cmd->key_id = id;
1258 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1259 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1260 cmd->key_type = KEY_WEP;
1261
1262 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1263 if (ret < 0) {
1264 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1265 goto out;
1266 }
1267
1268 out:
1269 kfree(cmd);
1270
1271 return ret;
1272 }
1273
1274 int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1275 u16 action, u8 id, u8 key_type,
1276 u8 key_size, const u8 *key, const u8 *addr,
1277 u32 tx_seq_32, u16 tx_seq_16)
1278 {
1279 struct wl1271_cmd_set_keys *cmd;
1280 int ret = 0;
1281
1282 /* hlid might have already been deleted */
1283 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1284 return 0;
1285
1286 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1287 if (!cmd) {
1288 ret = -ENOMEM;
1289 goto out;
1290 }
1291
1292 cmd->hlid = wlvif->sta.hlid;
1293
1294 if (key_type == KEY_WEP)
1295 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1296 else if (is_broadcast_ether_addr(addr))
1297 cmd->lid_key_type = BROADCAST_LID_TYPE;
1298 else
1299 cmd->lid_key_type = UNICAST_LID_TYPE;
1300
1301 cmd->key_action = cpu_to_le16(action);
1302 cmd->key_size = key_size;
1303 cmd->key_type = key_type;
1304
1305 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1306 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1307
1308 cmd->key_id = id;
1309
1310 if (key_type == KEY_TKIP) {
1311 /*
1312 * We get the key in the following form:
1313 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1314 * but the target is expecting:
1315 * TKIP - RX MIC - TX MIC
1316 */
1317 memcpy(cmd->key, key, 16);
1318 memcpy(cmd->key + 16, key + 24, 8);
1319 memcpy(cmd->key + 24, key + 16, 8);
1320
1321 } else {
1322 memcpy(cmd->key, key, key_size);
1323 }
1324
1325 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1326
1327 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1328 if (ret < 0) {
1329 wl1271_warning("could not set keys");
1330 goto out;
1331 }
1332
1333 out:
1334 kfree(cmd);
1335
1336 return ret;
1337 }
1338
1339 /*
1340 * TODO: merge with sta/ibss into 1 set_key function.
1341 * note there are slight diffs
1342 */
1343 int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1344 u16 action, u8 id, u8 key_type,
1345 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1346 u16 tx_seq_16)
1347 {
1348 struct wl1271_cmd_set_keys *cmd;
1349 int ret = 0;
1350 u8 lid_type;
1351
1352 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1353 if (!cmd)
1354 return -ENOMEM;
1355
1356 if (hlid == wlvif->ap.bcast_hlid) {
1357 if (key_type == KEY_WEP)
1358 lid_type = WEP_DEFAULT_LID_TYPE;
1359 else
1360 lid_type = BROADCAST_LID_TYPE;
1361 } else {
1362 lid_type = UNICAST_LID_TYPE;
1363 }
1364
1365 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1366 " hlid: %d", (int)action, (int)id, (int)lid_type,
1367 (int)key_type, (int)hlid);
1368
1369 cmd->lid_key_type = lid_type;
1370 cmd->hlid = hlid;
1371 cmd->key_action = cpu_to_le16(action);
1372 cmd->key_size = key_size;
1373 cmd->key_type = key_type;
1374 cmd->key_id = id;
1375 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1376 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1377
1378 if (key_type == KEY_TKIP) {
1379 /*
1380 * We get the key in the following form:
1381 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1382 * but the target is expecting:
1383 * TKIP - RX MIC - TX MIC
1384 */
1385 memcpy(cmd->key, key, 16);
1386 memcpy(cmd->key + 16, key + 24, 8);
1387 memcpy(cmd->key + 24, key + 16, 8);
1388 } else {
1389 memcpy(cmd->key, key, key_size);
1390 }
1391
1392 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1393
1394 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1395 if (ret < 0) {
1396 wl1271_warning("could not set ap keys");
1397 goto out;
1398 }
1399
1400 out:
1401 kfree(cmd);
1402 return ret;
1403 }
1404
1405 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
1406 {
1407 struct wl12xx_cmd_set_peer_state *cmd;
1408 int ret = 0;
1409
1410 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1411
1412 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1413 if (!cmd) {
1414 ret = -ENOMEM;
1415 goto out;
1416 }
1417
1418 cmd->hlid = hlid;
1419 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1420
1421 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1422 if (ret < 0) {
1423 wl1271_error("failed to send set peer state command");
1424 goto out_free;
1425 }
1426
1427 out_free:
1428 kfree(cmd);
1429
1430 out:
1431 return ret;
1432 }
1433
1434 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1435 struct ieee80211_sta *sta, u8 hlid)
1436 {
1437 struct wl12xx_cmd_add_peer *cmd;
1438 int i, ret;
1439 u32 sta_rates;
1440
1441 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1442
1443 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1444 if (!cmd) {
1445 ret = -ENOMEM;
1446 goto out;
1447 }
1448
1449 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1450 cmd->bss_index = WL1271_AP_BSS_INDEX;
1451 cmd->aid = sta->aid;
1452 cmd->hlid = hlid;
1453 cmd->sp_len = sta->max_sp;
1454 cmd->wmm = sta->wme ? 1 : 0;
1455
1456 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1457 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1458 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1459 else
1460 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1461
1462 sta_rates = sta->supp_rates[wlvif->band];
1463 if (sta->ht_cap.ht_supported)
1464 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1465
1466 cmd->supported_rates =
1467 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1468 wlvif->band));
1469
1470 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1471 cmd->supported_rates, sta->uapsd_queues);
1472
1473 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1474 if (ret < 0) {
1475 wl1271_error("failed to initiate cmd add peer");
1476 goto out_free;
1477 }
1478
1479 out_free:
1480 kfree(cmd);
1481
1482 out:
1483 return ret;
1484 }
1485
1486 int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
1487 {
1488 struct wl12xx_cmd_remove_peer *cmd;
1489 int ret;
1490
1491 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1492
1493 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1494 if (!cmd) {
1495 ret = -ENOMEM;
1496 goto out;
1497 }
1498
1499 cmd->hlid = hlid;
1500 /* We never send a deauth, mac80211 is in charge of this */
1501 cmd->reason_opcode = 0;
1502 cmd->send_deauth_flag = 0;
1503
1504 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1505 if (ret < 0) {
1506 wl1271_error("failed to initiate cmd remove peer");
1507 goto out_free;
1508 }
1509
1510 /*
1511 * We are ok with a timeout here. The event is sometimes not sent
1512 * due to a firmware bug.
1513 */
1514 wl1271_cmd_wait_for_event_or_timeout(wl,
1515 PEER_REMOVE_COMPLETE_EVENT_ID);
1516
1517 out_free:
1518 kfree(cmd);
1519
1520 out:
1521 return ret;
1522 }
1523
1524 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1525 {
1526 struct wl12xx_cmd_config_fwlog *cmd;
1527 int ret = 0;
1528
1529 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1530
1531 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1532 if (!cmd) {
1533 ret = -ENOMEM;
1534 goto out;
1535 }
1536
1537 cmd->logger_mode = wl->conf.fwlog.mode;
1538 cmd->log_severity = wl->conf.fwlog.severity;
1539 cmd->timestamp = wl->conf.fwlog.timestamp;
1540 cmd->output = wl->conf.fwlog.output;
1541 cmd->threshold = wl->conf.fwlog.threshold;
1542
1543 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1544 if (ret < 0) {
1545 wl1271_error("failed to send config firmware logger command");
1546 goto out_free;
1547 }
1548
1549 out_free:
1550 kfree(cmd);
1551
1552 out:
1553 return ret;
1554 }
1555
1556 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1557 {
1558 struct wl12xx_cmd_start_fwlog *cmd;
1559 int ret = 0;
1560
1561 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1562
1563 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1564 if (!cmd) {
1565 ret = -ENOMEM;
1566 goto out;
1567 }
1568
1569 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1570 if (ret < 0) {
1571 wl1271_error("failed to send start firmware logger command");
1572 goto out_free;
1573 }
1574
1575 out_free:
1576 kfree(cmd);
1577
1578 out:
1579 return ret;
1580 }
1581
1582 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1583 {
1584 struct wl12xx_cmd_stop_fwlog *cmd;
1585 int ret = 0;
1586
1587 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1588
1589 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1590 if (!cmd) {
1591 ret = -ENOMEM;
1592 goto out;
1593 }
1594
1595 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1596 if (ret < 0) {
1597 wl1271_error("failed to send stop firmware logger command");
1598 goto out_free;
1599 }
1600
1601 out_free:
1602 kfree(cmd);
1603
1604 out:
1605 return ret;
1606 }
1607
1608 static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1609 u8 role_id)
1610 {
1611 struct wl12xx_cmd_roc *cmd;
1612 int ret = 0;
1613
1614 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wlvif->channel, role_id);
1615
1616 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1617 return -EINVAL;
1618
1619 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1620 if (!cmd) {
1621 ret = -ENOMEM;
1622 goto out;
1623 }
1624
1625 cmd->role_id = role_id;
1626 cmd->channel = wlvif->channel;
1627 switch (wlvif->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)wlvif->band);
1636 ret = -EINVAL;
1637 goto out_free;
1638 }
1639
1640
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;
1645 }
1646
1647 out_free:
1648 kfree(cmd);
1649
1650 out:
1651 return ret;
1652 }
1653
1654 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1655 {
1656 struct wl12xx_cmd_croc *cmd;
1657 int ret = 0;
1658
1659 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1660
1661 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1662 if (!cmd) {
1663 ret = -ENOMEM;
1664 goto out;
1665 }
1666 cmd->role_id = role_id;
1667
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;
1673 }
1674
1675 out_free:
1676 kfree(cmd);
1677
1678 out:
1679 return ret;
1680 }
1681
1682 int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id)
1683 {
1684 int ret = 0;
1685
1686 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1687 return 0;
1688
1689 ret = wl12xx_cmd_roc(wl, wlvif, role_id);
1690 if (ret < 0)
1691 goto out;
1692
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;
1698 }
1699
1700 __set_bit(role_id, wl->roc_map);
1701 out:
1702 return ret;
1703 }
1704
1705 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1706 {
1707 int ret = 0;
1708
1709 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1710 return 0;
1711
1712 ret = wl12xx_cmd_croc(wl, role_id);
1713 if (ret < 0)
1714 goto out;
1715
1716 __clear_bit(role_id, wl->roc_map);
1717 out:
1718 return ret;
1719 }
1720
1721 int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1722 struct ieee80211_channel_switch *ch_switch)
1723 {
1724 struct wl12xx_cmd_channel_switch *cmd;
1725 int ret;
1726
1727 wl1271_debug(DEBUG_ACX, "cmd channel switch");
1728
1729 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1730 if (!cmd) {
1731 ret = -ENOMEM;
1732 goto out;
1733 }
1734
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 */
1739
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;
1744 }
1745
1746 out_free:
1747 kfree(cmd);
1748
1749 out:
1750 return ret;
1751 }
1752
1753 int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1754 {
1755 struct wl12xx_cmd_stop_channel_switch *cmd;
1756 int ret;
1757
1758 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1759
1760 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1761 if (!cmd) {
1762 ret = -ENOMEM;
1763 goto out;
1764 }
1765
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;
1770 }
1771
1772 out_free:
1773 kfree(cmd);
1774
1775 out:
1776 return ret;
1777 }