wl1271: Update join usage
[safe/jmp/linux-2.6] / 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         join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
212                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
213
214         join->beacon_interval = wl->beacon_int;
215         join->dtim_interval = wl->dtim_period;
216         join->bss_type = wl->bss_type;
217         join->channel = wl->channel;
218         join->ssid_len = wl->ssid_len;
219         memcpy(join->ssid, wl->ssid, wl->ssid_len);
220         join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
221
222         /* increment the session counter */
223         wl->session_counter++;
224         if (wl->session_counter >= SESSION_COUNTER_MAX)
225                 wl->session_counter = 0;
226
227         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
228
229         /* reset TX security counters */
230         wl->tx_security_last_seq = 0;
231         wl->tx_security_seq_16 = 0;
232         wl->tx_security_seq_32 = 0;
233
234         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
235         if (ret < 0) {
236                 wl1271_error("failed to initiate cmd join");
237                 goto out_free;
238         }
239
240         /*
241          * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
242          * simplify locking we just sleep instead, for now
243          */
244         msleep(10);
245
246 out_free:
247         kfree(join);
248
249 out:
250         return ret;
251 }
252
253 /**
254  * send test command to firmware
255  *
256  * @wl: wl struct
257  * @buf: buffer containing the command, with all headers, must work with dma
258  * @len: length of the buffer
259  * @answer: is answer needed
260  */
261 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
262 {
263         int ret;
264
265         wl1271_debug(DEBUG_CMD, "cmd test");
266
267         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len);
268
269         if (ret < 0) {
270                 wl1271_warning("TEST command failed");
271                 return ret;
272         }
273
274         if (answer) {
275                 struct wl1271_command *cmd_answer;
276
277                 /*
278                  * The test command got in, we can read the answer.
279                  * The answer would be a wl1271_command, where the
280                  * parameter array contains the actual answer.
281                  */
282                 wl1271_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
283
284                 cmd_answer = buf;
285
286                 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
287                         wl1271_error("TEST command answer error: %d",
288                                      cmd_answer->header.status);
289         }
290
291         return 0;
292 }
293
294 /**
295  * read acx from firmware
296  *
297  * @wl: wl struct
298  * @id: acx id
299  * @buf: buffer for the response, including all headers, must work with dma
300  * @len: lenght of buf
301  */
302 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
303 {
304         struct acx_header *acx = buf;
305         int ret;
306
307         wl1271_debug(DEBUG_CMD, "cmd interrogate");
308
309         acx->id = id;
310
311         /* payload length, does not include any headers */
312         acx->len = len - sizeof(*acx);
313
314         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
315         if (ret < 0) {
316                 wl1271_error("INTERROGATE command failed");
317                 goto out;
318         }
319
320         /* the interrogate command got in, we can read the answer */
321         wl1271_spi_mem_read(wl, wl->cmd_box_addr, buf, len);
322
323         acx = buf;
324         if (acx->cmd.status != CMD_STATUS_SUCCESS)
325                 wl1271_error("INTERROGATE command error: %d",
326                              acx->cmd.status);
327
328 out:
329         return ret;
330 }
331
332 /**
333  * write acx value to firmware
334  *
335  * @wl: wl struct
336  * @id: acx id
337  * @buf: buffer containing acx, including all headers, must work with dma
338  * @len: length of buf
339  */
340 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
341 {
342         struct acx_header *acx = buf;
343         int ret;
344
345         wl1271_debug(DEBUG_CMD, "cmd configure");
346
347         acx->id = id;
348
349         /* payload length, does not include any headers */
350         acx->len = len - sizeof(*acx);
351
352         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len);
353         if (ret < 0) {
354                 wl1271_warning("CONFIGURE command NOK");
355                 return ret;
356         }
357
358         return 0;
359 }
360
361 int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
362 {
363         struct cmd_enabledisable_path *cmd;
364         int ret;
365         u16 cmd_rx, cmd_tx;
366
367         wl1271_debug(DEBUG_CMD, "cmd data path");
368
369         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
370         if (!cmd) {
371                 ret = -ENOMEM;
372                 goto out;
373         }
374
375         cmd->channel = channel;
376
377         if (enable) {
378                 cmd_rx = CMD_ENABLE_RX;
379                 cmd_tx = CMD_ENABLE_TX;
380         } else {
381                 cmd_rx = CMD_DISABLE_RX;
382                 cmd_tx = CMD_DISABLE_TX;
383         }
384
385         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
386         if (ret < 0) {
387                 wl1271_error("rx %s cmd for channel %d failed",
388                              enable ? "start" : "stop", channel);
389                 goto out;
390         }
391
392         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
393                      enable ? "start" : "stop", channel);
394
395         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
396         if (ret < 0) {
397                 wl1271_error("tx %s cmd for channel %d failed",
398                              enable ? "start" : "stop", channel);
399                 return ret;
400         }
401
402         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
403                      enable ? "start" : "stop", channel);
404
405 out:
406         kfree(cmd);
407         return ret;
408 }
409
410 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
411 {
412         struct wl1271_cmd_ps_params *ps_params = NULL;
413         int ret = 0;
414
415         /* FIXME: this should be in ps.c */
416         ret = wl1271_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
417                                             wl->listen_int);
418         if (ret < 0) {
419                 wl1271_error("couldn't set wake up conditions");
420                 goto out;
421         }
422
423         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
424
425         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
426         if (!ps_params) {
427                 ret = -ENOMEM;
428                 goto out;
429         }
430
431         ps_params->ps_mode = ps_mode;
432         ps_params->send_null_data = 1;
433         ps_params->retries = 5;
434         ps_params->hang_over_period = 128;
435         ps_params->null_data_rate = 1; /* 1 Mbps */
436
437         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
438                               sizeof(*ps_params));
439         if (ret < 0) {
440                 wl1271_error("cmd set_ps_mode failed");
441                 goto out;
442         }
443
444 out:
445         kfree(ps_params);
446         return ret;
447 }
448
449 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
450                            size_t len)
451 {
452         struct cmd_read_write_memory *cmd;
453         int ret = 0;
454
455         wl1271_debug(DEBUG_CMD, "cmd read memory");
456
457         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
458         if (!cmd) {
459                 ret = -ENOMEM;
460                 goto out;
461         }
462
463         WARN_ON(len > MAX_READ_SIZE);
464         len = min_t(size_t, len, MAX_READ_SIZE);
465
466         cmd->addr = addr;
467         cmd->size = len;
468
469         ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
470         if (ret < 0) {
471                 wl1271_error("read memory command failed: %d", ret);
472                 goto out;
473         }
474
475         /* the read command got in, we can now read the answer */
476         wl1271_spi_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
477
478         if (cmd->header.status != CMD_STATUS_SUCCESS)
479                 wl1271_error("error in read command result: %d",
480                              cmd->header.status);
481
482         memcpy(answer, cmd->value, len);
483
484 out:
485         kfree(cmd);
486         return ret;
487 }
488
489 int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
490                     u8 active_scan, u8 high_prio, u8 num_channels,
491                     u8 probe_requests)
492 {
493
494         struct wl1271_cmd_trigger_scan_to *trigger = NULL;
495         struct wl1271_cmd_scan *params = NULL;
496         int i, ret;
497         u16 scan_options = 0;
498
499         if (wl->scanning)
500                 return -EINVAL;
501
502         params = kzalloc(sizeof(*params), GFP_KERNEL);
503         if (!params)
504                 return -ENOMEM;
505
506         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
507         params->params.rx_filter_options =
508                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
509
510         if (!active_scan)
511                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
512         if (high_prio)
513                 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
514         params->params.scan_options = scan_options;
515
516         params->params.num_channels = num_channels;
517         params->params.num_probe_requests = probe_requests;
518         params->params.tx_rate = cpu_to_le32(RATE_MASK_2MBPS);
519         params->params.tid_trigger = 0;
520         params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
521
522         for (i = 0; i < num_channels; i++) {
523                 params->channels[i].min_duration =
524                         cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
525                 params->channels[i].max_duration =
526                         cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
527                 memset(&params->channels[i].bssid_lsb, 0xff, 4);
528                 memset(&params->channels[i].bssid_msb, 0xff, 2);
529                 params->channels[i].early_termination = 0;
530                 params->channels[i].tx_power_att = WL1271_SCAN_CURRENT_TX_PWR;
531                 params->channels[i].channel = i + 1;
532         }
533
534         if (len && ssid) {
535                 params->params.ssid_len = len;
536                 memcpy(params->params.ssid, ssid, len);
537         }
538
539         ret = wl1271_cmd_build_probe_req(wl, ssid, len);
540         if (ret < 0) {
541                 wl1271_error("PROBE request template failed");
542                 goto out;
543         }
544
545         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
546         if (!trigger) {
547                 ret = -ENOMEM;
548                 goto out;
549         }
550
551         /* disable the timeout */
552         trigger->timeout = 0;
553
554         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
555                               sizeof(*trigger));
556         if (ret < 0) {
557                 wl1271_error("trigger scan to failed for hw scan");
558                 goto out;
559         }
560
561         wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
562
563         wl->scanning = true;
564
565         ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
566         if (ret < 0) {
567                 wl1271_error("SCAN failed");
568                 goto out;
569         }
570
571         wl1271_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
572
573         if (params->header.status != CMD_STATUS_SUCCESS) {
574                 wl1271_error("Scan command error: %d",
575                              params->header.status);
576                 wl->scanning = false;
577                 ret = -EIO;
578                 goto out;
579         }
580
581 out:
582         kfree(params);
583         return ret;
584 }
585
586 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
587                             void *buf, size_t buf_len)
588 {
589         struct wl1271_cmd_template_set *cmd;
590         int ret = 0;
591
592         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
593
594         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
595         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
596
597         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
598         if (!cmd) {
599                 ret = -ENOMEM;
600                 goto out;
601         }
602
603         cmd->len = cpu_to_le16(buf_len);
604         cmd->template_type = template_id;
605         cmd->enabled_rates = ACX_RATE_MASK_UNSPECIFIED;
606         cmd->short_retry_limit = ACX_RATE_RETRY_LIMIT;
607         cmd->long_retry_limit = ACX_RATE_RETRY_LIMIT;
608
609         if (buf)
610                 memcpy(cmd->template_data, buf, buf_len);
611
612         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd));
613         if (ret < 0) {
614                 wl1271_warning("cmd set_template failed: %d", ret);
615                 goto out_free;
616         }
617
618 out_free:
619         kfree(cmd);
620
621 out:
622         return ret;
623 }
624
625 static int wl1271_build_basic_rates(char *rates)
626 {
627         u8 index = 0;
628
629         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
630         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
631         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
632         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
633
634         return index;
635 }
636
637 static int wl1271_build_extended_rates(char *rates)
638 {
639         u8 index = 0;
640
641         rates[index++] = IEEE80211_OFDM_RATE_6MB;
642         rates[index++] = IEEE80211_OFDM_RATE_9MB;
643         rates[index++] = IEEE80211_OFDM_RATE_12MB;
644         rates[index++] = IEEE80211_OFDM_RATE_18MB;
645         rates[index++] = IEEE80211_OFDM_RATE_24MB;
646         rates[index++] = IEEE80211_OFDM_RATE_36MB;
647         rates[index++] = IEEE80211_OFDM_RATE_48MB;
648         rates[index++] = IEEE80211_OFDM_RATE_54MB;
649
650         return index;
651 }
652
653 int wl1271_cmd_build_null_data(struct wl1271 *wl)
654 {
655         struct wl12xx_null_data_template template;
656
657         if (!is_zero_ether_addr(wl->bssid)) {
658                 memcpy(template.header.da, wl->bssid, ETH_ALEN);
659                 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
660         } else {
661                 memset(template.header.da, 0xff, ETH_ALEN);
662                 memset(template.header.bssid, 0xff, ETH_ALEN);
663         }
664
665         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
666         template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
667                                                 IEEE80211_STYPE_NULLFUNC);
668
669         return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
670                                        sizeof(template));
671
672 }
673
674 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
675 {
676         struct wl12xx_ps_poll_template template;
677
678         memcpy(template.bssid, wl->bssid, ETH_ALEN);
679         memcpy(template.ta, wl->mac_addr, ETH_ALEN);
680
681         /* aid in PS-Poll has its two MSBs each set to 1 */
682         template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
683
684         template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
685
686         return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
687                                        sizeof(template));
688
689 }
690
691 int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len)
692 {
693         struct wl12xx_probe_req_template template;
694         struct wl12xx_ie_rates *rates;
695         char *ptr;
696         u16 size;
697
698         ptr = (char *)&template;
699         size = sizeof(struct ieee80211_header);
700
701         memset(template.header.da, 0xff, ETH_ALEN);
702         memset(template.header.bssid, 0xff, ETH_ALEN);
703         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
704         template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
705
706         /* IEs */
707         /* SSID */
708         template.ssid.header.id = WLAN_EID_SSID;
709         template.ssid.header.len = ssid_len;
710         if (ssid_len && ssid)
711                 memcpy(template.ssid.ssid, ssid, ssid_len);
712         size += sizeof(struct wl12xx_ie_header) + ssid_len;
713         ptr += size;
714
715         /* Basic Rates */
716         rates = (struct wl12xx_ie_rates *)ptr;
717         rates->header.id = WLAN_EID_SUPP_RATES;
718         rates->header.len = wl1271_build_basic_rates(rates->rates);
719         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
720         ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
721
722         /* Extended rates */
723         rates = (struct wl12xx_ie_rates *)ptr;
724         rates->header.id = WLAN_EID_EXT_SUPP_RATES;
725         rates->header.len = wl1271_build_extended_rates(rates->rates);
726         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
727
728         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
729
730         return wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
731                                        &template, size);
732 }
733
734 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
735 {
736         struct wl1271_cmd_set_keys *cmd;
737         int ret = 0;
738
739         wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
740
741         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
742         if (!cmd) {
743                 ret = -ENOMEM;
744                 goto out;
745         }
746
747         cmd->id = id;
748         cmd->key_action = KEY_SET_ID;
749         cmd->key_type = KEY_WEP;
750
751         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
752         if (ret < 0) {
753                 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
754                 goto out;
755         }
756
757 out:
758         kfree(cmd);
759
760         return ret;
761 }
762
763 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
764                        u8 key_size, const u8 *key, const u8 *addr,
765                        u32 tx_seq_32, u16 tx_seq_16)
766 {
767         struct wl1271_cmd_set_keys *cmd;
768         int ret = 0;
769
770         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
771         if (!cmd) {
772                 ret = -ENOMEM;
773                 goto out;
774         }
775
776         if (key_type != KEY_WEP)
777                 memcpy(cmd->addr, addr, ETH_ALEN);
778
779         cmd->key_action = action;
780         cmd->key_size = key_size;
781         cmd->key_type = key_type;
782
783         cmd->ac_seq_num16[0] = tx_seq_16;
784         cmd->ac_seq_num32[0] = tx_seq_32;
785
786         /* we have only one SSID profile */
787         cmd->ssid_profile = 0;
788
789         cmd->id = id;
790
791         if (key_type == KEY_TKIP) {
792                 /*
793                  * We get the key in the following form:
794                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
795                  * but the target is expecting:
796                  * TKIP - RX MIC - TX MIC
797                  */
798                 memcpy(cmd->key, key, 16);
799                 memcpy(cmd->key + 16, key + 24, 8);
800                 memcpy(cmd->key + 24, key + 16, 8);
801
802         } else {
803                 memcpy(cmd->key, key, key_size);
804         }
805
806         wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
807
808         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd));
809         if (ret < 0) {
810                 wl1271_warning("could not set keys");
811                 goto out;
812         }
813
814 out:
815         kfree(cmd);
816
817         return ret;
818 }