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