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