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