]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/wl12xx/main.c
wl12xx: check if elp wakeup failed
[karo-tx-linux.git] / drivers / net / wireless / wl12xx / main.c
1 /*
2  * This file is part of wl12xx
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Contact: Kalle Valo <kalle.valo@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/interrupt.h>
26 #include <linux/firmware.h>
27 #include <linux/delay.h>
28 #include <linux/irq.h>
29 #include <linux/spi/spi.h>
30 #include <linux/crc32.h>
31 #include <linux/etherdevice.h>
32 #include <linux/spi/wl12xx.h>
33
34 #include "wl12xx.h"
35 #include "wl12xx_80211.h"
36 #include "reg.h"
37 #include "wl1251.h"
38 #include "spi.h"
39 #include "event.h"
40 #include "wl1251_tx.h"
41 #include "rx.h"
42 #include "ps.h"
43 #include "init.h"
44 #include "debugfs.h"
45
46 static void wl12xx_disable_interrupts(struct wl12xx *wl)
47 {
48         disable_irq(wl->irq);
49 }
50
51 static void wl12xx_power_off(struct wl12xx *wl)
52 {
53         wl->set_power(false);
54 }
55
56 static void wl12xx_power_on(struct wl12xx *wl)
57 {
58         wl->set_power(true);
59 }
60
61 static irqreturn_t wl12xx_irq(int irq, void *cookie)
62 {
63         struct wl12xx *wl;
64
65         wl12xx_debug(DEBUG_IRQ, "IRQ");
66
67         wl = cookie;
68
69         schedule_work(&wl->irq_work);
70
71         return IRQ_HANDLED;
72 }
73
74 static int wl12xx_fetch_firmware(struct wl12xx *wl)
75 {
76         const struct firmware *fw;
77         int ret;
78
79         ret = request_firmware(&fw, wl->chip.fw_filename, &wl->spi->dev);
80
81         if (ret < 0) {
82                 wl12xx_error("could not get firmware: %d", ret);
83                 return ret;
84         }
85
86         if (fw->size % 4) {
87                 wl12xx_error("firmware size is not multiple of 32 bits: %zu",
88                              fw->size);
89                 ret = -EILSEQ;
90                 goto out;
91         }
92
93         wl->fw_len = fw->size;
94         wl->fw = kmalloc(wl->fw_len, GFP_KERNEL);
95
96         if (!wl->fw) {
97                 wl12xx_error("could not allocate memory for the firmware");
98                 ret = -ENOMEM;
99                 goto out;
100         }
101
102         memcpy(wl->fw, fw->data, wl->fw_len);
103
104         ret = 0;
105
106 out:
107         release_firmware(fw);
108
109         return ret;
110 }
111
112 static int wl12xx_fetch_nvs(struct wl12xx *wl)
113 {
114         const struct firmware *fw;
115         int ret;
116
117         ret = request_firmware(&fw, wl->chip.nvs_filename, &wl->spi->dev);
118
119         if (ret < 0) {
120                 wl12xx_error("could not get nvs file: %d", ret);
121                 return ret;
122         }
123
124         if (fw->size % 4) {
125                 wl12xx_error("nvs size is not multiple of 32 bits: %zu",
126                              fw->size);
127                 ret = -EILSEQ;
128                 goto out;
129         }
130
131         wl->nvs_len = fw->size;
132         wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
133
134         if (!wl->nvs) {
135                 wl12xx_error("could not allocate memory for the nvs file");
136                 ret = -ENOMEM;
137                 goto out;
138         }
139
140         memcpy(wl->nvs, fw->data, wl->nvs_len);
141
142         ret = 0;
143
144 out:
145         release_firmware(fw);
146
147         return ret;
148 }
149
150 static void wl12xx_fw_wakeup(struct wl12xx *wl)
151 {
152         u32 elp_reg;
153
154         elp_reg = ELPCTRL_WAKE_UP;
155         wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
156         elp_reg = wl12xx_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
157
158         if (!(elp_reg & ELPCTRL_WLAN_READY)) {
159                 wl12xx_warning("WLAN not ready");
160         }
161 }
162
163 static int wl12xx_chip_wakeup(struct wl12xx *wl)
164 {
165         int ret = 0;
166
167         wl12xx_power_on(wl);
168         msleep(wl->chip.power_on_sleep);
169         wl12xx_spi_reset(wl);
170         wl12xx_spi_init(wl);
171
172         /* We don't need a real memory partition here, because we only want
173          * to use the registers at this point. */
174         wl12xx_set_partition(wl,
175                              0x00000000,
176                              0x00000000,
177                              REGISTERS_BASE,
178                              REGISTERS_DOWN_SIZE);
179
180         /* ELP module wake up */
181         wl12xx_fw_wakeup(wl);
182
183         /* whal_FwCtrl_BootSm() */
184
185         /* 0. read chip id from CHIP_ID */
186         wl->chip.id = wl12xx_reg_read32(wl, CHIP_ID_B);
187
188         /* 1. check if chip id is valid */
189
190         switch (wl->chip.id) {
191         case CHIP_ID_1251_PG12:
192                 wl12xx_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
193                              wl->chip.id);
194
195                 wl1251_setup(wl);
196
197                 break;
198         case CHIP_ID_1271_PG10:
199                 wl12xx_warning("chip id 0x%x (1271 PG10) support is obsolete",
200                                wl->chip.id);
201                 break;
202         case CHIP_ID_1271_PG20:
203                 wl12xx_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
204                              wl->chip.id);
205                 break;
206         case CHIP_ID_1251_PG10:
207         case CHIP_ID_1251_PG11:
208         default:
209                 wl12xx_error("unsupported chip id: 0x%x", wl->chip.id);
210                 ret = -ENODEV;
211                 goto out;
212         }
213
214         if (wl->fw == NULL) {
215                 ret = wl12xx_fetch_firmware(wl);
216                 if (ret < 0)
217                         goto out;
218         }
219
220         /* No NVS from netlink, try to get it from the filesystem */
221         if (wl->nvs == NULL) {
222                 ret = wl12xx_fetch_nvs(wl);
223                 if (ret < 0)
224                         goto out;
225         }
226
227 out:
228         return ret;
229 }
230
231 static void wl12xx_filter_work(struct work_struct *work)
232 {
233         struct wl12xx *wl =
234                 container_of(work, struct wl12xx, filter_work);
235         int ret;
236
237         mutex_lock(&wl->mutex);
238
239         if (wl->state == WL12XX_STATE_OFF)
240                 goto out;
241
242         ret = wl12xx_ps_elp_wakeup(wl);
243         if (ret < 0)
244                 goto out;
245
246         ret = wl12xx_cmd_join(wl, wl->bss_type, 1, 100, 0);
247         if (ret < 0)
248                 goto out_sleep;
249
250 out_sleep:
251         wl12xx_ps_elp_sleep(wl);
252
253 out:
254         mutex_unlock(&wl->mutex);
255 }
256
257 int wl12xx_plt_start(struct wl12xx *wl)
258 {
259         int ret;
260
261         wl12xx_notice("power up");
262
263         if (wl->state != WL12XX_STATE_OFF) {
264                 wl12xx_error("cannot go into PLT state because not "
265                              "in off state: %d", wl->state);
266                 return -EBUSY;
267         }
268
269         wl->state = WL12XX_STATE_PLT;
270
271         ret = wl12xx_chip_wakeup(wl);
272         if (ret < 0)
273                 return ret;
274
275         ret = wl->chip.op_boot(wl);
276         if (ret < 0)
277                 return ret;
278
279         wl12xx_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
280
281         ret = wl->chip.op_plt_init(wl);
282         if (ret < 0)
283                 return ret;
284
285         return 0;
286 }
287
288 int wl12xx_plt_stop(struct wl12xx *wl)
289 {
290         wl12xx_notice("power down");
291
292         if (wl->state != WL12XX_STATE_PLT) {
293                 wl12xx_error("cannot power down because not in PLT "
294                              "state: %d", wl->state);
295                 return -EBUSY;
296         }
297
298         wl12xx_disable_interrupts(wl);
299         wl12xx_power_off(wl);
300
301         wl->state = WL12XX_STATE_OFF;
302
303         return 0;
304 }
305
306
307 static int wl12xx_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
308 {
309         struct wl12xx *wl = hw->priv;
310
311         skb_queue_tail(&wl->tx_queue, skb);
312
313         /*
314          * The chip specific setup must run before the first TX packet -
315          * before that, the tx_work will not be initialized!
316          */
317
318         schedule_work(&wl->tx_work);
319
320         /*
321          * The workqueue is slow to process the tx_queue and we need stop
322          * the queue here, otherwise the queue will get too long.
323          */
324         if (skb_queue_len(&wl->tx_queue) >= WL12XX_TX_QUEUE_MAX_LENGTH) {
325                 ieee80211_stop_queues(wl->hw);
326
327                 /*
328                  * FIXME: this is racy, the variable is not properly
329                  * protected. Maybe fix this by removing the stupid
330                  * variable altogether and checking the real queue state?
331                  */
332                 wl->tx_queue_stopped = true;
333         }
334
335         return NETDEV_TX_OK;
336 }
337
338 static int wl12xx_op_start(struct ieee80211_hw *hw)
339 {
340         struct wl12xx *wl = hw->priv;
341         int ret = 0;
342
343         wl12xx_debug(DEBUG_MAC80211, "mac80211 start");
344
345         mutex_lock(&wl->mutex);
346
347         if (wl->state != WL12XX_STATE_OFF) {
348                 wl12xx_error("cannot start because not in off state: %d",
349                              wl->state);
350                 ret = -EBUSY;
351                 goto out;
352         }
353
354         ret = wl12xx_chip_wakeup(wl);
355         if (ret < 0)
356                 return ret;
357
358         ret = wl->chip.op_boot(wl);
359         if (ret < 0)
360                 goto out;
361
362         ret = wl->chip.op_hw_init(wl);
363         if (ret < 0)
364                 goto out;
365
366         ret = wl12xx_acx_station_id(wl);
367         if (ret < 0)
368                 goto out;
369
370         wl->state = WL12XX_STATE_ON;
371
372         wl12xx_info("firmware booted (%s)", wl->chip.fw_ver);
373
374 out:
375         if (ret < 0)
376                 wl12xx_power_off(wl);
377
378         mutex_unlock(&wl->mutex);
379
380         return ret;
381 }
382
383 static void wl12xx_op_stop(struct ieee80211_hw *hw)
384 {
385         struct wl12xx *wl = hw->priv;
386
387         wl12xx_info("down");
388
389         wl12xx_debug(DEBUG_MAC80211, "mac80211 stop");
390
391         mutex_lock(&wl->mutex);
392
393         WARN_ON(wl->state != WL12XX_STATE_ON);
394
395         if (wl->scanning) {
396                 mutex_unlock(&wl->mutex);
397                 ieee80211_scan_completed(wl->hw, true);
398                 mutex_lock(&wl->mutex);
399                 wl->scanning = false;
400         }
401
402         wl->state = WL12XX_STATE_OFF;
403
404         wl12xx_disable_interrupts(wl);
405
406         mutex_unlock(&wl->mutex);
407
408         cancel_work_sync(&wl->irq_work);
409         cancel_work_sync(&wl->tx_work);
410         cancel_work_sync(&wl->filter_work);
411
412         mutex_lock(&wl->mutex);
413
414         /* let's notify MAC80211 about the remaining pending TX frames */
415         wl->chip.op_tx_flush(wl);
416         wl12xx_power_off(wl);
417
418         memset(wl->bssid, 0, ETH_ALEN);
419         wl->listen_int = 1;
420         wl->bss_type = MAX_BSS_TYPE;
421
422         wl->data_in_count = 0;
423         wl->rx_counter = 0;
424         wl->rx_handled = 0;
425         wl->rx_current_buffer = 0;
426         wl->rx_last_id = 0;
427         wl->next_tx_complete = 0;
428         wl->elp = false;
429         wl->psm = 0;
430         wl->tx_queue_stopped = false;
431         wl->power_level = WL12XX_DEFAULT_POWER_LEVEL;
432
433         wl12xx_debugfs_reset(wl);
434
435         mutex_unlock(&wl->mutex);
436 }
437
438 static int wl12xx_op_add_interface(struct ieee80211_hw *hw,
439                                    struct ieee80211_if_init_conf *conf)
440 {
441         struct wl12xx *wl = hw->priv;
442         DECLARE_MAC_BUF(mac);
443         int ret = 0;
444
445         wl12xx_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %s",
446                      conf->type, print_mac(mac, conf->mac_addr));
447
448         mutex_lock(&wl->mutex);
449
450         switch (conf->type) {
451         case NL80211_IFTYPE_STATION:
452                 wl->bss_type = BSS_TYPE_STA_BSS;
453                 break;
454         case NL80211_IFTYPE_ADHOC:
455                 wl->bss_type = BSS_TYPE_IBSS;
456                 break;
457         default:
458                 ret = -EOPNOTSUPP;
459                 goto out;
460         }
461
462         if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
463                 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
464                 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
465                 ret = wl12xx_acx_station_id(wl);
466                 if (ret < 0)
467                         goto out;
468         }
469
470 out:
471         mutex_unlock(&wl->mutex);
472         return ret;
473 }
474
475 static void wl12xx_op_remove_interface(struct ieee80211_hw *hw,
476                                          struct ieee80211_if_init_conf *conf)
477 {
478         wl12xx_debug(DEBUG_MAC80211, "mac80211 remove interface");
479 }
480
481 static int wl12xx_build_null_data(struct wl12xx *wl)
482 {
483         struct wl12xx_null_data_template template;
484
485         if (!is_zero_ether_addr(wl->bssid)) {
486                 memcpy(template.header.da, wl->bssid, ETH_ALEN);
487                 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
488         } else {
489                 memset(template.header.da, 0xff, ETH_ALEN);
490                 memset(template.header.bssid, 0xff, ETH_ALEN);
491         }
492
493         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
494         template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
495                                                 IEEE80211_STYPE_NULLFUNC);
496
497         return wl12xx_cmd_template_set(wl, CMD_NULL_DATA, &template,
498                                        sizeof(template));
499
500 }
501
502 static int wl12xx_build_ps_poll(struct wl12xx *wl, u16 aid)
503 {
504         struct wl12xx_ps_poll_template template;
505
506         memcpy(template.bssid, wl->bssid, ETH_ALEN);
507         memcpy(template.ta, wl->mac_addr, ETH_ALEN);
508         template.aid = aid;
509         template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
510
511         return wl12xx_cmd_template_set(wl, CMD_PS_POLL, &template,
512                                        sizeof(template));
513
514 }
515
516 static int wl12xx_op_config(struct ieee80211_hw *hw, u32 changed)
517 {
518         struct wl12xx *wl = hw->priv;
519         struct ieee80211_conf *conf = &hw->conf;
520         int channel, ret = 0;
521
522         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
523
524         wl12xx_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
525                      channel,
526                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
527                      conf->power_level);
528
529         mutex_lock(&wl->mutex);
530
531         ret = wl12xx_ps_elp_wakeup(wl);
532         if (ret < 0)
533                 goto out;
534
535         if (channel != wl->channel) {
536                 /* FIXME: use beacon interval provided by mac80211 */
537                 ret = wl12xx_cmd_join(wl, wl->bss_type, 1, 100, 0);
538                 if (ret < 0)
539                         goto out_sleep;
540
541                 wl->channel = channel;
542         }
543
544         ret = wl12xx_build_null_data(wl);
545         if (ret < 0)
546                 goto out_sleep;
547
548         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
549                 wl12xx_info("psm enabled");
550
551                 wl->psm_requested = true;
552
553                 /*
554                  * We enter PSM only if we're already associated.
555                  * If we're not, we'll enter it when joining an SSID,
556                  * through the bss_info_changed() hook.
557                  */
558                 ret = wl12xx_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
559         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
560                    wl->psm_requested) {
561                 wl12xx_info("psm disabled");
562
563                 wl->psm_requested = false;
564
565                 if (wl->psm)
566                         ret = wl12xx_ps_set_mode(wl, STATION_ACTIVE_MODE);
567         }
568
569         if (conf->power_level != wl->power_level) {
570                 ret = wl12xx_acx_tx_power(wl, conf->power_level);
571                 if (ret < 0)
572                         goto out;
573
574                 wl->power_level = conf->power_level;
575         }
576
577 out_sleep:
578         wl12xx_ps_elp_sleep(wl);
579
580 out:
581         mutex_unlock(&wl->mutex);
582
583         return ret;
584 }
585
586 #define WL12XX_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
587                                   FIF_ALLMULTI | \
588                                   FIF_FCSFAIL | \
589                                   FIF_BCN_PRBRESP_PROMISC | \
590                                   FIF_CONTROL | \
591                                   FIF_OTHER_BSS)
592
593 static void wl12xx_op_configure_filter(struct ieee80211_hw *hw,
594                                        unsigned int changed,
595                                        unsigned int *total,
596                                        int mc_count,
597                                        struct dev_addr_list *mc_list)
598 {
599         struct wl12xx *wl = hw->priv;
600
601         wl12xx_debug(DEBUG_MAC80211, "mac80211 configure filter");
602
603         *total &= WL12XX_SUPPORTED_FILTERS;
604         changed &= WL12XX_SUPPORTED_FILTERS;
605
606         if (changed == 0)
607                 /* no filters which we support changed */
608                 return;
609
610         /* FIXME: wl->rx_config and wl->rx_filter are not protected */
611
612         wl->rx_config = WL12XX_DEFAULT_RX_CONFIG;
613         wl->rx_filter = WL12XX_DEFAULT_RX_FILTER;
614
615         if (*total & FIF_PROMISC_IN_BSS) {
616                 wl->rx_config |= CFG_BSSID_FILTER_EN;
617                 wl->rx_config |= CFG_RX_ALL_GOOD;
618         }
619         if (*total & FIF_ALLMULTI)
620                 /*
621                  * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
622                  * all multicast frames
623                  */
624                 wl->rx_config &= ~CFG_MC_FILTER_EN;
625         if (*total & FIF_FCSFAIL)
626                 wl->rx_filter |= CFG_RX_FCS_ERROR;
627         if (*total & FIF_BCN_PRBRESP_PROMISC) {
628                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
629                 wl->rx_config &= ~CFG_SSID_FILTER_EN;
630         }
631         if (*total & FIF_CONTROL)
632                 wl->rx_filter |= CFG_RX_CTL_EN;
633         if (*total & FIF_OTHER_BSS)
634                 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
635
636         /*
637          * FIXME: workqueues need to be properly cancelled on stop(), for
638          * now let's just disable changing the filter settings. They will
639          * be updated any on config().
640          */
641         /* schedule_work(&wl->filter_work); */
642 }
643
644 /* HW encryption */
645 static int wl12xx_set_key_type(struct wl12xx *wl,
646                                struct wl12xx_cmd_set_keys *key,
647                                enum set_key_cmd cmd,
648                                struct ieee80211_key_conf *mac80211_key,
649                                const u8 *addr)
650 {
651         switch (mac80211_key->alg) {
652         case ALG_WEP:
653                 if (is_broadcast_ether_addr(addr))
654                         key->key_type = KEY_WEP_DEFAULT;
655                 else
656                         key->key_type = KEY_WEP_ADDR;
657
658                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
659                 break;
660         case ALG_TKIP:
661                 if (is_broadcast_ether_addr(addr))
662                         key->key_type = KEY_TKIP_MIC_GROUP;
663                 else
664                         key->key_type = KEY_TKIP_MIC_PAIRWISE;
665
666                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
667                 break;
668         case ALG_CCMP:
669                 if (is_broadcast_ether_addr(addr))
670                         key->key_type = KEY_AES_GROUP;
671                 else
672                         key->key_type = KEY_AES_PAIRWISE;
673                 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
674                 break;
675         default:
676                 wl12xx_error("Unknown key algo 0x%x", mac80211_key->alg);
677                 return -EOPNOTSUPP;
678         }
679
680         return 0;
681 }
682
683 static int wl12xx_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
684                              struct ieee80211_vif *vif,
685                              struct ieee80211_sta *sta,
686                              struct ieee80211_key_conf *key)
687 {
688         struct wl12xx *wl = hw->priv;
689         struct wl12xx_cmd_set_keys *wl_cmd;
690         const u8 *addr;
691         int ret;
692
693         static const u8 bcast_addr[ETH_ALEN] =
694                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
695
696         wl12xx_debug(DEBUG_MAC80211, "mac80211 set key");
697
698         wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
699         if (!wl_cmd) {
700                 ret = -ENOMEM;
701                 goto out;
702         }
703
704         addr = sta ? sta->addr : bcast_addr;
705
706         wl12xx_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
707         wl12xx_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
708         wl12xx_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
709                      key->alg, key->keyidx, key->keylen, key->flags);
710         wl12xx_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
711
712         if (is_zero_ether_addr(addr)) {
713                 /* We dont support TX only encryption */
714                 ret = -EOPNOTSUPP;
715                 goto out;
716         }
717
718         mutex_lock(&wl->mutex);
719
720         ret = wl12xx_ps_elp_wakeup(wl);
721         if (ret < 0)
722                 goto out_unlock;
723
724         switch (cmd) {
725         case SET_KEY:
726                 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
727                 break;
728         case DISABLE_KEY:
729                 wl_cmd->key_action = KEY_REMOVE;
730                 break;
731         default:
732                 wl12xx_error("Unsupported key cmd 0x%x", cmd);
733                 break;
734         }
735
736         ret = wl12xx_set_key_type(wl, wl_cmd, cmd, key, addr);
737         if (ret < 0) {
738                 wl12xx_error("Set KEY type failed");
739                 goto out_sleep;
740         }
741
742         if (wl_cmd->key_type != KEY_WEP_DEFAULT)
743                 memcpy(wl_cmd->addr, addr, ETH_ALEN);
744
745         if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
746             (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
747                 /*
748                  * We get the key in the following form:
749                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
750                  * but the target is expecting:
751                  * TKIP - RX MIC - TX MIC
752                  */
753                 memcpy(wl_cmd->key, key->key, 16);
754                 memcpy(wl_cmd->key + 16, key->key + 24, 8);
755                 memcpy(wl_cmd->key + 24, key->key + 16, 8);
756
757         } else {
758                 memcpy(wl_cmd->key, key->key, key->keylen);
759         }
760         wl_cmd->key_size = key->keylen;
761
762         wl_cmd->id = key->keyidx;
763         wl_cmd->ssid_profile = 0;
764
765         wl12xx_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
766
767         ret = wl12xx_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
768         if (ret < 0) {
769                 wl12xx_warning("could not set keys");
770                 goto out_sleep;
771         }
772
773 out_sleep:
774         wl12xx_ps_elp_sleep(wl);
775
776 out_unlock:
777         mutex_unlock(&wl->mutex);
778
779 out:
780         kfree(wl_cmd);
781
782         return ret;
783 }
784
785 static int wl12xx_build_basic_rates(char *rates)
786 {
787         u8 index = 0;
788
789         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
790         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
791         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
792         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
793
794         return index;
795 }
796
797 static int wl12xx_build_extended_rates(char *rates)
798 {
799         u8 index = 0;
800
801         rates[index++] = IEEE80211_OFDM_RATE_6MB;
802         rates[index++] = IEEE80211_OFDM_RATE_9MB;
803         rates[index++] = IEEE80211_OFDM_RATE_12MB;
804         rates[index++] = IEEE80211_OFDM_RATE_18MB;
805         rates[index++] = IEEE80211_OFDM_RATE_24MB;
806         rates[index++] = IEEE80211_OFDM_RATE_36MB;
807         rates[index++] = IEEE80211_OFDM_RATE_48MB;
808         rates[index++] = IEEE80211_OFDM_RATE_54MB;
809
810         return index;
811 }
812
813
814 static int wl12xx_build_probe_req(struct wl12xx *wl, u8 *ssid, size_t ssid_len)
815 {
816         struct wl12xx_probe_req_template template;
817         struct wl12xx_ie_rates *rates;
818         char *ptr;
819         u16 size;
820
821         ptr = (char *)&template;
822         size = sizeof(struct ieee80211_header);
823
824         memset(template.header.da, 0xff, ETH_ALEN);
825         memset(template.header.bssid, 0xff, ETH_ALEN);
826         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
827         template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
828
829         /* IEs */
830         /* SSID */
831         template.ssid.header.id = WLAN_EID_SSID;
832         template.ssid.header.len = ssid_len;
833         if (ssid_len && ssid)
834                 memcpy(template.ssid.ssid, ssid, ssid_len);
835         size += sizeof(struct wl12xx_ie_header) + ssid_len;
836         ptr += size;
837
838         /* Basic Rates */
839         rates = (struct wl12xx_ie_rates *)ptr;
840         rates->header.id = WLAN_EID_SUPP_RATES;
841         rates->header.len = wl12xx_build_basic_rates(rates->rates);
842         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
843         ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
844
845         /* Extended rates */
846         rates = (struct wl12xx_ie_rates *)ptr;
847         rates->header.id = WLAN_EID_EXT_SUPP_RATES;
848         rates->header.len = wl12xx_build_extended_rates(rates->rates);
849         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
850
851         wl12xx_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
852
853         return wl12xx_cmd_template_set(wl, CMD_PROBE_REQ, &template,
854                                       size);
855 }
856
857 static int wl12xx_hw_scan(struct wl12xx *wl, u8 *ssid, size_t len,
858                           u8 active_scan, u8 high_prio, u8 num_channels,
859                           u8 probe_requests)
860 {
861         struct wl12xx_cmd_trigger_scan_to *trigger = NULL;
862         struct cmd_scan *params = NULL;
863         int i, ret;
864         u16 scan_options = 0;
865
866         if (wl->scanning)
867                 return -EINVAL;
868
869         params = kzalloc(sizeof(*params), GFP_KERNEL);
870         if (!params)
871                 return -ENOMEM;
872
873         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
874         params->params.rx_filter_options =
875                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
876
877         /* High priority scan */
878         if (!active_scan)
879                 scan_options |= SCAN_PASSIVE;
880         if (high_prio)
881                 scan_options |= SCAN_PRIORITY_HIGH;
882         params->params.scan_options = scan_options;
883
884         params->params.num_channels = num_channels;
885         params->params.num_probe_requests = probe_requests;
886         params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
887         params->params.tid_trigger = 0;
888
889         for (i = 0; i < num_channels; i++) {
890                 params->channels[i].min_duration = cpu_to_le32(30000);
891                 params->channels[i].max_duration = cpu_to_le32(60000);
892                 memset(&params->channels[i].bssid_lsb, 0xff, 4);
893                 memset(&params->channels[i].bssid_msb, 0xff, 2);
894                 params->channels[i].early_termination = 0;
895                 params->channels[i].tx_power_att = 0;
896                 params->channels[i].channel = i + 1;
897                 memset(params->channels[i].pad, 0, 3);
898         }
899
900         for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
901                 memset(&params->channels[i], 0,
902                        sizeof(struct basic_scan_channel_parameters));
903
904         if (len && ssid) {
905                 params->params.ssid_len = len;
906                 memcpy(params->params.ssid, ssid, len);
907         } else {
908                 params->params.ssid_len = 0;
909                 memset(params->params.ssid, 0, 32);
910         }
911
912         ret = wl12xx_build_probe_req(wl, ssid, len);
913         if (ret < 0) {
914                 wl12xx_error("PROBE request template failed");
915                 goto out;
916         }
917
918         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
919         if (!trigger)
920                 goto out;
921
922         trigger->timeout = 0;
923
924         ret = wl12xx_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
925                               sizeof(*trigger));
926         if (ret < 0) {
927                 wl12xx_error("trigger scan to failed for hw scan");
928                 goto out;
929         }
930
931         wl12xx_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
932
933         wl->scanning = true;
934
935         ret = wl12xx_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
936         if (ret < 0)
937                 wl12xx_error("SCAN failed");
938
939         wl12xx_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
940
941         if (params->header.status != CMD_STATUS_SUCCESS) {
942                 wl12xx_error("TEST command answer error: %d",
943                              params->header.status);
944                 wl->scanning = false;
945                 ret = -EIO;
946                 goto out;
947         }
948
949 out:
950         kfree(params);
951         return ret;
952
953 }
954
955 static int wl12xx_op_hw_scan(struct ieee80211_hw *hw,
956                              struct cfg80211_scan_request *req)
957 {
958         struct wl12xx *wl = hw->priv;
959         int ret;
960         u8 *ssid = NULL;
961         size_t ssid_len = 0;
962
963         wl12xx_debug(DEBUG_MAC80211, "mac80211 hw scan");
964
965         if (req->n_ssids) {
966                 ssid = req->ssids[0].ssid;
967                 ssid_len = req->ssids[0].ssid_len;
968         }
969
970         mutex_lock(&wl->mutex);
971
972         ret = wl12xx_ps_elp_wakeup(wl);
973         if (ret < 0)
974                 goto out;
975
976         ret = wl12xx_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
977
978         wl12xx_ps_elp_sleep(wl);
979
980 out:
981         mutex_unlock(&wl->mutex);
982
983         return ret;
984 }
985
986 static int wl12xx_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
987 {
988         struct wl12xx *wl = hw->priv;
989         int ret;
990
991         mutex_lock(&wl->mutex);
992
993         ret = wl12xx_ps_elp_wakeup(wl);
994         if (ret < 0)
995                 goto out;
996
997         ret = wl12xx_acx_rts_threshold(wl, (u16) value);
998         if (ret < 0)
999                 wl12xx_warning("wl12xx_op_set_rts_threshold failed: %d", ret);
1000
1001         wl12xx_ps_elp_sleep(wl);
1002
1003 out:
1004         mutex_unlock(&wl->mutex);
1005
1006         return ret;
1007 }
1008
1009 static void wl12xx_op_bss_info_changed(struct ieee80211_hw *hw,
1010                                        struct ieee80211_vif *vif,
1011                                        struct ieee80211_bss_conf *bss_conf,
1012                                        u32 changed)
1013 {
1014         enum wl12xx_cmd_ps_mode mode;
1015         struct wl12xx *wl = hw->priv;
1016         struct sk_buff *beacon;
1017         int ret;
1018
1019         wl12xx_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1020
1021         mutex_lock(&wl->mutex);
1022
1023         ret = wl12xx_ps_elp_wakeup(wl);
1024         if (ret < 0)
1025                 goto out;
1026
1027         if (changed & BSS_CHANGED_ASSOC) {
1028                 if (bss_conf->assoc) {
1029                         wl->aid = bss_conf->aid;
1030
1031                         ret = wl12xx_build_ps_poll(wl, wl->aid);
1032                         if (ret < 0)
1033                                 goto out_sleep;
1034
1035                         ret = wl12xx_acx_aid(wl, wl->aid);
1036                         if (ret < 0)
1037                                 goto out_sleep;
1038
1039                         /* If we want to go in PSM but we're not there yet */
1040                         if (wl->psm_requested && !wl->psm) {
1041                                 mode = STATION_POWER_SAVE_MODE;
1042                                 ret = wl12xx_ps_set_mode(wl, mode);
1043                                 if (ret < 0)
1044                                         goto out_sleep;
1045                         }
1046                 }
1047         }
1048         if (changed & BSS_CHANGED_ERP_SLOT) {
1049                 if (bss_conf->use_short_slot)
1050                         ret = wl12xx_acx_slot(wl, SLOT_TIME_SHORT);
1051                 else
1052                         ret = wl12xx_acx_slot(wl, SLOT_TIME_LONG);
1053                 if (ret < 0) {
1054                         wl12xx_warning("Set slot time failed %d", ret);
1055                         goto out_sleep;
1056                 }
1057         }
1058
1059         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1060                 if (bss_conf->use_short_preamble)
1061                         wl12xx_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1062                 else
1063                         wl12xx_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1064         }
1065
1066         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1067                 if (bss_conf->use_cts_prot)
1068                         ret = wl12xx_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1069                 else
1070                         ret = wl12xx_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1071                 if (ret < 0) {
1072                         wl12xx_warning("Set ctsprotect failed %d", ret);
1073                         goto out_sleep;
1074                 }
1075         }
1076
1077         if (changed & BSS_CHANGED_BSSID) {
1078                 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1079
1080                 ret = wl12xx_build_null_data(wl);
1081                 if (ret < 0)
1082                         goto out;
1083
1084                 if (wl->bss_type != BSS_TYPE_IBSS) {
1085                         ret = wl12xx_cmd_join(wl, wl->bss_type, 5, 100, 1);
1086                         if (ret < 0)
1087                                 goto out;
1088                 }
1089         }
1090
1091         if (changed & BSS_CHANGED_BEACON) {
1092                 beacon = ieee80211_beacon_get(hw, vif);
1093                 ret = wl12xx_cmd_template_set(wl, CMD_BEACON, beacon->data,
1094                                               beacon->len);
1095
1096                 if (ret < 0) {
1097                         dev_kfree_skb(beacon);
1098                         goto out;
1099                 }
1100
1101                 ret = wl12xx_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
1102                                               beacon->len);
1103
1104                 dev_kfree_skb(beacon);
1105
1106                 if (ret < 0)
1107                         goto out;
1108
1109                 ret = wl12xx_cmd_join(wl, wl->bss_type, 1, 100, 0);
1110
1111                 if (ret < 0)
1112                         goto out;
1113         }
1114
1115 out_sleep:
1116         wl12xx_ps_elp_sleep(wl);
1117
1118 out:
1119         mutex_unlock(&wl->mutex);
1120 }
1121
1122
1123 /* can't be const, mac80211 writes to this */
1124 static struct ieee80211_rate wl12xx_rates[] = {
1125         { .bitrate = 10,
1126           .hw_value = 0x1,
1127           .hw_value_short = 0x1, },
1128         { .bitrate = 20,
1129           .hw_value = 0x2,
1130           .hw_value_short = 0x2,
1131           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1132         { .bitrate = 55,
1133           .hw_value = 0x4,
1134           .hw_value_short = 0x4,
1135           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1136         { .bitrate = 110,
1137           .hw_value = 0x20,
1138           .hw_value_short = 0x20,
1139           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1140         { .bitrate = 60,
1141           .hw_value = 0x8,
1142           .hw_value_short = 0x8, },
1143         { .bitrate = 90,
1144           .hw_value = 0x10,
1145           .hw_value_short = 0x10, },
1146         { .bitrate = 120,
1147           .hw_value = 0x40,
1148           .hw_value_short = 0x40, },
1149         { .bitrate = 180,
1150           .hw_value = 0x80,
1151           .hw_value_short = 0x80, },
1152         { .bitrate = 240,
1153           .hw_value = 0x200,
1154           .hw_value_short = 0x200, },
1155         { .bitrate = 360,
1156          .hw_value = 0x400,
1157          .hw_value_short = 0x400, },
1158         { .bitrate = 480,
1159           .hw_value = 0x800,
1160           .hw_value_short = 0x800, },
1161         { .bitrate = 540,
1162           .hw_value = 0x1000,
1163           .hw_value_short = 0x1000, },
1164 };
1165
1166 /* can't be const, mac80211 writes to this */
1167 static struct ieee80211_channel wl12xx_channels[] = {
1168         { .hw_value = 1, .center_freq = 2412},
1169         { .hw_value = 2, .center_freq = 2417},
1170         { .hw_value = 3, .center_freq = 2422},
1171         { .hw_value = 4, .center_freq = 2427},
1172         { .hw_value = 5, .center_freq = 2432},
1173         { .hw_value = 6, .center_freq = 2437},
1174         { .hw_value = 7, .center_freq = 2442},
1175         { .hw_value = 8, .center_freq = 2447},
1176         { .hw_value = 9, .center_freq = 2452},
1177         { .hw_value = 10, .center_freq = 2457},
1178         { .hw_value = 11, .center_freq = 2462},
1179         { .hw_value = 12, .center_freq = 2467},
1180         { .hw_value = 13, .center_freq = 2472},
1181 };
1182
1183 /* can't be const, mac80211 writes to this */
1184 static struct ieee80211_supported_band wl12xx_band_2ghz = {
1185         .channels = wl12xx_channels,
1186         .n_channels = ARRAY_SIZE(wl12xx_channels),
1187         .bitrates = wl12xx_rates,
1188         .n_bitrates = ARRAY_SIZE(wl12xx_rates),
1189 };
1190
1191 static const struct ieee80211_ops wl12xx_ops = {
1192         .start = wl12xx_op_start,
1193         .stop = wl12xx_op_stop,
1194         .add_interface = wl12xx_op_add_interface,
1195         .remove_interface = wl12xx_op_remove_interface,
1196         .config = wl12xx_op_config,
1197         .configure_filter = wl12xx_op_configure_filter,
1198         .tx = wl12xx_op_tx,
1199         .set_key = wl12xx_op_set_key,
1200         .hw_scan = wl12xx_op_hw_scan,
1201         .bss_info_changed = wl12xx_op_bss_info_changed,
1202         .set_rts_threshold = wl12xx_op_set_rts_threshold,
1203 };
1204
1205 static int wl12xx_register_hw(struct wl12xx *wl)
1206 {
1207         int ret;
1208
1209         if (wl->mac80211_registered)
1210                 return 0;
1211
1212         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1213
1214         ret = ieee80211_register_hw(wl->hw);
1215         if (ret < 0) {
1216                 wl12xx_error("unable to register mac80211 hw: %d", ret);
1217                 return ret;
1218         }
1219
1220         wl->mac80211_registered = true;
1221
1222         wl12xx_notice("loaded");
1223
1224         return 0;
1225 }
1226
1227 static int wl12xx_init_ieee80211(struct wl12xx *wl)
1228 {
1229         /* The tx descriptor buffer and the TKIP space */
1230         wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
1231                 + WL1251_TKIP_IV_SPACE;
1232
1233         /* unit us */
1234         /* FIXME: find a proper value */
1235         wl->hw->channel_change_time = 10000;
1236
1237         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1238                 IEEE80211_HW_NOISE_DBM;
1239
1240         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1241         wl->hw->wiphy->max_scan_ssids = 1;
1242         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl12xx_band_2ghz;
1243
1244         SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1245
1246         return 0;
1247 }
1248
1249 #define WL12XX_DEFAULT_CHANNEL 1
1250 static int __devinit wl12xx_probe(struct spi_device *spi)
1251 {
1252         struct wl12xx_platform_data *pdata;
1253         struct ieee80211_hw *hw;
1254         struct wl12xx *wl;
1255         int ret, i;
1256         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1257
1258         pdata = spi->dev.platform_data;
1259         if (!pdata) {
1260                 wl12xx_error("no platform data");
1261                 return -ENODEV;
1262         }
1263
1264         hw = ieee80211_alloc_hw(sizeof(*wl), &wl12xx_ops);
1265         if (!hw) {
1266                 wl12xx_error("could not alloc ieee80211_hw");
1267                 return -ENOMEM;
1268         }
1269
1270         wl = hw->priv;
1271         memset(wl, 0, sizeof(*wl));
1272
1273         wl->hw = hw;
1274         dev_set_drvdata(&spi->dev, wl);
1275         wl->spi = spi;
1276
1277         wl->data_in_count = 0;
1278
1279         skb_queue_head_init(&wl->tx_queue);
1280
1281         INIT_WORK(&wl->filter_work, wl12xx_filter_work);
1282         wl->channel = WL12XX_DEFAULT_CHANNEL;
1283         wl->scanning = false;
1284         wl->default_key = 0;
1285         wl->listen_int = 1;
1286         wl->rx_counter = 0;
1287         wl->rx_handled = 0;
1288         wl->rx_current_buffer = 0;
1289         wl->rx_last_id = 0;
1290         wl->rx_config = WL12XX_DEFAULT_RX_CONFIG;
1291         wl->rx_filter = WL12XX_DEFAULT_RX_FILTER;
1292         wl->elp = false;
1293         wl->psm = 0;
1294         wl->psm_requested = false;
1295         wl->tx_queue_stopped = false;
1296         wl->power_level = WL12XX_DEFAULT_POWER_LEVEL;
1297
1298         /* We use the default power on sleep time until we know which chip
1299          * we're using */
1300         wl->chip.power_on_sleep = WL12XX_DEFAULT_POWER_ON_SLEEP;
1301
1302         for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1303                 wl->tx_frames[i] = NULL;
1304
1305         wl->next_tx_complete = 0;
1306
1307         /*
1308          * In case our MAC address is not correctly set,
1309          * we use a random but Nokia MAC.
1310          */
1311         memcpy(wl->mac_addr, nokia_oui, 3);
1312         get_random_bytes(wl->mac_addr + 3, 3);
1313
1314         wl->state = WL12XX_STATE_OFF;
1315         mutex_init(&wl->mutex);
1316
1317         wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1318         wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1319
1320         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1321         if (!wl->rx_descriptor) {
1322                 wl12xx_error("could not allocate memory for rx descriptor");
1323                 ret = -ENOMEM;
1324                 goto out_free;
1325         }
1326
1327         /* This is the only SPI value that we need to set here, the rest
1328          * comes from the board-peripherals file */
1329         spi->bits_per_word = 32;
1330
1331         ret = spi_setup(spi);
1332         if (ret < 0) {
1333                 wl12xx_error("spi_setup failed");
1334                 goto out_free;
1335         }
1336
1337         wl->set_power = pdata->set_power;
1338         if (!wl->set_power) {
1339                 wl12xx_error("set power function missing in platform data");
1340                 ret = -ENODEV;
1341                 goto out_free;
1342         }
1343
1344         wl->irq = spi->irq;
1345         if (wl->irq < 0) {
1346                 wl12xx_error("irq missing in platform data");
1347                 ret = -ENODEV;
1348                 goto out_free;
1349         }
1350
1351         ret = request_irq(wl->irq, wl12xx_irq, 0, DRIVER_NAME, wl);
1352         if (ret < 0) {
1353                 wl12xx_error("request_irq() failed: %d", ret);
1354                 goto out_free;
1355         }
1356
1357         set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1358
1359         disable_irq(wl->irq);
1360
1361         ret = wl12xx_init_ieee80211(wl);
1362         if (ret)
1363                 goto out_irq;
1364
1365         ret = wl12xx_register_hw(wl);
1366         if (ret)
1367                 goto out_irq;
1368
1369         wl12xx_debugfs_init(wl);
1370
1371         wl12xx_notice("initialized");
1372
1373         return 0;
1374
1375  out_irq:
1376         free_irq(wl->irq, wl);
1377
1378  out_free:
1379         kfree(wl->rx_descriptor);
1380         wl->rx_descriptor = NULL;
1381
1382         ieee80211_free_hw(hw);
1383
1384         return ret;
1385 }
1386
1387 static int __devexit wl12xx_remove(struct spi_device *spi)
1388 {
1389         struct wl12xx *wl = dev_get_drvdata(&spi->dev);
1390
1391         ieee80211_unregister_hw(wl->hw);
1392
1393         wl12xx_debugfs_exit(wl);
1394
1395         free_irq(wl->irq, wl);
1396         kfree(wl->target_mem_map);
1397         kfree(wl->data_path);
1398         kfree(wl->fw);
1399         wl->fw = NULL;
1400         kfree(wl->nvs);
1401         wl->nvs = NULL;
1402
1403         kfree(wl->rx_descriptor);
1404         wl->rx_descriptor = NULL;
1405
1406         ieee80211_free_hw(wl->hw);
1407
1408         return 0;
1409 }
1410
1411
1412 static struct spi_driver wl12xx_spi_driver = {
1413         .driver = {
1414                 .name           = "wl12xx",
1415                 .bus            = &spi_bus_type,
1416                 .owner          = THIS_MODULE,
1417         },
1418
1419         .probe          = wl12xx_probe,
1420         .remove         = __devexit_p(wl12xx_remove),
1421 };
1422
1423 static int __init wl12xx_init(void)
1424 {
1425         int ret;
1426
1427         ret = spi_register_driver(&wl12xx_spi_driver);
1428         if (ret < 0) {
1429                 wl12xx_error("failed to register spi driver: %d", ret);
1430                 goto out;
1431         }
1432
1433 out:
1434         return ret;
1435 }
1436
1437 static void __exit wl12xx_exit(void)
1438 {
1439         spi_unregister_driver(&wl12xx_spi_driver);
1440
1441         wl12xx_notice("unloaded");
1442 }
1443
1444 module_init(wl12xx_init);
1445 module_exit(wl12xx_exit);
1446
1447 MODULE_LICENSE("GPL");
1448 MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
1449                 "Luciano Coelho <luciano.coelho@nokia.com>");