wl1271: Add structure for firmware configuration values
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_main.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/firmware.h>
28 #include <linux/delay.h>
29 #include <linux/irq.h>
30 #include <linux/spi/spi.h>
31 #include <linux/crc32.h>
32 #include <linux/etherdevice.h>
33 #include <linux/vmalloc.h>
34 #include <linux/spi/wl12xx.h>
35
36 #include "wl1271.h"
37 #include "wl12xx_80211.h"
38 #include "wl1271_reg.h"
39 #include "wl1271_spi.h"
40 #include "wl1271_event.h"
41 #include "wl1271_tx.h"
42 #include "wl1271_rx.h"
43 #include "wl1271_ps.h"
44 #include "wl1271_init.h"
45 #include "wl1271_debugfs.h"
46 #include "wl1271_cmd.h"
47 #include "wl1271_boot.h"
48
49 static void wl1271_conf_init(struct wl1271 *wl)
50 {
51         struct conf_drv_settings conf = {
52                 .sg = {
53                         .per_threshold              = 7500,
54                         .max_scan_compensation_time = 120000,
55                         .nfs_sample_interval        = 400,
56                         .load_ratio                 = 50,
57                         .auto_ps_mode               = 0,
58                         .probe_req_compensation     = 170,
59                         .scan_window_compensation   = 50,
60                         .antenna_config             = 0,
61                         .beacon_miss_threshold      = 60,
62                         .rate_adaptation_threshold  = CONF_HW_BIT_RATE_12MBPS,
63                         .rate_adaptation_snr        = 0
64                 }
65         };
66
67         /*
68          * This function applies the default configuration to the driver. This
69          * function is invoked upon driver load (spi probe.)
70          *
71          * The configuration is stored in a run-time structure in order to
72          * facilitate for run-time adjustment of any of the parameters. Making
73          * changes to the configuration structure will apply the new values on
74          * the next interface up (wl1271_op_start.)
75          */
76
77         /* apply driver default configuration */
78         memcpy(&wl->conf, &conf, sizeof(conf));
79 }
80
81
82 static int wl1271_plt_init(struct wl1271 *wl)
83 {
84         int ret;
85
86         ret = wl1271_acx_init_mem_config(wl);
87         if (ret < 0)
88                 return ret;
89
90         ret = wl1271_cmd_data_path(wl, wl->channel, 1);
91         if (ret < 0)
92                 return ret;
93
94         return 0;
95 }
96
97 static void wl1271_disable_interrupts(struct wl1271 *wl)
98 {
99         disable_irq(wl->irq);
100 }
101
102 static void wl1271_power_off(struct wl1271 *wl)
103 {
104         wl->set_power(false);
105 }
106
107 static void wl1271_power_on(struct wl1271 *wl)
108 {
109         wl->set_power(true);
110 }
111
112 static void wl1271_fw_status(struct wl1271 *wl,
113                              struct wl1271_fw_status *status)
114 {
115         u32 total = 0;
116         int i;
117
118         wl1271_spi_read(wl, FW_STATUS_ADDR, status,
119                         sizeof(*status), false);
120
121         wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
122                      "drv_rx_counter = %d, tx_results_counter = %d)",
123                      status->intr,
124                      status->fw_rx_counter,
125                      status->drv_rx_counter,
126                      status->tx_results_counter);
127
128         /* update number of available TX blocks */
129         for (i = 0; i < NUM_TX_QUEUES; i++) {
130                 u32 cnt = status->tx_released_blks[i] - wl->tx_blocks_freed[i];
131                 wl->tx_blocks_freed[i] = status->tx_released_blks[i];
132                 wl->tx_blocks_available += cnt;
133                 total += cnt;
134         }
135
136         /* if more blocks are available now, schedule some tx work */
137         if (total && !skb_queue_empty(&wl->tx_queue))
138                 ieee80211_queue_work(wl->hw, &wl->tx_work);
139
140         /* update the host-chipset time offset */
141         wl->time_offset = jiffies_to_usecs(jiffies) - status->fw_localtime;
142 }
143
144 static void wl1271_irq_work(struct work_struct *work)
145 {
146         int ret;
147         u32 intr;
148         struct wl1271 *wl =
149                 container_of(work, struct wl1271, irq_work);
150
151         mutex_lock(&wl->mutex);
152
153         wl1271_debug(DEBUG_IRQ, "IRQ work");
154
155         if (wl->state == WL1271_STATE_OFF)
156                 goto out;
157
158         ret = wl1271_ps_elp_wakeup(wl, true);
159         if (ret < 0)
160                 goto out;
161
162         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
163
164         wl1271_fw_status(wl, wl->fw_status);
165         intr = wl->fw_status->intr;
166         if (!intr) {
167                 wl1271_debug(DEBUG_IRQ, "Zero interrupt received.");
168                 goto out_sleep;
169         }
170
171         intr &= WL1271_INTR_MASK;
172
173         if (intr & (WL1271_ACX_INTR_EVENT_A |
174                     WL1271_ACX_INTR_EVENT_B)) {
175                 wl1271_debug(DEBUG_IRQ,
176                              "WL1271_ACX_INTR_EVENT (0x%x)", intr);
177                 if (intr & WL1271_ACX_INTR_EVENT_A)
178                         wl1271_event_handle(wl, 0);
179                 else
180                         wl1271_event_handle(wl, 1);
181         }
182
183         if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
184                 wl1271_debug(DEBUG_IRQ,
185                              "WL1271_ACX_INTR_INIT_COMPLETE");
186
187         if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
188                 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
189
190         if (intr & WL1271_ACX_INTR_DATA) {
191                 u8 tx_res_cnt = wl->fw_status->tx_results_counter -
192                         wl->tx_results_count;
193
194                 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
195
196                 /* check for tx results */
197                 if (tx_res_cnt)
198                         wl1271_tx_complete(wl, tx_res_cnt);
199
200                 wl1271_rx(wl, wl->fw_status);
201         }
202
203 out_sleep:
204         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK,
205                            WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
206         wl1271_ps_elp_sleep(wl);
207
208 out:
209         mutex_unlock(&wl->mutex);
210 }
211
212 static irqreturn_t wl1271_irq(int irq, void *cookie)
213 {
214         struct wl1271 *wl;
215         unsigned long flags;
216
217         wl1271_debug(DEBUG_IRQ, "IRQ");
218
219         wl = cookie;
220
221         /* complete the ELP completion */
222         spin_lock_irqsave(&wl->wl_lock, flags);
223         if (wl->elp_compl) {
224                 complete(wl->elp_compl);
225                 wl->elp_compl = NULL;
226         }
227
228         ieee80211_queue_work(wl->hw, &wl->irq_work);
229         spin_unlock_irqrestore(&wl->wl_lock, flags);
230
231         return IRQ_HANDLED;
232 }
233
234 static int wl1271_fetch_firmware(struct wl1271 *wl)
235 {
236         const struct firmware *fw;
237         int ret;
238
239         ret = request_firmware(&fw, WL1271_FW_NAME, &wl->spi->dev);
240
241         if (ret < 0) {
242                 wl1271_error("could not get firmware: %d", ret);
243                 return ret;
244         }
245
246         if (fw->size % 4) {
247                 wl1271_error("firmware size is not multiple of 32 bits: %zu",
248                              fw->size);
249                 ret = -EILSEQ;
250                 goto out;
251         }
252
253         wl->fw_len = fw->size;
254         wl->fw = vmalloc(wl->fw_len);
255
256         if (!wl->fw) {
257                 wl1271_error("could not allocate memory for the firmware");
258                 ret = -ENOMEM;
259                 goto out;
260         }
261
262         memcpy(wl->fw, fw->data, wl->fw_len);
263
264         ret = 0;
265
266 out:
267         release_firmware(fw);
268
269         return ret;
270 }
271
272 static int wl1271_fetch_nvs(struct wl1271 *wl)
273 {
274         const struct firmware *fw;
275         int ret;
276
277         ret = request_firmware(&fw, WL1271_NVS_NAME, &wl->spi->dev);
278
279         if (ret < 0) {
280                 wl1271_error("could not get nvs file: %d", ret);
281                 return ret;
282         }
283
284         if (fw->size % 4) {
285                 wl1271_error("nvs size is not multiple of 32 bits: %zu",
286                              fw->size);
287                 ret = -EILSEQ;
288                 goto out;
289         }
290
291         wl->nvs_len = fw->size;
292         wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
293
294         if (!wl->nvs) {
295                 wl1271_error("could not allocate memory for the nvs file");
296                 ret = -ENOMEM;
297                 goto out;
298         }
299
300         memcpy(wl->nvs, fw->data, wl->nvs_len);
301
302         ret = 0;
303
304 out:
305         release_firmware(fw);
306
307         return ret;
308 }
309
310 static void wl1271_fw_wakeup(struct wl1271 *wl)
311 {
312         u32 elp_reg;
313
314         elp_reg = ELPCTRL_WAKE_UP;
315         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
316 }
317
318 static int wl1271_setup(struct wl1271 *wl)
319 {
320         wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
321         if (!wl->fw_status)
322                 return -ENOMEM;
323
324         wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
325         if (!wl->tx_res_if) {
326                 kfree(wl->fw_status);
327                 return -ENOMEM;
328         }
329
330         INIT_WORK(&wl->irq_work, wl1271_irq_work);
331         INIT_WORK(&wl->tx_work, wl1271_tx_work);
332         return 0;
333 }
334
335 static int wl1271_chip_wakeup(struct wl1271 *wl)
336 {
337         struct wl1271_partition_set partition;
338         int ret = 0;
339
340         wl1271_power_on(wl);
341         msleep(WL1271_POWER_ON_SLEEP);
342         wl1271_spi_reset(wl);
343         wl1271_spi_init(wl);
344
345         /* We don't need a real memory partition here, because we only want
346          * to use the registers at this point. */
347         memset(&partition, 0, sizeof(partition));
348         partition.reg.start = REGISTERS_BASE;
349         partition.reg.size = REGISTERS_DOWN_SIZE;
350         wl1271_set_partition(wl, &partition);
351
352         /* ELP module wake up */
353         wl1271_fw_wakeup(wl);
354
355         /* whal_FwCtrl_BootSm() */
356
357         /* 0. read chip id from CHIP_ID */
358         wl->chip.id = wl1271_spi_read32(wl, CHIP_ID_B);
359
360         /* 1. check if chip id is valid */
361
362         switch (wl->chip.id) {
363         case CHIP_ID_1271_PG10:
364                 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
365                                wl->chip.id);
366
367                 ret = wl1271_setup(wl);
368                 if (ret < 0)
369                         goto out;
370                 break;
371         case CHIP_ID_1271_PG20:
372                 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
373                              wl->chip.id);
374
375                 ret = wl1271_setup(wl);
376                 if (ret < 0)
377                         goto out;
378                 break;
379         default:
380                 wl1271_error("unsupported chip id: 0x%x", wl->chip.id);
381                 ret = -ENODEV;
382                 goto out;
383         }
384
385         if (wl->fw == NULL) {
386                 ret = wl1271_fetch_firmware(wl);
387                 if (ret < 0)
388                         goto out;
389         }
390
391         /* No NVS from netlink, try to get it from the filesystem */
392         if (wl->nvs == NULL) {
393                 ret = wl1271_fetch_nvs(wl);
394                 if (ret < 0)
395                         goto out;
396         }
397
398 out:
399         return ret;
400 }
401
402 struct wl1271_filter_params {
403         unsigned int filters;
404         unsigned int changed;
405         int mc_list_length;
406         u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
407 };
408
409 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
410                                   FIF_ALLMULTI | \
411                                   FIF_FCSFAIL | \
412                                   FIF_BCN_PRBRESP_PROMISC | \
413                                   FIF_CONTROL | \
414                                   FIF_OTHER_BSS)
415
416 static void wl1271_filter_work(struct work_struct *work)
417 {
418         struct wl1271 *wl =
419                 container_of(work, struct wl1271, filter_work);
420         struct wl1271_filter_params *fp;
421         unsigned long flags;
422         bool enabled = true;
423         int ret;
424
425         /* first, get the filter parameters */
426         spin_lock_irqsave(&wl->wl_lock, flags);
427         fp = wl->filter_params;
428         wl->filter_params = NULL;
429         spin_unlock_irqrestore(&wl->wl_lock, flags);
430
431         if (!fp)
432                 return;
433
434         /* then, lock the mutex without risk of lock-up */
435         mutex_lock(&wl->mutex);
436
437         if (wl->state == WL1271_STATE_OFF)
438                 goto out;
439
440         ret = wl1271_ps_elp_wakeup(wl, false);
441         if (ret < 0)
442                 goto out;
443
444         /* configure the mc filter regardless of the changed flags */
445         if (fp->filters & FIF_ALLMULTI)
446                 enabled = false;
447
448         ret = wl1271_acx_group_address_tbl(wl, enabled,
449                                            fp->mc_list, fp->mc_list_length);
450         if (ret < 0)
451                 goto out_sleep;
452
453         /* determine, whether supported filter values have changed */
454         if (fp->changed == 0)
455                 goto out;
456
457         /* apply configured filters */
458         ret = wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
459         if (ret < 0)
460                 goto out_sleep;
461
462 out_sleep:
463         wl1271_ps_elp_sleep(wl);
464
465 out:
466         mutex_unlock(&wl->mutex);
467         kfree(fp);
468 }
469
470 int wl1271_plt_start(struct wl1271 *wl)
471 {
472         int ret;
473
474         mutex_lock(&wl->mutex);
475
476         wl1271_notice("power up");
477
478         if (wl->state != WL1271_STATE_OFF) {
479                 wl1271_error("cannot go into PLT state because not "
480                              "in off state: %d", wl->state);
481                 ret = -EBUSY;
482                 goto out;
483         }
484
485         wl->state = WL1271_STATE_PLT;
486
487         ret = wl1271_chip_wakeup(wl);
488         if (ret < 0)
489                 goto out;
490
491         ret = wl1271_boot(wl);
492         if (ret < 0)
493                 goto out;
494
495         wl1271_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
496
497         ret = wl1271_plt_init(wl);
498         if (ret < 0)
499                 goto out;
500
501 out:
502         mutex_unlock(&wl->mutex);
503
504         return ret;
505 }
506
507 int wl1271_plt_stop(struct wl1271 *wl)
508 {
509         int ret = 0;
510
511         mutex_lock(&wl->mutex);
512
513         wl1271_notice("power down");
514
515         if (wl->state != WL1271_STATE_PLT) {
516                 wl1271_error("cannot power down because not in PLT "
517                              "state: %d", wl->state);
518                 ret = -EBUSY;
519                 goto out;
520         }
521
522         wl1271_disable_interrupts(wl);
523         wl1271_power_off(wl);
524
525         wl->state = WL1271_STATE_OFF;
526
527 out:
528         mutex_unlock(&wl->mutex);
529
530         return ret;
531 }
532
533
534 static int wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
535 {
536         struct wl1271 *wl = hw->priv;
537
538         skb_queue_tail(&wl->tx_queue, skb);
539
540         /*
541          * The chip specific setup must run before the first TX packet -
542          * before that, the tx_work will not be initialized!
543          */
544
545         ieee80211_queue_work(wl->hw, &wl->tx_work);
546
547         /*
548          * The workqueue is slow to process the tx_queue and we need stop
549          * the queue here, otherwise the queue will get too long.
550          */
551         if (skb_queue_len(&wl->tx_queue) >= WL1271_TX_QUEUE_MAX_LENGTH) {
552                 ieee80211_stop_queues(wl->hw);
553
554                 /*
555                  * FIXME: this is racy, the variable is not properly
556                  * protected. Maybe fix this by removing the stupid
557                  * variable altogether and checking the real queue state?
558                  */
559                 wl->tx_queue_stopped = true;
560         }
561
562         return NETDEV_TX_OK;
563 }
564
565 static int wl1271_op_start(struct ieee80211_hw *hw)
566 {
567         struct wl1271 *wl = hw->priv;
568         int ret = 0;
569
570         wl1271_debug(DEBUG_MAC80211, "mac80211 start");
571
572         mutex_lock(&wl->mutex);
573
574         if (wl->state != WL1271_STATE_OFF) {
575                 wl1271_error("cannot start because not in off state: %d",
576                              wl->state);
577                 ret = -EBUSY;
578                 goto out;
579         }
580
581         ret = wl1271_chip_wakeup(wl);
582         if (ret < 0)
583                 goto out;
584
585         ret = wl1271_boot(wl);
586         if (ret < 0)
587                 goto out;
588
589         ret = wl1271_hw_init(wl);
590         if (ret < 0)
591                 goto out;
592
593         wl->state = WL1271_STATE_ON;
594
595         wl1271_info("firmware booted (%s)", wl->chip.fw_ver);
596
597 out:
598         if (ret < 0)
599                 wl1271_power_off(wl);
600
601         mutex_unlock(&wl->mutex);
602
603         return ret;
604 }
605
606 static void wl1271_op_stop(struct ieee80211_hw *hw)
607 {
608         struct wl1271 *wl = hw->priv;
609         unsigned long flags;
610         int i;
611
612         wl1271_info("down");
613
614         wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
615
616         /* complete/cancel ongoing work */
617         cancel_work_sync(&wl->filter_work);
618         spin_lock_irqsave(&wl->wl_lock, flags);
619         kfree(wl->filter_params);
620         wl->filter_params = NULL;
621         spin_unlock_irqrestore(&wl->wl_lock, flags);
622
623         mutex_lock(&wl->mutex);
624
625         WARN_ON(wl->state != WL1271_STATE_ON);
626
627         if (wl->scanning) {
628                 mutex_unlock(&wl->mutex);
629                 ieee80211_scan_completed(wl->hw, true);
630                 mutex_lock(&wl->mutex);
631                 wl->scanning = false;
632         }
633
634         wl->state = WL1271_STATE_OFF;
635
636         wl1271_disable_interrupts(wl);
637
638         mutex_unlock(&wl->mutex);
639
640         cancel_work_sync(&wl->irq_work);
641         cancel_work_sync(&wl->tx_work);
642         cancel_work_sync(&wl->filter_work);
643
644         mutex_lock(&wl->mutex);
645
646         /* let's notify MAC80211 about the remaining pending TX frames */
647         wl1271_tx_flush(wl);
648         wl1271_power_off(wl);
649
650         memset(wl->bssid, 0, ETH_ALEN);
651         memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
652         wl->ssid_len = 0;
653         wl->listen_int = 1;
654         wl->bss_type = MAX_BSS_TYPE;
655         wl->band = IEEE80211_BAND_2GHZ;
656
657         wl->rx_counter = 0;
658         wl->elp = false;
659         wl->psm = 0;
660         wl->tx_queue_stopped = false;
661         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
662         wl->tx_blocks_available = 0;
663         wl->tx_results_count = 0;
664         wl->tx_packets_count = 0;
665         wl->tx_security_last_seq = 0;
666         wl->tx_security_seq_16 = 0;
667         wl->tx_security_seq_32 = 0;
668         wl->time_offset = 0;
669         wl->session_counter = 0;
670         wl->joined = false;
671
672         for (i = 0; i < NUM_TX_QUEUES; i++)
673                 wl->tx_blocks_freed[i] = 0;
674
675         wl1271_debugfs_reset(wl);
676         mutex_unlock(&wl->mutex);
677 }
678
679 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
680                                    struct ieee80211_if_init_conf *conf)
681 {
682         struct wl1271 *wl = hw->priv;
683         int ret = 0;
684
685         wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
686                      conf->type, conf->mac_addr);
687
688         mutex_lock(&wl->mutex);
689         if (wl->vif) {
690                 ret = -EBUSY;
691                 goto out;
692         }
693
694         wl->vif = conf->vif;
695
696         switch (conf->type) {
697         case NL80211_IFTYPE_STATION:
698                 wl->bss_type = BSS_TYPE_STA_BSS;
699                 break;
700         case NL80211_IFTYPE_ADHOC:
701                 wl->bss_type = BSS_TYPE_IBSS;
702                 break;
703         default:
704                 ret = -EOPNOTSUPP;
705                 goto out;
706         }
707
708         /* FIXME: what if conf->mac_addr changes? */
709
710 out:
711         mutex_unlock(&wl->mutex);
712         return ret;
713 }
714
715 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
716                                          struct ieee80211_if_init_conf *conf)
717 {
718         struct wl1271 *wl = hw->priv;
719
720         mutex_lock(&wl->mutex);
721         wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
722         wl->vif = NULL;
723         mutex_unlock(&wl->mutex);
724 }
725
726 #if 0
727 static int wl1271_op_config_interface(struct ieee80211_hw *hw,
728                                       struct ieee80211_vif *vif,
729                                       struct ieee80211_if_conf *conf)
730 {
731         struct wl1271 *wl = hw->priv;
732         struct sk_buff *beacon;
733         int ret;
734
735         wl1271_debug(DEBUG_MAC80211, "mac80211 config_interface bssid %pM",
736                      conf->bssid);
737         wl1271_dump_ascii(DEBUG_MAC80211, "ssid: ", conf->ssid,
738                           conf->ssid_len);
739
740         mutex_lock(&wl->mutex);
741
742         ret = wl1271_ps_elp_wakeup(wl, false);
743         if (ret < 0)
744                 goto out;
745
746         if (memcmp(wl->bssid, conf->bssid, ETH_ALEN)) {
747                 wl1271_debug(DEBUG_MAC80211, "bssid changed");
748
749                 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
750
751                 ret = wl1271_cmd_join(wl);
752                 if (ret < 0)
753                         goto out_sleep;
754         }
755
756         ret = wl1271_cmd_build_null_data(wl);
757         if (ret < 0)
758                 goto out_sleep;
759
760         wl->ssid_len = conf->ssid_len;
761         if (wl->ssid_len)
762                 memcpy(wl->ssid, conf->ssid, wl->ssid_len);
763
764         if (conf->changed & IEEE80211_IFCC_BEACON) {
765                 beacon = ieee80211_beacon_get(hw, vif);
766                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
767                                               beacon->data, beacon->len);
768
769                 if (ret < 0) {
770                         dev_kfree_skb(beacon);
771                         goto out_sleep;
772                 }
773
774                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PROBE_RESPONSE,
775                                               beacon->data, beacon->len);
776
777                 dev_kfree_skb(beacon);
778
779                 if (ret < 0)
780                         goto out_sleep;
781         }
782
783 out_sleep:
784         wl1271_ps_elp_sleep(wl);
785
786 out:
787         mutex_unlock(&wl->mutex);
788
789         return ret;
790 }
791 #endif
792
793 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
794 {
795         struct wl1271 *wl = hw->priv;
796         struct ieee80211_conf *conf = &hw->conf;
797         int channel, ret = 0;
798
799         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
800
801         wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
802                      channel,
803                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
804                      conf->power_level);
805
806         mutex_lock(&wl->mutex);
807
808         wl->band = conf->channel->band;
809
810         ret = wl1271_ps_elp_wakeup(wl, false);
811         if (ret < 0)
812                 goto out;
813
814         if (channel != wl->channel) {
815                 /*
816                  * We assume that the stack will configure the right channel
817                  * before associating, so we don't need to send a join
818                  * command here.  We will join the right channel when the
819                  * BSSID changes
820                  */
821                 wl->channel = channel;
822         }
823
824         ret = wl1271_cmd_build_null_data(wl);
825         if (ret < 0)
826                 goto out_sleep;
827
828         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
829                 wl1271_info("psm enabled");
830
831                 wl->psm_requested = true;
832
833                 /*
834                  * We enter PSM only if we're already associated.
835                  * If we're not, we'll enter it when joining an SSID,
836                  * through the bss_info_changed() hook.
837                  */
838                 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
839         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
840                    wl->psm_requested) {
841                 wl1271_info("psm disabled");
842
843                 wl->psm_requested = false;
844
845                 if (wl->psm)
846                         ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE);
847         }
848
849         if (conf->power_level != wl->power_level) {
850                 ret = wl1271_acx_tx_power(wl, conf->power_level);
851                 if (ret < 0)
852                         goto out;
853
854                 wl->power_level = conf->power_level;
855         }
856
857 out_sleep:
858         wl1271_ps_elp_sleep(wl);
859
860 out:
861         mutex_unlock(&wl->mutex);
862
863         return ret;
864 }
865
866 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
867                                        struct dev_addr_list *mc_list)
868 {
869         struct wl1271 *wl = hw->priv;
870         struct wl1271_filter_params *fp;
871         unsigned long flags;
872         int i;
873
874         /*
875          * FIXME: we should return a hash that will be passed to
876          * configure_filter() instead of saving everything in the context.
877          */
878
879         fp = kzalloc(sizeof(*fp), GFP_KERNEL);
880         if (!fp) {
881                 wl1271_error("Out of memory setting filters.");
882                 return 0;
883         }
884
885         /* update multicast filtering parameters */
886         if (mc_count > ACX_MC_ADDRESS_GROUP_MAX) {
887                 mc_count = 0;
888                 fp->filters |= FIF_ALLMULTI;
889         }
890
891         fp->mc_list_length = 0;
892         for (i = 0; i < mc_count; i++) {
893                 if (mc_list->da_addrlen == ETH_ALEN) {
894                         memcpy(fp->mc_list[fp->mc_list_length],
895                                mc_list->da_addr, ETH_ALEN);
896                         fp->mc_list_length++;
897                 } else
898                         wl1271_warning("Unknown mc address length.");
899         }
900
901         /* FIXME: We still need to set our filters properly */
902
903         spin_lock_irqsave(&wl->wl_lock, flags);
904         kfree(wl->filter_params);
905         wl->filter_params = fp;
906         spin_unlock_irqrestore(&wl->wl_lock, flags);
907
908         return 1;
909 }
910
911 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
912                                        unsigned int changed,
913                                        unsigned int *total, u64 multicast)
914 {
915         struct wl1271 *wl = hw->priv;
916
917         wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
918
919         *total &= WL1271_SUPPORTED_FILTERS;
920         changed &= WL1271_SUPPORTED_FILTERS;
921
922         if (!multicast)
923                 return;
924
925         /*
926          * FIXME: for now we are still using a workqueue for filter
927          * configuration, but with the new mac80211, this is not needed,
928          * since configure_filter can now sleep.  We now have
929          * prepare_multicast, which needs to be atomic instead.
930          */
931
932         /* store current filter config */
933         wl->filter_params->filters = *total;
934         wl->filter_params->changed = changed;
935
936         ieee80211_queue_work(wl->hw, &wl->filter_work);
937 }
938
939 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
940                              struct ieee80211_vif *vif,
941                              struct ieee80211_sta *sta,
942                              struct ieee80211_key_conf *key_conf)
943 {
944         struct wl1271 *wl = hw->priv;
945         const u8 *addr;
946         int ret;
947         u32 tx_seq_32 = 0;
948         u16 tx_seq_16 = 0;
949         u8 key_type;
950
951         static const u8 bcast_addr[ETH_ALEN] =
952                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
953
954         wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
955
956         addr = sta ? sta->addr : bcast_addr;
957
958         wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
959         wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
960         wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
961                      key_conf->alg, key_conf->keyidx,
962                      key_conf->keylen, key_conf->flags);
963         wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
964
965         if (is_zero_ether_addr(addr)) {
966                 /* We dont support TX only encryption */
967                 ret = -EOPNOTSUPP;
968                 goto out;
969         }
970
971         mutex_lock(&wl->mutex);
972
973         ret = wl1271_ps_elp_wakeup(wl, false);
974         if (ret < 0)
975                 goto out_unlock;
976
977         switch (key_conf->alg) {
978         case ALG_WEP:
979                 key_type = KEY_WEP;
980
981                 key_conf->hw_key_idx = key_conf->keyidx;
982                 break;
983         case ALG_TKIP:
984                 key_type = KEY_TKIP;
985
986                 key_conf->hw_key_idx = key_conf->keyidx;
987                 tx_seq_32 = wl->tx_security_seq_32;
988                 tx_seq_16 = wl->tx_security_seq_16;
989                 break;
990         case ALG_CCMP:
991                 key_type = KEY_AES;
992
993                 key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
994                 tx_seq_32 = wl->tx_security_seq_32;
995                 tx_seq_16 = wl->tx_security_seq_16;
996                 break;
997         default:
998                 wl1271_error("Unknown key algo 0x%x", key_conf->alg);
999
1000                 ret = -EOPNOTSUPP;
1001                 goto out_sleep;
1002         }
1003
1004         switch (cmd) {
1005         case SET_KEY:
1006                 ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
1007                                          key_conf->keyidx, key_type,
1008                                          key_conf->keylen, key_conf->key,
1009                                          addr, tx_seq_32, tx_seq_16);
1010                 if (ret < 0) {
1011                         wl1271_error("Could not add or replace key");
1012                         goto out_sleep;
1013                 }
1014                 break;
1015
1016         case DISABLE_KEY:
1017                 ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
1018                                          key_conf->keyidx, key_type,
1019                                          key_conf->keylen, key_conf->key,
1020                                          addr, 0, 0);
1021                 if (ret < 0) {
1022                         wl1271_error("Could not remove key");
1023                         goto out_sleep;
1024                 }
1025                 break;
1026
1027         default:
1028                 wl1271_error("Unsupported key cmd 0x%x", cmd);
1029                 ret = -EOPNOTSUPP;
1030                 goto out_sleep;
1031
1032                 break;
1033         }
1034
1035 out_sleep:
1036         wl1271_ps_elp_sleep(wl);
1037
1038 out_unlock:
1039         mutex_unlock(&wl->mutex);
1040
1041 out:
1042         return ret;
1043 }
1044
1045 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
1046                              struct cfg80211_scan_request *req)
1047 {
1048         struct wl1271 *wl = hw->priv;
1049         int ret;
1050         u8 *ssid = NULL;
1051         size_t ssid_len = 0;
1052
1053         wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
1054
1055         if (req->n_ssids) {
1056                 ssid = req->ssids[0].ssid;
1057                 ssid_len = req->ssids[0].ssid_len;
1058         }
1059
1060         mutex_lock(&wl->mutex);
1061
1062         ret = wl1271_ps_elp_wakeup(wl, false);
1063         if (ret < 0)
1064                 goto out;
1065
1066         ret = wl1271_cmd_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
1067
1068         wl1271_ps_elp_sleep(wl);
1069
1070 out:
1071         mutex_unlock(&wl->mutex);
1072
1073         return ret;
1074 }
1075
1076 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1077 {
1078         struct wl1271 *wl = hw->priv;
1079         int ret;
1080
1081         mutex_lock(&wl->mutex);
1082
1083         ret = wl1271_ps_elp_wakeup(wl, false);
1084         if (ret < 0)
1085                 goto out;
1086
1087         ret = wl1271_acx_rts_threshold(wl, (u16) value);
1088         if (ret < 0)
1089                 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
1090
1091         wl1271_ps_elp_sleep(wl);
1092
1093 out:
1094         mutex_unlock(&wl->mutex);
1095
1096         return ret;
1097 }
1098
1099 static u32 wl1271_enabled_rates_get(struct wl1271 *wl, u64 basic_rate_set)
1100 {
1101         struct ieee80211_supported_band *band;
1102         u32 enabled_rates = 0;
1103         int bit;
1104
1105         band = wl->hw->wiphy->bands[wl->band];
1106         for (bit = 0; bit < band->n_bitrates; bit++) {
1107                 if (basic_rate_set & 0x1)
1108                         enabled_rates |= band->bitrates[bit].hw_value;
1109                 basic_rate_set >>= 1;
1110         }
1111
1112         return enabled_rates;
1113 }
1114
1115 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
1116                                        struct ieee80211_vif *vif,
1117                                        struct ieee80211_bss_conf *bss_conf,
1118                                        u32 changed)
1119 {
1120         enum wl1271_cmd_ps_mode mode;
1121         struct wl1271 *wl = hw->priv;
1122         int ret;
1123
1124         wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1125
1126         mutex_lock(&wl->mutex);
1127
1128         ret = wl1271_ps_elp_wakeup(wl, false);
1129         if (ret < 0)
1130                 goto out;
1131
1132         if (changed & BSS_CHANGED_ASSOC) {
1133                 if (bss_conf->assoc) {
1134                         wl->aid = bss_conf->aid;
1135
1136                         /*
1137                          * with wl1271, we don't need to update the
1138                          * beacon_int and dtim_period, because the firmware
1139                          * updates it by itself when the first beacon is
1140                          * received after a join.
1141                          */
1142                         ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
1143                         if (ret < 0)
1144                                 goto out_sleep;
1145
1146                         ret = wl1271_acx_aid(wl, wl->aid);
1147                         if (ret < 0)
1148                                 goto out_sleep;
1149
1150                         /* If we want to go in PSM but we're not there yet */
1151                         if (wl->psm_requested && !wl->psm) {
1152                                 mode = STATION_POWER_SAVE_MODE;
1153                                 ret = wl1271_ps_set_mode(wl, mode);
1154                                 if (ret < 0)
1155                                         goto out_sleep;
1156                         }
1157                 } else {
1158                         /* use defaults when not associated */
1159                         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1160                         wl->aid = 0;
1161                 }
1162
1163         }
1164
1165         if (changed & BSS_CHANGED_ERP_SLOT) {
1166                 if (bss_conf->use_short_slot)
1167                         ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
1168                 else
1169                         ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
1170                 if (ret < 0) {
1171                         wl1271_warning("Set slot time failed %d", ret);
1172                         goto out_sleep;
1173                 }
1174         }
1175
1176         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1177                 if (bss_conf->use_short_preamble)
1178                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1179                 else
1180                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1181         }
1182
1183         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1184                 if (bss_conf->use_cts_prot)
1185                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1186                 else
1187                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1188                 if (ret < 0) {
1189                         wl1271_warning("Set ctsprotect failed %d", ret);
1190                         goto out_sleep;
1191                 }
1192         }
1193
1194         if (changed & BSS_CHANGED_BASIC_RATES) {
1195                 wl->basic_rate_set = wl1271_enabled_rates_get(
1196                         wl, bss_conf->basic_rates);
1197
1198                 ret = wl1271_acx_rate_policies(wl, wl->basic_rate_set);
1199                 if (ret < 0) {
1200                         wl1271_warning("Set rate policies failed %d", ret);
1201                         goto out_sleep;
1202                 }
1203         }
1204
1205 out_sleep:
1206         wl1271_ps_elp_sleep(wl);
1207
1208 out:
1209         mutex_unlock(&wl->mutex);
1210 }
1211
1212
1213 /* can't be const, mac80211 writes to this */
1214 static struct ieee80211_rate wl1271_rates[] = {
1215         { .bitrate = 10,
1216           .hw_value = CONF_HW_BIT_RATE_1MBPS,
1217           .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
1218         { .bitrate = 20,
1219           .hw_value = CONF_HW_BIT_RATE_2MBPS,
1220           .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
1221           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1222         { .bitrate = 55,
1223           .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
1224           .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
1225           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1226         { .bitrate = 110,
1227           .hw_value = CONF_HW_BIT_RATE_11MBPS,
1228           .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
1229           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1230         { .bitrate = 60,
1231           .hw_value = CONF_HW_BIT_RATE_6MBPS,
1232           .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
1233         { .bitrate = 90,
1234           .hw_value = CONF_HW_BIT_RATE_9MBPS,
1235           .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
1236         { .bitrate = 120,
1237           .hw_value = CONF_HW_BIT_RATE_12MBPS,
1238           .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
1239         { .bitrate = 180,
1240           .hw_value = CONF_HW_BIT_RATE_18MBPS,
1241           .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
1242         { .bitrate = 240,
1243           .hw_value = CONF_HW_BIT_RATE_24MBPS,
1244           .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
1245         { .bitrate = 360,
1246          .hw_value = CONF_HW_BIT_RATE_36MBPS,
1247          .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
1248         { .bitrate = 480,
1249           .hw_value = CONF_HW_BIT_RATE_48MBPS,
1250           .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
1251         { .bitrate = 540,
1252           .hw_value = CONF_HW_BIT_RATE_54MBPS,
1253           .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
1254 };
1255
1256 /* can't be const, mac80211 writes to this */
1257 static struct ieee80211_channel wl1271_channels[] = {
1258         { .hw_value = 1, .center_freq = 2412},
1259         { .hw_value = 2, .center_freq = 2417},
1260         { .hw_value = 3, .center_freq = 2422},
1261         { .hw_value = 4, .center_freq = 2427},
1262         { .hw_value = 5, .center_freq = 2432},
1263         { .hw_value = 6, .center_freq = 2437},
1264         { .hw_value = 7, .center_freq = 2442},
1265         { .hw_value = 8, .center_freq = 2447},
1266         { .hw_value = 9, .center_freq = 2452},
1267         { .hw_value = 10, .center_freq = 2457},
1268         { .hw_value = 11, .center_freq = 2462},
1269         { .hw_value = 12, .center_freq = 2467},
1270         { .hw_value = 13, .center_freq = 2472},
1271 };
1272
1273 /* can't be const, mac80211 writes to this */
1274 static struct ieee80211_supported_band wl1271_band_2ghz = {
1275         .channels = wl1271_channels,
1276         .n_channels = ARRAY_SIZE(wl1271_channels),
1277         .bitrates = wl1271_rates,
1278         .n_bitrates = ARRAY_SIZE(wl1271_rates),
1279 };
1280
1281 static const struct ieee80211_ops wl1271_ops = {
1282         .start = wl1271_op_start,
1283         .stop = wl1271_op_stop,
1284         .add_interface = wl1271_op_add_interface,
1285         .remove_interface = wl1271_op_remove_interface,
1286         .config = wl1271_op_config,
1287 /*      .config_interface = wl1271_op_config_interface, */
1288         .prepare_multicast = wl1271_op_prepare_multicast,
1289         .configure_filter = wl1271_op_configure_filter,
1290         .tx = wl1271_op_tx,
1291         .set_key = wl1271_op_set_key,
1292         .hw_scan = wl1271_op_hw_scan,
1293         .bss_info_changed = wl1271_op_bss_info_changed,
1294         .set_rts_threshold = wl1271_op_set_rts_threshold,
1295 };
1296
1297 static int wl1271_register_hw(struct wl1271 *wl)
1298 {
1299         int ret;
1300
1301         if (wl->mac80211_registered)
1302                 return 0;
1303
1304         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1305
1306         ret = ieee80211_register_hw(wl->hw);
1307         if (ret < 0) {
1308                 wl1271_error("unable to register mac80211 hw: %d", ret);
1309                 return ret;
1310         }
1311
1312         wl->mac80211_registered = true;
1313
1314         wl1271_notice("loaded");
1315
1316         return 0;
1317 }
1318
1319 static int wl1271_init_ieee80211(struct wl1271 *wl)
1320 {
1321         /* The tx descriptor buffer and the TKIP space. */
1322         wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
1323                 sizeof(struct wl1271_tx_hw_descr);
1324
1325         /* unit us */
1326         /* FIXME: find a proper value */
1327         wl->hw->channel_change_time = 10000;
1328
1329         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1330                 IEEE80211_HW_NOISE_DBM |
1331                 IEEE80211_HW_BEACON_FILTER;
1332
1333         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1334         wl->hw->wiphy->max_scan_ssids = 1;
1335         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
1336
1337         SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1338
1339         return 0;
1340 }
1341
1342 static void wl1271_device_release(struct device *dev)
1343 {
1344
1345 }
1346
1347 static struct platform_device wl1271_device = {
1348         .name           = "wl1271",
1349         .id             = -1,
1350
1351         /* device model insists to have a release function */
1352         .dev            = {
1353                 .release = wl1271_device_release,
1354         },
1355 };
1356
1357 #define WL1271_DEFAULT_CHANNEL 0
1358 static int __devinit wl1271_probe(struct spi_device *spi)
1359 {
1360         struct wl12xx_platform_data *pdata;
1361         struct ieee80211_hw *hw;
1362         struct wl1271 *wl;
1363         int ret, i;
1364         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1365
1366         pdata = spi->dev.platform_data;
1367         if (!pdata) {
1368                 wl1271_error("no platform data");
1369                 return -ENODEV;
1370         }
1371
1372         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
1373         if (!hw) {
1374                 wl1271_error("could not alloc ieee80211_hw");
1375                 return -ENOMEM;
1376         }
1377
1378         wl = hw->priv;
1379         memset(wl, 0, sizeof(*wl));
1380
1381         wl->hw = hw;
1382         dev_set_drvdata(&spi->dev, wl);
1383         wl->spi = spi;
1384
1385         skb_queue_head_init(&wl->tx_queue);
1386
1387         INIT_WORK(&wl->filter_work, wl1271_filter_work);
1388         INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
1389         wl->channel = WL1271_DEFAULT_CHANNEL;
1390         wl->scanning = false;
1391         wl->default_key = 0;
1392         wl->listen_int = 1;
1393         wl->rx_counter = 0;
1394         wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
1395         wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
1396         wl->elp = false;
1397         wl->psm = 0;
1398         wl->psm_requested = false;
1399         wl->tx_queue_stopped = false;
1400         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1401         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1402         wl->band = IEEE80211_BAND_2GHZ;
1403         wl->vif = NULL;
1404         wl->joined = false;
1405
1406         for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
1407                 wl->tx_frames[i] = NULL;
1408
1409         spin_lock_init(&wl->wl_lock);
1410
1411         /*
1412          * In case our MAC address is not correctly set,
1413          * we use a random but Nokia MAC.
1414          */
1415         memcpy(wl->mac_addr, nokia_oui, 3);
1416         get_random_bytes(wl->mac_addr + 3, 3);
1417
1418         wl->state = WL1271_STATE_OFF;
1419         mutex_init(&wl->mutex);
1420
1421         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1422         if (!wl->rx_descriptor) {
1423                 wl1271_error("could not allocate memory for rx descriptor");
1424                 ret = -ENOMEM;
1425                 goto out_free;
1426         }
1427
1428         /* This is the only SPI value that we need to set here, the rest
1429          * comes from the board-peripherals file */
1430         spi->bits_per_word = 32;
1431
1432         ret = spi_setup(spi);
1433         if (ret < 0) {
1434                 wl1271_error("spi_setup failed");
1435                 goto out_free;
1436         }
1437
1438         wl->set_power = pdata->set_power;
1439         if (!wl->set_power) {
1440                 wl1271_error("set power function missing in platform data");
1441                 ret = -ENODEV;
1442                 goto out_free;
1443         }
1444
1445         wl->irq = spi->irq;
1446         if (wl->irq < 0) {
1447                 wl1271_error("irq missing in platform data");
1448                 ret = -ENODEV;
1449                 goto out_free;
1450         }
1451
1452         ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
1453         if (ret < 0) {
1454                 wl1271_error("request_irq() failed: %d", ret);
1455                 goto out_free;
1456         }
1457
1458         set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1459
1460         disable_irq(wl->irq);
1461
1462         ret = platform_device_register(&wl1271_device);
1463         if (ret) {
1464                 wl1271_error("couldn't register platform device");
1465                 goto out_irq;
1466         }
1467         dev_set_drvdata(&wl1271_device.dev, wl);
1468
1469         /* Apply default driver configuration. */
1470         wl1271_conf_init(wl);
1471
1472         ret = wl1271_init_ieee80211(wl);
1473         if (ret)
1474                 goto out_platform;
1475
1476         ret = wl1271_register_hw(wl);
1477         if (ret)
1478                 goto out_platform;
1479
1480         wl1271_debugfs_init(wl);
1481
1482         wl1271_notice("initialized");
1483
1484         return 0;
1485
1486  out_platform:
1487         platform_device_unregister(&wl1271_device);
1488
1489  out_irq:
1490         free_irq(wl->irq, wl);
1491
1492  out_free:
1493         kfree(wl->rx_descriptor);
1494         wl->rx_descriptor = NULL;
1495
1496         ieee80211_free_hw(hw);
1497
1498         return ret;
1499 }
1500
1501 static int __devexit wl1271_remove(struct spi_device *spi)
1502 {
1503         struct wl1271 *wl = dev_get_drvdata(&spi->dev);
1504
1505         ieee80211_unregister_hw(wl->hw);
1506
1507         wl1271_debugfs_exit(wl);
1508         platform_device_unregister(&wl1271_device);
1509         free_irq(wl->irq, wl);
1510         kfree(wl->target_mem_map);
1511         vfree(wl->fw);
1512         wl->fw = NULL;
1513         kfree(wl->nvs);
1514         wl->nvs = NULL;
1515
1516         kfree(wl->rx_descriptor);
1517         wl->rx_descriptor = NULL;
1518
1519         kfree(wl->fw_status);
1520         kfree(wl->tx_res_if);
1521
1522         ieee80211_free_hw(wl->hw);
1523
1524         return 0;
1525 }
1526
1527
1528 static struct spi_driver wl1271_spi_driver = {
1529         .driver = {
1530                 .name           = "wl1271",
1531                 .bus            = &spi_bus_type,
1532                 .owner          = THIS_MODULE,
1533         },
1534
1535         .probe          = wl1271_probe,
1536         .remove         = __devexit_p(wl1271_remove),
1537 };
1538
1539 static int __init wl1271_init(void)
1540 {
1541         int ret;
1542
1543         ret = spi_register_driver(&wl1271_spi_driver);
1544         if (ret < 0) {
1545                 wl1271_error("failed to register spi driver: %d", ret);
1546                 goto out;
1547         }
1548
1549 out:
1550         return ret;
1551 }
1552
1553 static void __exit wl1271_exit(void)
1554 {
1555         spi_unregister_driver(&wl1271_spi_driver);
1556
1557         wl1271_notice("unloaded");
1558 }
1559
1560 module_init(wl1271_init);
1561 module_exit(wl1271_exit);
1562
1563 MODULE_LICENSE("GPL");
1564 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
1565 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");