wl1271: hack to disable filters
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_cmd.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2009 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/crc7.h>
27 #include <linux/spi/spi.h>
28 #include <linux/etherdevice.h>
29
30 #include "wl1271.h"
31 #include "wl1271_reg.h"
32 #include "wl1271_spi.h"
33 #include "wl1271_acx.h"
34 #include "wl12xx_80211.h"
35 #include "wl1271_cmd.h"
36
37 /*
38  * send command to firmware
39  *
40  * @wl: wl struct
41  * @id: command id
42  * @buf: buffer containing the command, must work with dma
43  * @len: length of the buffer
44  */
45 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len)
46 {
47         struct wl1271_cmd_header *cmd;
48         unsigned long timeout;
49         u32 intr;
50         int ret = 0;
51
52         cmd = buf;
53         cmd->id = id;
54         cmd->status = 0;
55
56         WARN_ON(len % 4 != 0);
57
58         wl1271_spi_mem_write(wl, wl->cmd_box_addr, buf, len);
59
60         wl1271_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
61
62         timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
63
64         intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
65         while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
66                 if (time_after(jiffies, timeout)) {
67                         wl1271_error("command complete timeout");
68                         ret = -ETIMEDOUT;
69                         goto out;
70                 }
71
72                 msleep(1);
73
74                 intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
75         }
76
77         wl1271_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
78                            WL1271_ACX_INTR_CMD_COMPLETE);
79
80 out:
81         return ret;
82 }
83
84 int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
85 {
86         struct wl1271_cmd_cal_channel_tune *cmd;
87         int ret = 0;
88
89         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
90         if (!cmd)
91                 return -ENOMEM;
92
93         cmd->test.id = TEST_CMD_CHANNEL_TUNE;
94
95         cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
96         /* set up any channel, 7 is in the middle of the range */
97         cmd->channel = 7;
98
99         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
100         if (ret < 0)
101                 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
102
103         kfree(cmd);
104         return ret;
105 }
106
107 int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
108 {
109         struct wl1271_cmd_cal_update_ref_point *cmd;
110         int ret = 0;
111
112         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
113         if (!cmd)
114                 return -ENOMEM;
115
116         cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
117
118         /* FIXME: still waiting for the correct values */
119         cmd->ref_power    = 0;
120         cmd->ref_detector = 0;
121
122         cmd->sub_band     = WL1271_PD_REFERENCE_POINT_BAND_B_G;
123
124         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
125         if (ret < 0)
126                 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
127
128         kfree(cmd);
129         return ret;
130 }
131
132 int wl1271_cmd_cal_p2g(struct wl1271 *wl)
133 {
134         struct wl1271_cmd_cal_p2g *cmd;
135         int ret = 0;
136
137         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
138         if (!cmd)
139                 return -ENOMEM;
140
141         cmd->test.id = TEST_CMD_P2G_CAL;
142
143         cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
144
145         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
146         if (ret < 0)
147                 wl1271_warning("TEST_CMD_P2G_CAL failed");
148
149         kfree(cmd);
150         return ret;
151 }
152
153 int wl1271_cmd_cal(struct wl1271 *wl)
154 {
155         /*
156          * FIXME: we must make sure that we're not sleeping when calibration
157          * is done
158          */
159         int ret;
160
161         wl1271_notice("performing tx calibration");
162
163         ret = wl1271_cmd_cal_channel_tune(wl);
164         if (ret < 0)
165                 return ret;
166
167         ret = wl1271_cmd_cal_update_ref_point(wl);
168         if (ret < 0)
169                 return ret;
170
171         ret = wl1271_cmd_cal_p2g(wl);
172         if (ret < 0)
173                 return ret;
174
175         return ret;
176 }
177
178 int wl1271_cmd_join(struct wl1271 *wl)
179 {
180         static bool do_cal = true;
181         struct wl1271_cmd_join *join;
182         int ret, i;
183         u8 *bssid;
184
185         /* FIXME: remove when we get calibration from the factory */
186         if (do_cal) {
187                 ret = wl1271_cmd_cal(wl);
188                 if (ret < 0)
189                         wl1271_warning("couldn't calibrate");
190                 else
191                         do_cal = false;
192         }
193
194
195         join = kzalloc(sizeof(*join), GFP_KERNEL);
196         if (!join) {
197                 ret = -ENOMEM;
198                 goto out;
199         }
200
201         wl1271_debug(DEBUG_CMD, "cmd join");
202
203         /* Reverse order BSSID */
204         bssid = (u8 *) &join->bssid_lsb;
205         for (i = 0; i < ETH_ALEN; i++)
206                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
207
208         join->rx_config_options = wl->rx_config;
209         join->rx_filter_options = wl->rx_filter;
210
211         /*
212          * FIXME: disable temporarily all filters because after commit
213          * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
214          * association. The filter logic needs to be implemented properly
215          * and once that is done, this hack can be removed.
216          */
217         join->rx_config_options = 0;
218         join->rx_filter_options = WL1271_DEFAULT_RX_FILTER;
219
220         join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
221                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
222
223         join->beacon_interval = wl->beacon_int;
224         join->dtim_interval = wl->dtim_period;
225         join->bss_type = wl->bss_type;
226         join->channel = wl->channel;
227         join->ssid_len = wl->ssid_len;
228         memcpy(join->ssid, wl->ssid, wl->ssid_len);
229         join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
230
231         /* increment the session counter */
232         wl->session_counter++;
233         if (wl->session_counter >= SESSION_COUNTER_MAX)
234                 wl->session_counter = 0;
235
236         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
237
238         /* reset TX security counters */
239         wl->tx_security_last_seq = 0;
240         wl->tx_security_seq_16 = 0;
241         wl->tx_security_seq_32 = 0;
242
243         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
244         if (ret < 0) {
245                 wl1271_error("failed to initiate cmd join");
246                 goto out_free;
247         }
248
249         /*
250          * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
251          * simplify locking we just sleep instead, for now
252          */
253         msleep(10);
254
255 out_free:
256         kfree(join);
257
258 out:
259         return ret;
260 }
261
262 /**
263  * send test command to firmware
264  *
265  * @wl: wl struct
266  * @buf: buffer containing the command, with all headers, must work with dma
267  * @len: length of the buffer
268  * @answer: is answer needed
269  */
270 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
271 {
272         int ret;
273
274         wl1271_debug(DEBUG_CMD, "cmd test");
275
276         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len);
277
278         if (ret < 0) {
279                 wl1271_warning("TEST command failed");
280                 return ret;
281         }
282
283         if (answer) {
284                 struct wl1271_command *cmd_answer;
285
286                 /*
287                  * The test command got in, we can read the answer.
288                  * The answer would be a wl1271_command, where the
289                  * parameter array contains the actual answer.
290                  */
291                 wl1271_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
292
293                 cmd_answer = buf;
294
295                 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
296                         wl1271_error("TEST command answer error: %d",
297                                      cmd_answer->header.status);
298         }
299
300         return 0;
301 }
302
303 /**
304  * read acx from firmware
305  *
306  * @wl: wl struct
307  * @id: acx id
308  * @buf: buffer for the response, including all headers, must work with dma
309  * @len: lenght of buf
310  */
311 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
312 {
313         struct acx_header *acx = buf;
314         int ret;
315
316         wl1271_debug(DEBUG_CMD, "cmd interrogate");
317
318         acx->id = id;
319
320         /* payload length, does not include any headers */
321         acx->len = len - sizeof(*acx);
322
323         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
324         if (ret < 0) {
325                 wl1271_error("INTERROGATE command failed");
326                 goto out;
327         }
328
329         /* the interrogate command got in, we can read the answer */
330         wl1271_spi_mem_read(wl, wl->cmd_box_addr, buf, len);
331
332         acx = buf;
333         if (acx->cmd.status != CMD_STATUS_SUCCESS)
334                 wl1271_error("INTERROGATE command error: %d",
335                              acx->cmd.status);
336
337 out:
338         return ret;
339 }
340
341 /**
342  * write acx value to firmware
343  *
344  * @wl: wl struct
345  * @id: acx id
346  * @buf: buffer containing acx, including all headers, must work with dma
347  * @len: length of buf
348  */
349 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
350 {
351         struct acx_header *acx = buf;
352         int ret;
353
354         wl1271_debug(DEBUG_CMD, "cmd configure");
355
356         acx->id = id;
357
358         /* payload length, does not include any headers */
359         acx->len = len - sizeof(*acx);
360
361         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len);
362         if (ret < 0) {
363                 wl1271_warning("CONFIGURE command NOK");
364                 return ret;
365         }
366
367         return 0;
368 }
369
370 int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
371 {
372         struct cmd_enabledisable_path *cmd;
373         int ret;
374         u16 cmd_rx, cmd_tx;
375
376         wl1271_debug(DEBUG_CMD, "cmd data path");
377
378         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
379         if (!cmd) {
380                 ret = -ENOMEM;
381                 goto out;
382         }
383
384         cmd->channel = channel;
385
386         if (enable) {
387                 cmd_rx = CMD_ENABLE_RX;
388                 cmd_tx = CMD_ENABLE_TX;
389         } else {
390                 cmd_rx = CMD_DISABLE_RX;
391                 cmd_tx = CMD_DISABLE_TX;
392         }
393
394         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
395         if (ret < 0) {
396                 wl1271_error("rx %s cmd for channel %d failed",
397                              enable ? "start" : "stop", channel);
398                 goto out;
399         }
400
401         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
402                      enable ? "start" : "stop", channel);
403
404         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
405         if (ret < 0) {
406                 wl1271_error("tx %s cmd for channel %d failed",
407                              enable ? "start" : "stop", channel);
408                 return ret;
409         }
410
411         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
412                      enable ? "start" : "stop", channel);
413
414 out:
415         kfree(cmd);
416         return ret;
417 }
418
419 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
420 {
421         struct wl1271_cmd_ps_params *ps_params = NULL;
422         int ret = 0;
423
424         /* FIXME: this should be in ps.c */
425         ret = wl1271_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
426                                             wl->listen_int);
427         if (ret < 0) {
428                 wl1271_error("couldn't set wake up conditions");
429                 goto out;
430         }
431
432         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
433
434         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
435         if (!ps_params) {
436                 ret = -ENOMEM;
437                 goto out;
438         }
439
440         ps_params->ps_mode = ps_mode;
441         ps_params->send_null_data = 1;
442         ps_params->retries = 5;
443         ps_params->hang_over_period = 128;
444         ps_params->null_data_rate = 1; /* 1 Mbps */
445
446         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
447                               sizeof(*ps_params));
448         if (ret < 0) {
449                 wl1271_error("cmd set_ps_mode failed");
450                 goto out;
451         }
452
453 out:
454         kfree(ps_params);
455         return ret;
456 }
457
458 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
459                            size_t len)
460 {
461         struct cmd_read_write_memory *cmd;
462         int ret = 0;
463
464         wl1271_debug(DEBUG_CMD, "cmd read memory");
465
466         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
467         if (!cmd) {
468                 ret = -ENOMEM;
469                 goto out;
470         }
471
472         WARN_ON(len > MAX_READ_SIZE);
473         len = min_t(size_t, len, MAX_READ_SIZE);
474
475         cmd->addr = addr;
476         cmd->size = len;
477
478         ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
479         if (ret < 0) {
480                 wl1271_error("read memory command failed: %d", ret);
481                 goto out;
482         }
483
484         /* the read command got in, we can now read the answer */
485         wl1271_spi_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
486
487         if (cmd->header.status != CMD_STATUS_SUCCESS)
488                 wl1271_error("error in read command result: %d",
489                              cmd->header.status);
490
491         memcpy(answer, cmd->value, len);
492
493 out:
494         kfree(cmd);
495         return ret;
496 }
497
498 int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
499                     u8 active_scan, u8 high_prio, u8 num_channels,
500                     u8 probe_requests)
501 {
502
503         struct wl1271_cmd_trigger_scan_to *trigger = NULL;
504         struct wl1271_cmd_scan *params = NULL;
505         int i, ret;
506         u16 scan_options = 0;
507
508         if (wl->scanning)
509                 return -EINVAL;
510
511         params = kzalloc(sizeof(*params), GFP_KERNEL);
512         if (!params)
513                 return -ENOMEM;
514
515         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
516         params->params.rx_filter_options =
517                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
518
519         if (!active_scan)
520                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
521         if (high_prio)
522                 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
523         params->params.scan_options = scan_options;
524
525         params->params.num_channels = num_channels;
526         params->params.num_probe_requests = probe_requests;
527         params->params.tx_rate = cpu_to_le32(RATE_MASK_2MBPS);
528         params->params.tid_trigger = 0;
529         params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
530
531         for (i = 0; i < num_channels; i++) {
532                 params->channels[i].min_duration =
533                         cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
534                 params->channels[i].max_duration =
535                         cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
536                 memset(&params->channels[i].bssid_lsb, 0xff, 4);
537                 memset(&params->channels[i].bssid_msb, 0xff, 2);
538                 params->channels[i].early_termination = 0;
539                 params->channels[i].tx_power_att = WL1271_SCAN_CURRENT_TX_PWR;
540                 params->channels[i].channel = i + 1;
541         }
542
543         if (len && ssid) {
544                 params->params.ssid_len = len;
545                 memcpy(params->params.ssid, ssid, len);
546         }
547
548         ret = wl1271_cmd_build_probe_req(wl, ssid, len);
549         if (ret < 0) {
550                 wl1271_error("PROBE request template failed");
551                 goto out;
552         }
553
554         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
555         if (!trigger) {
556                 ret = -ENOMEM;
557                 goto out;
558         }
559
560         /* disable the timeout */
561         trigger->timeout = 0;
562
563         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
564                               sizeof(*trigger));
565         if (ret < 0) {
566                 wl1271_error("trigger scan to failed for hw scan");
567                 goto out;
568         }
569
570         wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
571
572         wl->scanning = true;
573
574         ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
575         if (ret < 0) {
576                 wl1271_error("SCAN failed");
577                 goto out;
578         }
579
580         wl1271_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
581
582         if (params->header.status != CMD_STATUS_SUCCESS) {
583                 wl1271_error("Scan command error: %d",
584                              params->header.status);
585                 wl->scanning = false;
586                 ret = -EIO;
587                 goto out;
588         }
589
590 out:
591         kfree(params);
592         return ret;
593 }
594
595 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
596                             void *buf, size_t buf_len)
597 {
598         struct wl1271_cmd_template_set *cmd;
599         int ret = 0;
600
601         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
602
603         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
604         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
605
606         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
607         if (!cmd) {
608                 ret = -ENOMEM;
609                 goto out;
610         }
611
612         cmd->len = cpu_to_le16(buf_len);
613         cmd->template_type = template_id;
614         cmd->enabled_rates = ACX_RATE_MASK_UNSPECIFIED;
615         cmd->short_retry_limit = ACX_RATE_RETRY_LIMIT;
616         cmd->long_retry_limit = ACX_RATE_RETRY_LIMIT;
617
618         if (buf)
619                 memcpy(cmd->template_data, buf, buf_len);
620
621         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd));
622         if (ret < 0) {
623                 wl1271_warning("cmd set_template failed: %d", ret);
624                 goto out_free;
625         }
626
627 out_free:
628         kfree(cmd);
629
630 out:
631         return ret;
632 }
633
634 static int wl1271_build_basic_rates(char *rates)
635 {
636         u8 index = 0;
637
638         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
639         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
640         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
641         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
642
643         return index;
644 }
645
646 static int wl1271_build_extended_rates(char *rates)
647 {
648         u8 index = 0;
649
650         rates[index++] = IEEE80211_OFDM_RATE_6MB;
651         rates[index++] = IEEE80211_OFDM_RATE_9MB;
652         rates[index++] = IEEE80211_OFDM_RATE_12MB;
653         rates[index++] = IEEE80211_OFDM_RATE_18MB;
654         rates[index++] = IEEE80211_OFDM_RATE_24MB;
655         rates[index++] = IEEE80211_OFDM_RATE_36MB;
656         rates[index++] = IEEE80211_OFDM_RATE_48MB;
657         rates[index++] = IEEE80211_OFDM_RATE_54MB;
658
659         return index;
660 }
661
662 int wl1271_cmd_build_null_data(struct wl1271 *wl)
663 {
664         struct wl12xx_null_data_template template;
665
666         if (!is_zero_ether_addr(wl->bssid)) {
667                 memcpy(template.header.da, wl->bssid, ETH_ALEN);
668                 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
669         } else {
670                 memset(template.header.da, 0xff, ETH_ALEN);
671                 memset(template.header.bssid, 0xff, ETH_ALEN);
672         }
673
674         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
675         template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
676                                                 IEEE80211_STYPE_NULLFUNC);
677
678         return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
679                                        sizeof(template));
680
681 }
682
683 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
684 {
685         struct wl12xx_ps_poll_template template;
686
687         memcpy(template.bssid, wl->bssid, ETH_ALEN);
688         memcpy(template.ta, wl->mac_addr, ETH_ALEN);
689
690         /* aid in PS-Poll has its two MSBs each set to 1 */
691         template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
692
693         template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
694
695         return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
696                                        sizeof(template));
697
698 }
699
700 int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len)
701 {
702         struct wl12xx_probe_req_template template;
703         struct wl12xx_ie_rates *rates;
704         char *ptr;
705         u16 size;
706
707         ptr = (char *)&template;
708         size = sizeof(struct ieee80211_header);
709
710         memset(template.header.da, 0xff, ETH_ALEN);
711         memset(template.header.bssid, 0xff, ETH_ALEN);
712         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
713         template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
714
715         /* IEs */
716         /* SSID */
717         template.ssid.header.id = WLAN_EID_SSID;
718         template.ssid.header.len = ssid_len;
719         if (ssid_len && ssid)
720                 memcpy(template.ssid.ssid, ssid, ssid_len);
721         size += sizeof(struct wl12xx_ie_header) + ssid_len;
722         ptr += size;
723
724         /* Basic Rates */
725         rates = (struct wl12xx_ie_rates *)ptr;
726         rates->header.id = WLAN_EID_SUPP_RATES;
727         rates->header.len = wl1271_build_basic_rates(rates->rates);
728         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
729         ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
730
731         /* Extended rates */
732         rates = (struct wl12xx_ie_rates *)ptr;
733         rates->header.id = WLAN_EID_EXT_SUPP_RATES;
734         rates->header.len = wl1271_build_extended_rates(rates->rates);
735         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
736
737         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
738
739         return wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
740                                        &template, size);
741 }
742
743 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
744 {
745         struct wl1271_cmd_set_keys *cmd;
746         int ret = 0;
747
748         wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
749
750         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
751         if (!cmd) {
752                 ret = -ENOMEM;
753                 goto out;
754         }
755
756         cmd->id = id;
757         cmd->key_action = KEY_SET_ID;
758         cmd->key_type = KEY_WEP;
759
760         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
761         if (ret < 0) {
762                 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
763                 goto out;
764         }
765
766 out:
767         kfree(cmd);
768
769         return ret;
770 }
771
772 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
773                        u8 key_size, const u8 *key, const u8 *addr,
774                        u32 tx_seq_32, u16 tx_seq_16)
775 {
776         struct wl1271_cmd_set_keys *cmd;
777         int ret = 0;
778
779         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
780         if (!cmd) {
781                 ret = -ENOMEM;
782                 goto out;
783         }
784
785         if (key_type != KEY_WEP)
786                 memcpy(cmd->addr, addr, ETH_ALEN);
787
788         cmd->key_action = action;
789         cmd->key_size = key_size;
790         cmd->key_type = key_type;
791
792         cmd->ac_seq_num16[0] = tx_seq_16;
793         cmd->ac_seq_num32[0] = tx_seq_32;
794
795         /* we have only one SSID profile */
796         cmd->ssid_profile = 0;
797
798         cmd->id = id;
799
800         if (key_type == KEY_TKIP) {
801                 /*
802                  * We get the key in the following form:
803                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
804                  * but the target is expecting:
805                  * TKIP - RX MIC - TX MIC
806                  */
807                 memcpy(cmd->key, key, 16);
808                 memcpy(cmd->key + 16, key + 24, 8);
809                 memcpy(cmd->key + 24, key + 16, 8);
810
811         } else {
812                 memcpy(cmd->key, key, key_size);
813         }
814
815         wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
816
817         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
818         if (ret < 0) {
819                 wl1271_warning("could not set keys");
820                 goto out;
821         }
822
823 out:
824         kfree(cmd);
825
826         return ret;
827 }