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