Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[sfrench/cifs-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                     size_t res_len)
47 {
48         struct wl1271_cmd_header *cmd;
49         unsigned long timeout;
50         u32 intr;
51         int ret = 0;
52         u16 status;
53
54         cmd = buf;
55         cmd->id = cpu_to_le16(id);
56         cmd->status = 0;
57
58         WARN_ON(len % 4 != 0);
59
60         wl1271_spi_write(wl, wl->cmd_box_addr, buf, len, false);
61
62         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
63
64         timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
65
66         intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
67         while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
68                 if (time_after(jiffies, timeout)) {
69                         wl1271_error("command complete timeout");
70                         ret = -ETIMEDOUT;
71                         goto out;
72                 }
73
74                 msleep(1);
75
76                 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
77         }
78
79         /* read back the status code of the command */
80         if (res_len == 0)
81                 res_len = sizeof(struct wl1271_cmd_header);
82         wl1271_spi_read(wl, wl->cmd_box_addr, cmd, res_len, false);
83
84         status = le16_to_cpu(cmd->status);
85         if (status != CMD_STATUS_SUCCESS) {
86                 wl1271_error("command execute failure %d", status);
87                 ret = -EIO;
88         }
89
90         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
91                            WL1271_ACX_INTR_CMD_COMPLETE);
92
93 out:
94         return ret;
95 }
96
97 static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
98 {
99         struct wl1271_cmd_cal_channel_tune *cmd;
100         int ret = 0;
101
102         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
103         if (!cmd)
104                 return -ENOMEM;
105
106         cmd->test.id = TEST_CMD_CHANNEL_TUNE;
107
108         cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
109         /* set up any channel, 7 is in the middle of the range */
110         cmd->channel = 7;
111
112         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
113         if (ret < 0)
114                 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
115
116         kfree(cmd);
117         return ret;
118 }
119
120 static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
121 {
122         struct wl1271_cmd_cal_update_ref_point *cmd;
123         int ret = 0;
124
125         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
126         if (!cmd)
127                 return -ENOMEM;
128
129         cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
130
131         /* FIXME: still waiting for the correct values */
132         cmd->ref_power    = 0;
133         cmd->ref_detector = 0;
134
135         cmd->sub_band     = WL1271_PD_REFERENCE_POINT_BAND_B_G;
136
137         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
138         if (ret < 0)
139                 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
140
141         kfree(cmd);
142         return ret;
143 }
144
145 static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
146 {
147         struct wl1271_cmd_cal_p2g *cmd;
148         int ret = 0;
149
150         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
151         if (!cmd)
152                 return -ENOMEM;
153
154         cmd->test.id = TEST_CMD_P2G_CAL;
155
156         cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
157
158         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
159         if (ret < 0)
160                 wl1271_warning("TEST_CMD_P2G_CAL failed");
161
162         kfree(cmd);
163         return ret;
164 }
165
166 static int wl1271_cmd_cal(struct wl1271 *wl)
167 {
168         /*
169          * FIXME: we must make sure that we're not sleeping when calibration
170          * is done
171          */
172         int ret;
173
174         wl1271_notice("performing tx calibration");
175
176         ret = wl1271_cmd_cal_channel_tune(wl);
177         if (ret < 0)
178                 return ret;
179
180         ret = wl1271_cmd_cal_update_ref_point(wl);
181         if (ret < 0)
182                 return ret;
183
184         ret = wl1271_cmd_cal_p2g(wl);
185         if (ret < 0)
186                 return ret;
187
188         return ret;
189 }
190
191 int wl1271_cmd_general_parms(struct wl1271 *wl)
192 {
193         struct wl1271_general_parms_cmd *gen_parms;
194         struct conf_general_parms *g = &wl->conf.init.genparam;
195         int ret;
196
197         gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
198         if (!gen_parms)
199                 return -ENOMEM;
200
201         gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
202
203         gen_parms->ref_clk = g->ref_clk;
204         gen_parms->settling_time = g->settling_time;
205         gen_parms->clk_valid_on_wakeup = g->clk_valid_on_wakeup;
206         gen_parms->dc2dcmode = g->dc2dcmode;
207         gen_parms->single_dual_band = g->single_dual_band;
208         gen_parms->tx_bip_fem_autodetect = g->tx_bip_fem_autodetect;
209         gen_parms->tx_bip_fem_manufacturer = g->tx_bip_fem_manufacturer;
210         gen_parms->settings = g->settings;
211
212         ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
213         if (ret < 0)
214                 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
215
216         kfree(gen_parms);
217         return ret;
218 }
219
220 int wl1271_cmd_radio_parms(struct wl1271 *wl)
221 {
222         struct wl1271_radio_parms_cmd *radio_parms;
223         struct conf_radio_parms *r = &wl->conf.init.radioparam;
224         int i, ret;
225
226         radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
227         if (!radio_parms)
228                 return -ENOMEM;
229
230         radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
231
232         /* Static radio parameters */
233         radio_parms->rx_trace_loss = r->rx_trace_loss;
234         radio_parms->tx_trace_loss = r->tx_trace_loss;
235         memcpy(radio_parms->rx_rssi_and_proc_compens,
236                r->rx_rssi_and_proc_compens,
237                CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
238
239         memcpy(radio_parms->rx_trace_loss_5, r->rx_trace_loss_5,
240                CONF_NUMBER_OF_SUB_BANDS_5);
241         memcpy(radio_parms->tx_trace_loss_5, r->tx_trace_loss_5,
242                CONF_NUMBER_OF_SUB_BANDS_5);
243         memcpy(radio_parms->rx_rssi_and_proc_compens_5,
244                r->rx_rssi_and_proc_compens_5,
245                CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
246
247         /* Dynamic radio parameters */
248         radio_parms->tx_ref_pd_voltage = cpu_to_le16(r->tx_ref_pd_voltage);
249         radio_parms->tx_ref_power = r->tx_ref_power;
250         radio_parms->tx_offset_db = r->tx_offset_db;
251
252         memcpy(radio_parms->tx_rate_limits_normal, r->tx_rate_limits_normal,
253                CONF_NUMBER_OF_RATE_GROUPS);
254         memcpy(radio_parms->tx_rate_limits_degraded, r->tx_rate_limits_degraded,
255                CONF_NUMBER_OF_RATE_GROUPS);
256
257         memcpy(radio_parms->tx_channel_limits_11b, r->tx_channel_limits_11b,
258                CONF_NUMBER_OF_CHANNELS_2_4);
259         memcpy(radio_parms->tx_channel_limits_ofdm, r->tx_channel_limits_ofdm,
260                CONF_NUMBER_OF_CHANNELS_2_4);
261         memcpy(radio_parms->tx_pdv_rate_offsets, r->tx_pdv_rate_offsets,
262                CONF_NUMBER_OF_RATE_GROUPS);
263         memcpy(radio_parms->tx_ibias, r->tx_ibias, CONF_NUMBER_OF_RATE_GROUPS);
264
265         radio_parms->rx_fem_insertion_loss = r->rx_fem_insertion_loss;
266
267         for (i = 0; i < CONF_NUMBER_OF_SUB_BANDS_5; i++)
268                 radio_parms->tx_ref_pd_voltage_5[i] =
269                         cpu_to_le16(r->tx_ref_pd_voltage_5[i]);
270         memcpy(radio_parms->tx_ref_power_5, r->tx_ref_power_5,
271                CONF_NUMBER_OF_SUB_BANDS_5);
272         memcpy(radio_parms->tx_offset_db_5, r->tx_offset_db_5,
273                CONF_NUMBER_OF_SUB_BANDS_5);
274         memcpy(radio_parms->tx_rate_limits_normal_5,
275                r->tx_rate_limits_normal_5, CONF_NUMBER_OF_RATE_GROUPS);
276         memcpy(radio_parms->tx_rate_limits_degraded_5,
277                r->tx_rate_limits_degraded_5, CONF_NUMBER_OF_RATE_GROUPS);
278         memcpy(radio_parms->tx_channel_limits_ofdm_5,
279                r->tx_channel_limits_ofdm_5, CONF_NUMBER_OF_CHANNELS_5);
280         memcpy(radio_parms->tx_pdv_rate_offsets_5, r->tx_pdv_rate_offsets_5,
281                CONF_NUMBER_OF_RATE_GROUPS);
282         memcpy(radio_parms->tx_ibias_5, r->tx_ibias_5,
283                CONF_NUMBER_OF_RATE_GROUPS);
284         memcpy(radio_parms->rx_fem_insertion_loss_5,
285                r->rx_fem_insertion_loss_5, CONF_NUMBER_OF_SUB_BANDS_5);
286
287         wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
288                     radio_parms, sizeof(*radio_parms));
289
290         ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
291         if (ret < 0)
292                 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
293
294         kfree(radio_parms);
295         return ret;
296 }
297
298 int wl1271_cmd_join(struct wl1271 *wl)
299 {
300         static bool do_cal = true;
301         struct wl1271_cmd_join *join;
302         int ret, i;
303         u8 *bssid;
304
305         /* FIXME: remove when we get calibration from the factory */
306         if (do_cal) {
307                 ret = wl1271_cmd_cal(wl);
308                 if (ret < 0)
309                         wl1271_warning("couldn't calibrate");
310                 else
311                         do_cal = false;
312         }
313
314         /* FIXME: This is a workaround, because with the current stack, we
315          * cannot know when we have disassociated.  So, if we have already
316          * joined, we disconnect before joining again. */
317         if (wl->joined) {
318                 ret = wl1271_cmd_disconnect(wl);
319                 if (ret < 0) {
320                         wl1271_error("failed to disconnect before rejoining");
321                         goto out;
322                 }
323
324                 wl->joined = false;
325         }
326
327         join = kzalloc(sizeof(*join), GFP_KERNEL);
328         if (!join) {
329                 ret = -ENOMEM;
330                 goto out;
331         }
332
333         wl1271_debug(DEBUG_CMD, "cmd join");
334
335         /* Reverse order BSSID */
336         bssid = (u8 *) &join->bssid_lsb;
337         for (i = 0; i < ETH_ALEN; i++)
338                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
339
340         join->rx_config_options = cpu_to_le32(wl->rx_config);
341         join->rx_filter_options = cpu_to_le32(wl->rx_filter);
342         join->bss_type = wl->bss_type;
343
344         /*
345          * FIXME: disable temporarily all filters because after commit
346          * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
347          * association. The filter logic needs to be implemented properly
348          * and once that is done, this hack can be removed.
349          */
350         join->rx_config_options = cpu_to_le32(0);
351         join->rx_filter_options = cpu_to_le32(WL1271_DEFAULT_RX_FILTER);
352
353         if (wl->band == IEEE80211_BAND_2GHZ)
354                 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_1MBPS   |
355                                                    CONF_HW_BIT_RATE_2MBPS   |
356                                                    CONF_HW_BIT_RATE_5_5MBPS |
357                                                    CONF_HW_BIT_RATE_11MBPS);
358         else {
359                 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
360                 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_6MBPS  |
361                                                    CONF_HW_BIT_RATE_12MBPS |
362                                                    CONF_HW_BIT_RATE_24MBPS);
363         }
364
365         join->beacon_interval = cpu_to_le16(WL1271_DEFAULT_BEACON_INT);
366         join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
367
368         join->channel = wl->channel;
369         join->ssid_len = wl->ssid_len;
370         memcpy(join->ssid, wl->ssid, wl->ssid_len);
371         join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
372
373         /* increment the session counter */
374         wl->session_counter++;
375         if (wl->session_counter >= SESSION_COUNTER_MAX)
376                 wl->session_counter = 0;
377
378         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
379
380         /* reset TX security counters */
381         wl->tx_security_last_seq = 0;
382         wl->tx_security_seq_16 = 0;
383         wl->tx_security_seq_32 = 0;
384
385         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
386         if (ret < 0) {
387                 wl1271_error("failed to initiate cmd join");
388                 goto out_free;
389         }
390
391         wl->joined = true;
392
393         /*
394          * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
395          * simplify locking we just sleep instead, for now
396          */
397         msleep(10);
398
399 out_free:
400         kfree(join);
401
402 out:
403         return ret;
404 }
405
406 /**
407  * send test command to firmware
408  *
409  * @wl: wl struct
410  * @buf: buffer containing the command, with all headers, must work with dma
411  * @len: length of the buffer
412  * @answer: is answer needed
413  */
414 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
415 {
416         int ret;
417         size_t res_len = 0;
418
419         wl1271_debug(DEBUG_CMD, "cmd test");
420
421         if (answer)
422                 res_len = buf_len;
423
424         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
425
426         if (ret < 0) {
427                 wl1271_warning("TEST command failed");
428                 return ret;
429         }
430
431         return ret;
432 }
433
434 /**
435  * read acx from firmware
436  *
437  * @wl: wl struct
438  * @id: acx id
439  * @buf: buffer for the response, including all headers, must work with dma
440  * @len: lenght of buf
441  */
442 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
443 {
444         struct acx_header *acx = buf;
445         int ret;
446
447         wl1271_debug(DEBUG_CMD, "cmd interrogate");
448
449         acx->id = cpu_to_le16(id);
450
451         /* payload length, does not include any headers */
452         acx->len = cpu_to_le16(len - sizeof(*acx));
453
454         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
455         if (ret < 0)
456                 wl1271_error("INTERROGATE command failed");
457
458         return ret;
459 }
460
461 /**
462  * write acx value to firmware
463  *
464  * @wl: wl struct
465  * @id: acx id
466  * @buf: buffer containing acx, including all headers, must work with dma
467  * @len: length of buf
468  */
469 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
470 {
471         struct acx_header *acx = buf;
472         int ret;
473
474         wl1271_debug(DEBUG_CMD, "cmd configure");
475
476         acx->id = cpu_to_le16(id);
477
478         /* payload length, does not include any headers */
479         acx->len = cpu_to_le16(len - sizeof(*acx));
480
481         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
482         if (ret < 0) {
483                 wl1271_warning("CONFIGURE command NOK");
484                 return ret;
485         }
486
487         return 0;
488 }
489
490 int wl1271_cmd_data_path(struct wl1271 *wl, u8 channel, bool enable)
491 {
492         struct cmd_enabledisable_path *cmd;
493         int ret;
494         u16 cmd_rx, cmd_tx;
495
496         wl1271_debug(DEBUG_CMD, "cmd data path");
497
498         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
499         if (!cmd) {
500                 ret = -ENOMEM;
501                 goto out;
502         }
503
504         cmd->channel = channel;
505
506         if (enable) {
507                 cmd_rx = CMD_ENABLE_RX;
508                 cmd_tx = CMD_ENABLE_TX;
509         } else {
510                 cmd_rx = CMD_DISABLE_RX;
511                 cmd_tx = CMD_DISABLE_TX;
512         }
513
514         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
515         if (ret < 0) {
516                 wl1271_error("rx %s cmd for channel %d failed",
517                              enable ? "start" : "stop", channel);
518                 goto out;
519         }
520
521         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
522                      enable ? "start" : "stop", channel);
523
524         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
525         if (ret < 0) {
526                 wl1271_error("tx %s cmd for channel %d failed",
527                              enable ? "start" : "stop", channel);
528                 return ret;
529         }
530
531         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
532                      enable ? "start" : "stop", channel);
533
534 out:
535         kfree(cmd);
536         return ret;
537 }
538
539 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
540 {
541         struct wl1271_cmd_ps_params *ps_params = NULL;
542         int ret = 0;
543
544         /* FIXME: this should be in ps.c */
545         ret = wl1271_acx_wake_up_conditions(wl);
546         if (ret < 0) {
547                 wl1271_error("couldn't set wake up conditions");
548                 goto out;
549         }
550
551         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
552
553         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
554         if (!ps_params) {
555                 ret = -ENOMEM;
556                 goto out;
557         }
558
559         ps_params->ps_mode = ps_mode;
560         ps_params->send_null_data = 1;
561         ps_params->retries = 5;
562         ps_params->hang_over_period = 128;
563         ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
564
565         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
566                               sizeof(*ps_params), 0);
567         if (ret < 0) {
568                 wl1271_error("cmd set_ps_mode failed");
569                 goto out;
570         }
571
572 out:
573         kfree(ps_params);
574         return ret;
575 }
576
577 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
578                            size_t len)
579 {
580         struct cmd_read_write_memory *cmd;
581         int ret = 0;
582
583         wl1271_debug(DEBUG_CMD, "cmd read memory");
584
585         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
586         if (!cmd) {
587                 ret = -ENOMEM;
588                 goto out;
589         }
590
591         WARN_ON(len > MAX_READ_SIZE);
592         len = min_t(size_t, len, MAX_READ_SIZE);
593
594         cmd->addr = cpu_to_le32(addr);
595         cmd->size = cpu_to_le32(len);
596
597         ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
598                               sizeof(*cmd));
599         if (ret < 0) {
600                 wl1271_error("read memory command failed: %d", ret);
601                 goto out;
602         }
603
604         /* the read command got in */
605         memcpy(answer, cmd->value, len);
606
607 out:
608         kfree(cmd);
609         return ret;
610 }
611
612 int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
613                     u8 active_scan, u8 high_prio, u8 band,
614                     u8 probe_requests)
615 {
616
617         struct wl1271_cmd_trigger_scan_to *trigger = NULL;
618         struct wl1271_cmd_scan *params = NULL;
619         struct ieee80211_channel *channels;
620         int i, j, n_ch, ret;
621         u16 scan_options = 0;
622         u8 ieee_band;
623
624         if (band == WL1271_SCAN_BAND_2_4_GHZ)
625                 ieee_band = IEEE80211_BAND_2GHZ;
626         else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
627                 ieee_band = IEEE80211_BAND_2GHZ;
628         else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
629                 ieee_band = IEEE80211_BAND_5GHZ;
630         else
631                 return -EINVAL;
632
633         if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
634                 return -EINVAL;
635
636         channels = wl->hw->wiphy->bands[ieee_band]->channels;
637         n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
638
639         if (wl->scanning)
640                 return -EINVAL;
641
642         params = kzalloc(sizeof(*params), GFP_KERNEL);
643         if (!params)
644                 return -ENOMEM;
645
646         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
647         params->params.rx_filter_options =
648                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
649
650         if (!active_scan)
651                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
652         if (high_prio)
653                 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
654         params->params.scan_options = cpu_to_le16(scan_options);
655
656         params->params.num_probe_requests = probe_requests;
657         /* Let the fw autodetect suitable tx_rate for probes */
658         params->params.tx_rate = 0;
659         params->params.tid_trigger = 0;
660         params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
661
662         if (band == WL1271_SCAN_BAND_DUAL)
663                 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
664         else
665                 params->params.band = band;
666
667         for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
668                 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
669                         params->channels[j].min_duration =
670                                 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
671                         params->channels[j].max_duration =
672                                 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
673                         memset(&params->channels[j].bssid_lsb, 0xff, 4);
674                         memset(&params->channels[j].bssid_msb, 0xff, 2);
675                         params->channels[j].early_termination = 0;
676                         params->channels[j].tx_power_att =
677                                 WL1271_SCAN_CURRENT_TX_PWR;
678                         params->channels[j].channel = channels[i].hw_value;
679                         j++;
680                 }
681         }
682
683         params->params.num_channels = j;
684
685         if (len && ssid) {
686                 params->params.ssid_len = len;
687                 memcpy(params->params.ssid, ssid, len);
688         }
689
690         ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
691         if (ret < 0) {
692                 wl1271_error("PROBE request template failed");
693                 goto out;
694         }
695
696         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
697         if (!trigger) {
698                 ret = -ENOMEM;
699                 goto out;
700         }
701
702         /* disable the timeout */
703         trigger->timeout = 0;
704
705         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
706                               sizeof(*trigger), 0);
707         if (ret < 0) {
708                 wl1271_error("trigger scan to failed for hw scan");
709                 goto out;
710         }
711
712         wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
713
714         wl->scanning = true;
715         if (wl1271_11a_enabled()) {
716                 wl->scan.state = band;
717                 if (band == WL1271_SCAN_BAND_DUAL) {
718                         wl->scan.active = active_scan;
719                         wl->scan.high_prio = high_prio;
720                         wl->scan.probe_requests = probe_requests;
721                         if (len && ssid) {
722                                 wl->scan.ssid_len = len;
723                                 memcpy(wl->scan.ssid, ssid, len);
724                         } else
725                                 wl->scan.ssid_len = 0;
726                 }
727         }
728
729         ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
730         if (ret < 0) {
731                 wl1271_error("SCAN failed");
732                 wl->scanning = false;
733                 goto out;
734         }
735
736 out:
737         kfree(params);
738         return ret;
739 }
740
741 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
742                             void *buf, size_t buf_len)
743 {
744         struct wl1271_cmd_template_set *cmd;
745         int ret = 0;
746
747         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
748
749         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
750         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
751
752         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
753         if (!cmd) {
754                 ret = -ENOMEM;
755                 goto out;
756         }
757
758         cmd->len = cpu_to_le16(buf_len);
759         cmd->template_type = template_id;
760         cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
761         cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
762         cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
763
764         if (buf)
765                 memcpy(cmd->template_data, buf, buf_len);
766
767         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
768         if (ret < 0) {
769                 wl1271_warning("cmd set_template failed: %d", ret);
770                 goto out_free;
771         }
772
773 out_free:
774         kfree(cmd);
775
776 out:
777         return ret;
778 }
779
780 static int wl1271_build_basic_rates(u8 *rates, u8 band)
781 {
782         u8 index = 0;
783
784         if (band == IEEE80211_BAND_2GHZ) {
785                 rates[index++] =
786                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
787                 rates[index++] =
788                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
789                 rates[index++] =
790                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
791                 rates[index++] =
792                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
793         } else if (band == IEEE80211_BAND_5GHZ) {
794                 rates[index++] =
795                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
796                 rates[index++] =
797                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
798                 rates[index++] =
799                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
800         } else {
801                 wl1271_error("build_basic_rates invalid band: %d", band);
802         }
803
804         return index;
805 }
806
807 static int wl1271_build_extended_rates(u8 *rates, u8 band)
808 {
809         u8 index = 0;
810
811         if (band == IEEE80211_BAND_2GHZ) {
812                 rates[index++] = IEEE80211_OFDM_RATE_6MB;
813                 rates[index++] = IEEE80211_OFDM_RATE_9MB;
814                 rates[index++] = IEEE80211_OFDM_RATE_12MB;
815                 rates[index++] = IEEE80211_OFDM_RATE_18MB;
816                 rates[index++] = IEEE80211_OFDM_RATE_24MB;
817                 rates[index++] = IEEE80211_OFDM_RATE_36MB;
818                 rates[index++] = IEEE80211_OFDM_RATE_48MB;
819                 rates[index++] = IEEE80211_OFDM_RATE_54MB;
820         } else if (band == IEEE80211_BAND_5GHZ) {
821                 rates[index++] =
822                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
823                 rates[index++] =
824                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
825                 rates[index++] =
826                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
827                 rates[index++] =
828                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
829                 rates[index++] =
830                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
831                 rates[index++] =
832                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
833         } else {
834                 wl1271_error("build_basic_rates invalid band: %d", band);
835         }
836
837         return index;
838 }
839
840 int wl1271_cmd_build_null_data(struct wl1271 *wl)
841 {
842         struct wl12xx_null_data_template template;
843
844         if (!is_zero_ether_addr(wl->bssid)) {
845                 memcpy(template.header.da, wl->bssid, ETH_ALEN);
846                 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
847         } else {
848                 memset(template.header.da, 0xff, ETH_ALEN);
849                 memset(template.header.bssid, 0xff, ETH_ALEN);
850         }
851
852         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
853         template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
854                                                 IEEE80211_STYPE_NULLFUNC |
855                                                 IEEE80211_FCTL_TODS);
856
857         return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
858                                        sizeof(template));
859
860 }
861
862 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
863 {
864         struct wl12xx_ps_poll_template template;
865
866         memcpy(template.bssid, wl->bssid, ETH_ALEN);
867         memcpy(template.ta, wl->mac_addr, ETH_ALEN);
868
869         /* aid in PS-Poll has its two MSBs each set to 1 */
870         template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
871
872         template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
873
874         return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
875                                        sizeof(template));
876
877 }
878
879 int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
880                                u8 band)
881 {
882         struct wl12xx_probe_req_template template;
883         struct wl12xx_ie_rates *rates;
884         char *ptr;
885         u16 size;
886         int ret;
887
888         ptr = (char *)&template;
889         size = sizeof(struct ieee80211_header);
890
891         memset(template.header.da, 0xff, ETH_ALEN);
892         memset(template.header.bssid, 0xff, ETH_ALEN);
893         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
894         template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
895
896         /* IEs */
897         /* SSID */
898         template.ssid.header.id = WLAN_EID_SSID;
899         template.ssid.header.len = ssid_len;
900         if (ssid_len && ssid)
901                 memcpy(template.ssid.ssid, ssid, ssid_len);
902         size += sizeof(struct wl12xx_ie_header) + ssid_len;
903         ptr += size;
904
905         /* Basic Rates */
906         rates = (struct wl12xx_ie_rates *)ptr;
907         rates->header.id = WLAN_EID_SUPP_RATES;
908         rates->header.len = wl1271_build_basic_rates(rates->rates, band);
909         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
910         ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
911
912         /* Extended rates */
913         rates = (struct wl12xx_ie_rates *)ptr;
914         rates->header.id = WLAN_EID_EXT_SUPP_RATES;
915         rates->header.len = wl1271_build_extended_rates(rates->rates, band);
916         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
917
918         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
919
920         if (band == IEEE80211_BAND_2GHZ)
921                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
922                                               &template, size);
923         else
924                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
925                                               &template, size);
926         return ret;
927 }
928
929 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
930 {
931         struct wl1271_cmd_set_keys *cmd;
932         int ret = 0;
933
934         wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
935
936         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
937         if (!cmd) {
938                 ret = -ENOMEM;
939                 goto out;
940         }
941
942         cmd->id = id;
943         cmd->key_action = cpu_to_le16(KEY_SET_ID);
944         cmd->key_type = KEY_WEP;
945
946         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
947         if (ret < 0) {
948                 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
949                 goto out;
950         }
951
952 out:
953         kfree(cmd);
954
955         return ret;
956 }
957
958 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
959                        u8 key_size, const u8 *key, const u8 *addr,
960                        u32 tx_seq_32, u16 tx_seq_16)
961 {
962         struct wl1271_cmd_set_keys *cmd;
963         int ret = 0;
964
965         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
966         if (!cmd) {
967                 ret = -ENOMEM;
968                 goto out;
969         }
970
971         if (key_type != KEY_WEP)
972                 memcpy(cmd->addr, addr, ETH_ALEN);
973
974         cmd->key_action = cpu_to_le16(action);
975         cmd->key_size = key_size;
976         cmd->key_type = key_type;
977
978         cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
979         cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
980
981         /* we have only one SSID profile */
982         cmd->ssid_profile = 0;
983
984         cmd->id = id;
985
986         if (key_type == KEY_TKIP) {
987                 /*
988                  * We get the key in the following form:
989                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
990                  * but the target is expecting:
991                  * TKIP - RX MIC - TX MIC
992                  */
993                 memcpy(cmd->key, key, 16);
994                 memcpy(cmd->key + 16, key + 24, 8);
995                 memcpy(cmd->key + 24, key + 16, 8);
996
997         } else {
998                 memcpy(cmd->key, key, key_size);
999         }
1000
1001         wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1002
1003         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1004         if (ret < 0) {
1005                 wl1271_warning("could not set keys");
1006                 goto out;
1007         }
1008
1009 out:
1010         kfree(cmd);
1011
1012         return ret;
1013 }
1014
1015 int wl1271_cmd_disconnect(struct wl1271 *wl)
1016 {
1017         struct wl1271_cmd_disconnect *cmd;
1018         int ret = 0;
1019
1020         wl1271_debug(DEBUG_CMD, "cmd disconnect");
1021
1022         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1023         if (!cmd) {
1024                 ret = -ENOMEM;
1025                 goto out;
1026         }
1027
1028         cmd->rx_config_options = cpu_to_le32(wl->rx_config);
1029         cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
1030         /* disconnect reason is not used in immediate disconnections */
1031         cmd->type = DISCONNECT_IMMEDIATE;
1032
1033         ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
1034         if (ret < 0) {
1035                 wl1271_error("failed to send disconnect command");
1036                 goto out_free;
1037         }
1038
1039 out_free:
1040         kfree(cmd);
1041
1042 out:
1043         return ret;
1044 }