wl1271: Add config structure for RX path parameters
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_main.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/firmware.h>
28 #include <linux/delay.h>
29 #include <linux/irq.h>
30 #include <linux/spi/spi.h>
31 #include <linux/crc32.h>
32 #include <linux/etherdevice.h>
33 #include <linux/vmalloc.h>
34 #include <linux/spi/wl12xx.h>
35
36 #include "wl1271.h"
37 #include "wl12xx_80211.h"
38 #include "wl1271_reg.h"
39 #include "wl1271_spi.h"
40 #include "wl1271_event.h"
41 #include "wl1271_tx.h"
42 #include "wl1271_rx.h"
43 #include "wl1271_ps.h"
44 #include "wl1271_init.h"
45 #include "wl1271_debugfs.h"
46 #include "wl1271_cmd.h"
47 #include "wl1271_boot.h"
48
49 static void wl1271_conf_init(struct wl1271 *wl)
50 {
51         struct conf_drv_settings conf = {
52                 .sg = {
53                         .per_threshold              = 7500,
54                         .max_scan_compensation_time = 120000,
55                         .nfs_sample_interval        = 400,
56                         .load_ratio                 = 50,
57                         .auto_ps_mode               = 0,
58                         .probe_req_compensation     = 170,
59                         .scan_window_compensation   = 50,
60                         .antenna_config             = 0,
61                         .beacon_miss_threshold      = 60,
62                         .rate_adaptation_threshold  = CONF_HW_BIT_RATE_12MBPS,
63                         .rate_adaptation_snr        = 0
64                 },
65                 .rx = {
66                         .rx_msdu_life_time          = 512000,
67                         .packet_detection_threshold = 0,
68                         .ps_poll_timeout            = 15,
69                         .upsd_timeout               = 15,
70                         .rts_threshold              = 2347,
71                         .rx_cca_threshold           = 0xFFEF,
72                         .irq_blk_threshold          = 0,
73                         .irq_pkt_threshold          = USHORT_MAX,
74                         .irq_timeout                = 5,
75                         .queue_type           = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
76                 }
77         };
78
79         /*
80          * This function applies the default configuration to the driver. This
81          * function is invoked upon driver load (spi probe.)
82          *
83          * The configuration is stored in a run-time structure in order to
84          * facilitate for run-time adjustment of any of the parameters. Making
85          * changes to the configuration structure will apply the new values on
86          * the next interface up (wl1271_op_start.)
87          */
88
89         /* apply driver default configuration */
90         memcpy(&wl->conf, &conf, sizeof(conf));
91 }
92
93
94 static int wl1271_plt_init(struct wl1271 *wl)
95 {
96         int ret;
97
98         ret = wl1271_acx_init_mem_config(wl);
99         if (ret < 0)
100                 return ret;
101
102         ret = wl1271_cmd_data_path(wl, wl->channel, 1);
103         if (ret < 0)
104                 return ret;
105
106         return 0;
107 }
108
109 static void wl1271_disable_interrupts(struct wl1271 *wl)
110 {
111         disable_irq(wl->irq);
112 }
113
114 static void wl1271_power_off(struct wl1271 *wl)
115 {
116         wl->set_power(false);
117 }
118
119 static void wl1271_power_on(struct wl1271 *wl)
120 {
121         wl->set_power(true);
122 }
123
124 static void wl1271_fw_status(struct wl1271 *wl,
125                              struct wl1271_fw_status *status)
126 {
127         u32 total = 0;
128         int i;
129
130         wl1271_spi_read(wl, FW_STATUS_ADDR, status,
131                         sizeof(*status), false);
132
133         wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
134                      "drv_rx_counter = %d, tx_results_counter = %d)",
135                      status->intr,
136                      status->fw_rx_counter,
137                      status->drv_rx_counter,
138                      status->tx_results_counter);
139
140         /* update number of available TX blocks */
141         for (i = 0; i < NUM_TX_QUEUES; i++) {
142                 u32 cnt = status->tx_released_blks[i] - wl->tx_blocks_freed[i];
143                 wl->tx_blocks_freed[i] = status->tx_released_blks[i];
144                 wl->tx_blocks_available += cnt;
145                 total += cnt;
146         }
147
148         /* if more blocks are available now, schedule some tx work */
149         if (total && !skb_queue_empty(&wl->tx_queue))
150                 ieee80211_queue_work(wl->hw, &wl->tx_work);
151
152         /* update the host-chipset time offset */
153         wl->time_offset = jiffies_to_usecs(jiffies) - status->fw_localtime;
154 }
155
156 static void wl1271_irq_work(struct work_struct *work)
157 {
158         int ret;
159         u32 intr;
160         struct wl1271 *wl =
161                 container_of(work, struct wl1271, irq_work);
162
163         mutex_lock(&wl->mutex);
164
165         wl1271_debug(DEBUG_IRQ, "IRQ work");
166
167         if (wl->state == WL1271_STATE_OFF)
168                 goto out;
169
170         ret = wl1271_ps_elp_wakeup(wl, true);
171         if (ret < 0)
172                 goto out;
173
174         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
175
176         wl1271_fw_status(wl, wl->fw_status);
177         intr = wl->fw_status->intr;
178         if (!intr) {
179                 wl1271_debug(DEBUG_IRQ, "Zero interrupt received.");
180                 goto out_sleep;
181         }
182
183         intr &= WL1271_INTR_MASK;
184
185         if (intr & (WL1271_ACX_INTR_EVENT_A |
186                     WL1271_ACX_INTR_EVENT_B)) {
187                 wl1271_debug(DEBUG_IRQ,
188                              "WL1271_ACX_INTR_EVENT (0x%x)", intr);
189                 if (intr & WL1271_ACX_INTR_EVENT_A)
190                         wl1271_event_handle(wl, 0);
191                 else
192                         wl1271_event_handle(wl, 1);
193         }
194
195         if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
196                 wl1271_debug(DEBUG_IRQ,
197                              "WL1271_ACX_INTR_INIT_COMPLETE");
198
199         if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
200                 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
201
202         if (intr & WL1271_ACX_INTR_DATA) {
203                 u8 tx_res_cnt = wl->fw_status->tx_results_counter -
204                         wl->tx_results_count;
205
206                 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
207
208                 /* check for tx results */
209                 if (tx_res_cnt)
210                         wl1271_tx_complete(wl, tx_res_cnt);
211
212                 wl1271_rx(wl, wl->fw_status);
213         }
214
215 out_sleep:
216         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK,
217                            WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
218         wl1271_ps_elp_sleep(wl);
219
220 out:
221         mutex_unlock(&wl->mutex);
222 }
223
224 static irqreturn_t wl1271_irq(int irq, void *cookie)
225 {
226         struct wl1271 *wl;
227         unsigned long flags;
228
229         wl1271_debug(DEBUG_IRQ, "IRQ");
230
231         wl = cookie;
232
233         /* complete the ELP completion */
234         spin_lock_irqsave(&wl->wl_lock, flags);
235         if (wl->elp_compl) {
236                 complete(wl->elp_compl);
237                 wl->elp_compl = NULL;
238         }
239
240         ieee80211_queue_work(wl->hw, &wl->irq_work);
241         spin_unlock_irqrestore(&wl->wl_lock, flags);
242
243         return IRQ_HANDLED;
244 }
245
246 static int wl1271_fetch_firmware(struct wl1271 *wl)
247 {
248         const struct firmware *fw;
249         int ret;
250
251         ret = request_firmware(&fw, WL1271_FW_NAME, &wl->spi->dev);
252
253         if (ret < 0) {
254                 wl1271_error("could not get firmware: %d", ret);
255                 return ret;
256         }
257
258         if (fw->size % 4) {
259                 wl1271_error("firmware size is not multiple of 32 bits: %zu",
260                              fw->size);
261                 ret = -EILSEQ;
262                 goto out;
263         }
264
265         wl->fw_len = fw->size;
266         wl->fw = vmalloc(wl->fw_len);
267
268         if (!wl->fw) {
269                 wl1271_error("could not allocate memory for the firmware");
270                 ret = -ENOMEM;
271                 goto out;
272         }
273
274         memcpy(wl->fw, fw->data, wl->fw_len);
275
276         ret = 0;
277
278 out:
279         release_firmware(fw);
280
281         return ret;
282 }
283
284 static int wl1271_fetch_nvs(struct wl1271 *wl)
285 {
286         const struct firmware *fw;
287         int ret;
288
289         ret = request_firmware(&fw, WL1271_NVS_NAME, &wl->spi->dev);
290
291         if (ret < 0) {
292                 wl1271_error("could not get nvs file: %d", ret);
293                 return ret;
294         }
295
296         if (fw->size % 4) {
297                 wl1271_error("nvs size is not multiple of 32 bits: %zu",
298                              fw->size);
299                 ret = -EILSEQ;
300                 goto out;
301         }
302
303         wl->nvs_len = fw->size;
304         wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
305
306         if (!wl->nvs) {
307                 wl1271_error("could not allocate memory for the nvs file");
308                 ret = -ENOMEM;
309                 goto out;
310         }
311
312         memcpy(wl->nvs, fw->data, wl->nvs_len);
313
314         ret = 0;
315
316 out:
317         release_firmware(fw);
318
319         return ret;
320 }
321
322 static void wl1271_fw_wakeup(struct wl1271 *wl)
323 {
324         u32 elp_reg;
325
326         elp_reg = ELPCTRL_WAKE_UP;
327         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
328 }
329
330 static int wl1271_setup(struct wl1271 *wl)
331 {
332         wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
333         if (!wl->fw_status)
334                 return -ENOMEM;
335
336         wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
337         if (!wl->tx_res_if) {
338                 kfree(wl->fw_status);
339                 return -ENOMEM;
340         }
341
342         INIT_WORK(&wl->irq_work, wl1271_irq_work);
343         INIT_WORK(&wl->tx_work, wl1271_tx_work);
344         return 0;
345 }
346
347 static int wl1271_chip_wakeup(struct wl1271 *wl)
348 {
349         struct wl1271_partition_set partition;
350         int ret = 0;
351
352         wl1271_power_on(wl);
353         msleep(WL1271_POWER_ON_SLEEP);
354         wl1271_spi_reset(wl);
355         wl1271_spi_init(wl);
356
357         /* We don't need a real memory partition here, because we only want
358          * to use the registers at this point. */
359         memset(&partition, 0, sizeof(partition));
360         partition.reg.start = REGISTERS_BASE;
361         partition.reg.size = REGISTERS_DOWN_SIZE;
362         wl1271_set_partition(wl, &partition);
363
364         /* ELP module wake up */
365         wl1271_fw_wakeup(wl);
366
367         /* whal_FwCtrl_BootSm() */
368
369         /* 0. read chip id from CHIP_ID */
370         wl->chip.id = wl1271_spi_read32(wl, CHIP_ID_B);
371
372         /* 1. check if chip id is valid */
373
374         switch (wl->chip.id) {
375         case CHIP_ID_1271_PG10:
376                 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
377                                wl->chip.id);
378
379                 ret = wl1271_setup(wl);
380                 if (ret < 0)
381                         goto out;
382                 break;
383         case CHIP_ID_1271_PG20:
384                 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
385                              wl->chip.id);
386
387                 ret = wl1271_setup(wl);
388                 if (ret < 0)
389                         goto out;
390                 break;
391         default:
392                 wl1271_error("unsupported chip id: 0x%x", wl->chip.id);
393                 ret = -ENODEV;
394                 goto out;
395         }
396
397         if (wl->fw == NULL) {
398                 ret = wl1271_fetch_firmware(wl);
399                 if (ret < 0)
400                         goto out;
401         }
402
403         /* No NVS from netlink, try to get it from the filesystem */
404         if (wl->nvs == NULL) {
405                 ret = wl1271_fetch_nvs(wl);
406                 if (ret < 0)
407                         goto out;
408         }
409
410 out:
411         return ret;
412 }
413
414 struct wl1271_filter_params {
415         unsigned int filters;
416         unsigned int changed;
417         int mc_list_length;
418         u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
419 };
420
421 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
422                                   FIF_ALLMULTI | \
423                                   FIF_FCSFAIL | \
424                                   FIF_BCN_PRBRESP_PROMISC | \
425                                   FIF_CONTROL | \
426                                   FIF_OTHER_BSS)
427
428 static void wl1271_filter_work(struct work_struct *work)
429 {
430         struct wl1271 *wl =
431                 container_of(work, struct wl1271, filter_work);
432         struct wl1271_filter_params *fp;
433         unsigned long flags;
434         bool enabled = true;
435         int ret;
436
437         /* first, get the filter parameters */
438         spin_lock_irqsave(&wl->wl_lock, flags);
439         fp = wl->filter_params;
440         wl->filter_params = NULL;
441         spin_unlock_irqrestore(&wl->wl_lock, flags);
442
443         if (!fp)
444                 return;
445
446         /* then, lock the mutex without risk of lock-up */
447         mutex_lock(&wl->mutex);
448
449         if (wl->state == WL1271_STATE_OFF)
450                 goto out;
451
452         ret = wl1271_ps_elp_wakeup(wl, false);
453         if (ret < 0)
454                 goto out;
455
456         /* configure the mc filter regardless of the changed flags */
457         if (fp->filters & FIF_ALLMULTI)
458                 enabled = false;
459
460         ret = wl1271_acx_group_address_tbl(wl, enabled,
461                                            fp->mc_list, fp->mc_list_length);
462         if (ret < 0)
463                 goto out_sleep;
464
465         /* determine, whether supported filter values have changed */
466         if (fp->changed == 0)
467                 goto out;
468
469         /* apply configured filters */
470         ret = wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
471         if (ret < 0)
472                 goto out_sleep;
473
474 out_sleep:
475         wl1271_ps_elp_sleep(wl);
476
477 out:
478         mutex_unlock(&wl->mutex);
479         kfree(fp);
480 }
481
482 int wl1271_plt_start(struct wl1271 *wl)
483 {
484         int ret;
485
486         mutex_lock(&wl->mutex);
487
488         wl1271_notice("power up");
489
490         if (wl->state != WL1271_STATE_OFF) {
491                 wl1271_error("cannot go into PLT state because not "
492                              "in off state: %d", wl->state);
493                 ret = -EBUSY;
494                 goto out;
495         }
496
497         wl->state = WL1271_STATE_PLT;
498
499         ret = wl1271_chip_wakeup(wl);
500         if (ret < 0)
501                 goto out;
502
503         ret = wl1271_boot(wl);
504         if (ret < 0)
505                 goto out;
506
507         wl1271_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
508
509         ret = wl1271_plt_init(wl);
510         if (ret < 0)
511                 goto out;
512
513 out:
514         mutex_unlock(&wl->mutex);
515
516         return ret;
517 }
518
519 int wl1271_plt_stop(struct wl1271 *wl)
520 {
521         int ret = 0;
522
523         mutex_lock(&wl->mutex);
524
525         wl1271_notice("power down");
526
527         if (wl->state != WL1271_STATE_PLT) {
528                 wl1271_error("cannot power down because not in PLT "
529                              "state: %d", wl->state);
530                 ret = -EBUSY;
531                 goto out;
532         }
533
534         wl1271_disable_interrupts(wl);
535         wl1271_power_off(wl);
536
537         wl->state = WL1271_STATE_OFF;
538
539 out:
540         mutex_unlock(&wl->mutex);
541
542         return ret;
543 }
544
545
546 static int wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
547 {
548         struct wl1271 *wl = hw->priv;
549
550         skb_queue_tail(&wl->tx_queue, skb);
551
552         /*
553          * The chip specific setup must run before the first TX packet -
554          * before that, the tx_work will not be initialized!
555          */
556
557         ieee80211_queue_work(wl->hw, &wl->tx_work);
558
559         /*
560          * The workqueue is slow to process the tx_queue and we need stop
561          * the queue here, otherwise the queue will get too long.
562          */
563         if (skb_queue_len(&wl->tx_queue) >= WL1271_TX_QUEUE_MAX_LENGTH) {
564                 ieee80211_stop_queues(wl->hw);
565
566                 /*
567                  * FIXME: this is racy, the variable is not properly
568                  * protected. Maybe fix this by removing the stupid
569                  * variable altogether and checking the real queue state?
570                  */
571                 wl->tx_queue_stopped = true;
572         }
573
574         return NETDEV_TX_OK;
575 }
576
577 static int wl1271_op_start(struct ieee80211_hw *hw)
578 {
579         struct wl1271 *wl = hw->priv;
580         int ret = 0;
581
582         wl1271_debug(DEBUG_MAC80211, "mac80211 start");
583
584         mutex_lock(&wl->mutex);
585
586         if (wl->state != WL1271_STATE_OFF) {
587                 wl1271_error("cannot start because not in off state: %d",
588                              wl->state);
589                 ret = -EBUSY;
590                 goto out;
591         }
592
593         ret = wl1271_chip_wakeup(wl);
594         if (ret < 0)
595                 goto out;
596
597         ret = wl1271_boot(wl);
598         if (ret < 0)
599                 goto out;
600
601         ret = wl1271_hw_init(wl);
602         if (ret < 0)
603                 goto out;
604
605         wl->state = WL1271_STATE_ON;
606
607         wl1271_info("firmware booted (%s)", wl->chip.fw_ver);
608
609 out:
610         if (ret < 0)
611                 wl1271_power_off(wl);
612
613         mutex_unlock(&wl->mutex);
614
615         return ret;
616 }
617
618 static void wl1271_op_stop(struct ieee80211_hw *hw)
619 {
620         struct wl1271 *wl = hw->priv;
621         unsigned long flags;
622         int i;
623
624         wl1271_info("down");
625
626         wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
627
628         /* complete/cancel ongoing work */
629         cancel_work_sync(&wl->filter_work);
630         spin_lock_irqsave(&wl->wl_lock, flags);
631         kfree(wl->filter_params);
632         wl->filter_params = NULL;
633         spin_unlock_irqrestore(&wl->wl_lock, flags);
634
635         mutex_lock(&wl->mutex);
636
637         WARN_ON(wl->state != WL1271_STATE_ON);
638
639         if (wl->scanning) {
640                 mutex_unlock(&wl->mutex);
641                 ieee80211_scan_completed(wl->hw, true);
642                 mutex_lock(&wl->mutex);
643                 wl->scanning = false;
644         }
645
646         wl->state = WL1271_STATE_OFF;
647
648         wl1271_disable_interrupts(wl);
649
650         mutex_unlock(&wl->mutex);
651
652         cancel_work_sync(&wl->irq_work);
653         cancel_work_sync(&wl->tx_work);
654         cancel_work_sync(&wl->filter_work);
655
656         mutex_lock(&wl->mutex);
657
658         /* let's notify MAC80211 about the remaining pending TX frames */
659         wl1271_tx_flush(wl);
660         wl1271_power_off(wl);
661
662         memset(wl->bssid, 0, ETH_ALEN);
663         memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
664         wl->ssid_len = 0;
665         wl->listen_int = 1;
666         wl->bss_type = MAX_BSS_TYPE;
667         wl->band = IEEE80211_BAND_2GHZ;
668
669         wl->rx_counter = 0;
670         wl->elp = false;
671         wl->psm = 0;
672         wl->tx_queue_stopped = false;
673         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
674         wl->tx_blocks_available = 0;
675         wl->tx_results_count = 0;
676         wl->tx_packets_count = 0;
677         wl->tx_security_last_seq = 0;
678         wl->tx_security_seq_16 = 0;
679         wl->tx_security_seq_32 = 0;
680         wl->time_offset = 0;
681         wl->session_counter = 0;
682         wl->joined = false;
683
684         for (i = 0; i < NUM_TX_QUEUES; i++)
685                 wl->tx_blocks_freed[i] = 0;
686
687         wl1271_debugfs_reset(wl);
688         mutex_unlock(&wl->mutex);
689 }
690
691 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
692                                    struct ieee80211_if_init_conf *conf)
693 {
694         struct wl1271 *wl = hw->priv;
695         int ret = 0;
696
697         wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
698                      conf->type, conf->mac_addr);
699
700         mutex_lock(&wl->mutex);
701         if (wl->vif) {
702                 ret = -EBUSY;
703                 goto out;
704         }
705
706         wl->vif = conf->vif;
707
708         switch (conf->type) {
709         case NL80211_IFTYPE_STATION:
710                 wl->bss_type = BSS_TYPE_STA_BSS;
711                 break;
712         case NL80211_IFTYPE_ADHOC:
713                 wl->bss_type = BSS_TYPE_IBSS;
714                 break;
715         default:
716                 ret = -EOPNOTSUPP;
717                 goto out;
718         }
719
720         /* FIXME: what if conf->mac_addr changes? */
721
722 out:
723         mutex_unlock(&wl->mutex);
724         return ret;
725 }
726
727 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
728                                          struct ieee80211_if_init_conf *conf)
729 {
730         struct wl1271 *wl = hw->priv;
731
732         mutex_lock(&wl->mutex);
733         wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
734         wl->vif = NULL;
735         mutex_unlock(&wl->mutex);
736 }
737
738 #if 0
739 static int wl1271_op_config_interface(struct ieee80211_hw *hw,
740                                       struct ieee80211_vif *vif,
741                                       struct ieee80211_if_conf *conf)
742 {
743         struct wl1271 *wl = hw->priv;
744         struct sk_buff *beacon;
745         int ret;
746
747         wl1271_debug(DEBUG_MAC80211, "mac80211 config_interface bssid %pM",
748                      conf->bssid);
749         wl1271_dump_ascii(DEBUG_MAC80211, "ssid: ", conf->ssid,
750                           conf->ssid_len);
751
752         mutex_lock(&wl->mutex);
753
754         ret = wl1271_ps_elp_wakeup(wl, false);
755         if (ret < 0)
756                 goto out;
757
758         if (memcmp(wl->bssid, conf->bssid, ETH_ALEN)) {
759                 wl1271_debug(DEBUG_MAC80211, "bssid changed");
760
761                 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
762
763                 ret = wl1271_cmd_join(wl);
764                 if (ret < 0)
765                         goto out_sleep;
766         }
767
768         ret = wl1271_cmd_build_null_data(wl);
769         if (ret < 0)
770                 goto out_sleep;
771
772         wl->ssid_len = conf->ssid_len;
773         if (wl->ssid_len)
774                 memcpy(wl->ssid, conf->ssid, wl->ssid_len);
775
776         if (conf->changed & IEEE80211_IFCC_BEACON) {
777                 beacon = ieee80211_beacon_get(hw, vif);
778                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
779                                               beacon->data, beacon->len);
780
781                 if (ret < 0) {
782                         dev_kfree_skb(beacon);
783                         goto out_sleep;
784                 }
785
786                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PROBE_RESPONSE,
787                                               beacon->data, beacon->len);
788
789                 dev_kfree_skb(beacon);
790
791                 if (ret < 0)
792                         goto out_sleep;
793         }
794
795 out_sleep:
796         wl1271_ps_elp_sleep(wl);
797
798 out:
799         mutex_unlock(&wl->mutex);
800
801         return ret;
802 }
803 #endif
804
805 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
806 {
807         struct wl1271 *wl = hw->priv;
808         struct ieee80211_conf *conf = &hw->conf;
809         int channel, ret = 0;
810
811         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
812
813         wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
814                      channel,
815                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
816                      conf->power_level);
817
818         mutex_lock(&wl->mutex);
819
820         wl->band = conf->channel->band;
821
822         ret = wl1271_ps_elp_wakeup(wl, false);
823         if (ret < 0)
824                 goto out;
825
826         if (channel != wl->channel) {
827                 /*
828                  * We assume that the stack will configure the right channel
829                  * before associating, so we don't need to send a join
830                  * command here.  We will join the right channel when the
831                  * BSSID changes
832                  */
833                 wl->channel = channel;
834         }
835
836         ret = wl1271_cmd_build_null_data(wl);
837         if (ret < 0)
838                 goto out_sleep;
839
840         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
841                 wl1271_info("psm enabled");
842
843                 wl->psm_requested = true;
844
845                 /*
846                  * We enter PSM only if we're already associated.
847                  * If we're not, we'll enter it when joining an SSID,
848                  * through the bss_info_changed() hook.
849                  */
850                 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
851         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
852                    wl->psm_requested) {
853                 wl1271_info("psm disabled");
854
855                 wl->psm_requested = false;
856
857                 if (wl->psm)
858                         ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE);
859         }
860
861         if (conf->power_level != wl->power_level) {
862                 ret = wl1271_acx_tx_power(wl, conf->power_level);
863                 if (ret < 0)
864                         goto out;
865
866                 wl->power_level = conf->power_level;
867         }
868
869 out_sleep:
870         wl1271_ps_elp_sleep(wl);
871
872 out:
873         mutex_unlock(&wl->mutex);
874
875         return ret;
876 }
877
878 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
879                                        struct dev_addr_list *mc_list)
880 {
881         struct wl1271 *wl = hw->priv;
882         struct wl1271_filter_params *fp;
883         unsigned long flags;
884         int i;
885
886         /*
887          * FIXME: we should return a hash that will be passed to
888          * configure_filter() instead of saving everything in the context.
889          */
890
891         fp = kzalloc(sizeof(*fp), GFP_KERNEL);
892         if (!fp) {
893                 wl1271_error("Out of memory setting filters.");
894                 return 0;
895         }
896
897         /* update multicast filtering parameters */
898         if (mc_count > ACX_MC_ADDRESS_GROUP_MAX) {
899                 mc_count = 0;
900                 fp->filters |= FIF_ALLMULTI;
901         }
902
903         fp->mc_list_length = 0;
904         for (i = 0; i < mc_count; i++) {
905                 if (mc_list->da_addrlen == ETH_ALEN) {
906                         memcpy(fp->mc_list[fp->mc_list_length],
907                                mc_list->da_addr, ETH_ALEN);
908                         fp->mc_list_length++;
909                 } else
910                         wl1271_warning("Unknown mc address length.");
911         }
912
913         /* FIXME: We still need to set our filters properly */
914
915         spin_lock_irqsave(&wl->wl_lock, flags);
916         kfree(wl->filter_params);
917         wl->filter_params = fp;
918         spin_unlock_irqrestore(&wl->wl_lock, flags);
919
920         return 1;
921 }
922
923 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
924                                        unsigned int changed,
925                                        unsigned int *total, u64 multicast)
926 {
927         struct wl1271 *wl = hw->priv;
928
929         wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
930
931         *total &= WL1271_SUPPORTED_FILTERS;
932         changed &= WL1271_SUPPORTED_FILTERS;
933
934         if (!multicast)
935                 return;
936
937         /*
938          * FIXME: for now we are still using a workqueue for filter
939          * configuration, but with the new mac80211, this is not needed,
940          * since configure_filter can now sleep.  We now have
941          * prepare_multicast, which needs to be atomic instead.
942          */
943
944         /* store current filter config */
945         wl->filter_params->filters = *total;
946         wl->filter_params->changed = changed;
947
948         ieee80211_queue_work(wl->hw, &wl->filter_work);
949 }
950
951 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
952                              struct ieee80211_vif *vif,
953                              struct ieee80211_sta *sta,
954                              struct ieee80211_key_conf *key_conf)
955 {
956         struct wl1271 *wl = hw->priv;
957         const u8 *addr;
958         int ret;
959         u32 tx_seq_32 = 0;
960         u16 tx_seq_16 = 0;
961         u8 key_type;
962
963         static const u8 bcast_addr[ETH_ALEN] =
964                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
965
966         wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
967
968         addr = sta ? sta->addr : bcast_addr;
969
970         wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
971         wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
972         wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
973                      key_conf->alg, key_conf->keyidx,
974                      key_conf->keylen, key_conf->flags);
975         wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
976
977         if (is_zero_ether_addr(addr)) {
978                 /* We dont support TX only encryption */
979                 ret = -EOPNOTSUPP;
980                 goto out;
981         }
982
983         mutex_lock(&wl->mutex);
984
985         ret = wl1271_ps_elp_wakeup(wl, false);
986         if (ret < 0)
987                 goto out_unlock;
988
989         switch (key_conf->alg) {
990         case ALG_WEP:
991                 key_type = KEY_WEP;
992
993                 key_conf->hw_key_idx = key_conf->keyidx;
994                 break;
995         case ALG_TKIP:
996                 key_type = KEY_TKIP;
997
998                 key_conf->hw_key_idx = key_conf->keyidx;
999                 tx_seq_32 = wl->tx_security_seq_32;
1000                 tx_seq_16 = wl->tx_security_seq_16;
1001                 break;
1002         case ALG_CCMP:
1003                 key_type = KEY_AES;
1004
1005                 key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1006                 tx_seq_32 = wl->tx_security_seq_32;
1007                 tx_seq_16 = wl->tx_security_seq_16;
1008                 break;
1009         default:
1010                 wl1271_error("Unknown key algo 0x%x", key_conf->alg);
1011
1012                 ret = -EOPNOTSUPP;
1013                 goto out_sleep;
1014         }
1015
1016         switch (cmd) {
1017         case SET_KEY:
1018                 ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
1019                                          key_conf->keyidx, key_type,
1020                                          key_conf->keylen, key_conf->key,
1021                                          addr, tx_seq_32, tx_seq_16);
1022                 if (ret < 0) {
1023                         wl1271_error("Could not add or replace key");
1024                         goto out_sleep;
1025                 }
1026                 break;
1027
1028         case DISABLE_KEY:
1029                 ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
1030                                          key_conf->keyidx, key_type,
1031                                          key_conf->keylen, key_conf->key,
1032                                          addr, 0, 0);
1033                 if (ret < 0) {
1034                         wl1271_error("Could not remove key");
1035                         goto out_sleep;
1036                 }
1037                 break;
1038
1039         default:
1040                 wl1271_error("Unsupported key cmd 0x%x", cmd);
1041                 ret = -EOPNOTSUPP;
1042                 goto out_sleep;
1043
1044                 break;
1045         }
1046
1047 out_sleep:
1048         wl1271_ps_elp_sleep(wl);
1049
1050 out_unlock:
1051         mutex_unlock(&wl->mutex);
1052
1053 out:
1054         return ret;
1055 }
1056
1057 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
1058                              struct cfg80211_scan_request *req)
1059 {
1060         struct wl1271 *wl = hw->priv;
1061         int ret;
1062         u8 *ssid = NULL;
1063         size_t ssid_len = 0;
1064
1065         wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
1066
1067         if (req->n_ssids) {
1068                 ssid = req->ssids[0].ssid;
1069                 ssid_len = req->ssids[0].ssid_len;
1070         }
1071
1072         mutex_lock(&wl->mutex);
1073
1074         ret = wl1271_ps_elp_wakeup(wl, false);
1075         if (ret < 0)
1076                 goto out;
1077
1078         ret = wl1271_cmd_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
1079
1080         wl1271_ps_elp_sleep(wl);
1081
1082 out:
1083         mutex_unlock(&wl->mutex);
1084
1085         return ret;
1086 }
1087
1088 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1089 {
1090         struct wl1271 *wl = hw->priv;
1091         int ret;
1092
1093         mutex_lock(&wl->mutex);
1094
1095         ret = wl1271_ps_elp_wakeup(wl, false);
1096         if (ret < 0)
1097                 goto out;
1098
1099         ret = wl1271_acx_rts_threshold(wl, (u16) value);
1100         if (ret < 0)
1101                 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
1102
1103         wl1271_ps_elp_sleep(wl);
1104
1105 out:
1106         mutex_unlock(&wl->mutex);
1107
1108         return ret;
1109 }
1110
1111 static u32 wl1271_enabled_rates_get(struct wl1271 *wl, u64 basic_rate_set)
1112 {
1113         struct ieee80211_supported_band *band;
1114         u32 enabled_rates = 0;
1115         int bit;
1116
1117         band = wl->hw->wiphy->bands[wl->band];
1118         for (bit = 0; bit < band->n_bitrates; bit++) {
1119                 if (basic_rate_set & 0x1)
1120                         enabled_rates |= band->bitrates[bit].hw_value;
1121                 basic_rate_set >>= 1;
1122         }
1123
1124         return enabled_rates;
1125 }
1126
1127 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
1128                                        struct ieee80211_vif *vif,
1129                                        struct ieee80211_bss_conf *bss_conf,
1130                                        u32 changed)
1131 {
1132         enum wl1271_cmd_ps_mode mode;
1133         struct wl1271 *wl = hw->priv;
1134         int ret;
1135
1136         wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1137
1138         mutex_lock(&wl->mutex);
1139
1140         ret = wl1271_ps_elp_wakeup(wl, false);
1141         if (ret < 0)
1142                 goto out;
1143
1144         if (changed & BSS_CHANGED_ASSOC) {
1145                 if (bss_conf->assoc) {
1146                         wl->aid = bss_conf->aid;
1147
1148                         /*
1149                          * with wl1271, we don't need to update the
1150                          * beacon_int and dtim_period, because the firmware
1151                          * updates it by itself when the first beacon is
1152                          * received after a join.
1153                          */
1154                         ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
1155                         if (ret < 0)
1156                                 goto out_sleep;
1157
1158                         ret = wl1271_acx_aid(wl, wl->aid);
1159                         if (ret < 0)
1160                                 goto out_sleep;
1161
1162                         /* If we want to go in PSM but we're not there yet */
1163                         if (wl->psm_requested && !wl->psm) {
1164                                 mode = STATION_POWER_SAVE_MODE;
1165                                 ret = wl1271_ps_set_mode(wl, mode);
1166                                 if (ret < 0)
1167                                         goto out_sleep;
1168                         }
1169                 } else {
1170                         /* use defaults when not associated */
1171                         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1172                         wl->aid = 0;
1173                 }
1174
1175         }
1176
1177         if (changed & BSS_CHANGED_ERP_SLOT) {
1178                 if (bss_conf->use_short_slot)
1179                         ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
1180                 else
1181                         ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
1182                 if (ret < 0) {
1183                         wl1271_warning("Set slot time failed %d", ret);
1184                         goto out_sleep;
1185                 }
1186         }
1187
1188         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1189                 if (bss_conf->use_short_preamble)
1190                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1191                 else
1192                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1193         }
1194
1195         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1196                 if (bss_conf->use_cts_prot)
1197                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1198                 else
1199                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1200                 if (ret < 0) {
1201                         wl1271_warning("Set ctsprotect failed %d", ret);
1202                         goto out_sleep;
1203                 }
1204         }
1205
1206         if (changed & BSS_CHANGED_BASIC_RATES) {
1207                 wl->basic_rate_set = wl1271_enabled_rates_get(
1208                         wl, bss_conf->basic_rates);
1209
1210                 ret = wl1271_acx_rate_policies(wl, wl->basic_rate_set);
1211                 if (ret < 0) {
1212                         wl1271_warning("Set rate policies failed %d", ret);
1213                         goto out_sleep;
1214                 }
1215         }
1216
1217 out_sleep:
1218         wl1271_ps_elp_sleep(wl);
1219
1220 out:
1221         mutex_unlock(&wl->mutex);
1222 }
1223
1224
1225 /* can't be const, mac80211 writes to this */
1226 static struct ieee80211_rate wl1271_rates[] = {
1227         { .bitrate = 10,
1228           .hw_value = CONF_HW_BIT_RATE_1MBPS,
1229           .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
1230         { .bitrate = 20,
1231           .hw_value = CONF_HW_BIT_RATE_2MBPS,
1232           .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
1233           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1234         { .bitrate = 55,
1235           .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
1236           .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
1237           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1238         { .bitrate = 110,
1239           .hw_value = CONF_HW_BIT_RATE_11MBPS,
1240           .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
1241           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1242         { .bitrate = 60,
1243           .hw_value = CONF_HW_BIT_RATE_6MBPS,
1244           .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
1245         { .bitrate = 90,
1246           .hw_value = CONF_HW_BIT_RATE_9MBPS,
1247           .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
1248         { .bitrate = 120,
1249           .hw_value = CONF_HW_BIT_RATE_12MBPS,
1250           .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
1251         { .bitrate = 180,
1252           .hw_value = CONF_HW_BIT_RATE_18MBPS,
1253           .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
1254         { .bitrate = 240,
1255           .hw_value = CONF_HW_BIT_RATE_24MBPS,
1256           .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
1257         { .bitrate = 360,
1258          .hw_value = CONF_HW_BIT_RATE_36MBPS,
1259          .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
1260         { .bitrate = 480,
1261           .hw_value = CONF_HW_BIT_RATE_48MBPS,
1262           .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
1263         { .bitrate = 540,
1264           .hw_value = CONF_HW_BIT_RATE_54MBPS,
1265           .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
1266 };
1267
1268 /* can't be const, mac80211 writes to this */
1269 static struct ieee80211_channel wl1271_channels[] = {
1270         { .hw_value = 1, .center_freq = 2412},
1271         { .hw_value = 2, .center_freq = 2417},
1272         { .hw_value = 3, .center_freq = 2422},
1273         { .hw_value = 4, .center_freq = 2427},
1274         { .hw_value = 5, .center_freq = 2432},
1275         { .hw_value = 6, .center_freq = 2437},
1276         { .hw_value = 7, .center_freq = 2442},
1277         { .hw_value = 8, .center_freq = 2447},
1278         { .hw_value = 9, .center_freq = 2452},
1279         { .hw_value = 10, .center_freq = 2457},
1280         { .hw_value = 11, .center_freq = 2462},
1281         { .hw_value = 12, .center_freq = 2467},
1282         { .hw_value = 13, .center_freq = 2472},
1283 };
1284
1285 /* can't be const, mac80211 writes to this */
1286 static struct ieee80211_supported_band wl1271_band_2ghz = {
1287         .channels = wl1271_channels,
1288         .n_channels = ARRAY_SIZE(wl1271_channels),
1289         .bitrates = wl1271_rates,
1290         .n_bitrates = ARRAY_SIZE(wl1271_rates),
1291 };
1292
1293 static const struct ieee80211_ops wl1271_ops = {
1294         .start = wl1271_op_start,
1295         .stop = wl1271_op_stop,
1296         .add_interface = wl1271_op_add_interface,
1297         .remove_interface = wl1271_op_remove_interface,
1298         .config = wl1271_op_config,
1299 /*      .config_interface = wl1271_op_config_interface, */
1300         .prepare_multicast = wl1271_op_prepare_multicast,
1301         .configure_filter = wl1271_op_configure_filter,
1302         .tx = wl1271_op_tx,
1303         .set_key = wl1271_op_set_key,
1304         .hw_scan = wl1271_op_hw_scan,
1305         .bss_info_changed = wl1271_op_bss_info_changed,
1306         .set_rts_threshold = wl1271_op_set_rts_threshold,
1307 };
1308
1309 static int wl1271_register_hw(struct wl1271 *wl)
1310 {
1311         int ret;
1312
1313         if (wl->mac80211_registered)
1314                 return 0;
1315
1316         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1317
1318         ret = ieee80211_register_hw(wl->hw);
1319         if (ret < 0) {
1320                 wl1271_error("unable to register mac80211 hw: %d", ret);
1321                 return ret;
1322         }
1323
1324         wl->mac80211_registered = true;
1325
1326         wl1271_notice("loaded");
1327
1328         return 0;
1329 }
1330
1331 static int wl1271_init_ieee80211(struct wl1271 *wl)
1332 {
1333         /* The tx descriptor buffer and the TKIP space. */
1334         wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
1335                 sizeof(struct wl1271_tx_hw_descr);
1336
1337         /* unit us */
1338         /* FIXME: find a proper value */
1339         wl->hw->channel_change_time = 10000;
1340
1341         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1342                 IEEE80211_HW_NOISE_DBM |
1343                 IEEE80211_HW_BEACON_FILTER;
1344
1345         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1346         wl->hw->wiphy->max_scan_ssids = 1;
1347         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
1348
1349         SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1350
1351         return 0;
1352 }
1353
1354 static void wl1271_device_release(struct device *dev)
1355 {
1356
1357 }
1358
1359 static struct platform_device wl1271_device = {
1360         .name           = "wl1271",
1361         .id             = -1,
1362
1363         /* device model insists to have a release function */
1364         .dev            = {
1365                 .release = wl1271_device_release,
1366         },
1367 };
1368
1369 #define WL1271_DEFAULT_CHANNEL 0
1370 static int __devinit wl1271_probe(struct spi_device *spi)
1371 {
1372         struct wl12xx_platform_data *pdata;
1373         struct ieee80211_hw *hw;
1374         struct wl1271 *wl;
1375         int ret, i;
1376         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1377
1378         pdata = spi->dev.platform_data;
1379         if (!pdata) {
1380                 wl1271_error("no platform data");
1381                 return -ENODEV;
1382         }
1383
1384         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
1385         if (!hw) {
1386                 wl1271_error("could not alloc ieee80211_hw");
1387                 return -ENOMEM;
1388         }
1389
1390         wl = hw->priv;
1391         memset(wl, 0, sizeof(*wl));
1392
1393         wl->hw = hw;
1394         dev_set_drvdata(&spi->dev, wl);
1395         wl->spi = spi;
1396
1397         skb_queue_head_init(&wl->tx_queue);
1398
1399         INIT_WORK(&wl->filter_work, wl1271_filter_work);
1400         INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
1401         wl->channel = WL1271_DEFAULT_CHANNEL;
1402         wl->scanning = false;
1403         wl->default_key = 0;
1404         wl->listen_int = 1;
1405         wl->rx_counter = 0;
1406         wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
1407         wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
1408         wl->elp = false;
1409         wl->psm = 0;
1410         wl->psm_requested = false;
1411         wl->tx_queue_stopped = false;
1412         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1413         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1414         wl->band = IEEE80211_BAND_2GHZ;
1415         wl->vif = NULL;
1416         wl->joined = false;
1417
1418         for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
1419                 wl->tx_frames[i] = NULL;
1420
1421         spin_lock_init(&wl->wl_lock);
1422
1423         /*
1424          * In case our MAC address is not correctly set,
1425          * we use a random but Nokia MAC.
1426          */
1427         memcpy(wl->mac_addr, nokia_oui, 3);
1428         get_random_bytes(wl->mac_addr + 3, 3);
1429
1430         wl->state = WL1271_STATE_OFF;
1431         mutex_init(&wl->mutex);
1432
1433         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1434         if (!wl->rx_descriptor) {
1435                 wl1271_error("could not allocate memory for rx descriptor");
1436                 ret = -ENOMEM;
1437                 goto out_free;
1438         }
1439
1440         /* This is the only SPI value that we need to set here, the rest
1441          * comes from the board-peripherals file */
1442         spi->bits_per_word = 32;
1443
1444         ret = spi_setup(spi);
1445         if (ret < 0) {
1446                 wl1271_error("spi_setup failed");
1447                 goto out_free;
1448         }
1449
1450         wl->set_power = pdata->set_power;
1451         if (!wl->set_power) {
1452                 wl1271_error("set power function missing in platform data");
1453                 ret = -ENODEV;
1454                 goto out_free;
1455         }
1456
1457         wl->irq = spi->irq;
1458         if (wl->irq < 0) {
1459                 wl1271_error("irq missing in platform data");
1460                 ret = -ENODEV;
1461                 goto out_free;
1462         }
1463
1464         ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
1465         if (ret < 0) {
1466                 wl1271_error("request_irq() failed: %d", ret);
1467                 goto out_free;
1468         }
1469
1470         set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1471
1472         disable_irq(wl->irq);
1473
1474         ret = platform_device_register(&wl1271_device);
1475         if (ret) {
1476                 wl1271_error("couldn't register platform device");
1477                 goto out_irq;
1478         }
1479         dev_set_drvdata(&wl1271_device.dev, wl);
1480
1481         /* Apply default driver configuration. */
1482         wl1271_conf_init(wl);
1483
1484         ret = wl1271_init_ieee80211(wl);
1485         if (ret)
1486                 goto out_platform;
1487
1488         ret = wl1271_register_hw(wl);
1489         if (ret)
1490                 goto out_platform;
1491
1492         wl1271_debugfs_init(wl);
1493
1494         wl1271_notice("initialized");
1495
1496         return 0;
1497
1498  out_platform:
1499         platform_device_unregister(&wl1271_device);
1500
1501  out_irq:
1502         free_irq(wl->irq, wl);
1503
1504  out_free:
1505         kfree(wl->rx_descriptor);
1506         wl->rx_descriptor = NULL;
1507
1508         ieee80211_free_hw(hw);
1509
1510         return ret;
1511 }
1512
1513 static int __devexit wl1271_remove(struct spi_device *spi)
1514 {
1515         struct wl1271 *wl = dev_get_drvdata(&spi->dev);
1516
1517         ieee80211_unregister_hw(wl->hw);
1518
1519         wl1271_debugfs_exit(wl);
1520         platform_device_unregister(&wl1271_device);
1521         free_irq(wl->irq, wl);
1522         kfree(wl->target_mem_map);
1523         vfree(wl->fw);
1524         wl->fw = NULL;
1525         kfree(wl->nvs);
1526         wl->nvs = NULL;
1527
1528         kfree(wl->rx_descriptor);
1529         wl->rx_descriptor = NULL;
1530
1531         kfree(wl->fw_status);
1532         kfree(wl->tx_res_if);
1533
1534         ieee80211_free_hw(wl->hw);
1535
1536         return 0;
1537 }
1538
1539
1540 static struct spi_driver wl1271_spi_driver = {
1541         .driver = {
1542                 .name           = "wl1271",
1543                 .bus            = &spi_bus_type,
1544                 .owner          = THIS_MODULE,
1545         },
1546
1547         .probe          = wl1271_probe,
1548         .remove         = __devexit_p(wl1271_remove),
1549 };
1550
1551 static int __init wl1271_init(void)
1552 {
1553         int ret;
1554
1555         ret = spi_register_driver(&wl1271_spi_driver);
1556         if (ret < 0) {
1557                 wl1271_error("failed to register spi driver: %d", ret);
1558                 goto out;
1559         }
1560
1561 out:
1562         return ret;
1563 }
1564
1565 static void __exit wl1271_exit(void)
1566 {
1567         spi_unregister_driver(&wl1271_spi_driver);
1568
1569         wl1271_notice("unloaded");
1570 }
1571
1572 module_init(wl1271_init);
1573 module_exit(wl1271_exit);
1574
1575 MODULE_LICENSE("GPL");
1576 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
1577 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");