Merge branches '3m', 'egalax', 'logitech', 'magicmouse', 'ntrig' and 'roccat' into...
[pandora-kernel.git] / drivers / net / wireless / wl1251 / main.c
1 /*
2  * This file is part of wl1251
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  *
20  */
21
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/firmware.h>
25 #include <linux/delay.h>
26 #include <linux/irq.h>
27 #include <linux/crc32.h>
28 #include <linux/etherdevice.h>
29 #include <linux/vmalloc.h>
30 #include <linux/slab.h>
31
32 #include "wl1251.h"
33 #include "wl12xx_80211.h"
34 #include "reg.h"
35 #include "io.h"
36 #include "cmd.h"
37 #include "event.h"
38 #include "tx.h"
39 #include "rx.h"
40 #include "ps.h"
41 #include "init.h"
42 #include "debugfs.h"
43 #include "boot.h"
44
45 void wl1251_enable_interrupts(struct wl1251 *wl)
46 {
47         wl->if_ops->enable_irq(wl);
48 }
49
50 void wl1251_disable_interrupts(struct wl1251 *wl)
51 {
52         wl->if_ops->disable_irq(wl);
53 }
54
55 static void wl1251_power_off(struct wl1251 *wl)
56 {
57         wl->set_power(false);
58 }
59
60 static void wl1251_power_on(struct wl1251 *wl)
61 {
62         wl->set_power(true);
63 }
64
65 static int wl1251_fetch_firmware(struct wl1251 *wl)
66 {
67         const struct firmware *fw;
68         struct device *dev = wiphy_dev(wl->hw->wiphy);
69         int ret;
70
71         ret = request_firmware(&fw, WL1251_FW_NAME, dev);
72
73         if (ret < 0) {
74                 wl1251_error("could not get firmware: %d", ret);
75                 return ret;
76         }
77
78         if (fw->size % 4) {
79                 wl1251_error("firmware size is not multiple of 32 bits: %zu",
80                              fw->size);
81                 ret = -EILSEQ;
82                 goto out;
83         }
84
85         wl->fw_len = fw->size;
86         wl->fw = vmalloc(wl->fw_len);
87
88         if (!wl->fw) {
89                 wl1251_error("could not allocate memory for the firmware");
90                 ret = -ENOMEM;
91                 goto out;
92         }
93
94         memcpy(wl->fw, fw->data, wl->fw_len);
95
96         ret = 0;
97
98 out:
99         release_firmware(fw);
100
101         return ret;
102 }
103
104 static int wl1251_fetch_nvs(struct wl1251 *wl)
105 {
106         const struct firmware *fw;
107         struct device *dev = wiphy_dev(wl->hw->wiphy);
108         int ret;
109
110         ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
111
112         if (ret < 0) {
113                 wl1251_error("could not get nvs file: %d", ret);
114                 return ret;
115         }
116
117         if (fw->size % 4) {
118                 wl1251_error("nvs size is not multiple of 32 bits: %zu",
119                              fw->size);
120                 ret = -EILSEQ;
121                 goto out;
122         }
123
124         wl->nvs_len = fw->size;
125         wl->nvs = kmemdup(fw->data, wl->nvs_len, GFP_KERNEL);
126
127         if (!wl->nvs) {
128                 wl1251_error("could not allocate memory for the nvs file");
129                 ret = -ENOMEM;
130                 goto out;
131         }
132
133         ret = 0;
134
135 out:
136         release_firmware(fw);
137
138         return ret;
139 }
140
141 static void wl1251_fw_wakeup(struct wl1251 *wl)
142 {
143         u32 elp_reg;
144
145         elp_reg = ELPCTRL_WAKE_UP;
146         wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
147         elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
148
149         if (!(elp_reg & ELPCTRL_WLAN_READY))
150                 wl1251_warning("WLAN not ready");
151 }
152
153 static int wl1251_chip_wakeup(struct wl1251 *wl)
154 {
155         int ret = 0;
156
157         wl1251_power_on(wl);
158         msleep(WL1251_POWER_ON_SLEEP);
159         wl->if_ops->reset(wl);
160
161         /* We don't need a real memory partition here, because we only want
162          * to use the registers at this point. */
163         wl1251_set_partition(wl,
164                              0x00000000,
165                              0x00000000,
166                              REGISTERS_BASE,
167                              REGISTERS_DOWN_SIZE);
168
169         /* ELP module wake up */
170         wl1251_fw_wakeup(wl);
171
172         /* whal_FwCtrl_BootSm() */
173
174         /* 0. read chip id from CHIP_ID */
175         wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
176
177         /* 1. check if chip id is valid */
178
179         switch (wl->chip_id) {
180         case CHIP_ID_1251_PG12:
181                 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
182                              wl->chip_id);
183                 break;
184         case CHIP_ID_1251_PG11:
185                 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
186                              wl->chip_id);
187                 break;
188         case CHIP_ID_1251_PG10:
189         default:
190                 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
191                 ret = -ENODEV;
192                 goto out;
193         }
194
195         if (wl->fw == NULL) {
196                 ret = wl1251_fetch_firmware(wl);
197                 if (ret < 0)
198                         goto out;
199         }
200
201         if (wl->nvs == NULL && !wl->use_eeprom) {
202                 /* No NVS from netlink, try to get it from the filesystem */
203                 ret = wl1251_fetch_nvs(wl);
204                 if (ret < 0)
205                         goto out;
206         }
207
208 out:
209         return ret;
210 }
211
212 #define WL1251_IRQ_LOOP_COUNT 10
213 static void wl1251_irq_work(struct work_struct *work)
214 {
215         u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
216         struct wl1251 *wl =
217                 container_of(work, struct wl1251, irq_work);
218         int ret;
219
220         mutex_lock(&wl->mutex);
221
222         wl1251_debug(DEBUG_IRQ, "IRQ work");
223
224         if (wl->state == WL1251_STATE_OFF)
225                 goto out;
226
227         ret = wl1251_ps_elp_wakeup(wl);
228         if (ret < 0)
229                 goto out;
230
231         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
232
233         intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
234         wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
235
236         do {
237                 if (wl->data_path) {
238                         wl->rx_counter = wl1251_mem_read32(
239                                 wl, wl->data_path->rx_control_addr);
240
241                         /* We handle a frmware bug here */
242                         switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
243                         case 0:
244                                 wl1251_debug(DEBUG_IRQ,
245                                              "RX: FW and host in sync");
246                                 intr &= ~WL1251_ACX_INTR_RX0_DATA;
247                                 intr &= ~WL1251_ACX_INTR_RX1_DATA;
248                                 break;
249                         case 1:
250                                 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
251                                 intr |= WL1251_ACX_INTR_RX0_DATA;
252                                 intr &= ~WL1251_ACX_INTR_RX1_DATA;
253                                 break;
254                         case 2:
255                                 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
256                                 intr |= WL1251_ACX_INTR_RX0_DATA;
257                                 intr |= WL1251_ACX_INTR_RX1_DATA;
258                                 break;
259                         default:
260                                 wl1251_warning(
261                                         "RX: FW and host out of sync: %d",
262                                         wl->rx_counter - wl->rx_handled);
263                                 break;
264                         }
265
266                         wl->rx_handled = wl->rx_counter;
267
268                         wl1251_debug(DEBUG_IRQ, "RX counter: %d",
269                                      wl->rx_counter);
270                 }
271
272                 intr &= wl->intr_mask;
273
274                 if (intr == 0) {
275                         wl1251_debug(DEBUG_IRQ, "INTR is 0");
276                         goto out_sleep;
277                 }
278
279                 if (intr & WL1251_ACX_INTR_RX0_DATA) {
280                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
281                         wl1251_rx(wl);
282                 }
283
284                 if (intr & WL1251_ACX_INTR_RX1_DATA) {
285                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
286                         wl1251_rx(wl);
287                 }
288
289                 if (intr & WL1251_ACX_INTR_TX_RESULT) {
290                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
291                         wl1251_tx_complete(wl);
292                 }
293
294                 if (intr & WL1251_ACX_INTR_EVENT_A) {
295                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
296                         wl1251_event_handle(wl, 0);
297                 }
298
299                 if (intr & WL1251_ACX_INTR_EVENT_B) {
300                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
301                         wl1251_event_handle(wl, 1);
302                 }
303
304                 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
305                         wl1251_debug(DEBUG_IRQ,
306                                      "WL1251_ACX_INTR_INIT_COMPLETE");
307
308                 if (--ctr == 0)
309                         break;
310
311                 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
312         } while (intr);
313
314 out_sleep:
315         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
316         wl1251_ps_elp_sleep(wl);
317
318 out:
319         mutex_unlock(&wl->mutex);
320 }
321
322 static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
323                        u16 beacon_interval, u8 dtim_period)
324 {
325         int ret;
326
327         ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
328                                      DEFAULT_HW_GEN_MODULATION_TYPE,
329                                      wl->tx_mgmt_frm_rate,
330                                      wl->tx_mgmt_frm_mod);
331         if (ret < 0)
332                 goto out;
333
334
335         ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
336                               dtim_period);
337         if (ret < 0)
338                 goto out;
339
340         ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
341         if (ret < 0)
342                 wl1251_warning("join timeout");
343
344 out:
345         return ret;
346 }
347
348 static void wl1251_filter_work(struct work_struct *work)
349 {
350         struct wl1251 *wl =
351                 container_of(work, struct wl1251, filter_work);
352         int ret;
353
354         mutex_lock(&wl->mutex);
355
356         if (wl->state == WL1251_STATE_OFF)
357                 goto out;
358
359         ret = wl1251_ps_elp_wakeup(wl);
360         if (ret < 0)
361                 goto out;
362
363         ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
364                           wl->dtim_period);
365         if (ret < 0)
366                 goto out_sleep;
367
368 out_sleep:
369         wl1251_ps_elp_sleep(wl);
370
371 out:
372         mutex_unlock(&wl->mutex);
373 }
374
375 static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
376 {
377         struct wl1251 *wl = hw->priv;
378         unsigned long flags;
379
380         skb_queue_tail(&wl->tx_queue, skb);
381
382         /*
383          * The chip specific setup must run before the first TX packet -
384          * before that, the tx_work will not be initialized!
385          */
386
387         ieee80211_queue_work(wl->hw, &wl->tx_work);
388
389         /*
390          * The workqueue is slow to process the tx_queue and we need stop
391          * the queue here, otherwise the queue will get too long.
392          */
393         if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_HIGH_WATERMARK) {
394                 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
395
396                 spin_lock_irqsave(&wl->wl_lock, flags);
397                 ieee80211_stop_queues(wl->hw);
398                 wl->tx_queue_stopped = true;
399                 spin_unlock_irqrestore(&wl->wl_lock, flags);
400         }
401
402         return NETDEV_TX_OK;
403 }
404
405 static int wl1251_op_start(struct ieee80211_hw *hw)
406 {
407         struct wl1251 *wl = hw->priv;
408         struct wiphy *wiphy = hw->wiphy;
409         int ret = 0;
410
411         wl1251_debug(DEBUG_MAC80211, "mac80211 start");
412
413         mutex_lock(&wl->mutex);
414
415         if (wl->state != WL1251_STATE_OFF) {
416                 wl1251_error("cannot start because not in off state: %d",
417                              wl->state);
418                 ret = -EBUSY;
419                 goto out;
420         }
421
422         ret = wl1251_chip_wakeup(wl);
423         if (ret < 0)
424                 goto out;
425
426         ret = wl1251_boot(wl);
427         if (ret < 0)
428                 goto out;
429
430         ret = wl1251_hw_init(wl);
431         if (ret < 0)
432                 goto out;
433
434         ret = wl1251_acx_station_id(wl);
435         if (ret < 0)
436                 goto out;
437
438         wl->state = WL1251_STATE_ON;
439
440         wl1251_info("firmware booted (%s)", wl->fw_ver);
441
442         /* update hw/fw version info in wiphy struct */
443         wiphy->hw_version = wl->chip_id;
444         strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
445
446 out:
447         if (ret < 0)
448                 wl1251_power_off(wl);
449
450         mutex_unlock(&wl->mutex);
451
452         return ret;
453 }
454
455 static void wl1251_op_stop(struct ieee80211_hw *hw)
456 {
457         struct wl1251 *wl = hw->priv;
458
459         wl1251_info("down");
460
461         wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
462
463         mutex_lock(&wl->mutex);
464
465         WARN_ON(wl->state != WL1251_STATE_ON);
466
467         if (wl->scanning) {
468                 ieee80211_scan_completed(wl->hw, true);
469                 wl->scanning = false;
470         }
471
472         wl->state = WL1251_STATE_OFF;
473
474         wl1251_disable_interrupts(wl);
475
476         mutex_unlock(&wl->mutex);
477
478         cancel_work_sync(&wl->irq_work);
479         cancel_work_sync(&wl->tx_work);
480         cancel_work_sync(&wl->filter_work);
481
482         mutex_lock(&wl->mutex);
483
484         /* let's notify MAC80211 about the remaining pending TX frames */
485         wl1251_tx_flush(wl);
486         wl1251_power_off(wl);
487
488         memset(wl->bssid, 0, ETH_ALEN);
489         wl->listen_int = 1;
490         wl->bss_type = MAX_BSS_TYPE;
491
492         wl->data_in_count = 0;
493         wl->rx_counter = 0;
494         wl->rx_handled = 0;
495         wl->rx_current_buffer = 0;
496         wl->rx_last_id = 0;
497         wl->next_tx_complete = 0;
498         wl->elp = false;
499         wl->psm = 0;
500         wl->tx_queue_stopped = false;
501         wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
502         wl->channel = WL1251_DEFAULT_CHANNEL;
503
504         wl1251_debugfs_reset(wl);
505
506         mutex_unlock(&wl->mutex);
507 }
508
509 static int wl1251_op_add_interface(struct ieee80211_hw *hw,
510                                    struct ieee80211_vif *vif)
511 {
512         struct wl1251 *wl = hw->priv;
513         int ret = 0;
514
515         wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
516                      vif->type, vif->addr);
517
518         mutex_lock(&wl->mutex);
519         if (wl->vif) {
520                 ret = -EBUSY;
521                 goto out;
522         }
523
524         wl->vif = vif;
525
526         switch (vif->type) {
527         case NL80211_IFTYPE_STATION:
528                 wl->bss_type = BSS_TYPE_STA_BSS;
529                 break;
530         case NL80211_IFTYPE_ADHOC:
531                 wl->bss_type = BSS_TYPE_IBSS;
532                 break;
533         default:
534                 ret = -EOPNOTSUPP;
535                 goto out;
536         }
537
538         if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
539                 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
540                 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
541                 ret = wl1251_acx_station_id(wl);
542                 if (ret < 0)
543                         goto out;
544         }
545
546 out:
547         mutex_unlock(&wl->mutex);
548         return ret;
549 }
550
551 static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
552                                          struct ieee80211_vif *vif)
553 {
554         struct wl1251 *wl = hw->priv;
555
556         mutex_lock(&wl->mutex);
557         wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
558         wl->vif = NULL;
559         mutex_unlock(&wl->mutex);
560 }
561
562 static int wl1251_build_qos_null_data(struct wl1251 *wl)
563 {
564         struct ieee80211_qos_hdr template;
565
566         memset(&template, 0, sizeof(template));
567
568         memcpy(template.addr1, wl->bssid, ETH_ALEN);
569         memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
570         memcpy(template.addr3, wl->bssid, ETH_ALEN);
571
572         template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
573                                              IEEE80211_STYPE_QOS_NULLFUNC |
574                                              IEEE80211_FCTL_TODS);
575
576         /* FIXME: not sure what priority to use here */
577         template.qos_ctrl = cpu_to_le16(0);
578
579         return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
580                                        sizeof(template));
581 }
582
583 static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
584 {
585         struct wl1251 *wl = hw->priv;
586         struct ieee80211_conf *conf = &hw->conf;
587         int channel, ret = 0;
588
589         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
590
591         wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
592                      channel,
593                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
594                      conf->power_level);
595
596         mutex_lock(&wl->mutex);
597
598         ret = wl1251_ps_elp_wakeup(wl);
599         if (ret < 0)
600                 goto out;
601
602         if (channel != wl->channel) {
603                 wl->channel = channel;
604
605                 ret = wl1251_join(wl, wl->bss_type, wl->channel,
606                                   wl->beacon_int, wl->dtim_period);
607                 if (ret < 0)
608                         goto out_sleep;
609         }
610
611         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
612                 wl1251_debug(DEBUG_PSM, "psm enabled");
613
614                 wl->psm_requested = true;
615
616                 wl->dtim_period = conf->ps_dtim_period;
617
618                 ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
619                                                   wl->dtim_period);
620
621                 /*
622                  * mac80211 enables PSM only if we're already associated.
623                  */
624                 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
625                 if (ret < 0)
626                         goto out_sleep;
627         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
628                    wl->psm_requested) {
629                 wl1251_debug(DEBUG_PSM, "psm disabled");
630
631                 wl->psm_requested = false;
632
633                 if (wl->psm) {
634                         ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
635                         if (ret < 0)
636                                 goto out_sleep;
637                 }
638         }
639
640         if (conf->power_level != wl->power_level) {
641                 ret = wl1251_acx_tx_power(wl, conf->power_level);
642                 if (ret < 0)
643                         goto out_sleep;
644
645                 wl->power_level = conf->power_level;
646         }
647
648 out_sleep:
649         wl1251_ps_elp_sleep(wl);
650
651 out:
652         mutex_unlock(&wl->mutex);
653
654         return ret;
655 }
656
657 #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
658                                   FIF_ALLMULTI | \
659                                   FIF_FCSFAIL | \
660                                   FIF_BCN_PRBRESP_PROMISC | \
661                                   FIF_CONTROL | \
662                                   FIF_OTHER_BSS)
663
664 static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
665                                        unsigned int changed,
666                                        unsigned int *total,u64 multicast)
667 {
668         struct wl1251 *wl = hw->priv;
669
670         wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
671
672         *total &= WL1251_SUPPORTED_FILTERS;
673         changed &= WL1251_SUPPORTED_FILTERS;
674
675         if (changed == 0)
676                 /* no filters which we support changed */
677                 return;
678
679         /* FIXME: wl->rx_config and wl->rx_filter are not protected */
680
681         wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
682         wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
683
684         if (*total & FIF_PROMISC_IN_BSS) {
685                 wl->rx_config |= CFG_BSSID_FILTER_EN;
686                 wl->rx_config |= CFG_RX_ALL_GOOD;
687         }
688         if (*total & FIF_ALLMULTI)
689                 /*
690                  * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
691                  * all multicast frames
692                  */
693                 wl->rx_config &= ~CFG_MC_FILTER_EN;
694         if (*total & FIF_FCSFAIL)
695                 wl->rx_filter |= CFG_RX_FCS_ERROR;
696         if (*total & FIF_BCN_PRBRESP_PROMISC) {
697                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
698                 wl->rx_config &= ~CFG_SSID_FILTER_EN;
699         }
700         if (*total & FIF_CONTROL)
701                 wl->rx_filter |= CFG_RX_CTL_EN;
702         if (*total & FIF_OTHER_BSS)
703                 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
704
705         /*
706          * FIXME: workqueues need to be properly cancelled on stop(), for
707          * now let's just disable changing the filter settings. They will
708          * be updated any on config().
709          */
710         /* schedule_work(&wl->filter_work); */
711 }
712
713 /* HW encryption */
714 static int wl1251_set_key_type(struct wl1251 *wl,
715                                struct wl1251_cmd_set_keys *key,
716                                enum set_key_cmd cmd,
717                                struct ieee80211_key_conf *mac80211_key,
718                                const u8 *addr)
719 {
720         switch (mac80211_key->cipher) {
721         case WLAN_CIPHER_SUITE_WEP40:
722         case WLAN_CIPHER_SUITE_WEP104:
723                 if (is_broadcast_ether_addr(addr))
724                         key->key_type = KEY_WEP_DEFAULT;
725                 else
726                         key->key_type = KEY_WEP_ADDR;
727
728                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
729                 break;
730         case WLAN_CIPHER_SUITE_TKIP:
731                 if (is_broadcast_ether_addr(addr))
732                         key->key_type = KEY_TKIP_MIC_GROUP;
733                 else
734                         key->key_type = KEY_TKIP_MIC_PAIRWISE;
735
736                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
737                 break;
738         case WLAN_CIPHER_SUITE_CCMP:
739                 if (is_broadcast_ether_addr(addr))
740                         key->key_type = KEY_AES_GROUP;
741                 else
742                         key->key_type = KEY_AES_PAIRWISE;
743                 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
744                 break;
745         default:
746                 wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
747                 return -EOPNOTSUPP;
748         }
749
750         return 0;
751 }
752
753 static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
754                              struct ieee80211_vif *vif,
755                              struct ieee80211_sta *sta,
756                              struct ieee80211_key_conf *key)
757 {
758         struct wl1251 *wl = hw->priv;
759         struct wl1251_cmd_set_keys *wl_cmd;
760         const u8 *addr;
761         int ret;
762
763         static const u8 bcast_addr[ETH_ALEN] =
764                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
765
766         wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
767
768         wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
769         if (!wl_cmd) {
770                 ret = -ENOMEM;
771                 goto out;
772         }
773
774         addr = sta ? sta->addr : bcast_addr;
775
776         wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
777         wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
778         wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
779                      key->cipher, key->keyidx, key->keylen, key->flags);
780         wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
781
782         if (is_zero_ether_addr(addr)) {
783                 /* We dont support TX only encryption */
784                 ret = -EOPNOTSUPP;
785                 goto out;
786         }
787
788         mutex_lock(&wl->mutex);
789
790         ret = wl1251_ps_elp_wakeup(wl);
791         if (ret < 0)
792                 goto out_unlock;
793
794         switch (cmd) {
795         case SET_KEY:
796                 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
797                 break;
798         case DISABLE_KEY:
799                 wl_cmd->key_action = KEY_REMOVE;
800                 break;
801         default:
802                 wl1251_error("Unsupported key cmd 0x%x", cmd);
803                 break;
804         }
805
806         ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
807         if (ret < 0) {
808                 wl1251_error("Set KEY type failed");
809                 goto out_sleep;
810         }
811
812         if (wl_cmd->key_type != KEY_WEP_DEFAULT)
813                 memcpy(wl_cmd->addr, addr, ETH_ALEN);
814
815         if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
816             (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
817                 /*
818                  * We get the key in the following form:
819                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
820                  * but the target is expecting:
821                  * TKIP - RX MIC - TX MIC
822                  */
823                 memcpy(wl_cmd->key, key->key, 16);
824                 memcpy(wl_cmd->key + 16, key->key + 24, 8);
825                 memcpy(wl_cmd->key + 24, key->key + 16, 8);
826
827         } else {
828                 memcpy(wl_cmd->key, key->key, key->keylen);
829         }
830         wl_cmd->key_size = key->keylen;
831
832         wl_cmd->id = key->keyidx;
833         wl_cmd->ssid_profile = 0;
834
835         wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
836
837         ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
838         if (ret < 0) {
839                 wl1251_warning("could not set keys");
840                 goto out_sleep;
841         }
842
843 out_sleep:
844         wl1251_ps_elp_sleep(wl);
845
846 out_unlock:
847         mutex_unlock(&wl->mutex);
848
849 out:
850         kfree(wl_cmd);
851
852         return ret;
853 }
854
855 static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
856                              struct ieee80211_vif *vif,
857                              struct cfg80211_scan_request *req)
858 {
859         struct wl1251 *wl = hw->priv;
860         struct sk_buff *skb;
861         size_t ssid_len = 0;
862         u8 *ssid = NULL;
863         int ret;
864
865         wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
866
867         if (req->n_ssids) {
868                 ssid = req->ssids[0].ssid;
869                 ssid_len = req->ssids[0].ssid_len;
870         }
871
872         mutex_lock(&wl->mutex);
873
874         if (wl->scanning) {
875                 wl1251_debug(DEBUG_SCAN, "scan already in progress");
876                 ret = -EINVAL;
877                 goto out;
878         }
879
880         ret = wl1251_ps_elp_wakeup(wl);
881         if (ret < 0)
882                 goto out;
883
884         skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
885                                      req->ie, req->ie_len);
886         if (!skb) {
887                 ret = -ENOMEM;
888                 goto out;
889         }
890
891         ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
892                                       skb->len);
893         dev_kfree_skb(skb);
894         if (ret < 0)
895                 goto out_sleep;
896
897         ret = wl1251_cmd_trigger_scan_to(wl, 0);
898         if (ret < 0)
899                 goto out_sleep;
900
901         wl->scanning = true;
902
903         ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
904                               req->n_channels, WL1251_SCAN_NUM_PROBES);
905         if (ret < 0) {
906                 wl->scanning = false;
907                 goto out_sleep;
908         }
909
910 out_sleep:
911         wl1251_ps_elp_sleep(wl);
912
913 out:
914         mutex_unlock(&wl->mutex);
915
916         return ret;
917 }
918
919 static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
920 {
921         struct wl1251 *wl = hw->priv;
922         int ret;
923
924         mutex_lock(&wl->mutex);
925
926         ret = wl1251_ps_elp_wakeup(wl);
927         if (ret < 0)
928                 goto out;
929
930         ret = wl1251_acx_rts_threshold(wl, (u16) value);
931         if (ret < 0)
932                 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
933
934         wl1251_ps_elp_sleep(wl);
935
936 out:
937         mutex_unlock(&wl->mutex);
938
939         return ret;
940 }
941
942 static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
943                                        struct ieee80211_vif *vif,
944                                        struct ieee80211_bss_conf *bss_conf,
945                                        u32 changed)
946 {
947         struct wl1251 *wl = hw->priv;
948         struct sk_buff *beacon, *skb;
949         int ret;
950
951         wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
952
953         mutex_lock(&wl->mutex);
954
955         ret = wl1251_ps_elp_wakeup(wl);
956         if (ret < 0)
957                 goto out;
958
959         if (changed & BSS_CHANGED_BSSID) {
960                 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
961
962                 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
963                 if (!skb)
964                         goto out_sleep;
965
966                 ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
967                                               skb->data, skb->len);
968                 dev_kfree_skb(skb);
969                 if (ret < 0)
970                         goto out_sleep;
971
972                 ret = wl1251_build_qos_null_data(wl);
973                 if (ret < 0)
974                         goto out;
975
976                 if (wl->bss_type != BSS_TYPE_IBSS) {
977                         ret = wl1251_join(wl, wl->bss_type, wl->channel,
978                                           wl->beacon_int, wl->dtim_period);
979                         if (ret < 0)
980                                 goto out_sleep;
981                 }
982         }
983
984         if (changed & BSS_CHANGED_ASSOC) {
985                 if (bss_conf->assoc) {
986                         wl->beacon_int = bss_conf->beacon_int;
987
988                         skb = ieee80211_pspoll_get(wl->hw, wl->vif);
989                         if (!skb)
990                                 goto out_sleep;
991
992                         ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
993                                                       skb->data,
994                                                       skb->len);
995                         dev_kfree_skb(skb);
996                         if (ret < 0)
997                                 goto out_sleep;
998
999                         ret = wl1251_acx_aid(wl, bss_conf->aid);
1000                         if (ret < 0)
1001                                 goto out_sleep;
1002                 } else {
1003                         /* use defaults when not associated */
1004                         wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1005                         wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
1006                 }
1007         }
1008         if (changed & BSS_CHANGED_ERP_SLOT) {
1009                 if (bss_conf->use_short_slot)
1010                         ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
1011                 else
1012                         ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
1013                 if (ret < 0) {
1014                         wl1251_warning("Set slot time failed %d", ret);
1015                         goto out_sleep;
1016                 }
1017         }
1018
1019         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1020                 if (bss_conf->use_short_preamble)
1021                         wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1022                 else
1023                         wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1024         }
1025
1026         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1027                 if (bss_conf->use_cts_prot)
1028                         ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1029                 else
1030                         ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1031                 if (ret < 0) {
1032                         wl1251_warning("Set ctsprotect failed %d", ret);
1033                         goto out_sleep;
1034                 }
1035         }
1036
1037         if (changed & BSS_CHANGED_BEACON) {
1038                 beacon = ieee80211_beacon_get(hw, vif);
1039                 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
1040                                               beacon->len);
1041
1042                 if (ret < 0) {
1043                         dev_kfree_skb(beacon);
1044                         goto out_sleep;
1045                 }
1046
1047                 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
1048                                               beacon->len);
1049
1050                 dev_kfree_skb(beacon);
1051
1052                 if (ret < 0)
1053                         goto out_sleep;
1054
1055                 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1056                                   wl->channel, wl->dtim_period);
1057
1058                 if (ret < 0)
1059                         goto out_sleep;
1060         }
1061
1062 out_sleep:
1063         wl1251_ps_elp_sleep(wl);
1064
1065 out:
1066         mutex_unlock(&wl->mutex);
1067 }
1068
1069
1070 /* can't be const, mac80211 writes to this */
1071 static struct ieee80211_rate wl1251_rates[] = {
1072         { .bitrate = 10,
1073           .hw_value = 0x1,
1074           .hw_value_short = 0x1, },
1075         { .bitrate = 20,
1076           .hw_value = 0x2,
1077           .hw_value_short = 0x2,
1078           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1079         { .bitrate = 55,
1080           .hw_value = 0x4,
1081           .hw_value_short = 0x4,
1082           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1083         { .bitrate = 110,
1084           .hw_value = 0x20,
1085           .hw_value_short = 0x20,
1086           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1087         { .bitrate = 60,
1088           .hw_value = 0x8,
1089           .hw_value_short = 0x8, },
1090         { .bitrate = 90,
1091           .hw_value = 0x10,
1092           .hw_value_short = 0x10, },
1093         { .bitrate = 120,
1094           .hw_value = 0x40,
1095           .hw_value_short = 0x40, },
1096         { .bitrate = 180,
1097           .hw_value = 0x80,
1098           .hw_value_short = 0x80, },
1099         { .bitrate = 240,
1100           .hw_value = 0x200,
1101           .hw_value_short = 0x200, },
1102         { .bitrate = 360,
1103          .hw_value = 0x400,
1104          .hw_value_short = 0x400, },
1105         { .bitrate = 480,
1106           .hw_value = 0x800,
1107           .hw_value_short = 0x800, },
1108         { .bitrate = 540,
1109           .hw_value = 0x1000,
1110           .hw_value_short = 0x1000, },
1111 };
1112
1113 /* can't be const, mac80211 writes to this */
1114 static struct ieee80211_channel wl1251_channels[] = {
1115         { .hw_value = 1, .center_freq = 2412},
1116         { .hw_value = 2, .center_freq = 2417},
1117         { .hw_value = 3, .center_freq = 2422},
1118         { .hw_value = 4, .center_freq = 2427},
1119         { .hw_value = 5, .center_freq = 2432},
1120         { .hw_value = 6, .center_freq = 2437},
1121         { .hw_value = 7, .center_freq = 2442},
1122         { .hw_value = 8, .center_freq = 2447},
1123         { .hw_value = 9, .center_freq = 2452},
1124         { .hw_value = 10, .center_freq = 2457},
1125         { .hw_value = 11, .center_freq = 2462},
1126         { .hw_value = 12, .center_freq = 2467},
1127         { .hw_value = 13, .center_freq = 2472},
1128 };
1129
1130 static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1131                              const struct ieee80211_tx_queue_params *params)
1132 {
1133         enum wl1251_acx_ps_scheme ps_scheme;
1134         struct wl1251 *wl = hw->priv;
1135         int ret;
1136
1137         mutex_lock(&wl->mutex);
1138
1139         wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1140
1141         ret = wl1251_ps_elp_wakeup(wl);
1142         if (ret < 0)
1143                 goto out;
1144
1145         /* mac80211 uses units of 32 usec */
1146         ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
1147                                 params->cw_min, params->cw_max,
1148                                 params->aifs, params->txop * 32);
1149         if (ret < 0)
1150                 goto out_sleep;
1151
1152         if (params->uapsd)
1153                 ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
1154         else
1155                 ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
1156
1157         ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
1158                                  CHANNEL_TYPE_EDCF,
1159                                  wl1251_tx_get_queue(queue), ps_scheme,
1160                                  WL1251_ACX_ACK_POLICY_LEGACY);
1161         if (ret < 0)
1162                 goto out_sleep;
1163
1164 out_sleep:
1165         wl1251_ps_elp_sleep(wl);
1166
1167 out:
1168         mutex_unlock(&wl->mutex);
1169
1170         return ret;
1171 }
1172
1173 static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
1174                                 struct survey_info *survey)
1175 {
1176         struct wl1251 *wl = hw->priv;
1177         struct ieee80211_conf *conf = &hw->conf;
1178  
1179         if (idx != 0)
1180                 return -ENOENT;
1181  
1182         survey->channel = conf->channel;
1183         survey->filled = SURVEY_INFO_NOISE_DBM;
1184         survey->noise = wl->noise;
1185  
1186         return 0;
1187 }
1188
1189 /* can't be const, mac80211 writes to this */
1190 static struct ieee80211_supported_band wl1251_band_2ghz = {
1191         .channels = wl1251_channels,
1192         .n_channels = ARRAY_SIZE(wl1251_channels),
1193         .bitrates = wl1251_rates,
1194         .n_bitrates = ARRAY_SIZE(wl1251_rates),
1195 };
1196
1197 static const struct ieee80211_ops wl1251_ops = {
1198         .start = wl1251_op_start,
1199         .stop = wl1251_op_stop,
1200         .add_interface = wl1251_op_add_interface,
1201         .remove_interface = wl1251_op_remove_interface,
1202         .config = wl1251_op_config,
1203         .configure_filter = wl1251_op_configure_filter,
1204         .tx = wl1251_op_tx,
1205         .set_key = wl1251_op_set_key,
1206         .hw_scan = wl1251_op_hw_scan,
1207         .bss_info_changed = wl1251_op_bss_info_changed,
1208         .set_rts_threshold = wl1251_op_set_rts_threshold,
1209         .conf_tx = wl1251_op_conf_tx,
1210         .get_survey = wl1251_op_get_survey,
1211 };
1212
1213 static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
1214 {
1215         unsigned long timeout;
1216
1217         wl1251_reg_write32(wl, EE_ADDR, offset);
1218         wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
1219
1220         /* EE_CTL_READ clears when data is ready */
1221         timeout = jiffies + msecs_to_jiffies(100);
1222         while (1) {
1223                 if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
1224                         break;
1225
1226                 if (time_after(jiffies, timeout))
1227                         return -ETIMEDOUT;
1228
1229                 msleep(1);
1230         }
1231
1232         *data = wl1251_reg_read32(wl, EE_DATA);
1233         return 0;
1234 }
1235
1236 static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
1237                               u8 *data, size_t len)
1238 {
1239         size_t i;
1240         int ret;
1241
1242         wl1251_reg_write32(wl, EE_START, 0);
1243
1244         for (i = 0; i < len; i++) {
1245                 ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
1246                 if (ret < 0)
1247                         return ret;
1248         }
1249
1250         return 0;
1251 }
1252
1253 static int wl1251_read_eeprom_mac(struct wl1251 *wl)
1254 {
1255         u8 mac[ETH_ALEN];
1256         int i, ret;
1257
1258         wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1259
1260         ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
1261         if (ret < 0) {
1262                 wl1251_warning("failed to read MAC address from EEPROM");
1263                 return ret;
1264         }
1265
1266         /* MAC is stored in reverse order */
1267         for (i = 0; i < ETH_ALEN; i++)
1268                 wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
1269
1270         return 0;
1271 }
1272
1273 static int wl1251_register_hw(struct wl1251 *wl)
1274 {
1275         int ret;
1276
1277         if (wl->mac80211_registered)
1278                 return 0;
1279
1280         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1281
1282         ret = ieee80211_register_hw(wl->hw);
1283         if (ret < 0) {
1284                 wl1251_error("unable to register mac80211 hw: %d", ret);
1285                 return ret;
1286         }
1287
1288         wl->mac80211_registered = true;
1289
1290         wl1251_notice("loaded");
1291
1292         return 0;
1293 }
1294
1295 int wl1251_init_ieee80211(struct wl1251 *wl)
1296 {
1297         int ret;
1298
1299         /* The tx descriptor buffer and the TKIP space */
1300         wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
1301                 + WL1251_TKIP_IV_SPACE;
1302
1303         /* unit us */
1304         /* FIXME: find a proper value */
1305         wl->hw->channel_change_time = 10000;
1306
1307         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1308                 IEEE80211_HW_SUPPORTS_PS |
1309                 IEEE80211_HW_BEACON_FILTER |
1310                 IEEE80211_HW_SUPPORTS_UAPSD;
1311
1312         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1313         wl->hw->wiphy->max_scan_ssids = 1;
1314         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
1315
1316         wl->hw->queues = 4;
1317
1318         if (wl->use_eeprom)
1319                 wl1251_read_eeprom_mac(wl);
1320
1321         ret = wl1251_register_hw(wl);
1322         if (ret)
1323                 goto out;
1324
1325         wl1251_debugfs_init(wl);
1326         wl1251_notice("initialized");
1327
1328         ret = 0;
1329
1330 out:
1331         return ret;
1332 }
1333 EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
1334
1335 struct ieee80211_hw *wl1251_alloc_hw(void)
1336 {
1337         struct ieee80211_hw *hw;
1338         struct wl1251 *wl;
1339         int i;
1340         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1341
1342         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
1343         if (!hw) {
1344                 wl1251_error("could not alloc ieee80211_hw");
1345                 return ERR_PTR(-ENOMEM);
1346         }
1347
1348         wl = hw->priv;
1349         memset(wl, 0, sizeof(*wl));
1350
1351         wl->hw = hw;
1352
1353         wl->data_in_count = 0;
1354
1355         skb_queue_head_init(&wl->tx_queue);
1356
1357         INIT_WORK(&wl->filter_work, wl1251_filter_work);
1358         INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
1359         wl->channel = WL1251_DEFAULT_CHANNEL;
1360         wl->scanning = false;
1361         wl->default_key = 0;
1362         wl->listen_int = 1;
1363         wl->rx_counter = 0;
1364         wl->rx_handled = 0;
1365         wl->rx_current_buffer = 0;
1366         wl->rx_last_id = 0;
1367         wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1368         wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
1369         wl->elp = false;
1370         wl->psm = 0;
1371         wl->psm_requested = false;
1372         wl->tx_queue_stopped = false;
1373         wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
1374         wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1375         wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
1376         wl->vif = NULL;
1377
1378         for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1379                 wl->tx_frames[i] = NULL;
1380
1381         wl->next_tx_complete = 0;
1382
1383         INIT_WORK(&wl->irq_work, wl1251_irq_work);
1384         INIT_WORK(&wl->tx_work, wl1251_tx_work);
1385
1386         /*
1387          * In case our MAC address is not correctly set,
1388          * we use a random but Nokia MAC.
1389          */
1390         memcpy(wl->mac_addr, nokia_oui, 3);
1391         get_random_bytes(wl->mac_addr + 3, 3);
1392
1393         wl->state = WL1251_STATE_OFF;
1394         mutex_init(&wl->mutex);
1395
1396         wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1397         wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1398
1399         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1400         if (!wl->rx_descriptor) {
1401                 wl1251_error("could not allocate memory for rx descriptor");
1402                 ieee80211_free_hw(hw);
1403                 return ERR_PTR(-ENOMEM);
1404         }
1405
1406         return hw;
1407 }
1408 EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
1409
1410 int wl1251_free_hw(struct wl1251 *wl)
1411 {
1412         ieee80211_unregister_hw(wl->hw);
1413
1414         wl1251_debugfs_exit(wl);
1415
1416         kfree(wl->target_mem_map);
1417         kfree(wl->data_path);
1418         vfree(wl->fw);
1419         wl->fw = NULL;
1420         kfree(wl->nvs);
1421         wl->nvs = NULL;
1422
1423         kfree(wl->rx_descriptor);
1424         wl->rx_descriptor = NULL;
1425
1426         ieee80211_free_hw(wl->hw);
1427
1428         return 0;
1429 }
1430 EXPORT_SYMBOL_GPL(wl1251_free_hw);
1431
1432 MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1433 MODULE_LICENSE("GPL");
1434 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
1435 MODULE_FIRMWARE(WL1251_FW_NAME);