wl1271: Work around AP's with broken ps-poll functionality
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_main.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2008-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/firmware.h>
26 #include <linux/delay.h>
27 #include <linux/spi/spi.h>
28 #include <linux/crc32.h>
29 #include <linux/etherdevice.h>
30 #include <linux/vmalloc.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33
34 #include "wl1271.h"
35 #include "wl12xx_80211.h"
36 #include "wl1271_reg.h"
37 #include "wl1271_io.h"
38 #include "wl1271_event.h"
39 #include "wl1271_tx.h"
40 #include "wl1271_rx.h"
41 #include "wl1271_ps.h"
42 #include "wl1271_init.h"
43 #include "wl1271_debugfs.h"
44 #include "wl1271_cmd.h"
45 #include "wl1271_boot.h"
46 #include "wl1271_testmode.h"
47
48 #define WL1271_BOOT_RETRIES 3
49
50 static struct conf_drv_settings default_conf = {
51         .sg = {
52                 .params = {
53                         [CONF_SG_BT_PER_THRESHOLD]                  = 7500,
54                         [CONF_SG_HV3_MAX_OVERRIDE]                  = 0,
55                         [CONF_SG_BT_NFS_SAMPLE_INTERVAL]            = 400,
56                         [CONF_SG_BT_LOAD_RATIO]                     = 50,
57                         [CONF_SG_AUTO_PS_MODE]                      = 0,
58                         [CONF_SG_AUTO_SCAN_PROBE_REQ]               = 170,
59                         [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_HV3]   = 50,
60                         [CONF_SG_ANTENNA_CONFIGURATION]             = 0,
61                         [CONF_SG_BEACON_MISS_PERCENT]               = 60,
62                         [CONF_SG_RATE_ADAPT_THRESH]                 = 12,
63                         [CONF_SG_RATE_ADAPT_SNR]                    = 0,
64                         [CONF_SG_WLAN_PS_BT_ACL_MASTER_MIN_BR]      = 10,
65                         [CONF_SG_WLAN_PS_BT_ACL_MASTER_MAX_BR]      = 30,
66                         [CONF_SG_WLAN_PS_MAX_BT_ACL_MASTER_BR]      = 8,
67                         [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MIN_BR]       = 20,
68                         [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MAX_BR]       = 50,
69                         /* Note: with UPSD, this should be 4 */
70                         [CONF_SG_WLAN_PS_MAX_BT_ACL_SLAVE_BR]       = 8,
71                         [CONF_SG_WLAN_PS_BT_ACL_MASTER_MIN_EDR]     = 7,
72                         [CONF_SG_WLAN_PS_BT_ACL_MASTER_MAX_EDR]     = 25,
73                         [CONF_SG_WLAN_PS_MAX_BT_ACL_MASTER_EDR]     = 20,
74                         /* Note: with UPDS, this should be 15 */
75                         [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MIN_EDR]      = 8,
76                         /* Note: with UPDS, this should be 50 */
77                         [CONF_SG_WLAN_PS_BT_ACL_SLAVE_MAX_EDR]      = 40,
78                         /* Note: with UPDS, this should be 10 */
79                         [CONF_SG_WLAN_PS_MAX_BT_ACL_SLAVE_EDR]      = 20,
80                         [CONF_SG_RXT]                               = 1200,
81                         [CONF_SG_TXT]                               = 1000,
82                         [CONF_SG_ADAPTIVE_RXT_TXT]                  = 1,
83                         [CONF_SG_PS_POLL_TIMEOUT]                   = 10,
84                         [CONF_SG_UPSD_TIMEOUT]                      = 10,
85                         [CONF_SG_WLAN_ACTIVE_BT_ACL_MASTER_MIN_EDR] = 7,
86                         [CONF_SG_WLAN_ACTIVE_BT_ACL_MASTER_MAX_EDR] = 15,
87                         [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_MASTER_EDR] = 15,
88                         [CONF_SG_WLAN_ACTIVE_BT_ACL_SLAVE_MIN_EDR]  = 8,
89                         [CONF_SG_WLAN_ACTIVE_BT_ACL_SLAVE_MAX_EDR]  = 20,
90                         [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_SLAVE_EDR]  = 15,
91                         [CONF_SG_WLAN_ACTIVE_BT_ACL_MIN_BR]         = 20,
92                         [CONF_SG_WLAN_ACTIVE_BT_ACL_MAX_BR]         = 50,
93                         [CONF_SG_WLAN_ACTIVE_MAX_BT_ACL_BR]         = 10,
94                         [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_HV3]  = 200,
95                         [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP] = 800,
96                         [CONF_SG_PASSIVE_SCAN_A2DP_BT_TIME]         = 75,
97                         [CONF_SG_PASSIVE_SCAN_A2DP_WLAN_TIME]       = 15,
98                         [CONF_SG_HV3_MAX_SERVED]                    = 6,
99                         [CONF_SG_DHCP_TIME]                         = 5000,
100                         [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_A2DP]  = 100,
101                 },
102                 .state = CONF_SG_PROTECTIVE,
103         },
104         .rx = {
105                 .rx_msdu_life_time           = 512000,
106                 .packet_detection_threshold  = 0,
107                 .ps_poll_timeout             = 15,
108                 .upsd_timeout                = 15,
109                 .rts_threshold               = 2347,
110                 .rx_cca_threshold            = 0,
111                 .irq_blk_threshold           = 0xFFFF,
112                 .irq_pkt_threshold           = 0,
113                 .irq_timeout                 = 600,
114                 .queue_type                  = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
115         },
116         .tx = {
117                 .tx_energy_detection         = 0,
118                 .rc_conf                     = {
119                         .enabled_rates       = 0,
120                         .short_retry_limit   = 10,
121                         .long_retry_limit    = 10,
122                         .aflags              = 0
123                 },
124                 .ac_conf_count               = 4,
125                 .ac_conf                     = {
126                         [0] = {
127                                 .ac          = CONF_TX_AC_BE,
128                                 .cw_min      = 15,
129                                 .cw_max      = 63,
130                                 .aifsn       = 3,
131                                 .tx_op_limit = 0,
132                         },
133                         [1] = {
134                                 .ac          = CONF_TX_AC_BK,
135                                 .cw_min      = 15,
136                                 .cw_max      = 63,
137                                 .aifsn       = 7,
138                                 .tx_op_limit = 0,
139                         },
140                         [2] = {
141                                 .ac          = CONF_TX_AC_VI,
142                                 .cw_min      = 15,
143                                 .cw_max      = 63,
144                                 .aifsn       = CONF_TX_AIFS_PIFS,
145                                 .tx_op_limit = 3008,
146                         },
147                         [3] = {
148                                 .ac          = CONF_TX_AC_VO,
149                                 .cw_min      = 15,
150                                 .cw_max      = 63,
151                                 .aifsn       = CONF_TX_AIFS_PIFS,
152                                 .tx_op_limit = 1504,
153                         },
154                 },
155                 .tid_conf_count = 7,
156                 .tid_conf = {
157                         [0] = {
158                                 .queue_id    = 0,
159                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
160                                 .tsid        = CONF_TX_AC_BE,
161                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
162                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
163                                 .apsd_conf   = {0, 0},
164                         },
165                         [1] = {
166                                 .queue_id    = 1,
167                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
168                                 .tsid        = CONF_TX_AC_BE,
169                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
170                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
171                                 .apsd_conf   = {0, 0},
172                         },
173                         [2] = {
174                                 .queue_id    = 2,
175                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
176                                 .tsid        = CONF_TX_AC_BE,
177                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
178                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
179                                 .apsd_conf   = {0, 0},
180                         },
181                         [3] = {
182                                 .queue_id    = 3,
183                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
184                                 .tsid        = CONF_TX_AC_BE,
185                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
186                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
187                                 .apsd_conf   = {0, 0},
188                         },
189                         [4] = {
190                                 .queue_id    = 4,
191                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
192                                 .tsid        = CONF_TX_AC_BE,
193                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
194                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
195                                 .apsd_conf   = {0, 0},
196                         },
197                         [5] = {
198                                 .queue_id    = 5,
199                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
200                                 .tsid        = CONF_TX_AC_BE,
201                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
202                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
203                                 .apsd_conf   = {0, 0},
204                         },
205                         [6] = {
206                                 .queue_id    = 6,
207                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
208                                 .tsid        = CONF_TX_AC_BE,
209                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
210                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
211                                 .apsd_conf   = {0, 0},
212                         }
213                 },
214                 .frag_threshold              = IEEE80211_MAX_FRAG_THRESHOLD,
215                 .tx_compl_timeout            = 700,
216                 .tx_compl_threshold          = 4,
217                 .basic_rate                  = CONF_HW_BIT_RATE_1MBPS,
218                 .basic_rate_5                = CONF_HW_BIT_RATE_6MBPS,
219         },
220         .conn = {
221                 .wake_up_event               = CONF_WAKE_UP_EVENT_DTIM,
222                 .listen_interval             = 1,
223                 .bcn_filt_mode               = CONF_BCN_FILT_MODE_ENABLED,
224                 .bcn_filt_ie_count           = 1,
225                 .bcn_filt_ie = {
226                         [0] = {
227                                 .ie          = WLAN_EID_CHANNEL_SWITCH,
228                                 .rule        = CONF_BCN_RULE_PASS_ON_APPEARANCE,
229                         }
230                 },
231                 .synch_fail_thold            = 10,
232                 .bss_lose_timeout            = 100,
233                 .beacon_rx_timeout           = 10000,
234                 .broadcast_timeout           = 20000,
235                 .rx_broadcast_in_ps          = 1,
236                 .ps_poll_threshold           = 10,
237                 .ps_poll_recovery_period     = 700,
238                 .bet_enable                  = CONF_BET_MODE_ENABLE,
239                 .bet_max_consecutive         = 10,
240                 .psm_entry_retries           = 3,
241                 .keep_alive_interval         = 55000,
242                 .max_listen_interval         = 20,
243         },
244         .init = {
245                 .radioparam = {
246                         .fem                 = 1,
247                 }
248         },
249         .itrim = {
250                 .enable = false,
251                 .timeout = 50000,
252         },
253         .pm_config = {
254                 .host_clk_settling_time = 5000,
255                 .host_fast_wakeup_support = false
256         },
257         .roam_trigger = {
258                 /* FIXME: due to firmware bug, must use value 1 for now */
259                 .trigger_pacing               = 1,
260                 .avg_weight_rssi_beacon       = 20,
261                 .avg_weight_rssi_data         = 10,
262                 .avg_weight_snr_beacon        = 20,
263                 .avg_weight_snr_data          = 10
264         }
265 };
266
267 static void wl1271_device_release(struct device *dev)
268 {
269
270 }
271
272 static struct platform_device wl1271_device = {
273         .name           = "wl1271",
274         .id             = -1,
275
276         /* device model insists to have a release function */
277         .dev            = {
278                 .release = wl1271_device_release,
279         },
280 };
281
282 static LIST_HEAD(wl_list);
283
284 static void wl1271_conf_init(struct wl1271 *wl)
285 {
286
287         /*
288          * This function applies the default configuration to the driver. This
289          * function is invoked upon driver load (spi probe.)
290          *
291          * The configuration is stored in a run-time structure in order to
292          * facilitate for run-time adjustment of any of the parameters. Making
293          * changes to the configuration structure will apply the new values on
294          * the next interface up (wl1271_op_start.)
295          */
296
297         /* apply driver default configuration */
298         memcpy(&wl->conf, &default_conf, sizeof(default_conf));
299 }
300
301
302 static int wl1271_plt_init(struct wl1271 *wl)
303 {
304         struct conf_tx_ac_category *conf_ac;
305         struct conf_tx_tid *conf_tid;
306         int ret, i;
307
308         ret = wl1271_cmd_general_parms(wl);
309         if (ret < 0)
310                 return ret;
311
312         ret = wl1271_cmd_radio_parms(wl);
313         if (ret < 0)
314                 return ret;
315
316         ret = wl1271_init_templates_config(wl);
317         if (ret < 0)
318                 return ret;
319
320         ret = wl1271_acx_init_mem_config(wl);
321         if (ret < 0)
322                 return ret;
323
324         /* PHY layer config */
325         ret = wl1271_init_phy_config(wl);
326         if (ret < 0)
327                 goto out_free_memmap;
328
329         ret = wl1271_acx_dco_itrim_params(wl);
330         if (ret < 0)
331                 goto out_free_memmap;
332
333         /* Initialize connection monitoring thresholds */
334         ret = wl1271_acx_conn_monit_params(wl, false);
335         if (ret < 0)
336                 goto out_free_memmap;
337
338         /* Bluetooth WLAN coexistence */
339         ret = wl1271_init_pta(wl);
340         if (ret < 0)
341                 goto out_free_memmap;
342
343         /* Energy detection */
344         ret = wl1271_init_energy_detection(wl);
345         if (ret < 0)
346                 goto out_free_memmap;
347
348         /* Default fragmentation threshold */
349         ret = wl1271_acx_frag_threshold(wl);
350         if (ret < 0)
351                 goto out_free_memmap;
352
353         /* Default TID configuration */
354         for (i = 0; i < wl->conf.tx.tid_conf_count; i++) {
355                 conf_tid = &wl->conf.tx.tid_conf[i];
356                 ret = wl1271_acx_tid_cfg(wl, conf_tid->queue_id,
357                                          conf_tid->channel_type,
358                                          conf_tid->tsid,
359                                          conf_tid->ps_scheme,
360                                          conf_tid->ack_policy,
361                                          conf_tid->apsd_conf[0],
362                                          conf_tid->apsd_conf[1]);
363                 if (ret < 0)
364                         goto out_free_memmap;
365         }
366
367         /* Default AC configuration */
368         for (i = 0; i < wl->conf.tx.ac_conf_count; i++) {
369                 conf_ac = &wl->conf.tx.ac_conf[i];
370                 ret = wl1271_acx_ac_cfg(wl, conf_ac->ac, conf_ac->cw_min,
371                                         conf_ac->cw_max, conf_ac->aifsn,
372                                         conf_ac->tx_op_limit);
373                 if (ret < 0)
374                         goto out_free_memmap;
375         }
376
377         /* Enable data path */
378         ret = wl1271_cmd_data_path(wl, 1);
379         if (ret < 0)
380                 goto out_free_memmap;
381
382         /* Configure for CAM power saving (ie. always active) */
383         ret = wl1271_acx_sleep_auth(wl, WL1271_PSM_CAM);
384         if (ret < 0)
385                 goto out_free_memmap;
386
387         /* configure PM */
388         ret = wl1271_acx_pm_config(wl);
389         if (ret < 0)
390                 goto out_free_memmap;
391
392         return 0;
393
394  out_free_memmap:
395         kfree(wl->target_mem_map);
396         wl->target_mem_map = NULL;
397
398         return ret;
399 }
400
401 static void wl1271_fw_status(struct wl1271 *wl,
402                              struct wl1271_fw_status *status)
403 {
404         struct timespec ts;
405         u32 total = 0;
406         int i;
407
408         wl1271_raw_read(wl, FW_STATUS_ADDR, status, sizeof(*status), false);
409
410         wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
411                      "drv_rx_counter = %d, tx_results_counter = %d)",
412                      status->intr,
413                      status->fw_rx_counter,
414                      status->drv_rx_counter,
415                      status->tx_results_counter);
416
417         /* update number of available TX blocks */
418         for (i = 0; i < NUM_TX_QUEUES; i++) {
419                 u32 cnt = le32_to_cpu(status->tx_released_blks[i]) -
420                         wl->tx_blocks_freed[i];
421
422                 wl->tx_blocks_freed[i] =
423                         le32_to_cpu(status->tx_released_blks[i]);
424                 wl->tx_blocks_available += cnt;
425                 total += cnt;
426         }
427
428         /* if more blocks are available now, schedule some tx work */
429         if (total && !skb_queue_empty(&wl->tx_queue))
430                 ieee80211_queue_work(wl->hw, &wl->tx_work);
431
432         /* update the host-chipset time offset */
433         getnstimeofday(&ts);
434         wl->time_offset = (timespec_to_ns(&ts) >> 10) -
435                 (s64)le32_to_cpu(status->fw_localtime);
436 }
437
438 #define WL1271_IRQ_MAX_LOOPS 10
439
440 static void wl1271_irq_work(struct work_struct *work)
441 {
442         int ret;
443         u32 intr;
444         int loopcount = WL1271_IRQ_MAX_LOOPS;
445         unsigned long flags;
446         struct wl1271 *wl =
447                 container_of(work, struct wl1271, irq_work);
448
449         mutex_lock(&wl->mutex);
450
451         wl1271_debug(DEBUG_IRQ, "IRQ work");
452
453         if (unlikely(wl->state == WL1271_STATE_OFF))
454                 goto out;
455
456         ret = wl1271_ps_elp_wakeup(wl, true);
457         if (ret < 0)
458                 goto out;
459
460         spin_lock_irqsave(&wl->wl_lock, flags);
461         while (test_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags) && loopcount) {
462                 clear_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags);
463                 spin_unlock_irqrestore(&wl->wl_lock, flags);
464                 loopcount--;
465
466                 wl1271_fw_status(wl, wl->fw_status);
467                 intr = le32_to_cpu(wl->fw_status->intr);
468                 if (!intr) {
469                         wl1271_debug(DEBUG_IRQ, "Zero interrupt received.");
470                         spin_lock_irqsave(&wl->wl_lock, flags);
471                         continue;
472                 }
473
474                 intr &= WL1271_INTR_MASK;
475
476                 if (intr & WL1271_ACX_INTR_DATA) {
477                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
478
479                         /* check for tx results */
480                         if (wl->fw_status->tx_results_counter !=
481                             (wl->tx_results_count & 0xff))
482                                 wl1271_tx_complete(wl);
483
484                         wl1271_rx(wl, wl->fw_status);
485                 }
486
487                 if (intr & WL1271_ACX_INTR_EVENT_A) {
488                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
489                         wl1271_event_handle(wl, 0);
490                 }
491
492                 if (intr & WL1271_ACX_INTR_EVENT_B) {
493                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
494                         wl1271_event_handle(wl, 1);
495                 }
496
497                 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
498                         wl1271_debug(DEBUG_IRQ,
499                                      "WL1271_ACX_INTR_INIT_COMPLETE");
500
501                 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
502                         wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
503
504                 spin_lock_irqsave(&wl->wl_lock, flags);
505         }
506
507         if (test_bit(WL1271_FLAG_IRQ_PENDING, &wl->flags))
508                 ieee80211_queue_work(wl->hw, &wl->irq_work);
509         else
510                 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
511         spin_unlock_irqrestore(&wl->wl_lock, flags);
512
513         wl1271_ps_elp_sleep(wl);
514
515 out:
516         mutex_unlock(&wl->mutex);
517 }
518
519 static int wl1271_fetch_firmware(struct wl1271 *wl)
520 {
521         const struct firmware *fw;
522         int ret;
523
524         ret = request_firmware(&fw, WL1271_FW_NAME, wl1271_wl_to_dev(wl));
525
526         if (ret < 0) {
527                 wl1271_error("could not get firmware: %d", ret);
528                 return ret;
529         }
530
531         if (fw->size % 4) {
532                 wl1271_error("firmware size is not multiple of 32 bits: %zu",
533                              fw->size);
534                 ret = -EILSEQ;
535                 goto out;
536         }
537
538         wl->fw_len = fw->size;
539         wl->fw = vmalloc(wl->fw_len);
540
541         if (!wl->fw) {
542                 wl1271_error("could not allocate memory for the firmware");
543                 ret = -ENOMEM;
544                 goto out;
545         }
546
547         memcpy(wl->fw, fw->data, wl->fw_len);
548
549         ret = 0;
550
551 out:
552         release_firmware(fw);
553
554         return ret;
555 }
556
557 static int wl1271_fetch_nvs(struct wl1271 *wl)
558 {
559         const struct firmware *fw;
560         int ret;
561
562         ret = request_firmware(&fw, WL1271_NVS_NAME, wl1271_wl_to_dev(wl));
563
564         if (ret < 0) {
565                 wl1271_error("could not get nvs file: %d", ret);
566                 return ret;
567         }
568
569         /*
570          * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz band
571          * configurations) can be removed when those NVS files stop floating
572          * around.
573          */
574         if (fw->size != sizeof(struct wl1271_nvs_file) &&
575             (fw->size != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
576              wl1271_11a_enabled())) {
577                 wl1271_error("nvs size is not as expected: %zu != %zu",
578                              fw->size, sizeof(struct wl1271_nvs_file));
579                 ret = -EILSEQ;
580                 goto out;
581         }
582
583         wl->nvs = kzalloc(sizeof(struct wl1271_nvs_file), GFP_KERNEL);
584
585         if (!wl->nvs) {
586                 wl1271_error("could not allocate memory for the nvs file");
587                 ret = -ENOMEM;
588                 goto out;
589         }
590
591         memcpy(wl->nvs, fw->data, fw->size);
592
593 out:
594         release_firmware(fw);
595
596         return ret;
597 }
598
599 static void wl1271_fw_wakeup(struct wl1271 *wl)
600 {
601         u32 elp_reg;
602
603         elp_reg = ELPCTRL_WAKE_UP;
604         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
605 }
606
607 static int wl1271_setup(struct wl1271 *wl)
608 {
609         wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
610         if (!wl->fw_status)
611                 return -ENOMEM;
612
613         wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
614         if (!wl->tx_res_if) {
615                 kfree(wl->fw_status);
616                 return -ENOMEM;
617         }
618
619         INIT_WORK(&wl->irq_work, wl1271_irq_work);
620         INIT_WORK(&wl->tx_work, wl1271_tx_work);
621         return 0;
622 }
623
624 static int wl1271_chip_wakeup(struct wl1271 *wl)
625 {
626         struct wl1271_partition_set partition;
627         int ret = 0;
628
629         msleep(WL1271_PRE_POWER_ON_SLEEP);
630         wl1271_power_on(wl);
631         msleep(WL1271_POWER_ON_SLEEP);
632         wl1271_io_reset(wl);
633         wl1271_io_init(wl);
634
635         /* We don't need a real memory partition here, because we only want
636          * to use the registers at this point. */
637         memset(&partition, 0, sizeof(partition));
638         partition.reg.start = REGISTERS_BASE;
639         partition.reg.size = REGISTERS_DOWN_SIZE;
640         wl1271_set_partition(wl, &partition);
641
642         /* ELP module wake up */
643         wl1271_fw_wakeup(wl);
644
645         /* whal_FwCtrl_BootSm() */
646
647         /* 0. read chip id from CHIP_ID */
648         wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
649
650         /* 1. check if chip id is valid */
651
652         switch (wl->chip.id) {
653         case CHIP_ID_1271_PG10:
654                 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
655                                wl->chip.id);
656
657                 ret = wl1271_setup(wl);
658                 if (ret < 0)
659                         goto out;
660                 break;
661         case CHIP_ID_1271_PG20:
662                 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
663                              wl->chip.id);
664
665                 ret = wl1271_setup(wl);
666                 if (ret < 0)
667                         goto out;
668                 break;
669         default:
670                 wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
671                 ret = -ENODEV;
672                 goto out;
673         }
674
675         if (wl->fw == NULL) {
676                 ret = wl1271_fetch_firmware(wl);
677                 if (ret < 0)
678                         goto out;
679         }
680
681         /* No NVS from netlink, try to get it from the filesystem */
682         if (wl->nvs == NULL) {
683                 ret = wl1271_fetch_nvs(wl);
684                 if (ret < 0)
685                         goto out;
686         }
687
688 out:
689         return ret;
690 }
691
692 int wl1271_plt_start(struct wl1271 *wl)
693 {
694         int retries = WL1271_BOOT_RETRIES;
695         int ret;
696
697         mutex_lock(&wl->mutex);
698
699         wl1271_notice("power up");
700
701         if (wl->state != WL1271_STATE_OFF) {
702                 wl1271_error("cannot go into PLT state because not "
703                              "in off state: %d", wl->state);
704                 ret = -EBUSY;
705                 goto out;
706         }
707
708         while (retries) {
709                 retries--;
710                 ret = wl1271_chip_wakeup(wl);
711                 if (ret < 0)
712                         goto power_off;
713
714                 ret = wl1271_boot(wl);
715                 if (ret < 0)
716                         goto power_off;
717
718                 ret = wl1271_plt_init(wl);
719                 if (ret < 0)
720                         goto irq_disable;
721
722                 wl->state = WL1271_STATE_PLT;
723                 wl1271_notice("firmware booted in PLT mode (%s)",
724                               wl->chip.fw_ver);
725                 goto out;
726
727 irq_disable:
728                 wl1271_disable_interrupts(wl);
729                 mutex_unlock(&wl->mutex);
730                 /* Unlocking the mutex in the middle of handling is
731                    inherently unsafe. In this case we deem it safe to do,
732                    because we need to let any possibly pending IRQ out of
733                    the system (and while we are WL1271_STATE_OFF the IRQ
734                    work function will not do anything.) Also, any other
735                    possible concurrent operations will fail due to the
736                    current state, hence the wl1271 struct should be safe. */
737                 cancel_work_sync(&wl->irq_work);
738                 mutex_lock(&wl->mutex);
739 power_off:
740                 wl1271_power_off(wl);
741         }
742
743         wl1271_error("firmware boot in PLT mode failed despite %d retries",
744                      WL1271_BOOT_RETRIES);
745 out:
746         mutex_unlock(&wl->mutex);
747
748         return ret;
749 }
750
751 int wl1271_plt_stop(struct wl1271 *wl)
752 {
753         int ret = 0;
754
755         mutex_lock(&wl->mutex);
756
757         wl1271_notice("power down");
758
759         if (wl->state != WL1271_STATE_PLT) {
760                 wl1271_error("cannot power down because not in PLT "
761                              "state: %d", wl->state);
762                 ret = -EBUSY;
763                 goto out;
764         }
765
766         wl1271_disable_interrupts(wl);
767         wl1271_power_off(wl);
768
769         wl->state = WL1271_STATE_OFF;
770         wl->rx_counter = 0;
771
772 out:
773         mutex_unlock(&wl->mutex);
774
775         return ret;
776 }
777
778
779 static int wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
780 {
781         struct wl1271 *wl = hw->priv;
782         struct ieee80211_conf *conf = &hw->conf;
783         struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
784         struct ieee80211_sta *sta = txinfo->control.sta;
785         unsigned long flags;
786
787         /* peek into the rates configured in the STA entry */
788         spin_lock_irqsave(&wl->wl_lock, flags);
789         if (sta && sta->supp_rates[conf->channel->band] != wl->sta_rate_set) {
790                 wl->sta_rate_set = sta->supp_rates[conf->channel->band];
791                 set_bit(WL1271_FLAG_STA_RATES_CHANGED, &wl->flags);
792         }
793         spin_unlock_irqrestore(&wl->wl_lock, flags);
794
795         /* queue the packet */
796         skb_queue_tail(&wl->tx_queue, skb);
797
798         /*
799          * The chip specific setup must run before the first TX packet -
800          * before that, the tx_work will not be initialized!
801          */
802
803         ieee80211_queue_work(wl->hw, &wl->tx_work);
804
805         /*
806          * The workqueue is slow to process the tx_queue and we need stop
807          * the queue here, otherwise the queue will get too long.
808          */
809         if (skb_queue_len(&wl->tx_queue) >= WL1271_TX_QUEUE_HIGH_WATERMARK) {
810                 wl1271_debug(DEBUG_TX, "op_tx: stopping queues");
811
812                 spin_lock_irqsave(&wl->wl_lock, flags);
813                 ieee80211_stop_queues(wl->hw);
814                 set_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
815                 spin_unlock_irqrestore(&wl->wl_lock, flags);
816         }
817
818         return NETDEV_TX_OK;
819 }
820
821 static int wl1271_op_start(struct ieee80211_hw *hw)
822 {
823         wl1271_debug(DEBUG_MAC80211, "mac80211 start");
824
825         /*
826          * We have to delay the booting of the hardware because
827          * we need to know the local MAC address before downloading and
828          * initializing the firmware. The MAC address cannot be changed
829          * after boot, and without the proper MAC address, the firmware
830          * will not function properly.
831          *
832          * The MAC address is first known when the corresponding interface
833          * is added. That is where we will initialize the hardware.
834          */
835
836         return 0;
837 }
838
839 static void wl1271_op_stop(struct ieee80211_hw *hw)
840 {
841         wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
842 }
843
844 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
845                                    struct ieee80211_vif *vif)
846 {
847         struct wl1271 *wl = hw->priv;
848         int retries = WL1271_BOOT_RETRIES;
849         int ret = 0;
850
851         wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
852                      vif->type, vif->addr);
853
854         mutex_lock(&wl->mutex);
855         if (wl->vif) {
856                 ret = -EBUSY;
857                 goto out;
858         }
859
860         wl->vif = vif;
861
862         switch (vif->type) {
863         case NL80211_IFTYPE_STATION:
864                 wl->bss_type = BSS_TYPE_STA_BSS;
865                 wl->set_bss_type = BSS_TYPE_STA_BSS;
866                 break;
867         case NL80211_IFTYPE_ADHOC:
868                 wl->bss_type = BSS_TYPE_IBSS;
869                 wl->set_bss_type = BSS_TYPE_STA_BSS;
870                 break;
871         default:
872                 ret = -EOPNOTSUPP;
873                 goto out;
874         }
875
876         memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
877
878         if (wl->state != WL1271_STATE_OFF) {
879                 wl1271_error("cannot start because not in off state: %d",
880                              wl->state);
881                 ret = -EBUSY;
882                 goto out;
883         }
884
885         while (retries) {
886                 retries--;
887                 ret = wl1271_chip_wakeup(wl);
888                 if (ret < 0)
889                         goto power_off;
890
891                 ret = wl1271_boot(wl);
892                 if (ret < 0)
893                         goto power_off;
894
895                 ret = wl1271_hw_init(wl);
896                 if (ret < 0)
897                         goto irq_disable;
898
899                 wl->state = WL1271_STATE_ON;
900                 wl1271_info("firmware booted (%s)", wl->chip.fw_ver);
901                 goto out;
902
903 irq_disable:
904                 wl1271_disable_interrupts(wl);
905                 mutex_unlock(&wl->mutex);
906                 /* Unlocking the mutex in the middle of handling is
907                    inherently unsafe. In this case we deem it safe to do,
908                    because we need to let any possibly pending IRQ out of
909                    the system (and while we are WL1271_STATE_OFF the IRQ
910                    work function will not do anything.) Also, any other
911                    possible concurrent operations will fail due to the
912                    current state, hence the wl1271 struct should be safe. */
913                 cancel_work_sync(&wl->irq_work);
914                 mutex_lock(&wl->mutex);
915 power_off:
916                 wl1271_power_off(wl);
917         }
918
919         wl1271_error("firmware boot failed despite %d retries",
920                      WL1271_BOOT_RETRIES);
921 out:
922         mutex_unlock(&wl->mutex);
923
924         if (!ret)
925                 list_add(&wl->list, &wl_list);
926
927         return ret;
928 }
929
930 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
931                                        struct ieee80211_vif *vif)
932 {
933         struct wl1271 *wl = hw->priv;
934         int i;
935
936         mutex_lock(&wl->mutex);
937         wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
938
939         wl1271_info("down");
940
941         list_del(&wl->list);
942
943         WARN_ON(wl->state != WL1271_STATE_ON);
944
945         if (test_and_clear_bit(WL1271_FLAG_SCANNING, &wl->flags)) {
946                 mutex_unlock(&wl->mutex);
947                 ieee80211_scan_completed(wl->hw, true);
948                 mutex_lock(&wl->mutex);
949         }
950
951         wl->state = WL1271_STATE_OFF;
952
953         wl1271_disable_interrupts(wl);
954
955         mutex_unlock(&wl->mutex);
956
957         cancel_work_sync(&wl->irq_work);
958         cancel_work_sync(&wl->tx_work);
959         cancel_delayed_work_sync(&wl->pspoll_work);
960
961         mutex_lock(&wl->mutex);
962
963         /* let's notify MAC80211 about the remaining pending TX frames */
964         wl1271_tx_reset(wl);
965         wl1271_power_off(wl);
966
967         memset(wl->bssid, 0, ETH_ALEN);
968         memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
969         wl->ssid_len = 0;
970         wl->bss_type = MAX_BSS_TYPE;
971         wl->set_bss_type = MAX_BSS_TYPE;
972         wl->band = IEEE80211_BAND_2GHZ;
973
974         wl->rx_counter = 0;
975         wl->psm_entry_retry = 0;
976         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
977         wl->tx_blocks_available = 0;
978         wl->tx_results_count = 0;
979         wl->tx_packets_count = 0;
980         wl->tx_security_last_seq = 0;
981         wl->tx_security_seq = 0;
982         wl->time_offset = 0;
983         wl->session_counter = 0;
984         wl->rate_set = CONF_TX_RATE_MASK_BASIC;
985         wl->sta_rate_set = 0;
986         wl->flags = 0;
987         wl->vif = NULL;
988         wl->filters = 0;
989
990         for (i = 0; i < NUM_TX_QUEUES; i++)
991                 wl->tx_blocks_freed[i] = 0;
992
993         wl1271_debugfs_reset(wl);
994
995         kfree(wl->fw_status);
996         wl->fw_status = NULL;
997         kfree(wl->tx_res_if);
998         wl->tx_res_if = NULL;
999         kfree(wl->target_mem_map);
1000         wl->target_mem_map = NULL;
1001
1002         mutex_unlock(&wl->mutex);
1003 }
1004
1005 static void wl1271_configure_filters(struct wl1271 *wl, unsigned int filters)
1006 {
1007         wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
1008         wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
1009
1010         /* combine requested filters with current filter config */
1011         filters = wl->filters | filters;
1012
1013         wl1271_debug(DEBUG_FILTERS, "RX filters set: ");
1014
1015         if (filters & FIF_PROMISC_IN_BSS) {
1016                 wl1271_debug(DEBUG_FILTERS, " - FIF_PROMISC_IN_BSS");
1017                 wl->rx_config &= ~CFG_UNI_FILTER_EN;
1018                 wl->rx_config |= CFG_BSSID_FILTER_EN;
1019         }
1020         if (filters & FIF_BCN_PRBRESP_PROMISC) {
1021                 wl1271_debug(DEBUG_FILTERS, " - FIF_BCN_PRBRESP_PROMISC");
1022                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
1023                 wl->rx_config &= ~CFG_SSID_FILTER_EN;
1024         }
1025         if (filters & FIF_OTHER_BSS) {
1026                 wl1271_debug(DEBUG_FILTERS, " - FIF_OTHER_BSS");
1027                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
1028         }
1029         if (filters & FIF_CONTROL) {
1030                 wl1271_debug(DEBUG_FILTERS, " - FIF_CONTROL");
1031                 wl->rx_filter |= CFG_RX_CTL_EN;
1032         }
1033         if (filters & FIF_FCSFAIL) {
1034                 wl1271_debug(DEBUG_FILTERS, " - FIF_FCSFAIL");
1035                 wl->rx_filter |= CFG_RX_FCS_ERROR;
1036         }
1037 }
1038
1039 static int wl1271_dummy_join(struct wl1271 *wl)
1040 {
1041         int ret = 0;
1042         /* we need to use a dummy BSSID for now */
1043         static const u8 dummy_bssid[ETH_ALEN] = { 0x0b, 0xad, 0xde,
1044                                                   0xad, 0xbe, 0xef };
1045
1046         memcpy(wl->bssid, dummy_bssid, ETH_ALEN);
1047
1048         /* pass through frames from all BSS */
1049         wl1271_configure_filters(wl, FIF_OTHER_BSS);
1050
1051         ret = wl1271_cmd_join(wl, wl->set_bss_type);
1052         if (ret < 0)
1053                 goto out;
1054
1055         set_bit(WL1271_FLAG_JOINED, &wl->flags);
1056
1057 out:
1058         return ret;
1059 }
1060
1061 static int wl1271_join(struct wl1271 *wl, bool set_assoc)
1062 {
1063         int ret;
1064
1065         /*
1066          * One of the side effects of the JOIN command is that is clears
1067          * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
1068          * to a WPA/WPA2 access point will therefore kill the data-path.
1069          * Currently there is no supported scenario for JOIN during
1070          * association - if it becomes a supported scenario, the WPA/WPA2 keys
1071          * must be handled somehow.
1072          *
1073          */
1074         if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
1075                 wl1271_info("JOIN while associated.");
1076
1077         if (set_assoc)
1078                 set_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags);
1079
1080         ret = wl1271_cmd_join(wl, wl->set_bss_type);
1081         if (ret < 0)
1082                 goto out;
1083
1084         set_bit(WL1271_FLAG_JOINED, &wl->flags);
1085
1086         if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
1087                 goto out;
1088
1089         /*
1090          * The join command disable the keep-alive mode, shut down its process,
1091          * and also clear the template config, so we need to reset it all after
1092          * the join. The acx_aid starts the keep-alive process, and the order
1093          * of the commands below is relevant.
1094          */
1095         ret = wl1271_acx_keep_alive_mode(wl, true);
1096         if (ret < 0)
1097                 goto out;
1098
1099         ret = wl1271_acx_aid(wl, wl->aid);
1100         if (ret < 0)
1101                 goto out;
1102
1103         ret = wl1271_cmd_build_klv_null_data(wl);
1104         if (ret < 0)
1105                 goto out;
1106
1107         ret = wl1271_acx_keep_alive_config(wl, CMD_TEMPL_KLV_IDX_NULL_DATA,
1108                                            ACX_KEEP_ALIVE_TPL_VALID);
1109         if (ret < 0)
1110                 goto out;
1111
1112 out:
1113         return ret;
1114 }
1115
1116 static int wl1271_unjoin(struct wl1271 *wl)
1117 {
1118         int ret;
1119
1120         /* to stop listening to a channel, we disconnect */
1121         ret = wl1271_cmd_disconnect(wl);
1122         if (ret < 0)
1123                 goto out;
1124
1125         clear_bit(WL1271_FLAG_JOINED, &wl->flags);
1126         memset(wl->bssid, 0, ETH_ALEN);
1127
1128         /* stop filterting packets based on bssid */
1129         wl1271_configure_filters(wl, FIF_OTHER_BSS);
1130
1131 out:
1132         return ret;
1133 }
1134
1135 static void wl1271_set_band_rate(struct wl1271 *wl)
1136 {
1137         if (wl->band == IEEE80211_BAND_2GHZ)
1138                 wl->basic_rate_set = wl->conf.tx.basic_rate;
1139         else
1140                 wl->basic_rate_set = wl->conf.tx.basic_rate_5;
1141 }
1142
1143 static u32 wl1271_min_rate_get(struct wl1271 *wl)
1144 {
1145         int i;
1146         u32 rate = 0;
1147
1148         if (!wl->basic_rate_set) {
1149                 WARN_ON(1);
1150                 wl->basic_rate_set = wl->conf.tx.basic_rate;
1151         }
1152
1153         for (i = 0; !rate; i++) {
1154                 if ((wl->basic_rate_set >> i) & 0x1)
1155                         rate = 1 << i;
1156         }
1157
1158         return rate;
1159 }
1160
1161 static int wl1271_handle_idle(struct wl1271 *wl, bool idle)
1162 {
1163         int ret;
1164
1165         if (idle) {
1166                 if (test_bit(WL1271_FLAG_JOINED, &wl->flags)) {
1167                         ret = wl1271_unjoin(wl);
1168                         if (ret < 0)
1169                                 goto out;
1170                 }
1171                 wl->rate_set = wl1271_min_rate_get(wl);
1172                 wl->sta_rate_set = 0;
1173                 ret = wl1271_acx_rate_policies(wl);
1174                 if (ret < 0)
1175                         goto out;
1176                 ret = wl1271_acx_keep_alive_config(
1177                         wl, CMD_TEMPL_KLV_IDX_NULL_DATA,
1178                         ACX_KEEP_ALIVE_TPL_INVALID);
1179                 if (ret < 0)
1180                         goto out;
1181                 set_bit(WL1271_FLAG_IDLE, &wl->flags);
1182         } else {
1183                 /* increment the session counter */
1184                 wl->session_counter++;
1185                 if (wl->session_counter >= SESSION_COUNTER_MAX)
1186                         wl->session_counter = 0;
1187                 ret = wl1271_dummy_join(wl);
1188                 if (ret < 0)
1189                         goto out;
1190                 clear_bit(WL1271_FLAG_IDLE, &wl->flags);
1191         }
1192
1193 out:
1194         return ret;
1195 }
1196
1197 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
1198 {
1199         struct wl1271 *wl = hw->priv;
1200         struct ieee80211_conf *conf = &hw->conf;
1201         int channel, ret = 0;
1202
1203         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
1204
1205         wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d %s",
1206                      channel,
1207                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
1208                      conf->power_level,
1209                      conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use");
1210
1211         /*
1212          * mac80211 will go to idle nearly immediately after transmitting some
1213          * frames, such as the deauth. To make sure those frames reach the air,
1214          * wait here until the TX queue is fully flushed.
1215          */
1216         if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
1217             (conf->flags & IEEE80211_CONF_IDLE))
1218                 wl1271_tx_flush(wl);
1219
1220         mutex_lock(&wl->mutex);
1221
1222         if (unlikely(wl->state == WL1271_STATE_OFF))
1223                 goto out;
1224
1225         ret = wl1271_ps_elp_wakeup(wl, false);
1226         if (ret < 0)
1227                 goto out;
1228
1229         /* if the channel changes while joined, join again */
1230         if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
1231             ((wl->band != conf->channel->band) ||
1232              (wl->channel != channel))) {
1233                 wl->band = conf->channel->band;
1234                 wl->channel = channel;
1235
1236                 /*
1237                  * FIXME: the mac80211 should really provide a fixed rate
1238                  * to use here. for now, just use the smallest possible rate
1239                  * for the band as a fixed rate for association frames and
1240                  * other control messages.
1241                  */
1242                 if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
1243                         wl1271_set_band_rate(wl);
1244
1245                 wl->basic_rate = wl1271_min_rate_get(wl);
1246                 ret = wl1271_acx_rate_policies(wl);
1247                 if (ret < 0)
1248                         wl1271_warning("rate policy for update channel "
1249                                        "failed %d", ret);
1250
1251                 if (test_bit(WL1271_FLAG_JOINED, &wl->flags)) {
1252                         ret = wl1271_join(wl, false);
1253                         if (ret < 0)
1254                                 wl1271_warning("cmd join to update channel "
1255                                                "failed %d", ret);
1256                 }
1257         }
1258
1259         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1260                 ret = wl1271_handle_idle(wl, conf->flags & IEEE80211_CONF_IDLE);
1261                 if (ret < 0)
1262                         wl1271_warning("idle mode change failed %d", ret);
1263         }
1264
1265         /*
1266          * if mac80211 changes the PSM mode, make sure the mode is not
1267          * incorrectly changed after the pspoll failure active window.
1268          */
1269         if (changed & IEEE80211_CONF_CHANGE_PS)
1270                 clear_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags);
1271
1272         if (conf->flags & IEEE80211_CONF_PS &&
1273             !test_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags)) {
1274                 set_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags);
1275
1276                 /*
1277                  * We enter PSM only if we're already associated.
1278                  * If we're not, we'll enter it when joining an SSID,
1279                  * through the bss_info_changed() hook.
1280                  */
1281                 if (test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) {
1282                         wl1271_debug(DEBUG_PSM, "psm enabled");
1283                         ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE,
1284                                                  true);
1285                 }
1286         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
1287                    test_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags)) {
1288                 wl1271_debug(DEBUG_PSM, "psm disabled");
1289
1290                 clear_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags);
1291
1292                 if (test_bit(WL1271_FLAG_PSM, &wl->flags))
1293                         ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE,
1294                                                  true);
1295         }
1296
1297         if (conf->power_level != wl->power_level) {
1298                 ret = wl1271_acx_tx_power(wl, conf->power_level);
1299                 if (ret < 0)
1300                         goto out_sleep;
1301
1302                 wl->power_level = conf->power_level;
1303         }
1304
1305 out_sleep:
1306         wl1271_ps_elp_sleep(wl);
1307
1308 out:
1309         mutex_unlock(&wl->mutex);
1310
1311         return ret;
1312 }
1313
1314 struct wl1271_filter_params {
1315         bool enabled;
1316         int mc_list_length;
1317         u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
1318 };
1319
1320 static int wl1271_op_configure_arp_filter(struct ieee80211_hw *hw,
1321                                           struct ieee80211_vif *vif,
1322                                           struct in_ifaddr *ifa_list)
1323 {
1324         struct wl1271 *wl = hw->priv;
1325         int ret = 0;
1326
1327         WARN_ON(vif != wl->vif);
1328
1329         /* disable filtering if there are multiple addresses */
1330         if (ifa_list && ifa_list->ifa_next)
1331                 ifa_list = NULL;
1332
1333         mutex_lock(&wl->mutex);
1334
1335         if (wl->state == WL1271_STATE_OFF)
1336                 goto out;
1337
1338         WARN_ON(wl->bss_type != BSS_TYPE_STA_BSS);
1339
1340         ret = wl1271_ps_elp_wakeup(wl, false);
1341         if (ret < 0)
1342                 goto out;
1343
1344         if (ifa_list) {
1345                 ret = wl1271_cmd_build_arp_reply(wl, &ifa_list->ifa_address);
1346                 if (ret < 0)
1347                         goto out_sleep;
1348                 ret = wl1271_acx_arp_ip_filter(wl, ACX_ARP_FILTER_AND_REPLY,
1349                                                (u8 *)&ifa_list->ifa_address);
1350                 if (ret < 0)
1351                         goto out_sleep;
1352         } else {
1353                 ret = wl1271_acx_arp_ip_filter(wl, ACX_ARP_DISABLE, NULL);
1354                 if (ret < 0)
1355                         goto out_sleep;
1356         }
1357
1358 out_sleep:
1359         wl1271_ps_elp_sleep(wl);
1360
1361 out:
1362         mutex_unlock(&wl->mutex);
1363
1364         return ret;
1365 }
1366
1367 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
1368                                        struct netdev_hw_addr_list *mc_list)
1369 {
1370         struct wl1271_filter_params *fp;
1371         struct netdev_hw_addr *ha;
1372         struct wl1271 *wl = hw->priv;
1373
1374         if (unlikely(wl->state == WL1271_STATE_OFF))
1375                 return 0;
1376
1377         fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
1378         if (!fp) {
1379                 wl1271_error("Out of memory setting filters.");
1380                 return 0;
1381         }
1382
1383         /* update multicast filtering parameters */
1384         fp->mc_list_length = 0;
1385         if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
1386                 fp->enabled = false;
1387         } else {
1388                 fp->enabled = true;
1389                 netdev_hw_addr_list_for_each(ha, mc_list) {
1390                         memcpy(fp->mc_list[fp->mc_list_length],
1391                                         ha->addr, ETH_ALEN);
1392                         fp->mc_list_length++;
1393                 }
1394         }
1395
1396         return (u64)(unsigned long)fp;
1397 }
1398
1399 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
1400                                   FIF_ALLMULTI | \
1401                                   FIF_FCSFAIL | \
1402                                   FIF_BCN_PRBRESP_PROMISC | \
1403                                   FIF_CONTROL | \
1404                                   FIF_OTHER_BSS)
1405
1406 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
1407                                        unsigned int changed,
1408                                        unsigned int *total, u64 multicast)
1409 {
1410         struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
1411         struct wl1271 *wl = hw->priv;
1412         int ret;
1413
1414         wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
1415
1416         mutex_lock(&wl->mutex);
1417
1418         *total &= WL1271_SUPPORTED_FILTERS;
1419         changed &= WL1271_SUPPORTED_FILTERS;
1420
1421         if (unlikely(wl->state == WL1271_STATE_OFF))
1422                 goto out;
1423
1424         ret = wl1271_ps_elp_wakeup(wl, false);
1425         if (ret < 0)
1426                 goto out;
1427
1428
1429         if (*total & FIF_ALLMULTI)
1430                 ret = wl1271_acx_group_address_tbl(wl, false, NULL, 0);
1431         else if (fp)
1432                 ret = wl1271_acx_group_address_tbl(wl, fp->enabled,
1433                                                    fp->mc_list,
1434                                                    fp->mc_list_length);
1435         if (ret < 0)
1436                 goto out_sleep;
1437
1438         /* determine, whether supported filter values have changed */
1439         if (changed == 0)
1440                 goto out_sleep;
1441
1442         /* configure filters */
1443         wl->filters = *total;
1444         wl1271_configure_filters(wl, 0);
1445
1446         /* apply configured filters */
1447         ret = wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
1448         if (ret < 0)
1449                 goto out_sleep;
1450
1451 out_sleep:
1452         wl1271_ps_elp_sleep(wl);
1453
1454 out:
1455         mutex_unlock(&wl->mutex);
1456         kfree(fp);
1457 }
1458
1459 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1460                              struct ieee80211_vif *vif,
1461                              struct ieee80211_sta *sta,
1462                              struct ieee80211_key_conf *key_conf)
1463 {
1464         struct wl1271 *wl = hw->priv;
1465         const u8 *addr;
1466         int ret;
1467         u32 tx_seq_32 = 0;
1468         u16 tx_seq_16 = 0;
1469         u8 key_type;
1470
1471         static const u8 bcast_addr[ETH_ALEN] =
1472                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1473
1474         wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
1475
1476         addr = sta ? sta->addr : bcast_addr;
1477
1478         wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
1479         wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
1480         wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
1481                      key_conf->alg, key_conf->keyidx,
1482                      key_conf->keylen, key_conf->flags);
1483         wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
1484
1485         if (is_zero_ether_addr(addr)) {
1486                 /* We dont support TX only encryption */
1487                 ret = -EOPNOTSUPP;
1488                 goto out;
1489         }
1490
1491         mutex_lock(&wl->mutex);
1492
1493         ret = wl1271_ps_elp_wakeup(wl, false);
1494         if (ret < 0)
1495                 goto out_unlock;
1496
1497         switch (key_conf->alg) {
1498         case ALG_WEP:
1499                 key_type = KEY_WEP;
1500
1501                 key_conf->hw_key_idx = key_conf->keyidx;
1502                 break;
1503         case ALG_TKIP:
1504                 key_type = KEY_TKIP;
1505
1506                 key_conf->hw_key_idx = key_conf->keyidx;
1507                 tx_seq_32 = WL1271_TX_SECURITY_HI32(wl->tx_security_seq);
1508                 tx_seq_16 = WL1271_TX_SECURITY_LO16(wl->tx_security_seq);
1509                 break;
1510         case ALG_CCMP:
1511                 key_type = KEY_AES;
1512
1513                 key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1514                 tx_seq_32 = WL1271_TX_SECURITY_HI32(wl->tx_security_seq);
1515                 tx_seq_16 = WL1271_TX_SECURITY_LO16(wl->tx_security_seq);
1516                 break;
1517         default:
1518                 wl1271_error("Unknown key algo 0x%x", key_conf->alg);
1519
1520                 ret = -EOPNOTSUPP;
1521                 goto out_sleep;
1522         }
1523
1524         switch (cmd) {
1525         case SET_KEY:
1526                 ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
1527                                          key_conf->keyidx, key_type,
1528                                          key_conf->keylen, key_conf->key,
1529                                          addr, tx_seq_32, tx_seq_16);
1530                 if (ret < 0) {
1531                         wl1271_error("Could not add or replace key");
1532                         goto out_sleep;
1533                 }
1534
1535                 /* the default WEP key needs to be configured at least once */
1536                 if (key_type == KEY_WEP) {
1537                         ret = wl1271_cmd_set_default_wep_key(wl,
1538                                                              wl->default_key);
1539                         if (ret < 0)
1540                                 goto out_sleep;
1541                 }
1542                 break;
1543
1544         case DISABLE_KEY:
1545                 /* The wl1271 does not allow to remove unicast keys - they
1546                    will be cleared automatically on next CMD_JOIN. Ignore the
1547                    request silently, as we dont want the mac80211 to emit
1548                    an error message. */
1549                 if (!is_broadcast_ether_addr(addr))
1550                         break;
1551
1552                 ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
1553                                          key_conf->keyidx, key_type,
1554                                          key_conf->keylen, key_conf->key,
1555                                          addr, 0, 0);
1556                 if (ret < 0) {
1557                         wl1271_error("Could not remove key");
1558                         goto out_sleep;
1559                 }
1560                 break;
1561
1562         default:
1563                 wl1271_error("Unsupported key cmd 0x%x", cmd);
1564                 ret = -EOPNOTSUPP;
1565                 break;
1566         }
1567
1568 out_sleep:
1569         wl1271_ps_elp_sleep(wl);
1570
1571 out_unlock:
1572         mutex_unlock(&wl->mutex);
1573
1574 out:
1575         return ret;
1576 }
1577
1578 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
1579                              struct ieee80211_vif *vif,
1580                              struct cfg80211_scan_request *req)
1581 {
1582         struct wl1271 *wl = hw->priv;
1583         int ret;
1584         u8 *ssid = NULL;
1585         size_t len = 0;
1586
1587         wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
1588
1589         if (req->n_ssids) {
1590                 ssid = req->ssids[0].ssid;
1591                 len = req->ssids[0].ssid_len;
1592         }
1593
1594         mutex_lock(&wl->mutex);
1595
1596         ret = wl1271_ps_elp_wakeup(wl, false);
1597         if (ret < 0)
1598                 goto out;
1599
1600         if (wl1271_11a_enabled())
1601                 ret = wl1271_cmd_scan(hw->priv, ssid, len, req,
1602                                       1, 0, WL1271_SCAN_BAND_DUAL, 3);
1603         else
1604                 ret = wl1271_cmd_scan(hw->priv, ssid, len, req,
1605                                       1, 0, WL1271_SCAN_BAND_2_4_GHZ, 3);
1606
1607         wl1271_ps_elp_sleep(wl);
1608
1609 out:
1610         mutex_unlock(&wl->mutex);
1611
1612         return ret;
1613 }
1614
1615 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1616 {
1617         struct wl1271 *wl = hw->priv;
1618         int ret = 0;
1619
1620         mutex_lock(&wl->mutex);
1621
1622         if (unlikely(wl->state == WL1271_STATE_OFF))
1623                 goto out;
1624
1625         ret = wl1271_ps_elp_wakeup(wl, false);
1626         if (ret < 0)
1627                 goto out;
1628
1629         ret = wl1271_acx_rts_threshold(wl, (u16) value);
1630         if (ret < 0)
1631                 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
1632
1633         wl1271_ps_elp_sleep(wl);
1634
1635 out:
1636         mutex_unlock(&wl->mutex);
1637
1638         return ret;
1639 }
1640
1641 static void wl1271_ssid_set(struct wl1271 *wl, struct sk_buff *beacon)
1642 {
1643         u8 *ptr = beacon->data +
1644                 offsetof(struct ieee80211_mgmt, u.beacon.variable);
1645
1646         /* find the location of the ssid in the beacon */
1647         while (ptr < beacon->data + beacon->len) {
1648                 if (ptr[0] == WLAN_EID_SSID) {
1649                         wl->ssid_len = ptr[1];
1650                         memcpy(wl->ssid, ptr+2, wl->ssid_len);
1651                         return;
1652                 }
1653                 ptr += ptr[1];
1654         }
1655         wl1271_error("ad-hoc beacon template has no SSID!\n");
1656 }
1657
1658 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
1659                                        struct ieee80211_vif *vif,
1660                                        struct ieee80211_bss_conf *bss_conf,
1661                                        u32 changed)
1662 {
1663         enum wl1271_cmd_ps_mode mode;
1664         struct wl1271 *wl = hw->priv;
1665         bool do_join = false;
1666         bool set_assoc = false;
1667         int ret;
1668
1669         wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1670
1671         mutex_lock(&wl->mutex);
1672
1673         ret = wl1271_ps_elp_wakeup(wl, false);
1674         if (ret < 0)
1675                 goto out;
1676
1677         if ((changed && BSS_CHANGED_BEACON_INT) &&
1678             (wl->bss_type == BSS_TYPE_IBSS)) {
1679                 wl1271_debug(DEBUG_ADHOC, "ad-hoc beacon interval updated: %d",
1680                         bss_conf->beacon_int);
1681
1682                 wl->beacon_int = bss_conf->beacon_int;
1683                 do_join = true;
1684         }
1685
1686         if ((changed && BSS_CHANGED_BEACON) &&
1687             (wl->bss_type == BSS_TYPE_IBSS)) {
1688                 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1689
1690                 wl1271_debug(DEBUG_ADHOC, "ad-hoc beacon updated");
1691
1692                 if (beacon) {
1693                         struct ieee80211_hdr *hdr;
1694
1695                         wl1271_ssid_set(wl, beacon);
1696                         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
1697                                                       beacon->data,
1698                                                       beacon->len, 0,
1699                                                       wl1271_min_rate_get(wl));
1700
1701                         if (ret < 0) {
1702                                 dev_kfree_skb(beacon);
1703                                 goto out_sleep;
1704                         }
1705
1706                         hdr = (struct ieee80211_hdr *) beacon->data;
1707                         hdr->frame_control = cpu_to_le16(
1708                                 IEEE80211_FTYPE_MGMT |
1709                                 IEEE80211_STYPE_PROBE_RESP);
1710
1711                         ret = wl1271_cmd_template_set(wl,
1712                                                       CMD_TEMPL_PROBE_RESPONSE,
1713                                                       beacon->data,
1714                                                       beacon->len, 0,
1715                                                       wl1271_min_rate_get(wl));
1716                         dev_kfree_skb(beacon);
1717                         if (ret < 0)
1718                                 goto out_sleep;
1719
1720                         /* Need to update the SSID (for filtering etc) */
1721                         do_join = true;
1722                 }
1723         }
1724
1725         if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1726             (wl->bss_type == BSS_TYPE_IBSS)) {
1727                 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
1728                              bss_conf->enable_beacon ? "enabled" : "disabled");
1729
1730                 if (bss_conf->enable_beacon)
1731                         wl->set_bss_type = BSS_TYPE_IBSS;
1732                 else
1733                         wl->set_bss_type = BSS_TYPE_STA_BSS;
1734                 do_join = true;
1735         }
1736
1737         if (changed & BSS_CHANGED_CQM) {
1738                 bool enable = false;
1739                 if (bss_conf->cqm_rssi_thold)
1740                         enable = true;
1741                 ret = wl1271_acx_rssi_snr_trigger(wl, enable,
1742                                                   bss_conf->cqm_rssi_thold,
1743                                                   bss_conf->cqm_rssi_hyst);
1744                 if (ret < 0)
1745                         goto out;
1746                 wl->rssi_thold = bss_conf->cqm_rssi_thold;
1747         }
1748
1749         if ((changed & BSS_CHANGED_BSSID) &&
1750             /*
1751              * Now we know the correct bssid, so we send a new join command
1752              * and enable the BSSID filter
1753              */
1754             memcmp(wl->bssid, bss_conf->bssid, ETH_ALEN)) {
1755                         memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1756
1757                         ret = wl1271_cmd_build_null_data(wl);
1758                         if (ret < 0)
1759                                 goto out_sleep;
1760
1761                         ret = wl1271_build_qos_null_data(wl);
1762                         if (ret < 0)
1763                                 goto out_sleep;
1764
1765                         /* filter out all packets not from this BSSID */
1766                         wl1271_configure_filters(wl, 0);
1767
1768                         /* Need to update the BSSID (for filtering etc) */
1769                         do_join = true;
1770         }
1771
1772         if (changed & BSS_CHANGED_ASSOC) {
1773                 if (bss_conf->assoc) {
1774                         u32 rates;
1775                         wl->aid = bss_conf->aid;
1776                         set_assoc = true;
1777
1778                         wl->ps_poll_failures = 0;
1779
1780                         /*
1781                          * use basic rates from AP, and determine lowest rate
1782                          * to use with control frames.
1783                          */
1784                         rates = bss_conf->basic_rates;
1785                         wl->basic_rate_set = wl1271_tx_enabled_rates_get(wl,
1786                                                                          rates);
1787                         wl->basic_rate = wl1271_min_rate_get(wl);
1788                         ret = wl1271_acx_rate_policies(wl);
1789                         if (ret < 0)
1790                                 goto out_sleep;
1791
1792                         /*
1793                          * with wl1271, we don't need to update the
1794                          * beacon_int and dtim_period, because the firmware
1795                          * updates it by itself when the first beacon is
1796                          * received after a join.
1797                          */
1798                         ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
1799                         if (ret < 0)
1800                                 goto out_sleep;
1801
1802                         /*
1803                          * The SSID is intentionally set to NULL here - the
1804                          * firmware will set the probe request with a
1805                          * broadcast SSID regardless of what we set in the
1806                          * template.
1807                          */
1808                         ret = wl1271_cmd_build_probe_req(wl, NULL, 0,
1809                                                          NULL, 0, wl->band);
1810
1811                         /* enable the connection monitoring feature */
1812                         ret = wl1271_acx_conn_monit_params(wl, true);
1813                         if (ret < 0)
1814                                 goto out_sleep;
1815
1816                         /* If we want to go in PSM but we're not there yet */
1817                         if (test_bit(WL1271_FLAG_PSM_REQUESTED, &wl->flags) &&
1818                             !test_bit(WL1271_FLAG_PSM, &wl->flags)) {
1819                                 mode = STATION_POWER_SAVE_MODE;
1820                                 ret = wl1271_ps_set_mode(wl, mode, true);
1821                                 if (ret < 0)
1822                                         goto out_sleep;
1823                         }
1824                 } else {
1825                         /* use defaults when not associated */
1826                         clear_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags);
1827                         wl->aid = 0;
1828
1829                         /* revert back to minimum rates for the current band */
1830                         wl1271_set_band_rate(wl);
1831                         wl->basic_rate = wl1271_min_rate_get(wl);
1832                         ret = wl1271_acx_rate_policies(wl);
1833                         if (ret < 0)
1834                                 goto out_sleep;
1835
1836                         /* disable connection monitor features */
1837                         ret = wl1271_acx_conn_monit_params(wl, false);
1838
1839                         /* Disable the keep-alive feature */
1840                         ret = wl1271_acx_keep_alive_mode(wl, false);
1841
1842                         if (ret < 0)
1843                                 goto out_sleep;
1844                 }
1845
1846         }
1847
1848         if (changed & BSS_CHANGED_ERP_SLOT) {
1849                 if (bss_conf->use_short_slot)
1850                         ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
1851                 else
1852                         ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
1853                 if (ret < 0) {
1854                         wl1271_warning("Set slot time failed %d", ret);
1855                         goto out_sleep;
1856                 }
1857         }
1858
1859         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1860                 if (bss_conf->use_short_preamble)
1861                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1862                 else
1863                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1864         }
1865
1866         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1867                 if (bss_conf->use_cts_prot)
1868                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1869                 else
1870                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1871                 if (ret < 0) {
1872                         wl1271_warning("Set ctsprotect failed %d", ret);
1873                         goto out_sleep;
1874                 }
1875         }
1876
1877         if (do_join) {
1878                 ret = wl1271_join(wl, set_assoc);
1879                 if (ret < 0) {
1880                         wl1271_warning("cmd join failed %d", ret);
1881                         goto out_sleep;
1882                 }
1883         }
1884
1885 out_sleep:
1886         wl1271_ps_elp_sleep(wl);
1887
1888 out:
1889         mutex_unlock(&wl->mutex);
1890 }
1891
1892 static int wl1271_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1893                              const struct ieee80211_tx_queue_params *params)
1894 {
1895         struct wl1271 *wl = hw->priv;
1896         u8 ps_scheme;
1897         int ret;
1898
1899         mutex_lock(&wl->mutex);
1900
1901         wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1902
1903         ret = wl1271_ps_elp_wakeup(wl, false);
1904         if (ret < 0)
1905                 goto out;
1906
1907         /* the txop is confed in units of 32us by the mac80211, we need us */
1908         ret = wl1271_acx_ac_cfg(wl, wl1271_tx_get_queue(queue),
1909                                 params->cw_min, params->cw_max,
1910                                 params->aifs, params->txop << 5);
1911         if (ret < 0)
1912                 goto out_sleep;
1913
1914         if (params->uapsd)
1915                 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
1916         else
1917                 ps_scheme = CONF_PS_SCHEME_LEGACY;
1918
1919         ret = wl1271_acx_tid_cfg(wl, wl1271_tx_get_queue(queue),
1920                                  CONF_CHANNEL_TYPE_EDCF,
1921                                  wl1271_tx_get_queue(queue),
1922                                  ps_scheme, CONF_ACK_POLICY_LEGACY, 0, 0);
1923         if (ret < 0)
1924                 goto out_sleep;
1925
1926 out_sleep:
1927         wl1271_ps_elp_sleep(wl);
1928
1929 out:
1930         mutex_unlock(&wl->mutex);
1931
1932         return ret;
1933 }
1934
1935 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw)
1936 {
1937
1938         struct wl1271 *wl = hw->priv;
1939         u64 mactime = ULLONG_MAX;
1940         int ret;
1941
1942         wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
1943
1944         mutex_lock(&wl->mutex);
1945
1946         ret = wl1271_ps_elp_wakeup(wl, false);
1947         if (ret < 0)
1948                 goto out;
1949
1950         ret = wl1271_acx_tsf_info(wl, &mactime);
1951         if (ret < 0)
1952                 goto out_sleep;
1953
1954 out_sleep:
1955         wl1271_ps_elp_sleep(wl);
1956
1957 out:
1958         mutex_unlock(&wl->mutex);
1959         return mactime;
1960 }
1961
1962 /* can't be const, mac80211 writes to this */
1963 static struct ieee80211_rate wl1271_rates[] = {
1964         { .bitrate = 10,
1965           .hw_value = CONF_HW_BIT_RATE_1MBPS,
1966           .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
1967         { .bitrate = 20,
1968           .hw_value = CONF_HW_BIT_RATE_2MBPS,
1969           .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
1970           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1971         { .bitrate = 55,
1972           .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
1973           .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
1974           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1975         { .bitrate = 110,
1976           .hw_value = CONF_HW_BIT_RATE_11MBPS,
1977           .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
1978           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1979         { .bitrate = 60,
1980           .hw_value = CONF_HW_BIT_RATE_6MBPS,
1981           .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
1982         { .bitrate = 90,
1983           .hw_value = CONF_HW_BIT_RATE_9MBPS,
1984           .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
1985         { .bitrate = 120,
1986           .hw_value = CONF_HW_BIT_RATE_12MBPS,
1987           .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
1988         { .bitrate = 180,
1989           .hw_value = CONF_HW_BIT_RATE_18MBPS,
1990           .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
1991         { .bitrate = 240,
1992           .hw_value = CONF_HW_BIT_RATE_24MBPS,
1993           .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
1994         { .bitrate = 360,
1995          .hw_value = CONF_HW_BIT_RATE_36MBPS,
1996          .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
1997         { .bitrate = 480,
1998           .hw_value = CONF_HW_BIT_RATE_48MBPS,
1999           .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
2000         { .bitrate = 540,
2001           .hw_value = CONF_HW_BIT_RATE_54MBPS,
2002           .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
2003 };
2004
2005 /* can't be const, mac80211 writes to this */
2006 static struct ieee80211_channel wl1271_channels[] = {
2007         { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
2008         { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
2009         { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
2010         { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
2011         { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
2012         { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
2013         { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
2014         { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
2015         { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
2016         { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
2017         { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
2018         { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
2019         { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
2020 };
2021
2022 /* mapping to indexes for wl1271_rates */
2023 static const u8 wl1271_rate_to_idx_2ghz[] = {
2024         /* MCS rates are used only with 11n */
2025         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS7 */
2026         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS6 */
2027         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS5 */
2028         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS4 */
2029         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS3 */
2030         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS2 */
2031         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS1 */
2032         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS0 */
2033
2034         11,                            /* CONF_HW_RXTX_RATE_54   */
2035         10,                            /* CONF_HW_RXTX_RATE_48   */
2036         9,                             /* CONF_HW_RXTX_RATE_36   */
2037         8,                             /* CONF_HW_RXTX_RATE_24   */
2038
2039         /* TI-specific rate */
2040         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22   */
2041
2042         7,                             /* CONF_HW_RXTX_RATE_18   */
2043         6,                             /* CONF_HW_RXTX_RATE_12   */
2044         3,                             /* CONF_HW_RXTX_RATE_11   */
2045         5,                             /* CONF_HW_RXTX_RATE_9    */
2046         4,                             /* CONF_HW_RXTX_RATE_6    */
2047         2,                             /* CONF_HW_RXTX_RATE_5_5  */
2048         1,                             /* CONF_HW_RXTX_RATE_2    */
2049         0                              /* CONF_HW_RXTX_RATE_1    */
2050 };
2051
2052 /* can't be const, mac80211 writes to this */
2053 static struct ieee80211_supported_band wl1271_band_2ghz = {
2054         .channels = wl1271_channels,
2055         .n_channels = ARRAY_SIZE(wl1271_channels),
2056         .bitrates = wl1271_rates,
2057         .n_bitrates = ARRAY_SIZE(wl1271_rates),
2058 };
2059
2060 /* 5 GHz data rates for WL1273 */
2061 static struct ieee80211_rate wl1271_rates_5ghz[] = {
2062         { .bitrate = 60,
2063           .hw_value = CONF_HW_BIT_RATE_6MBPS,
2064           .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
2065         { .bitrate = 90,
2066           .hw_value = CONF_HW_BIT_RATE_9MBPS,
2067           .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
2068         { .bitrate = 120,
2069           .hw_value = CONF_HW_BIT_RATE_12MBPS,
2070           .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
2071         { .bitrate = 180,
2072           .hw_value = CONF_HW_BIT_RATE_18MBPS,
2073           .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
2074         { .bitrate = 240,
2075           .hw_value = CONF_HW_BIT_RATE_24MBPS,
2076           .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
2077         { .bitrate = 360,
2078          .hw_value = CONF_HW_BIT_RATE_36MBPS,
2079          .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
2080         { .bitrate = 480,
2081           .hw_value = CONF_HW_BIT_RATE_48MBPS,
2082           .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
2083         { .bitrate = 540,
2084           .hw_value = CONF_HW_BIT_RATE_54MBPS,
2085           .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
2086 };
2087
2088 /* 5 GHz band channels for WL1273 */
2089 static struct ieee80211_channel wl1271_channels_5ghz[] = {
2090         { .hw_value = 183, .center_freq = 4915},
2091         { .hw_value = 184, .center_freq = 4920},
2092         { .hw_value = 185, .center_freq = 4925},
2093         { .hw_value = 187, .center_freq = 4935},
2094         { .hw_value = 188, .center_freq = 4940},
2095         { .hw_value = 189, .center_freq = 4945},
2096         { .hw_value = 192, .center_freq = 4960},
2097         { .hw_value = 196, .center_freq = 4980},
2098         { .hw_value = 7, .center_freq = 5035},
2099         { .hw_value = 8, .center_freq = 5040},
2100         { .hw_value = 9, .center_freq = 5045},
2101         { .hw_value = 11, .center_freq = 5055},
2102         { .hw_value = 12, .center_freq = 5060},
2103         { .hw_value = 16, .center_freq = 5080},
2104         { .hw_value = 34, .center_freq = 5170},
2105         { .hw_value = 36, .center_freq = 5180},
2106         { .hw_value = 38, .center_freq = 5190},
2107         { .hw_value = 40, .center_freq = 5200},
2108         { .hw_value = 42, .center_freq = 5210},
2109         { .hw_value = 44, .center_freq = 5220},
2110         { .hw_value = 46, .center_freq = 5230},
2111         { .hw_value = 48, .center_freq = 5240},
2112         { .hw_value = 52, .center_freq = 5260},
2113         { .hw_value = 56, .center_freq = 5280},
2114         { .hw_value = 60, .center_freq = 5300},
2115         { .hw_value = 64, .center_freq = 5320},
2116         { .hw_value = 100, .center_freq = 5500},
2117         { .hw_value = 104, .center_freq = 5520},
2118         { .hw_value = 108, .center_freq = 5540},
2119         { .hw_value = 112, .center_freq = 5560},
2120         { .hw_value = 116, .center_freq = 5580},
2121         { .hw_value = 120, .center_freq = 5600},
2122         { .hw_value = 124, .center_freq = 5620},
2123         { .hw_value = 128, .center_freq = 5640},
2124         { .hw_value = 132, .center_freq = 5660},
2125         { .hw_value = 136, .center_freq = 5680},
2126         { .hw_value = 140, .center_freq = 5700},
2127         { .hw_value = 149, .center_freq = 5745},
2128         { .hw_value = 153, .center_freq = 5765},
2129         { .hw_value = 157, .center_freq = 5785},
2130         { .hw_value = 161, .center_freq = 5805},
2131         { .hw_value = 165, .center_freq = 5825},
2132 };
2133
2134 /* mapping to indexes for wl1271_rates_5ghz */
2135 static const u8 wl1271_rate_to_idx_5ghz[] = {
2136         /* MCS rates are used only with 11n */
2137         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS7 */
2138         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS6 */
2139         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS5 */
2140         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS4 */
2141         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS3 */
2142         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS2 */
2143         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS1 */
2144         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_MCS0 */
2145
2146         7,                             /* CONF_HW_RXTX_RATE_54   */
2147         6,                             /* CONF_HW_RXTX_RATE_48   */
2148         5,                             /* CONF_HW_RXTX_RATE_36   */
2149         4,                             /* CONF_HW_RXTX_RATE_24   */
2150
2151         /* TI-specific rate */
2152         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22   */
2153
2154         3,                             /* CONF_HW_RXTX_RATE_18   */
2155         2,                             /* CONF_HW_RXTX_RATE_12   */
2156         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_11   */
2157         1,                             /* CONF_HW_RXTX_RATE_9    */
2158         0,                             /* CONF_HW_RXTX_RATE_6    */
2159         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_5_5  */
2160         CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_2    */
2161         CONF_HW_RXTX_RATE_UNSUPPORTED  /* CONF_HW_RXTX_RATE_1    */
2162 };
2163
2164 static struct ieee80211_supported_band wl1271_band_5ghz = {
2165         .channels = wl1271_channels_5ghz,
2166         .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
2167         .bitrates = wl1271_rates_5ghz,
2168         .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
2169 };
2170
2171 static const u8 *wl1271_band_rate_to_idx[] = {
2172         [IEEE80211_BAND_2GHZ] = wl1271_rate_to_idx_2ghz,
2173         [IEEE80211_BAND_5GHZ] = wl1271_rate_to_idx_5ghz
2174 };
2175
2176 static const struct ieee80211_ops wl1271_ops = {
2177         .start = wl1271_op_start,
2178         .stop = wl1271_op_stop,
2179         .add_interface = wl1271_op_add_interface,
2180         .remove_interface = wl1271_op_remove_interface,
2181         .config = wl1271_op_config,
2182         .configure_arp_filter = wl1271_op_configure_arp_filter,
2183         .prepare_multicast = wl1271_op_prepare_multicast,
2184         .configure_filter = wl1271_op_configure_filter,
2185         .tx = wl1271_op_tx,
2186         .set_key = wl1271_op_set_key,
2187         .hw_scan = wl1271_op_hw_scan,
2188         .bss_info_changed = wl1271_op_bss_info_changed,
2189         .set_rts_threshold = wl1271_op_set_rts_threshold,
2190         .conf_tx = wl1271_op_conf_tx,
2191         .get_tsf = wl1271_op_get_tsf,
2192         CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
2193 };
2194
2195
2196 u8 wl1271_rate_to_idx(struct wl1271 *wl, int rate)
2197 {
2198         u8 idx;
2199
2200         BUG_ON(wl->band >= sizeof(wl1271_band_rate_to_idx)/sizeof(u8 *));
2201
2202         if (unlikely(rate >= CONF_HW_RXTX_RATE_MAX)) {
2203                 wl1271_error("Illegal RX rate from HW: %d", rate);
2204                 return 0;
2205         }
2206
2207         idx = wl1271_band_rate_to_idx[wl->band][rate];
2208         if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
2209                 wl1271_error("Unsupported RX rate from HW: %d", rate);
2210                 return 0;
2211         }
2212
2213         return idx;
2214 }
2215
2216 static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
2217                                                struct device_attribute *attr,
2218                                                char *buf)
2219 {
2220         struct wl1271 *wl = dev_get_drvdata(dev);
2221         ssize_t len;
2222
2223         /* FIXME: what's the maximum length of buf? page size?*/
2224         len = 500;
2225
2226         mutex_lock(&wl->mutex);
2227         len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
2228                        wl->sg_enabled);
2229         mutex_unlock(&wl->mutex);
2230
2231         return len;
2232
2233 }
2234
2235 static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
2236                                                 struct device_attribute *attr,
2237                                                 const char *buf, size_t count)
2238 {
2239         struct wl1271 *wl = dev_get_drvdata(dev);
2240         unsigned long res;
2241         int ret;
2242
2243         ret = strict_strtoul(buf, 10, &res);
2244
2245         if (ret < 0) {
2246                 wl1271_warning("incorrect value written to bt_coex_mode");
2247                 return count;
2248         }
2249
2250         mutex_lock(&wl->mutex);
2251
2252         res = !!res;
2253
2254         if (res == wl->sg_enabled)
2255                 goto out;
2256
2257         wl->sg_enabled = res;
2258
2259         if (wl->state == WL1271_STATE_OFF)
2260                 goto out;
2261
2262         ret = wl1271_ps_elp_wakeup(wl, false);
2263         if (ret < 0)
2264                 goto out;
2265
2266         wl1271_acx_sg_enable(wl, wl->sg_enabled);
2267         wl1271_ps_elp_sleep(wl);
2268
2269  out:
2270         mutex_unlock(&wl->mutex);
2271         return count;
2272 }
2273
2274 static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
2275                    wl1271_sysfs_show_bt_coex_state,
2276                    wl1271_sysfs_store_bt_coex_state);
2277
2278 static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
2279                                            struct device_attribute *attr,
2280                                            char *buf)
2281 {
2282         struct wl1271 *wl = dev_get_drvdata(dev);
2283         ssize_t len;
2284
2285         /* FIXME: what's the maximum length of buf? page size?*/
2286         len = 500;
2287
2288         mutex_lock(&wl->mutex);
2289         if (wl->hw_pg_ver >= 0)
2290                 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
2291         else
2292                 len = snprintf(buf, len, "n/a\n");
2293         mutex_unlock(&wl->mutex);
2294
2295         return len;
2296 }
2297
2298 static DEVICE_ATTR(hw_pg_ver, S_IRUGO | S_IWUSR,
2299                    wl1271_sysfs_show_hw_pg_ver, NULL);
2300
2301 int wl1271_register_hw(struct wl1271 *wl)
2302 {
2303         int ret;
2304
2305         if (wl->mac80211_registered)
2306                 return 0;
2307
2308         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
2309
2310         ret = ieee80211_register_hw(wl->hw);
2311         if (ret < 0) {
2312                 wl1271_error("unable to register mac80211 hw: %d", ret);
2313                 return ret;
2314         }
2315
2316         wl->mac80211_registered = true;
2317
2318         wl1271_notice("loaded");
2319
2320         return 0;
2321 }
2322 EXPORT_SYMBOL_GPL(wl1271_register_hw);
2323
2324 void wl1271_unregister_hw(struct wl1271 *wl)
2325 {
2326         ieee80211_unregister_hw(wl->hw);
2327         wl->mac80211_registered = false;
2328
2329 }
2330 EXPORT_SYMBOL_GPL(wl1271_unregister_hw);
2331
2332 int wl1271_init_ieee80211(struct wl1271 *wl)
2333 {
2334         /* The tx descriptor buffer and the TKIP space. */
2335         wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
2336                 sizeof(struct wl1271_tx_hw_descr);
2337
2338         /* unit us */
2339         /* FIXME: find a proper value */
2340         wl->hw->channel_change_time = 10000;
2341         wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
2342
2343         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
2344                 IEEE80211_HW_BEACON_FILTER |
2345                 IEEE80211_HW_SUPPORTS_PS |
2346                 IEEE80211_HW_SUPPORTS_UAPSD |
2347                 IEEE80211_HW_HAS_RATE_CONTROL |
2348                 IEEE80211_HW_CONNECTION_MONITOR |
2349                 IEEE80211_HW_SUPPORTS_CQM_RSSI;
2350
2351         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2352                 BIT(NL80211_IFTYPE_ADHOC);
2353         wl->hw->wiphy->max_scan_ssids = 1;
2354         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
2355
2356         if (wl1271_11a_enabled())
2357                 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &wl1271_band_5ghz;
2358
2359         wl->hw->queues = 4;
2360         wl->hw->max_rates = 1;
2361
2362         SET_IEEE80211_DEV(wl->hw, wl1271_wl_to_dev(wl));
2363
2364         return 0;
2365 }
2366 EXPORT_SYMBOL_GPL(wl1271_init_ieee80211);
2367
2368 #define WL1271_DEFAULT_CHANNEL 0
2369
2370 struct ieee80211_hw *wl1271_alloc_hw(void)
2371 {
2372         struct ieee80211_hw *hw;
2373         struct platform_device *plat_dev = NULL;
2374         struct wl1271 *wl;
2375         int i, ret;
2376
2377         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
2378         if (!hw) {
2379                 wl1271_error("could not alloc ieee80211_hw");
2380                 ret = -ENOMEM;
2381                 goto err_hw_alloc;
2382         }
2383
2384         plat_dev = kmalloc(sizeof(wl1271_device), GFP_KERNEL);
2385         if (!plat_dev) {
2386                 wl1271_error("could not allocate platform_device");
2387                 ret = -ENOMEM;
2388                 goto err_plat_alloc;
2389         }
2390
2391         memcpy(plat_dev, &wl1271_device, sizeof(wl1271_device));
2392
2393         wl = hw->priv;
2394         memset(wl, 0, sizeof(*wl));
2395
2396         INIT_LIST_HEAD(&wl->list);
2397
2398         wl->hw = hw;
2399         wl->plat_dev = plat_dev;
2400
2401         skb_queue_head_init(&wl->tx_queue);
2402
2403         INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
2404         INIT_DELAYED_WORK(&wl->pspoll_work, wl1271_pspoll_work);
2405         wl->channel = WL1271_DEFAULT_CHANNEL;
2406         wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
2407         wl->default_key = 0;
2408         wl->rx_counter = 0;
2409         wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
2410         wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
2411         wl->psm_entry_retry = 0;
2412         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
2413         wl->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
2414         wl->basic_rate = CONF_TX_RATE_MASK_BASIC;
2415         wl->rate_set = CONF_TX_RATE_MASK_BASIC;
2416         wl->sta_rate_set = 0;
2417         wl->band = IEEE80211_BAND_2GHZ;
2418         wl->vif = NULL;
2419         wl->flags = 0;
2420         wl->sg_enabled = true;
2421         wl->hw_pg_ver = -1;
2422
2423         for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
2424                 wl->tx_frames[i] = NULL;
2425
2426         spin_lock_init(&wl->wl_lock);
2427
2428         wl->state = WL1271_STATE_OFF;
2429         mutex_init(&wl->mutex);
2430
2431         /* Apply default driver configuration. */
2432         wl1271_conf_init(wl);
2433
2434         wl1271_debugfs_init(wl);
2435
2436         /* Register platform device */
2437         ret = platform_device_register(wl->plat_dev);
2438         if (ret) {
2439                 wl1271_error("couldn't register platform device");
2440                 goto err_hw;
2441         }
2442         dev_set_drvdata(&wl->plat_dev->dev, wl);
2443
2444         /* Create sysfs file to control bt coex state */
2445         ret = device_create_file(&wl->plat_dev->dev, &dev_attr_bt_coex_state);
2446         if (ret < 0) {
2447                 wl1271_error("failed to create sysfs file bt_coex_state");
2448                 goto err_platform;
2449         }
2450
2451         /* Create sysfs file to get HW PG version */
2452         ret = device_create_file(&wl->plat_dev->dev, &dev_attr_hw_pg_ver);
2453         if (ret < 0) {
2454                 wl1271_error("failed to create sysfs file hw_pg_ver");
2455                 goto err_bt_coex_state;
2456         }
2457
2458         return hw;
2459
2460 err_bt_coex_state:
2461         device_remove_file(&wl->plat_dev->dev, &dev_attr_bt_coex_state);
2462
2463 err_platform:
2464         platform_device_unregister(wl->plat_dev);
2465
2466 err_hw:
2467         wl1271_debugfs_exit(wl);
2468         kfree(plat_dev);
2469
2470 err_plat_alloc:
2471         ieee80211_free_hw(hw);
2472
2473 err_hw_alloc:
2474
2475         return ERR_PTR(ret);
2476 }
2477 EXPORT_SYMBOL_GPL(wl1271_alloc_hw);
2478
2479 int wl1271_free_hw(struct wl1271 *wl)
2480 {
2481         platform_device_unregister(wl->plat_dev);
2482         kfree(wl->plat_dev);
2483
2484         wl1271_debugfs_exit(wl);
2485
2486         vfree(wl->fw);
2487         wl->fw = NULL;
2488         kfree(wl->nvs);
2489         wl->nvs = NULL;
2490
2491         kfree(wl->fw_status);
2492         kfree(wl->tx_res_if);
2493
2494         ieee80211_free_hw(wl->hw);
2495
2496         return 0;
2497 }
2498 EXPORT_SYMBOL_GPL(wl1271_free_hw);
2499
2500 MODULE_LICENSE("GPL");
2501 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
2502 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");