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