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