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