wl1251: change psm enabled/disabled info to debug
[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                 return ret;
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         DECLARE_MAC_BUF(mac);
439         int ret = 0;
440
441         wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %s",
442                      conf->type, print_mac(mac, conf->mac_addr));
443
444         mutex_lock(&wl->mutex);
445
446         switch (conf->type) {
447         case NL80211_IFTYPE_STATION:
448                 wl->bss_type = BSS_TYPE_STA_BSS;
449                 break;
450         case NL80211_IFTYPE_ADHOC:
451                 wl->bss_type = BSS_TYPE_IBSS;
452                 break;
453         default:
454                 ret = -EOPNOTSUPP;
455                 goto out;
456         }
457
458         if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
459                 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
460                 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
461                 ret = wl1251_acx_station_id(wl);
462                 if (ret < 0)
463                         goto out;
464         }
465
466 out:
467         mutex_unlock(&wl->mutex);
468         return ret;
469 }
470
471 static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
472                                          struct ieee80211_if_init_conf *conf)
473 {
474         wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
475 }
476
477 static int wl1251_build_null_data(struct wl1251 *wl)
478 {
479         struct wl12xx_null_data_template template;
480
481         if (!is_zero_ether_addr(wl->bssid)) {
482                 memcpy(template.header.da, wl->bssid, ETH_ALEN);
483                 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
484         } else {
485                 memset(template.header.da, 0xff, ETH_ALEN);
486                 memset(template.header.bssid, 0xff, ETH_ALEN);
487         }
488
489         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
490         template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
491                                                 IEEE80211_STYPE_NULLFUNC);
492
493         return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
494                                        sizeof(template));
495
496 }
497
498 static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
499 {
500         struct wl12xx_ps_poll_template template;
501
502         memcpy(template.bssid, wl->bssid, ETH_ALEN);
503         memcpy(template.ta, wl->mac_addr, ETH_ALEN);
504         template.aid = aid;
505         template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
506
507         return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
508                                        sizeof(template));
509
510 }
511
512 static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
513 {
514         struct wl1251 *wl = hw->priv;
515         struct ieee80211_conf *conf = &hw->conf;
516         int channel, ret = 0;
517
518         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
519
520         wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
521                      channel,
522                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
523                      conf->power_level);
524
525         mutex_lock(&wl->mutex);
526
527         ret = wl1251_ps_elp_wakeup(wl);
528         if (ret < 0)
529                 goto out;
530
531         if (channel != wl->channel) {
532                 /* FIXME: use beacon interval provided by mac80211 */
533                 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
534                 if (ret < 0)
535                         goto out_sleep;
536
537                 wl->channel = channel;
538         }
539
540         ret = wl1251_build_null_data(wl);
541         if (ret < 0)
542                 goto out_sleep;
543
544         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
545                 wl1251_debug(DEBUG_PSM, "psm enabled");
546
547                 wl->psm_requested = true;
548
549                 /*
550                  * We enter PSM only if we're already associated.
551                  * If we're not, we'll enter it when joining an SSID,
552                  * through the bss_info_changed() hook.
553                  */
554                 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
555         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
556                    wl->psm_requested) {
557                 wl1251_debug(DEBUG_PSM, "psm disabled");
558
559                 wl->psm_requested = false;
560
561                 if (wl->psm)
562                         ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
563         }
564
565         if (conf->power_level != wl->power_level) {
566                 ret = wl1251_acx_tx_power(wl, conf->power_level);
567                 if (ret < 0)
568                         goto out;
569
570                 wl->power_level = conf->power_level;
571         }
572
573 out_sleep:
574         wl1251_ps_elp_sleep(wl);
575
576 out:
577         mutex_unlock(&wl->mutex);
578
579         return ret;
580 }
581
582 #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
583                                   FIF_ALLMULTI | \
584                                   FIF_FCSFAIL | \
585                                   FIF_BCN_PRBRESP_PROMISC | \
586                                   FIF_CONTROL | \
587                                   FIF_OTHER_BSS)
588
589 static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
590                                        unsigned int changed,
591                                        unsigned int *total,
592                                        int mc_count,
593                                        struct dev_addr_list *mc_list)
594 {
595         struct wl1251 *wl = hw->priv;
596
597         wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
598
599         *total &= WL1251_SUPPORTED_FILTERS;
600         changed &= WL1251_SUPPORTED_FILTERS;
601
602         if (changed == 0)
603                 /* no filters which we support changed */
604                 return;
605
606         /* FIXME: wl->rx_config and wl->rx_filter are not protected */
607
608         wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
609         wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
610
611         if (*total & FIF_PROMISC_IN_BSS) {
612                 wl->rx_config |= CFG_BSSID_FILTER_EN;
613                 wl->rx_config |= CFG_RX_ALL_GOOD;
614         }
615         if (*total & FIF_ALLMULTI)
616                 /*
617                  * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
618                  * all multicast frames
619                  */
620                 wl->rx_config &= ~CFG_MC_FILTER_EN;
621         if (*total & FIF_FCSFAIL)
622                 wl->rx_filter |= CFG_RX_FCS_ERROR;
623         if (*total & FIF_BCN_PRBRESP_PROMISC) {
624                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
625                 wl->rx_config &= ~CFG_SSID_FILTER_EN;
626         }
627         if (*total & FIF_CONTROL)
628                 wl->rx_filter |= CFG_RX_CTL_EN;
629         if (*total & FIF_OTHER_BSS)
630                 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
631
632         /*
633          * FIXME: workqueues need to be properly cancelled on stop(), for
634          * now let's just disable changing the filter settings. They will
635          * be updated any on config().
636          */
637         /* schedule_work(&wl->filter_work); */
638 }
639
640 /* HW encryption */
641 static int wl1251_set_key_type(struct wl1251 *wl,
642                                struct wl1251_cmd_set_keys *key,
643                                enum set_key_cmd cmd,
644                                struct ieee80211_key_conf *mac80211_key,
645                                const u8 *addr)
646 {
647         switch (mac80211_key->alg) {
648         case ALG_WEP:
649                 if (is_broadcast_ether_addr(addr))
650                         key->key_type = KEY_WEP_DEFAULT;
651                 else
652                         key->key_type = KEY_WEP_ADDR;
653
654                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
655                 break;
656         case ALG_TKIP:
657                 if (is_broadcast_ether_addr(addr))
658                         key->key_type = KEY_TKIP_MIC_GROUP;
659                 else
660                         key->key_type = KEY_TKIP_MIC_PAIRWISE;
661
662                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
663                 break;
664         case ALG_CCMP:
665                 if (is_broadcast_ether_addr(addr))
666                         key->key_type = KEY_AES_GROUP;
667                 else
668                         key->key_type = KEY_AES_PAIRWISE;
669                 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
670                 break;
671         default:
672                 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
673                 return -EOPNOTSUPP;
674         }
675
676         return 0;
677 }
678
679 static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
680                              struct ieee80211_vif *vif,
681                              struct ieee80211_sta *sta,
682                              struct ieee80211_key_conf *key)
683 {
684         struct wl1251 *wl = hw->priv;
685         struct wl1251_cmd_set_keys *wl_cmd;
686         const u8 *addr;
687         int ret;
688
689         static const u8 bcast_addr[ETH_ALEN] =
690                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
691
692         wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
693
694         wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
695         if (!wl_cmd) {
696                 ret = -ENOMEM;
697                 goto out;
698         }
699
700         addr = sta ? sta->addr : bcast_addr;
701
702         wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
703         wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
704         wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
705                      key->alg, key->keyidx, key->keylen, key->flags);
706         wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
707
708         if (is_zero_ether_addr(addr)) {
709                 /* We dont support TX only encryption */
710                 ret = -EOPNOTSUPP;
711                 goto out;
712         }
713
714         mutex_lock(&wl->mutex);
715
716         ret = wl1251_ps_elp_wakeup(wl);
717         if (ret < 0)
718                 goto out_unlock;
719
720         switch (cmd) {
721         case SET_KEY:
722                 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
723                 break;
724         case DISABLE_KEY:
725                 wl_cmd->key_action = KEY_REMOVE;
726                 break;
727         default:
728                 wl1251_error("Unsupported key cmd 0x%x", cmd);
729                 break;
730         }
731
732         ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
733         if (ret < 0) {
734                 wl1251_error("Set KEY type failed");
735                 goto out_sleep;
736         }
737
738         if (wl_cmd->key_type != KEY_WEP_DEFAULT)
739                 memcpy(wl_cmd->addr, addr, ETH_ALEN);
740
741         if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
742             (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
743                 /*
744                  * We get the key in the following form:
745                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
746                  * but the target is expecting:
747                  * TKIP - RX MIC - TX MIC
748                  */
749                 memcpy(wl_cmd->key, key->key, 16);
750                 memcpy(wl_cmd->key + 16, key->key + 24, 8);
751                 memcpy(wl_cmd->key + 24, key->key + 16, 8);
752
753         } else {
754                 memcpy(wl_cmd->key, key->key, key->keylen);
755         }
756         wl_cmd->key_size = key->keylen;
757
758         wl_cmd->id = key->keyidx;
759         wl_cmd->ssid_profile = 0;
760
761         wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
762
763         ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
764         if (ret < 0) {
765                 wl1251_warning("could not set keys");
766                 goto out_sleep;
767         }
768
769 out_sleep:
770         wl1251_ps_elp_sleep(wl);
771
772 out_unlock:
773         mutex_unlock(&wl->mutex);
774
775 out:
776         kfree(wl_cmd);
777
778         return ret;
779 }
780
781 static int wl1251_build_basic_rates(char *rates)
782 {
783         u8 index = 0;
784
785         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
786         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
787         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
788         rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
789
790         return index;
791 }
792
793 static int wl1251_build_extended_rates(char *rates)
794 {
795         u8 index = 0;
796
797         rates[index++] = IEEE80211_OFDM_RATE_6MB;
798         rates[index++] = IEEE80211_OFDM_RATE_9MB;
799         rates[index++] = IEEE80211_OFDM_RATE_12MB;
800         rates[index++] = IEEE80211_OFDM_RATE_18MB;
801         rates[index++] = IEEE80211_OFDM_RATE_24MB;
802         rates[index++] = IEEE80211_OFDM_RATE_36MB;
803         rates[index++] = IEEE80211_OFDM_RATE_48MB;
804         rates[index++] = IEEE80211_OFDM_RATE_54MB;
805
806         return index;
807 }
808
809
810 static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
811 {
812         struct wl12xx_probe_req_template template;
813         struct wl12xx_ie_rates *rates;
814         char *ptr;
815         u16 size;
816
817         ptr = (char *)&template;
818         size = sizeof(struct ieee80211_header);
819
820         memset(template.header.da, 0xff, ETH_ALEN);
821         memset(template.header.bssid, 0xff, ETH_ALEN);
822         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
823         template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
824
825         /* IEs */
826         /* SSID */
827         template.ssid.header.id = WLAN_EID_SSID;
828         template.ssid.header.len = ssid_len;
829         if (ssid_len && ssid)
830                 memcpy(template.ssid.ssid, ssid, ssid_len);
831         size += sizeof(struct wl12xx_ie_header) + ssid_len;
832         ptr += size;
833
834         /* Basic Rates */
835         rates = (struct wl12xx_ie_rates *)ptr;
836         rates->header.id = WLAN_EID_SUPP_RATES;
837         rates->header.len = wl1251_build_basic_rates(rates->rates);
838         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
839         ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
840
841         /* Extended rates */
842         rates = (struct wl12xx_ie_rates *)ptr;
843         rates->header.id = WLAN_EID_EXT_SUPP_RATES;
844         rates->header.len = wl1251_build_extended_rates(rates->rates);
845         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
846
847         wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
848
849         return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
850                                       size);
851 }
852
853 static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
854                           u8 active_scan, u8 high_prio, u8 num_channels,
855                           u8 probe_requests)
856 {
857         struct wl1251_cmd_trigger_scan_to *trigger = NULL;
858         struct cmd_scan *params = NULL;
859         int i, ret;
860         u16 scan_options = 0;
861
862         if (wl->scanning)
863                 return -EINVAL;
864
865         params = kzalloc(sizeof(*params), GFP_KERNEL);
866         if (!params)
867                 return -ENOMEM;
868
869         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
870         params->params.rx_filter_options =
871                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
872
873         /* High priority scan */
874         if (!active_scan)
875                 scan_options |= SCAN_PASSIVE;
876         if (high_prio)
877                 scan_options |= SCAN_PRIORITY_HIGH;
878         params->params.scan_options = scan_options;
879
880         params->params.num_channels = num_channels;
881         params->params.num_probe_requests = probe_requests;
882         params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
883         params->params.tid_trigger = 0;
884
885         for (i = 0; i < num_channels; i++) {
886                 params->channels[i].min_duration = cpu_to_le32(30000);
887                 params->channels[i].max_duration = cpu_to_le32(60000);
888                 memset(&params->channels[i].bssid_lsb, 0xff, 4);
889                 memset(&params->channels[i].bssid_msb, 0xff, 2);
890                 params->channels[i].early_termination = 0;
891                 params->channels[i].tx_power_att = 0;
892                 params->channels[i].channel = i + 1;
893                 memset(params->channels[i].pad, 0, 3);
894         }
895
896         for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
897                 memset(&params->channels[i], 0,
898                        sizeof(struct basic_scan_channel_parameters));
899
900         if (len && ssid) {
901                 params->params.ssid_len = len;
902                 memcpy(params->params.ssid, ssid, len);
903         } else {
904                 params->params.ssid_len = 0;
905                 memset(params->params.ssid, 0, 32);
906         }
907
908         ret = wl1251_build_probe_req(wl, ssid, len);
909         if (ret < 0) {
910                 wl1251_error("PROBE request template failed");
911                 goto out;
912         }
913
914         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
915         if (!trigger)
916                 goto out;
917
918         trigger->timeout = 0;
919
920         ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
921                               sizeof(*trigger));
922         if (ret < 0) {
923                 wl1251_error("trigger scan to failed for hw scan");
924                 goto out;
925         }
926
927         wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
928
929         wl->scanning = true;
930
931         ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
932         if (ret < 0)
933                 wl1251_error("SCAN failed");
934
935         wl1251_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
936
937         if (params->header.status != CMD_STATUS_SUCCESS) {
938                 wl1251_error("TEST command answer error: %d",
939                              params->header.status);
940                 wl->scanning = false;
941                 ret = -EIO;
942                 goto out;
943         }
944
945 out:
946         kfree(params);
947         return ret;
948
949 }
950
951 static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
952                              struct cfg80211_scan_request *req)
953 {
954         struct wl1251 *wl = hw->priv;
955         int ret;
956         u8 *ssid = NULL;
957         size_t ssid_len = 0;
958
959         wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
960
961         if (req->n_ssids) {
962                 ssid = req->ssids[0].ssid;
963                 ssid_len = req->ssids[0].ssid_len;
964         }
965
966         mutex_lock(&wl->mutex);
967
968         ret = wl1251_ps_elp_wakeup(wl);
969         if (ret < 0)
970                 goto out;
971
972         ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
973
974         wl1251_ps_elp_sleep(wl);
975
976 out:
977         mutex_unlock(&wl->mutex);
978
979         return ret;
980 }
981
982 static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
983 {
984         struct wl1251 *wl = hw->priv;
985         int ret;
986
987         mutex_lock(&wl->mutex);
988
989         ret = wl1251_ps_elp_wakeup(wl);
990         if (ret < 0)
991                 goto out;
992
993         ret = wl1251_acx_rts_threshold(wl, (u16) value);
994         if (ret < 0)
995                 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
996
997         wl1251_ps_elp_sleep(wl);
998
999 out:
1000         mutex_unlock(&wl->mutex);
1001
1002         return ret;
1003 }
1004
1005 static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
1006                                        struct ieee80211_vif *vif,
1007                                        struct ieee80211_bss_conf *bss_conf,
1008                                        u32 changed)
1009 {
1010         enum wl1251_cmd_ps_mode mode;
1011         struct wl1251 *wl = hw->priv;
1012         struct sk_buff *beacon;
1013         int ret;
1014
1015         wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1016
1017         mutex_lock(&wl->mutex);
1018
1019         ret = wl1251_ps_elp_wakeup(wl);
1020         if (ret < 0)
1021                 goto out;
1022
1023         if (changed & BSS_CHANGED_ASSOC) {
1024                 if (bss_conf->assoc) {
1025                         wl->aid = bss_conf->aid;
1026
1027                         ret = wl1251_build_ps_poll(wl, wl->aid);
1028                         if (ret < 0)
1029                                 goto out_sleep;
1030
1031                         ret = wl1251_acx_aid(wl, wl->aid);
1032                         if (ret < 0)
1033                                 goto out_sleep;
1034
1035                         /* If we want to go in PSM but we're not there yet */
1036                         if (wl->psm_requested && !wl->psm) {
1037                                 mode = STATION_POWER_SAVE_MODE;
1038                                 ret = wl1251_ps_set_mode(wl, mode);
1039                                 if (ret < 0)
1040                                         goto out_sleep;
1041                         }
1042                 }
1043         }
1044         if (changed & BSS_CHANGED_ERP_SLOT) {
1045                 if (bss_conf->use_short_slot)
1046                         ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
1047                 else
1048                         ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
1049                 if (ret < 0) {
1050                         wl1251_warning("Set slot time failed %d", ret);
1051                         goto out_sleep;
1052                 }
1053         }
1054
1055         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1056                 if (bss_conf->use_short_preamble)
1057                         wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1058                 else
1059                         wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1060         }
1061
1062         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1063                 if (bss_conf->use_cts_prot)
1064                         ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1065                 else
1066                         ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1067                 if (ret < 0) {
1068                         wl1251_warning("Set ctsprotect failed %d", ret);
1069                         goto out;
1070                 }
1071         }
1072
1073         if (changed & BSS_CHANGED_BSSID) {
1074                 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1075
1076                 ret = wl1251_build_null_data(wl);
1077                 if (ret < 0)
1078                         goto out;
1079
1080                 if (wl->bss_type != BSS_TYPE_IBSS) {
1081                         ret = wl1251_cmd_join(wl, wl->bss_type, 5, 100, 1);
1082                         if (ret < 0)
1083                                 goto out_sleep;
1084                         wl1251_warning("Set ctsprotect failed %d", ret);
1085                         goto out_sleep;
1086                 }
1087         }
1088
1089         if (changed & BSS_CHANGED_BEACON) {
1090                 beacon = ieee80211_beacon_get(hw, vif);
1091                 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
1092                                               beacon->len);
1093
1094                 if (ret < 0) {
1095                         dev_kfree_skb(beacon);
1096                         goto out;
1097                 }
1098
1099                 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
1100                                               beacon->len);
1101
1102                 dev_kfree_skb(beacon);
1103
1104                 if (ret < 0)
1105                         goto out;
1106
1107                 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
1108
1109                 if (ret < 0)
1110                         goto out;
1111         }
1112
1113 out_sleep:
1114         wl1251_ps_elp_sleep(wl);
1115
1116 out:
1117         mutex_unlock(&wl->mutex);
1118 }
1119
1120
1121 /* can't be const, mac80211 writes to this */
1122 static struct ieee80211_rate wl1251_rates[] = {
1123         { .bitrate = 10,
1124           .hw_value = 0x1,
1125           .hw_value_short = 0x1, },
1126         { .bitrate = 20,
1127           .hw_value = 0x2,
1128           .hw_value_short = 0x2,
1129           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1130         { .bitrate = 55,
1131           .hw_value = 0x4,
1132           .hw_value_short = 0x4,
1133           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1134         { .bitrate = 110,
1135           .hw_value = 0x20,
1136           .hw_value_short = 0x20,
1137           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1138         { .bitrate = 60,
1139           .hw_value = 0x8,
1140           .hw_value_short = 0x8, },
1141         { .bitrate = 90,
1142           .hw_value = 0x10,
1143           .hw_value_short = 0x10, },
1144         { .bitrate = 120,
1145           .hw_value = 0x40,
1146           .hw_value_short = 0x40, },
1147         { .bitrate = 180,
1148           .hw_value = 0x80,
1149           .hw_value_short = 0x80, },
1150         { .bitrate = 240,
1151           .hw_value = 0x200,
1152           .hw_value_short = 0x200, },
1153         { .bitrate = 360,
1154          .hw_value = 0x400,
1155          .hw_value_short = 0x400, },
1156         { .bitrate = 480,
1157           .hw_value = 0x800,
1158           .hw_value_short = 0x800, },
1159         { .bitrate = 540,
1160           .hw_value = 0x1000,
1161           .hw_value_short = 0x1000, },
1162 };
1163
1164 /* can't be const, mac80211 writes to this */
1165 static struct ieee80211_channel wl1251_channels[] = {
1166         { .hw_value = 1, .center_freq = 2412},
1167         { .hw_value = 2, .center_freq = 2417},
1168         { .hw_value = 3, .center_freq = 2422},
1169         { .hw_value = 4, .center_freq = 2427},
1170         { .hw_value = 5, .center_freq = 2432},
1171         { .hw_value = 6, .center_freq = 2437},
1172         { .hw_value = 7, .center_freq = 2442},
1173         { .hw_value = 8, .center_freq = 2447},
1174         { .hw_value = 9, .center_freq = 2452},
1175         { .hw_value = 10, .center_freq = 2457},
1176         { .hw_value = 11, .center_freq = 2462},
1177         { .hw_value = 12, .center_freq = 2467},
1178         { .hw_value = 13, .center_freq = 2472},
1179 };
1180
1181 /* can't be const, mac80211 writes to this */
1182 static struct ieee80211_supported_band wl1251_band_2ghz = {
1183         .channels = wl1251_channels,
1184         .n_channels = ARRAY_SIZE(wl1251_channels),
1185         .bitrates = wl1251_rates,
1186         .n_bitrates = ARRAY_SIZE(wl1251_rates),
1187 };
1188
1189 static const struct ieee80211_ops wl1251_ops = {
1190         .start = wl1251_op_start,
1191         .stop = wl1251_op_stop,
1192         .add_interface = wl1251_op_add_interface,
1193         .remove_interface = wl1251_op_remove_interface,
1194         .config = wl1251_op_config,
1195         .configure_filter = wl1251_op_configure_filter,
1196         .tx = wl1251_op_tx,
1197         .set_key = wl1251_op_set_key,
1198         .hw_scan = wl1251_op_hw_scan,
1199         .bss_info_changed = wl1251_op_bss_info_changed,
1200         .set_rts_threshold = wl1251_op_set_rts_threshold,
1201 };
1202
1203 static int wl1251_register_hw(struct wl1251 *wl)
1204 {
1205         int ret;
1206
1207         if (wl->mac80211_registered)
1208                 return 0;
1209
1210         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1211
1212         ret = ieee80211_register_hw(wl->hw);
1213         if (ret < 0) {
1214                 wl1251_error("unable to register mac80211 hw: %d", ret);
1215                 return ret;
1216         }
1217
1218         wl->mac80211_registered = true;
1219
1220         wl1251_notice("loaded");
1221
1222         return 0;
1223 }
1224
1225 static int wl1251_init_ieee80211(struct wl1251 *wl)
1226 {
1227         /* The tx descriptor buffer and the TKIP space */
1228         wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
1229                 + WL1251_TKIP_IV_SPACE;
1230
1231         /* unit us */
1232         /* FIXME: find a proper value */
1233         wl->hw->channel_change_time = 10000;
1234
1235         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1236                 IEEE80211_HW_NOISE_DBM;
1237
1238         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1239         wl->hw->wiphy->max_scan_ssids = 1;
1240         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
1241
1242         SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1243
1244         return 0;
1245 }
1246
1247 #define WL1251_DEFAULT_CHANNEL 1
1248 static int __devinit wl1251_probe(struct spi_device *spi)
1249 {
1250         struct wl12xx_platform_data *pdata;
1251         struct ieee80211_hw *hw;
1252         struct wl1251 *wl;
1253         int ret, i;
1254         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1255
1256         pdata = spi->dev.platform_data;
1257         if (!pdata) {
1258                 wl1251_error("no platform data");
1259                 return -ENODEV;
1260         }
1261
1262         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
1263         if (!hw) {
1264                 wl1251_error("could not alloc ieee80211_hw");
1265                 return -ENOMEM;
1266         }
1267
1268         wl = hw->priv;
1269         memset(wl, 0, sizeof(*wl));
1270
1271         wl->hw = hw;
1272         dev_set_drvdata(&spi->dev, wl);
1273         wl->spi = spi;
1274
1275         wl->data_in_count = 0;
1276
1277         skb_queue_head_init(&wl->tx_queue);
1278
1279         INIT_WORK(&wl->filter_work, wl1251_filter_work);
1280         wl->channel = WL1251_DEFAULT_CHANNEL;
1281         wl->scanning = false;
1282         wl->default_key = 0;
1283         wl->listen_int = 1;
1284         wl->rx_counter = 0;
1285         wl->rx_handled = 0;
1286         wl->rx_current_buffer = 0;
1287         wl->rx_last_id = 0;
1288         wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1289         wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
1290         wl->elp = false;
1291         wl->psm = 0;
1292         wl->psm_requested = false;
1293         wl->tx_queue_stopped = false;
1294         wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
1295
1296         /* We use the default power on sleep time until we know which chip
1297          * we're using */
1298         wl->chip.power_on_sleep = WL1251_DEFAULT_POWER_ON_SLEEP;
1299
1300         for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1301                 wl->tx_frames[i] = NULL;
1302
1303         wl->next_tx_complete = 0;
1304
1305         /*
1306          * In case our MAC address is not correctly set,
1307          * we use a random but Nokia MAC.
1308          */
1309         memcpy(wl->mac_addr, nokia_oui, 3);
1310         get_random_bytes(wl->mac_addr + 3, 3);
1311
1312         wl->state = WL1251_STATE_OFF;
1313         mutex_init(&wl->mutex);
1314
1315         wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1316         wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1317
1318         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1319         if (!wl->rx_descriptor) {
1320                 wl1251_error("could not allocate memory for rx descriptor");
1321                 ret = -ENOMEM;
1322                 goto out_free;
1323         }
1324
1325         /* This is the only SPI value that we need to set here, the rest
1326          * comes from the board-peripherals file */
1327         spi->bits_per_word = 32;
1328
1329         ret = spi_setup(spi);
1330         if (ret < 0) {
1331                 wl1251_error("spi_setup failed");
1332                 goto out_free;
1333         }
1334
1335         wl->set_power = pdata->set_power;
1336         if (!wl->set_power) {
1337                 wl1251_error("set power function missing in platform data");
1338                 ret = -ENODEV;
1339                 goto out_free;
1340         }
1341
1342         wl->irq = spi->irq;
1343         if (wl->irq < 0) {
1344                 wl1251_error("irq missing in platform data");
1345                 ret = -ENODEV;
1346                 goto out_free;
1347         }
1348
1349         ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
1350         if (ret < 0) {
1351                 wl1251_error("request_irq() failed: %d", ret);
1352                 goto out_free;
1353         }
1354
1355         set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1356
1357         disable_irq(wl->irq);
1358
1359         ret = wl1251_init_ieee80211(wl);
1360         if (ret)
1361                 goto out_irq;
1362
1363         ret = wl1251_register_hw(wl);
1364         if (ret)
1365                 goto out_irq;
1366
1367         wl1251_debugfs_init(wl);
1368
1369         wl1251_notice("initialized");
1370
1371         return 0;
1372
1373  out_irq:
1374         free_irq(wl->irq, wl);
1375
1376  out_free:
1377         kfree(wl->rx_descriptor);
1378         wl->rx_descriptor = NULL;
1379
1380         ieee80211_free_hw(hw);
1381
1382         return ret;
1383 }
1384
1385 static int __devexit wl1251_remove(struct spi_device *spi)
1386 {
1387         struct wl1251 *wl = dev_get_drvdata(&spi->dev);
1388
1389         ieee80211_unregister_hw(wl->hw);
1390
1391         wl1251_debugfs_exit(wl);
1392
1393         free_irq(wl->irq, wl);
1394         kfree(wl->target_mem_map);
1395         kfree(wl->data_path);
1396         kfree(wl->fw);
1397         wl->fw = NULL;
1398         kfree(wl->nvs);
1399         wl->nvs = NULL;
1400
1401         kfree(wl->rx_descriptor);
1402         wl->rx_descriptor = NULL;
1403
1404         ieee80211_free_hw(wl->hw);
1405
1406         return 0;
1407 }
1408
1409
1410 static struct spi_driver wl1251_spi_driver = {
1411         .driver = {
1412                 /* FIXME: use wl12xx name to not break the user space */
1413                 .name           = "wl12xx",
1414                 .bus            = &spi_bus_type,
1415                 .owner          = THIS_MODULE,
1416         },
1417
1418         .probe          = wl1251_probe,
1419         .remove         = __devexit_p(wl1251_remove),
1420 };
1421
1422 static int __init wl1251_init(void)
1423 {
1424         int ret;
1425
1426         ret = spi_register_driver(&wl1251_spi_driver);
1427         if (ret < 0) {
1428                 wl1251_error("failed to register spi driver: %d", ret);
1429                 goto out;
1430         }
1431
1432 out:
1433         return ret;
1434 }
1435
1436 static void __exit wl1251_exit(void)
1437 {
1438         spi_unregister_driver(&wl1251_spi_driver);
1439
1440         wl1251_notice("unloaded");
1441 }
1442
1443 module_init(wl1251_init);
1444 module_exit(wl1251_exit);
1445
1446 MODULE_LICENSE("GPL");
1447 MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
1448                 "Luciano Coelho <luciano.coelho@nokia.com>");