wl1271: workaround to send a disconnect before rejoining
[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         wl->joined = false;
652
653         for (i = 0; i < NUM_TX_QUEUES; i++)
654                 wl->tx_blocks_freed[i] = 0;
655
656         wl1271_debugfs_reset(wl);
657         mutex_unlock(&wl->mutex);
658 }
659
660 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
661                                    struct ieee80211_if_init_conf *conf)
662 {
663         struct wl1271 *wl = hw->priv;
664         int ret = 0;
665
666         wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
667                      conf->type, conf->mac_addr);
668
669         mutex_lock(&wl->mutex);
670         if (wl->vif) {
671                 ret = -EBUSY;
672                 goto out;
673         }
674
675         wl->vif = conf->vif;
676
677         switch (conf->type) {
678         case NL80211_IFTYPE_STATION:
679                 wl->bss_type = BSS_TYPE_STA_BSS;
680                 break;
681         case NL80211_IFTYPE_ADHOC:
682                 wl->bss_type = BSS_TYPE_IBSS;
683                 break;
684         default:
685                 ret = -EOPNOTSUPP;
686                 goto out;
687         }
688
689         /* FIXME: what if conf->mac_addr changes? */
690
691 out:
692         mutex_unlock(&wl->mutex);
693         return ret;
694 }
695
696 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
697                                          struct ieee80211_if_init_conf *conf)
698 {
699         struct wl1271 *wl = hw->priv;
700
701         mutex_lock(&wl->mutex);
702         wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
703         wl->vif = NULL;
704         mutex_unlock(&wl->mutex);
705 }
706
707 #if 0
708 static int wl1271_op_config_interface(struct ieee80211_hw *hw,
709                                       struct ieee80211_vif *vif,
710                                       struct ieee80211_if_conf *conf)
711 {
712         struct wl1271 *wl = hw->priv;
713         struct sk_buff *beacon;
714         int ret;
715
716         wl1271_debug(DEBUG_MAC80211, "mac80211 config_interface bssid %pM",
717                      conf->bssid);
718         wl1271_dump_ascii(DEBUG_MAC80211, "ssid: ", conf->ssid,
719                           conf->ssid_len);
720
721         mutex_lock(&wl->mutex);
722
723         ret = wl1271_ps_elp_wakeup(wl, false);
724         if (ret < 0)
725                 goto out;
726
727         memcpy(wl->bssid, conf->bssid, ETH_ALEN);
728
729         ret = wl1271_cmd_build_null_data(wl);
730         if (ret < 0)
731                 goto out_sleep;
732
733         wl->ssid_len = conf->ssid_len;
734         if (wl->ssid_len)
735                 memcpy(wl->ssid, conf->ssid, wl->ssid_len);
736
737         if (wl->bss_type != BSS_TYPE_IBSS) {
738                 ret = wl1271_cmd_join(wl);
739                 if (ret < 0)
740                         goto out_sleep;
741         }
742
743         if (conf->changed & IEEE80211_IFCC_BEACON) {
744                 beacon = ieee80211_beacon_get(hw, vif);
745                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
746                                               beacon->data, beacon->len);
747
748                 if (ret < 0) {
749                         dev_kfree_skb(beacon);
750                         goto out_sleep;
751                 }
752
753                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PROBE_RESPONSE,
754                                               beacon->data, beacon->len);
755
756                 dev_kfree_skb(beacon);
757
758                 if (ret < 0)
759                         goto out_sleep;
760
761                 ret = wl1271_cmd_join(wl);
762
763                 if (ret < 0)
764                         goto out_sleep;
765         }
766
767 out_sleep:
768         wl1271_ps_elp_sleep(wl);
769
770 out:
771         mutex_unlock(&wl->mutex);
772
773         return ret;
774 }
775 #endif
776
777 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
778 {
779         struct wl1271 *wl = hw->priv;
780         struct ieee80211_conf *conf = &hw->conf;
781         int channel, ret = 0;
782
783         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
784
785         wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
786                      channel,
787                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
788                      conf->power_level);
789
790         mutex_lock(&wl->mutex);
791
792         wl->band = conf->channel->band;
793
794         ret = wl1271_ps_elp_wakeup(wl, false);
795         if (ret < 0)
796                 goto out;
797
798         if (channel != wl->channel) {
799                 u8 old_channel = wl->channel;
800                 wl->channel = channel;
801
802                 ret = wl1271_cmd_join(wl);
803                 if (ret < 0) {
804                         wl->channel = old_channel;
805                         goto out_sleep;
806                 }
807         }
808
809         ret = wl1271_cmd_build_null_data(wl);
810         if (ret < 0)
811                 goto out_sleep;
812
813         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
814                 wl1271_info("psm enabled");
815
816                 wl->psm_requested = true;
817
818                 /*
819                  * We enter PSM only if we're already associated.
820                  * If we're not, we'll enter it when joining an SSID,
821                  * through the bss_info_changed() hook.
822                  */
823                 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
824         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
825                    wl->psm_requested) {
826                 wl1271_info("psm disabled");
827
828                 wl->psm_requested = false;
829
830                 if (wl->psm)
831                         ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE);
832         }
833
834         if (conf->power_level != wl->power_level) {
835                 ret = wl1271_acx_tx_power(wl, conf->power_level);
836                 if (ret < 0)
837                         goto out;
838
839                 wl->power_level = conf->power_level;
840         }
841
842 out_sleep:
843         wl1271_ps_elp_sleep(wl);
844
845 out:
846         mutex_unlock(&wl->mutex);
847
848         return ret;
849 }
850
851 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
852                                        struct dev_addr_list *mc_list)
853 {
854         struct wl1271 *wl = hw->priv;
855         struct wl1271_filter_params *fp;
856         unsigned long flags;
857         int i;
858
859         /*
860          * FIXME: we should return a hash that will be passed to
861          * configure_filter() instead of saving everything in the context.
862          */
863
864         fp = kzalloc(sizeof(*fp), GFP_KERNEL);
865         if (!fp) {
866                 wl1271_error("Out of memory setting filters.");
867                 return 0;
868         }
869
870         /* update multicast filtering parameters */
871         if (mc_count > ACX_MC_ADDRESS_GROUP_MAX) {
872                 mc_count = 0;
873                 fp->filters |= FIF_ALLMULTI;
874         }
875
876         fp->mc_list_length = 0;
877         for (i = 0; i < mc_count; i++) {
878                 if (mc_list->da_addrlen == ETH_ALEN) {
879                         memcpy(fp->mc_list[fp->mc_list_length],
880                                mc_list->da_addr, ETH_ALEN);
881                         fp->mc_list_length++;
882                 } else
883                         wl1271_warning("Unknown mc address length.");
884         }
885
886         spin_lock_irqsave(&wl->wl_lock, flags);
887         kfree(wl->filter_params);
888         wl->filter_params = fp;
889         spin_unlock_irqrestore(&wl->wl_lock, flags);
890
891         return 1;
892 }
893
894 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
895                                        unsigned int changed,
896                                        unsigned int *total, u64 multicast)
897 {
898         struct wl1271 *wl = hw->priv;
899
900         wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
901
902         *total &= WL1271_SUPPORTED_FILTERS;
903         changed &= WL1271_SUPPORTED_FILTERS;
904
905         if (!multicast)
906                 return;
907
908         /*
909          * FIXME: for now we are still using a workqueue for filter
910          * configuration, but with the new mac80211, this is not needed,
911          * since configure_filter can now sleep.  We now have
912          * prepare_multicast, which needs to be atomic instead.
913          */
914
915         /* store current filter config */
916         wl->filter_params->filters = *total;
917         wl->filter_params->changed = changed;
918
919         ieee80211_queue_work(wl->hw, &wl->filter_work);
920 }
921
922 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
923                              struct ieee80211_vif *vif,
924                              struct ieee80211_sta *sta,
925                              struct ieee80211_key_conf *key_conf)
926 {
927         struct wl1271 *wl = hw->priv;
928         const u8 *addr;
929         int ret;
930         u32 tx_seq_32 = 0;
931         u16 tx_seq_16 = 0;
932         u8 key_type;
933
934         static const u8 bcast_addr[ETH_ALEN] =
935                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
936
937         wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
938
939         addr = sta ? sta->addr : bcast_addr;
940
941         wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
942         wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
943         wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
944                      key_conf->alg, key_conf->keyidx,
945                      key_conf->keylen, key_conf->flags);
946         wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
947
948         if (is_zero_ether_addr(addr)) {
949                 /* We dont support TX only encryption */
950                 ret = -EOPNOTSUPP;
951                 goto out;
952         }
953
954         mutex_lock(&wl->mutex);
955
956         ret = wl1271_ps_elp_wakeup(wl, false);
957         if (ret < 0)
958                 goto out_unlock;
959
960         switch (key_conf->alg) {
961         case ALG_WEP:
962                 key_type = KEY_WEP;
963
964                 key_conf->hw_key_idx = key_conf->keyidx;
965                 break;
966         case ALG_TKIP:
967                 key_type = KEY_TKIP;
968
969                 key_conf->hw_key_idx = key_conf->keyidx;
970                 tx_seq_32 = wl->tx_security_seq_32;
971                 tx_seq_16 = wl->tx_security_seq_16;
972                 break;
973         case ALG_CCMP:
974                 key_type = KEY_AES;
975
976                 key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
977                 tx_seq_32 = wl->tx_security_seq_32;
978                 tx_seq_16 = wl->tx_security_seq_16;
979                 break;
980         default:
981                 wl1271_error("Unknown key algo 0x%x", key_conf->alg);
982
983                 ret = -EOPNOTSUPP;
984                 goto out_sleep;
985         }
986
987         switch (cmd) {
988         case SET_KEY:
989                 ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
990                                          key_conf->keyidx, key_type,
991                                          key_conf->keylen, key_conf->key,
992                                          addr, tx_seq_32, tx_seq_16);
993                 if (ret < 0) {
994                         wl1271_error("Could not add or replace key");
995                         goto out_sleep;
996                 }
997                 break;
998
999         case DISABLE_KEY:
1000                 ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
1001                                          key_conf->keyidx, key_type,
1002                                          key_conf->keylen, key_conf->key,
1003                                          addr, 0, 0);
1004                 if (ret < 0) {
1005                         wl1271_error("Could not remove key");
1006                         goto out_sleep;
1007                 }
1008                 break;
1009
1010         default:
1011                 wl1271_error("Unsupported key cmd 0x%x", cmd);
1012                 ret = -EOPNOTSUPP;
1013                 goto out_sleep;
1014
1015                 break;
1016         }
1017
1018 out_sleep:
1019         wl1271_ps_elp_sleep(wl);
1020
1021 out_unlock:
1022         mutex_unlock(&wl->mutex);
1023
1024 out:
1025         return ret;
1026 }
1027
1028 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
1029                              struct cfg80211_scan_request *req)
1030 {
1031         struct wl1271 *wl = hw->priv;
1032         int ret;
1033         u8 *ssid = NULL;
1034         size_t ssid_len = 0;
1035
1036         wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
1037
1038         if (req->n_ssids) {
1039                 ssid = req->ssids[0].ssid;
1040                 ssid_len = req->ssids[0].ssid_len;
1041         }
1042
1043         mutex_lock(&wl->mutex);
1044
1045         ret = wl1271_ps_elp_wakeup(wl, false);
1046         if (ret < 0)
1047                 goto out;
1048
1049         ret = wl1271_cmd_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
1050
1051         wl1271_ps_elp_sleep(wl);
1052
1053 out:
1054         mutex_unlock(&wl->mutex);
1055
1056         return ret;
1057 }
1058
1059 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1060 {
1061         struct wl1271 *wl = hw->priv;
1062         int ret;
1063
1064         mutex_lock(&wl->mutex);
1065
1066         ret = wl1271_ps_elp_wakeup(wl, false);
1067         if (ret < 0)
1068                 goto out;
1069
1070         ret = wl1271_acx_rts_threshold(wl, (u16) value);
1071         if (ret < 0)
1072                 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
1073
1074         wl1271_ps_elp_sleep(wl);
1075
1076 out:
1077         mutex_unlock(&wl->mutex);
1078
1079         return ret;
1080 }
1081
1082 static u32 wl1271_enabled_rates_get(struct wl1271 *wl, u64 basic_rate_set)
1083 {
1084         struct ieee80211_supported_band *band;
1085         u32 enabled_rates = 0;
1086         int bit;
1087
1088         band = wl->hw->wiphy->bands[wl->band];
1089         for (bit = 0; bit < band->n_bitrates; bit++) {
1090                 if (basic_rate_set & 0x1)
1091                         enabled_rates |= band->bitrates[bit].hw_value;
1092                 basic_rate_set >>= 1;
1093         }
1094
1095         return enabled_rates;
1096 }
1097
1098 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
1099                                        struct ieee80211_vif *vif,
1100                                        struct ieee80211_bss_conf *bss_conf,
1101                                        u32 changed)
1102 {
1103         enum wl1271_cmd_ps_mode mode;
1104         struct wl1271 *wl = hw->priv;
1105         int ret;
1106
1107         wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1108
1109         mutex_lock(&wl->mutex);
1110
1111         ret = wl1271_ps_elp_wakeup(wl, false);
1112         if (ret < 0)
1113                 goto out;
1114
1115         if (changed & BSS_CHANGED_ASSOC) {
1116                 if (bss_conf->assoc) {
1117                         wl->beacon_int = bss_conf->beacon_int;
1118                         wl->dtim_period = bss_conf->dtim_period;
1119                         wl->aid = bss_conf->aid;
1120
1121                         ret = wl1271_cmd_join(wl);
1122                         if (ret < 0) {
1123                                 wl1271_warning("Association configuration "
1124                                                "failed %d", ret);
1125                                 goto out_sleep;
1126                         }
1127
1128                         ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
1129                         if (ret < 0)
1130                                 goto out_sleep;
1131
1132                         ret = wl1271_acx_aid(wl, wl->aid);
1133                         if (ret < 0)
1134                                 goto out_sleep;
1135
1136                         /* If we want to go in PSM but we're not there yet */
1137                         if (wl->psm_requested && !wl->psm) {
1138                                 mode = STATION_POWER_SAVE_MODE;
1139                                 ret = wl1271_ps_set_mode(wl, mode);
1140                                 if (ret < 0)
1141                                         goto out_sleep;
1142                         }
1143                 } else {
1144                         /* use defaults when not associated */
1145                         wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
1146                         wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
1147                         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1148                         wl->aid = 0;
1149                 }
1150
1151         }
1152
1153         if (changed & BSS_CHANGED_ERP_SLOT) {
1154                 if (bss_conf->use_short_slot)
1155                         ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
1156                 else
1157                         ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
1158                 if (ret < 0) {
1159                         wl1271_warning("Set slot time failed %d", ret);
1160                         goto out_sleep;
1161                 }
1162         }
1163
1164         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1165                 if (bss_conf->use_short_preamble)
1166                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1167                 else
1168                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1169         }
1170
1171         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1172                 if (bss_conf->use_cts_prot)
1173                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1174                 else
1175                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1176                 if (ret < 0) {
1177                         wl1271_warning("Set ctsprotect failed %d", ret);
1178                         goto out_sleep;
1179                 }
1180         }
1181
1182         if (changed & BSS_CHANGED_BASIC_RATES) {
1183                 wl->basic_rate_set = wl1271_enabled_rates_get(
1184                         wl, bss_conf->basic_rates);
1185                 ret = wl1271_acx_rate_policies(wl, wl->basic_rate_set);
1186
1187                 if (ret < 0) {
1188                         wl1271_warning("Set rate policies failed %d", ret);
1189                         goto out_sleep;
1190                 }
1191                 ret = wl1271_cmd_join(wl);
1192                 if (ret < 0) {
1193                         wl1271_warning("Join with new basic rate "
1194                                        "set failed %d", ret);
1195                         goto out_sleep;
1196                 }
1197         }
1198
1199 out_sleep:
1200         wl1271_ps_elp_sleep(wl);
1201
1202 out:
1203         mutex_unlock(&wl->mutex);
1204 }
1205
1206
1207 /* can't be const, mac80211 writes to this */
1208 static struct ieee80211_rate wl1271_rates[] = {
1209         { .bitrate = 10,
1210           .hw_value = 0x1,
1211           .hw_value_short = 0x1, },
1212         { .bitrate = 20,
1213           .hw_value = 0x2,
1214           .hw_value_short = 0x2,
1215           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1216         { .bitrate = 55,
1217           .hw_value = 0x4,
1218           .hw_value_short = 0x4,
1219           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1220         { .bitrate = 110,
1221           .hw_value = 0x20,
1222           .hw_value_short = 0x20,
1223           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1224         { .bitrate = 60,
1225           .hw_value = 0x8,
1226           .hw_value_short = 0x8, },
1227         { .bitrate = 90,
1228           .hw_value = 0x10,
1229           .hw_value_short = 0x10, },
1230         { .bitrate = 120,
1231           .hw_value = 0x40,
1232           .hw_value_short = 0x40, },
1233         { .bitrate = 180,
1234           .hw_value = 0x80,
1235           .hw_value_short = 0x80, },
1236         { .bitrate = 240,
1237           .hw_value = 0x200,
1238           .hw_value_short = 0x200, },
1239         { .bitrate = 360,
1240          .hw_value = 0x400,
1241          .hw_value_short = 0x400, },
1242         { .bitrate = 480,
1243           .hw_value = 0x800,
1244           .hw_value_short = 0x800, },
1245         { .bitrate = 540,
1246           .hw_value = 0x1000,
1247           .hw_value_short = 0x1000, },
1248 };
1249
1250 /* can't be const, mac80211 writes to this */
1251 static struct ieee80211_channel wl1271_channels[] = {
1252         { .hw_value = 1, .center_freq = 2412},
1253         { .hw_value = 2, .center_freq = 2417},
1254         { .hw_value = 3, .center_freq = 2422},
1255         { .hw_value = 4, .center_freq = 2427},
1256         { .hw_value = 5, .center_freq = 2432},
1257         { .hw_value = 6, .center_freq = 2437},
1258         { .hw_value = 7, .center_freq = 2442},
1259         { .hw_value = 8, .center_freq = 2447},
1260         { .hw_value = 9, .center_freq = 2452},
1261         { .hw_value = 10, .center_freq = 2457},
1262         { .hw_value = 11, .center_freq = 2462},
1263         { .hw_value = 12, .center_freq = 2467},
1264         { .hw_value = 13, .center_freq = 2472},
1265 };
1266
1267 /* can't be const, mac80211 writes to this */
1268 static struct ieee80211_supported_band wl1271_band_2ghz = {
1269         .channels = wl1271_channels,
1270         .n_channels = ARRAY_SIZE(wl1271_channels),
1271         .bitrates = wl1271_rates,
1272         .n_bitrates = ARRAY_SIZE(wl1271_rates),
1273 };
1274
1275 static const struct ieee80211_ops wl1271_ops = {
1276         .start = wl1271_op_start,
1277         .stop = wl1271_op_stop,
1278         .add_interface = wl1271_op_add_interface,
1279         .remove_interface = wl1271_op_remove_interface,
1280         .config = wl1271_op_config,
1281 /*      .config_interface = wl1271_op_config_interface, */
1282         .prepare_multicast = wl1271_op_prepare_multicast,
1283         .configure_filter = wl1271_op_configure_filter,
1284         .tx = wl1271_op_tx,
1285         .set_key = wl1271_op_set_key,
1286         .hw_scan = wl1271_op_hw_scan,
1287         .bss_info_changed = wl1271_op_bss_info_changed,
1288         .set_rts_threshold = wl1271_op_set_rts_threshold,
1289 };
1290
1291 static int wl1271_register_hw(struct wl1271 *wl)
1292 {
1293         int ret;
1294
1295         if (wl->mac80211_registered)
1296                 return 0;
1297
1298         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1299
1300         ret = ieee80211_register_hw(wl->hw);
1301         if (ret < 0) {
1302                 wl1271_error("unable to register mac80211 hw: %d", ret);
1303                 return ret;
1304         }
1305
1306         wl->mac80211_registered = true;
1307
1308         wl1271_notice("loaded");
1309
1310         return 0;
1311 }
1312
1313 static int wl1271_init_ieee80211(struct wl1271 *wl)
1314 {
1315         /* The tx descriptor buffer and the TKIP space. */
1316         wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
1317                 sizeof(struct wl1271_tx_hw_descr);
1318
1319         /* unit us */
1320         /* FIXME: find a proper value */
1321         wl->hw->channel_change_time = 10000;
1322
1323         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1324                 IEEE80211_HW_NOISE_DBM |
1325                 IEEE80211_HW_BEACON_FILTER;
1326
1327         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1328         wl->hw->wiphy->max_scan_ssids = 1;
1329         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
1330
1331         SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1332
1333         return 0;
1334 }
1335
1336 static void wl1271_device_release(struct device *dev)
1337 {
1338
1339 }
1340
1341 static struct platform_device wl1271_device = {
1342         .name           = "wl1271",
1343         .id             = -1,
1344
1345         /* device model insists to have a release function */
1346         .dev            = {
1347                 .release = wl1271_device_release,
1348         },
1349 };
1350
1351 #define WL1271_DEFAULT_CHANNEL 0
1352 static int __devinit wl1271_probe(struct spi_device *spi)
1353 {
1354         struct wl12xx_platform_data *pdata;
1355         struct ieee80211_hw *hw;
1356         struct wl1271 *wl;
1357         int ret, i;
1358         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1359
1360         pdata = spi->dev.platform_data;
1361         if (!pdata) {
1362                 wl1271_error("no platform data");
1363                 return -ENODEV;
1364         }
1365
1366         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
1367         if (!hw) {
1368                 wl1271_error("could not alloc ieee80211_hw");
1369                 return -ENOMEM;
1370         }
1371
1372         wl = hw->priv;
1373         memset(wl, 0, sizeof(*wl));
1374
1375         wl->hw = hw;
1376         dev_set_drvdata(&spi->dev, wl);
1377         wl->spi = spi;
1378
1379         skb_queue_head_init(&wl->tx_queue);
1380
1381         INIT_WORK(&wl->filter_work, wl1271_filter_work);
1382         INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
1383         wl->channel = WL1271_DEFAULT_CHANNEL;
1384         wl->scanning = false;
1385         wl->default_key = 0;
1386         wl->listen_int = 1;
1387         wl->rx_counter = 0;
1388         wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
1389         wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
1390         wl->elp = false;
1391         wl->psm = 0;
1392         wl->psm_requested = false;
1393         wl->tx_queue_stopped = false;
1394         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1395         wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
1396         wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
1397         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1398         wl->band = IEEE80211_BAND_2GHZ;
1399         wl->vif = NULL;
1400         wl->joined = false;
1401
1402         for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
1403                 wl->tx_frames[i] = NULL;
1404
1405         spin_lock_init(&wl->wl_lock);
1406
1407         /*
1408          * In case our MAC address is not correctly set,
1409          * we use a random but Nokia MAC.
1410          */
1411         memcpy(wl->mac_addr, nokia_oui, 3);
1412         get_random_bytes(wl->mac_addr + 3, 3);
1413
1414         wl->state = WL1271_STATE_OFF;
1415         mutex_init(&wl->mutex);
1416
1417         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1418         if (!wl->rx_descriptor) {
1419                 wl1271_error("could not allocate memory for rx descriptor");
1420                 ret = -ENOMEM;
1421                 goto out_free;
1422         }
1423
1424         /* This is the only SPI value that we need to set here, the rest
1425          * comes from the board-peripherals file */
1426         spi->bits_per_word = 32;
1427
1428         ret = spi_setup(spi);
1429         if (ret < 0) {
1430                 wl1271_error("spi_setup failed");
1431                 goto out_free;
1432         }
1433
1434         wl->set_power = pdata->set_power;
1435         if (!wl->set_power) {
1436                 wl1271_error("set power function missing in platform data");
1437                 ret = -ENODEV;
1438                 goto out_free;
1439         }
1440
1441         wl->irq = spi->irq;
1442         if (wl->irq < 0) {
1443                 wl1271_error("irq missing in platform data");
1444                 ret = -ENODEV;
1445                 goto out_free;
1446         }
1447
1448         ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
1449         if (ret < 0) {
1450                 wl1271_error("request_irq() failed: %d", ret);
1451                 goto out_free;
1452         }
1453
1454         set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1455
1456         disable_irq(wl->irq);
1457
1458         ret = platform_device_register(&wl1271_device);
1459         if (ret) {
1460                 wl1271_error("couldn't register platform device");
1461                 goto out_irq;
1462         }
1463         dev_set_drvdata(&wl1271_device.dev, wl);
1464
1465         ret = wl1271_init_ieee80211(wl);
1466         if (ret)
1467                 goto out_platform;
1468
1469         ret = wl1271_register_hw(wl);
1470         if (ret)
1471                 goto out_platform;
1472
1473         wl1271_debugfs_init(wl);
1474
1475         wl1271_notice("initialized");
1476
1477         return 0;
1478
1479  out_platform:
1480         platform_device_unregister(&wl1271_device);
1481
1482  out_irq:
1483         free_irq(wl->irq, wl);
1484
1485  out_free:
1486         kfree(wl->rx_descriptor);
1487         wl->rx_descriptor = NULL;
1488
1489         ieee80211_free_hw(hw);
1490
1491         return ret;
1492 }
1493
1494 static int __devexit wl1271_remove(struct spi_device *spi)
1495 {
1496         struct wl1271 *wl = dev_get_drvdata(&spi->dev);
1497
1498         ieee80211_unregister_hw(wl->hw);
1499
1500         wl1271_debugfs_exit(wl);
1501         platform_device_unregister(&wl1271_device);
1502         free_irq(wl->irq, wl);
1503         kfree(wl->target_mem_map);
1504         vfree(wl->fw);
1505         wl->fw = NULL;
1506         kfree(wl->nvs);
1507         wl->nvs = NULL;
1508
1509         kfree(wl->rx_descriptor);
1510         wl->rx_descriptor = NULL;
1511
1512         kfree(wl->fw_status);
1513         kfree(wl->tx_res_if);
1514
1515         ieee80211_free_hw(wl->hw);
1516
1517         return 0;
1518 }
1519
1520
1521 static struct spi_driver wl1271_spi_driver = {
1522         .driver = {
1523                 .name           = "wl1271",
1524                 .bus            = &spi_bus_type,
1525                 .owner          = THIS_MODULE,
1526         },
1527
1528         .probe          = wl1271_probe,
1529         .remove         = __devexit_p(wl1271_remove),
1530 };
1531
1532 static int __init wl1271_init(void)
1533 {
1534         int ret;
1535
1536         ret = spi_register_driver(&wl1271_spi_driver);
1537         if (ret < 0) {
1538                 wl1271_error("failed to register spi driver: %d", ret);
1539                 goto out;
1540         }
1541
1542 out:
1543         return ret;
1544 }
1545
1546 static void __exit wl1271_exit(void)
1547 {
1548         spi_unregister_driver(&wl1271_spi_driver);
1549
1550         wl1271_notice("unloaded");
1551 }
1552
1553 module_init(wl1271_init);
1554 module_exit(wl1271_exit);
1555
1556 MODULE_LICENSE("GPL");
1557 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
1558 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");