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