wl1251: experimental PS hacks
[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 int wl1251_power_off(struct wl1251 *wl)
56 {
57         return wl->if_ops->power(wl, false);
58 }
59
60 static int wl1251_power_on(struct wl1251 *wl)
61 {
62         return wl->if_ops->power(wl, 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;
156
157         ret = wl1251_power_on(wl);
158         if (ret < 0)
159                 return ret;
160
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         if (wl->nvs == NULL && !wl->use_eeprom) {
205                 /* No NVS from netlink, try to get it from the filesystem */
206                 ret = wl1251_fetch_nvs(wl);
207                 if (ret < 0)
208                         goto out;
209         }
210
211 out:
212         return ret;
213 }
214
215 #define WL1251_IRQ_LOOP_COUNT 10
216 static void wl1251_irq_work(struct work_struct *work)
217 {
218         u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
219         struct wl1251 *wl =
220                 container_of(work, struct wl1251, irq_work);
221         int ret;
222
223         mutex_lock(&wl->mutex);
224
225         wl1251_debug(DEBUG_IRQ, "IRQ work");
226
227         if (wl->state == WL1251_STATE_OFF)
228                 goto out;
229
230         ret = wl1251_ps_elp_wakeup(wl);
231         if (ret < 0)
232                 goto out;
233
234         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
235
236         intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
237         wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
238
239         do {
240                 if (wl->data_path) {
241                         wl->rx_counter = wl1251_mem_read32(
242                                 wl, wl->data_path->rx_control_addr);
243
244                         /* We handle a frmware bug here */
245                         switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
246                         case 0:
247                                 wl1251_debug(DEBUG_IRQ,
248                                              "RX: FW and host in sync");
249                                 intr &= ~WL1251_ACX_INTR_RX0_DATA;
250                                 intr &= ~WL1251_ACX_INTR_RX1_DATA;
251                                 break;
252                         case 1:
253                                 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
254                                 intr |= WL1251_ACX_INTR_RX0_DATA;
255                                 intr &= ~WL1251_ACX_INTR_RX1_DATA;
256                                 break;
257                         case 2:
258                                 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
259                                 intr |= WL1251_ACX_INTR_RX0_DATA;
260                                 intr |= WL1251_ACX_INTR_RX1_DATA;
261                                 break;
262                         default:
263                                 wl1251_warning(
264                                         "RX: FW and host out of sync: %d",
265                                         wl->rx_counter - wl->rx_handled);
266                                 break;
267                         }
268
269                         wl->rx_handled = wl->rx_counter;
270
271                         wl1251_debug(DEBUG_IRQ, "RX counter: %d",
272                                      wl->rx_counter);
273                 }
274
275                 intr &= wl->intr_mask;
276
277                 if (intr == 0) {
278                         wl1251_debug(DEBUG_IRQ, "INTR is 0");
279                         goto out_sleep;
280                 }
281
282                 if (intr & WL1251_ACX_INTR_RX0_DATA) {
283                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
284                         wl1251_rx(wl);
285                 }
286
287                 if (intr & WL1251_ACX_INTR_RX1_DATA) {
288                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
289                         wl1251_rx(wl);
290                 }
291
292                 if (intr & WL1251_ACX_INTR_TX_RESULT) {
293                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
294                         wl1251_tx_complete(wl);
295                 }
296
297                 if (intr & WL1251_ACX_INTR_EVENT_A) {
298                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
299                         wl1251_event_handle(wl, 0);
300                 }
301
302                 if (intr & WL1251_ACX_INTR_EVENT_B) {
303                         wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
304                         wl1251_event_handle(wl, 1);
305                 }
306
307                 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
308                         wl1251_debug(DEBUG_IRQ,
309                                      "WL1251_ACX_INTR_INIT_COMPLETE");
310
311                 if (--ctr == 0)
312                         break;
313
314                 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
315         } while (intr);
316
317 out_sleep:
318         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
319         wl1251_ps_elp_sleep(wl);
320
321 out:
322         mutex_unlock(&wl->mutex);
323 }
324
325 static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
326                        u16 beacon_interval, u8 dtim_period)
327 {
328         int ret;
329
330         ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
331                                      DEFAULT_HW_GEN_MODULATION_TYPE,
332                                      wl->tx_mgmt_frm_rate,
333                                      wl->tx_mgmt_frm_mod);
334         if (ret < 0)
335                 goto out;
336
337         /*
338          * Join command applies filters, and if we are not associated,
339          * BSSID filter must be disabled for association to work.
340          */
341         if (is_zero_ether_addr(wl->bssid))
342                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
343
344         ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
345                               dtim_period);
346         if (ret < 0)
347                 goto out;
348
349         ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
350         if (ret < 0)
351                 wl1251_warning("join timeout");
352
353 out:
354         return ret;
355 }
356
357 static void wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
358 {
359         struct wl1251 *wl = hw->priv;
360         unsigned long flags;
361
362         skb_queue_tail(&wl->tx_queue, skb);
363
364         /*
365          * The chip specific setup must run before the first TX packet -
366          * before that, the tx_work will not be initialized!
367          */
368
369         ieee80211_queue_work(wl->hw, &wl->tx_work);
370
371         /*
372          * The workqueue is slow to process the tx_queue and we need stop
373          * the queue here, otherwise the queue will get too long.
374          */
375         if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_HIGH_WATERMARK) {
376                 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
377
378                 spin_lock_irqsave(&wl->wl_lock, flags);
379                 ieee80211_stop_queues(wl->hw);
380                 wl->tx_queue_stopped = true;
381                 spin_unlock_irqrestore(&wl->wl_lock, flags);
382         }
383 }
384
385 static int wl1251_op_start(struct ieee80211_hw *hw)
386 {
387         struct wl1251 *wl = hw->priv;
388         struct wiphy *wiphy = hw->wiphy;
389         int ret = 0;
390
391         wl1251_debug(DEBUG_MAC80211, "mac80211 start");
392
393         mutex_lock(&wl->mutex);
394
395         if (wl->state != WL1251_STATE_OFF) {
396                 wl1251_error("cannot start because not in off state: %d",
397                              wl->state);
398                 ret = -EBUSY;
399                 goto out;
400         }
401
402         ret = wl1251_chip_wakeup(wl);
403         if (ret < 0)
404                 goto out;
405
406         ret = wl1251_boot(wl);
407         if (ret < 0)
408                 goto out;
409
410         ret = wl1251_hw_init(wl);
411         if (ret < 0)
412                 goto out;
413
414         ret = wl1251_acx_station_id(wl);
415         if (ret < 0)
416                 goto out;
417
418         wl->state = WL1251_STATE_ON;
419
420         wl1251_info("firmware booted (%s)", wl->fw_ver);
421
422         /* update hw/fw version info in wiphy struct */
423         wiphy->hw_version = wl->chip_id;
424         strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
425
426 out:
427         if (ret < 0)
428                 wl1251_power_off(wl);
429
430         mutex_unlock(&wl->mutex);
431
432         return ret;
433 }
434
435 static void wl1251_op_stop(struct ieee80211_hw *hw)
436 {
437         struct wl1251 *wl = hw->priv;
438
439         wl1251_info("down");
440
441         wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
442
443         mutex_lock(&wl->mutex);
444
445         WARN_ON(wl->state != WL1251_STATE_ON);
446
447         if (wl->scanning) {
448                 ieee80211_scan_completed(wl->hw, true);
449                 wl->scanning = false;
450         }
451
452         wl->state = WL1251_STATE_OFF;
453
454         wl1251_disable_interrupts(wl);
455
456         mutex_unlock(&wl->mutex);
457
458         cancel_work_sync(&wl->irq_work);
459         cancel_work_sync(&wl->tx_work);
460         cancel_delayed_work_sync(&wl->ps_work);
461         cancel_delayed_work_sync(&wl->elp_work);
462
463         mutex_lock(&wl->mutex);
464
465         /* let's notify MAC80211 about the remaining pending TX frames */
466         wl1251_tx_flush(wl);
467         wl1251_power_off(wl);
468
469         memset(wl->bssid, 0, ETH_ALEN);
470         wl->listen_int = 1;
471         wl->bss_type = MAX_BSS_TYPE;
472
473         wl->data_in_count = 0;
474         wl->rx_counter = 0;
475         wl->rx_handled = 0;
476         wl->rx_current_buffer = 0;
477         wl->rx_last_id = 0;
478         wl->next_tx_complete = 0;
479         wl->elp = false;
480         wl->station_mode = STATION_ACTIVE_MODE;
481         wl->tx_queue_stopped = false;
482         wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
483         wl->rssi_thold = 0;
484         wl->channel = WL1251_DEFAULT_CHANNEL;
485
486         wl1251_debugfs_reset(wl);
487
488         mutex_unlock(&wl->mutex);
489 }
490
491 static int wl1251_op_add_interface(struct ieee80211_hw *hw,
492                                    struct ieee80211_vif *vif)
493 {
494         struct wl1251 *wl = hw->priv;
495         int ret = 0;
496
497         wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
498                      vif->type, vif->addr);
499
500         mutex_lock(&wl->mutex);
501         if (wl->vif) {
502                 ret = -EBUSY;
503                 goto out;
504         }
505
506         wl->vif = vif;
507
508         switch (vif->type) {
509         case NL80211_IFTYPE_STATION:
510                 wl->bss_type = BSS_TYPE_STA_BSS;
511                 break;
512         case NL80211_IFTYPE_ADHOC:
513                 wl->bss_type = BSS_TYPE_IBSS;
514                 break;
515         default:
516                 ret = -EOPNOTSUPP;
517                 goto out;
518         }
519
520         if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
521                 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
522                 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
523                 ret = wl1251_acx_station_id(wl);
524                 if (ret < 0)
525                         goto out;
526         }
527
528 out:
529         mutex_unlock(&wl->mutex);
530         return ret;
531 }
532
533 static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
534                                          struct ieee80211_vif *vif)
535 {
536         struct wl1251 *wl = hw->priv;
537
538         mutex_lock(&wl->mutex);
539         wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
540         wl->vif = NULL;
541         mutex_unlock(&wl->mutex);
542 }
543
544 static int wl1251_build_qos_null_data(struct wl1251 *wl)
545 {
546         struct ieee80211_qos_hdr template;
547
548         memset(&template, 0, sizeof(template));
549
550         memcpy(template.addr1, wl->bssid, ETH_ALEN);
551         memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
552         memcpy(template.addr3, wl->bssid, ETH_ALEN);
553
554         template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
555                                              IEEE80211_STYPE_QOS_NULLFUNC |
556                                              IEEE80211_FCTL_TODS);
557
558         /* FIXME: not sure what priority to use here */
559         template.qos_ctrl = cpu_to_le16(0);
560
561         return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
562                                        sizeof(template));
563 }
564
565 static void wl1251_ps_work(struct work_struct *work)
566 {
567         struct delayed_work *dwork;
568         struct wl1251 *wl;
569         unsigned long diff, wait;
570         bool need_ps;
571         bool have_ps;
572         int ret;
573         int i;
574
575         dwork = container_of(work, struct delayed_work, work);
576         wl = container_of(dwork, struct wl1251, ps_work);
577
578         mutex_lock(&wl->mutex);
579
580         /* don't change PS modes while still transitioning, to avoid possbile
581          * fw bugs (it normally takes ~130ms to enable and ~10ms to disable) */
582         if (wl->ps_transitioning) {
583                 diff = jiffies - wl->ps_change_jiffies;
584                 if (diff > msecs_to_jiffies(500)) {
585                         wl1251_error("PS change taking too long: %lu", diff);
586                         wl->ps_transitioning = false;
587                 } else {
588                         //wl1251_error("PS still transitioning");
589                         ieee80211_queue_delayed_work(wl->hw, &wl->ps_work,
590                                 msecs_to_jiffies(50));
591                         goto out;
592                 }
593         }
594
595         need_ps = wl->psm_requested && !wl->bss_lost;
596         have_ps = wl->station_mode == STATION_POWER_SAVE_MODE;
597
598         if (need_ps == have_ps) {
599                 //wl1251_info("ps: already in mode %d", have_ps);
600                 goto out;
601         }
602
603         /* don't enter PS if there was recent activity */
604         if (need_ps) {
605                 wait = 0;
606
607                 diff = jiffies - wl->last_io_jiffies;
608                 if (diff < msecs_to_jiffies(150)) {
609                         //wl1251_info("ps: postponed psm, j %ld", diff);
610                         wait = msecs_to_jiffies(150) - diff + 1;
611                 }
612
613                 for (i = 0; i < ARRAY_SIZE(wl->tx_frames); i++) {
614                         if (wl->tx_frames[i] != NULL) {
615                                 //wl1251_error("  frm %d busy", i);
616                                 if (wait < msecs_to_jiffies(50))
617                                         wait = msecs_to_jiffies(50);
618                                 break;
619                         }
620                 }
621
622                 if (wait > 0) {
623                         ieee80211_queue_delayed_work(wl->hw, &wl->ps_work, wait);
624                         goto out;
625                 }
626         }
627
628         ret = wl1251_ps_elp_wakeup(wl);
629         if (ret < 0)
630                 goto out;
631
632         if (need_ps) {
633                 wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
634                                             wl->dtim_period);
635         }
636         ret = wl1251_ps_set_mode(wl,
637                 need_ps ? STATION_POWER_SAVE_MODE : STATION_ACTIVE_MODE);
638         if (ret < 0)
639                 goto out_sleep;
640
641         //wl1251_info("psm %d, j %ld, d %ld", need_ps,
642         //      jiffies - wl->last_io_jiffies);
643
644 out_sleep:
645         wl1251_ps_elp_sleep(wl);
646
647 out:
648         mutex_unlock(&wl->mutex);
649 }
650
651 static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
652 {
653         struct wl1251 *wl = hw->priv;
654         struct ieee80211_conf *conf = &hw->conf;
655         int channel, ret = 0;
656
657         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
658
659         wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
660                      channel,
661                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
662                      conf->power_level);
663
664         mutex_lock(&wl->mutex);
665
666         ret = wl1251_ps_elp_wakeup(wl);
667         if (ret < 0)
668                 goto out;
669
670         if (channel != wl->channel) {
671                 wl->channel = channel;
672
673                 ret = wl1251_join(wl, wl->bss_type, wl->channel,
674                                   wl->beacon_int, wl->dtim_period);
675                 if (ret < 0)
676                         goto out_sleep;
677         }
678
679         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
680                 wl1251_debug(DEBUG_PSM, "psm enabled");
681
682                 wl->psm_requested = true;
683
684                 wl->dtim_period = conf->ps_dtim_period;
685
686                 ieee80211_queue_delayed_work(wl->hw, &wl->ps_work, 0);
687         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
688                    wl->psm_requested) {
689                 wl1251_debug(DEBUG_PSM, "psm disabled");
690
691                 wl->psm_requested = false;
692
693                 ieee80211_queue_delayed_work(wl->hw, &wl->ps_work, 0);
694         }
695
696         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
697                 if (conf->flags & IEEE80211_CONF_IDLE) {
698                         ret = wl1251_ps_set_mode(wl, STATION_IDLE);
699                         if (ret < 0)
700                                 goto out_sleep;
701                 } else {
702                         ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
703                         if (ret < 0)
704                                 goto out_sleep;
705
706                         ret = wl1251_event_wait(wl, PS_REPORT_EVENT_ID, 100);
707                         if (ret < 0)
708                                 wl1251_error("error waiting for wakeup");
709
710                         ret = wl1251_join(wl, wl->bss_type, wl->channel,
711                                           wl->beacon_int, wl->dtim_period);
712                         if (ret < 0)
713                                 goto out_sleep;
714                 }
715         }
716
717         if (conf->power_level != wl->power_level) {
718                 ret = wl1251_acx_tx_power(wl, conf->power_level);
719                 if (ret < 0)
720                         goto out_sleep;
721
722                 wl->power_level = conf->power_level;
723         }
724
725 out_sleep:
726         wl1251_ps_elp_sleep(wl);
727
728 out:
729         mutex_unlock(&wl->mutex);
730
731         return ret;
732 }
733
734 #define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
735                                   FIF_ALLMULTI | \
736                                   FIF_FCSFAIL | \
737                                   FIF_BCN_PRBRESP_PROMISC | \
738                                   FIF_CONTROL | \
739                                   FIF_OTHER_BSS | \
740                                   FIF_PROBE_REQ)
741
742 static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
743                                        unsigned int changed,
744                                        unsigned int *total,u64 multicast)
745 {
746         struct wl1251 *wl = hw->priv;
747         int ret;
748
749         wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
750
751         *total &= WL1251_SUPPORTED_FILTERS;
752         changed &= WL1251_SUPPORTED_FILTERS;
753
754         if (changed == 0)
755                 /* no filters which we support changed */
756                 return;
757
758         mutex_lock(&wl->mutex);
759
760         wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
761         wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
762
763         if (*total & FIF_PROMISC_IN_BSS) {
764                 wl->rx_config |= CFG_BSSID_FILTER_EN;
765                 wl->rx_config |= CFG_RX_ALL_GOOD;
766         }
767         if (*total & FIF_ALLMULTI)
768                 /*
769                  * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
770                  * all multicast frames
771                  */
772                 wl->rx_config &= ~CFG_MC_FILTER_EN;
773         if (*total & FIF_FCSFAIL)
774                 wl->rx_filter |= CFG_RX_FCS_ERROR;
775         if (*total & FIF_BCN_PRBRESP_PROMISC) {
776                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
777                 wl->rx_config &= ~CFG_SSID_FILTER_EN;
778         }
779         if (*total & FIF_CONTROL)
780                 wl->rx_filter |= CFG_RX_CTL_EN;
781         if (*total & FIF_OTHER_BSS || is_zero_ether_addr(wl->bssid))
782                 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
783         if (*total & FIF_PROBE_REQ)
784                 wl->rx_filter |= CFG_RX_PREQ_EN;
785
786         if (wl->state == WL1251_STATE_OFF)
787                 goto out;
788
789         ret = wl1251_ps_elp_wakeup(wl);
790         if (ret < 0)
791                 goto out;
792
793         /* send filters to firmware */
794         wl1251_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
795
796         wl1251_ps_elp_sleep(wl);
797
798 out:
799         mutex_unlock(&wl->mutex);
800 }
801
802 /* HW encryption */
803 static int wl1251_set_key_type(struct wl1251 *wl,
804                                struct wl1251_cmd_set_keys *key,
805                                enum set_key_cmd cmd,
806                                struct ieee80211_key_conf *mac80211_key,
807                                const u8 *addr)
808 {
809         switch (mac80211_key->cipher) {
810         case WLAN_CIPHER_SUITE_WEP40:
811         case WLAN_CIPHER_SUITE_WEP104:
812                 if (is_broadcast_ether_addr(addr))
813                         key->key_type = KEY_WEP_DEFAULT;
814                 else
815                         key->key_type = KEY_WEP_ADDR;
816
817                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
818                 break;
819         case WLAN_CIPHER_SUITE_TKIP:
820                 if (is_broadcast_ether_addr(addr))
821                         key->key_type = KEY_TKIP_MIC_GROUP;
822                 else
823                         key->key_type = KEY_TKIP_MIC_PAIRWISE;
824
825                 mac80211_key->hw_key_idx = mac80211_key->keyidx;
826                 break;
827         case WLAN_CIPHER_SUITE_CCMP:
828                 if (is_broadcast_ether_addr(addr))
829                         key->key_type = KEY_AES_GROUP;
830                 else
831                         key->key_type = KEY_AES_PAIRWISE;
832                 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
833                 break;
834         default:
835                 wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
836                 return -EOPNOTSUPP;
837         }
838
839         return 0;
840 }
841
842 static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
843                              struct ieee80211_vif *vif,
844                              struct ieee80211_sta *sta,
845                              struct ieee80211_key_conf *key)
846 {
847         struct wl1251 *wl = hw->priv;
848         struct wl1251_cmd_set_keys *wl_cmd;
849         const u8 *addr;
850         int ret;
851
852         static const u8 bcast_addr[ETH_ALEN] =
853                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
854
855         wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
856
857         wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
858         if (!wl_cmd) {
859                 ret = -ENOMEM;
860                 goto out;
861         }
862
863         addr = sta ? sta->addr : bcast_addr;
864
865         wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
866         wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
867         wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
868                      key->cipher, key->keyidx, key->keylen, key->flags);
869         wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
870
871         if (is_zero_ether_addr(addr)) {
872                 /* We dont support TX only encryption */
873                 ret = -EOPNOTSUPP;
874                 goto out;
875         }
876
877         mutex_lock(&wl->mutex);
878
879         ret = wl1251_ps_elp_wakeup(wl);
880         if (ret < 0)
881                 goto out_unlock;
882
883         switch (cmd) {
884         case SET_KEY:
885                 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
886                 break;
887         case DISABLE_KEY:
888                 wl_cmd->key_action = KEY_REMOVE;
889                 break;
890         default:
891                 wl1251_error("Unsupported key cmd 0x%x", cmd);
892                 break;
893         }
894
895         ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
896         if (ret < 0) {
897                 wl1251_error("Set KEY type failed");
898                 goto out_sleep;
899         }
900
901         if (wl_cmd->key_type != KEY_WEP_DEFAULT)
902                 memcpy(wl_cmd->addr, addr, ETH_ALEN);
903
904         if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
905             (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
906                 /*
907                  * We get the key in the following form:
908                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
909                  * but the target is expecting:
910                  * TKIP - RX MIC - TX MIC
911                  */
912                 memcpy(wl_cmd->key, key->key, 16);
913                 memcpy(wl_cmd->key + 16, key->key + 24, 8);
914                 memcpy(wl_cmd->key + 24, key->key + 16, 8);
915
916         } else {
917                 memcpy(wl_cmd->key, key->key, key->keylen);
918         }
919         wl_cmd->key_size = key->keylen;
920
921         wl_cmd->id = key->keyidx;
922         wl_cmd->ssid_profile = 0;
923
924         wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
925
926         ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
927         if (ret < 0) {
928                 wl1251_warning("could not set keys");
929                 goto out_sleep;
930         }
931
932 out_sleep:
933         wl1251_ps_elp_sleep(wl);
934
935 out_unlock:
936         mutex_unlock(&wl->mutex);
937
938 out:
939         kfree(wl_cmd);
940
941         return ret;
942 }
943
944 static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
945                              struct ieee80211_vif *vif,
946                              struct cfg80211_scan_request *req)
947 {
948         struct wl1251 *wl = hw->priv;
949         struct sk_buff *skb;
950         size_t ssid_len = 0;
951         u8 *ssid = NULL;
952         int ret;
953
954         wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
955
956         if (req->n_ssids) {
957                 ssid = req->ssids[0].ssid;
958                 ssid_len = req->ssids[0].ssid_len;
959         }
960
961         mutex_lock(&wl->mutex);
962
963         if (wl->scanning) {
964                 wl1251_debug(DEBUG_SCAN, "scan already in progress");
965                 ret = -EINVAL;
966                 goto out;
967         }
968
969         ret = wl1251_ps_elp_wakeup(wl);
970         if (ret < 0)
971                 goto out;
972
973         skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
974                                      req->ie, req->ie_len);
975         if (!skb) {
976                 ret = -ENOMEM;
977                 goto out;
978         }
979
980         ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
981                                       skb->len);
982         dev_kfree_skb(skb);
983         if (ret < 0)
984                 goto out_sleep;
985
986         ret = wl1251_cmd_trigger_scan_to(wl, 0);
987         if (ret < 0)
988                 goto out_sleep;
989
990         wl->scanning = true;
991
992         ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
993                               req->n_channels, WL1251_SCAN_NUM_PROBES);
994         if (ret < 0) {
995                 wl->scanning = false;
996                 goto out_sleep;
997         }
998
999 out_sleep:
1000         wl1251_ps_elp_sleep(wl);
1001
1002 out:
1003         mutex_unlock(&wl->mutex);
1004
1005         return ret;
1006 }
1007
1008 static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1009 {
1010         struct wl1251 *wl = hw->priv;
1011         int ret;
1012
1013         mutex_lock(&wl->mutex);
1014
1015         ret = wl1251_ps_elp_wakeup(wl);
1016         if (ret < 0)
1017                 goto out;
1018
1019         ret = wl1251_acx_rts_threshold(wl, (u16) value);
1020         if (ret < 0)
1021                 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
1022
1023         wl1251_ps_elp_sleep(wl);
1024
1025 out:
1026         mutex_unlock(&wl->mutex);
1027
1028         return ret;
1029 }
1030
1031 static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
1032                                        struct ieee80211_vif *vif,
1033                                        struct ieee80211_bss_conf *bss_conf,
1034                                        u32 changed)
1035 {
1036         struct wl1251 *wl = hw->priv;
1037         struct sk_buff *beacon, *skb;
1038         int ret;
1039
1040         wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1041
1042         mutex_lock(&wl->mutex);
1043
1044         ret = wl1251_ps_elp_wakeup(wl);
1045         if (ret < 0)
1046                 goto out;
1047
1048         if (changed & BSS_CHANGED_CQM) {
1049                 ret = wl1251_acx_low_rssi(wl, bss_conf->cqm_rssi_thold,
1050                                           WL1251_DEFAULT_LOW_RSSI_WEIGHT,
1051                                           WL1251_DEFAULT_LOW_RSSI_DEPTH,
1052                                           WL1251_ACX_LOW_RSSI_TYPE_EDGE);
1053                 if (ret < 0)
1054                         goto out;
1055                 wl->rssi_thold = bss_conf->cqm_rssi_thold;
1056         }
1057
1058         if (changed & BSS_CHANGED_BSSID) {
1059                 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1060
1061                 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1062                 if (!skb)
1063                         goto out_sleep;
1064
1065                 ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
1066                                               skb->data, skb->len);
1067                 dev_kfree_skb(skb);
1068                 if (ret < 0)
1069                         goto out_sleep;
1070
1071                 ret = wl1251_build_qos_null_data(wl);
1072                 if (ret < 0)
1073                         goto out;
1074
1075                 if (wl->bss_type != BSS_TYPE_IBSS) {
1076                         ret = wl1251_join(wl, wl->bss_type, wl->channel,
1077                                           wl->beacon_int, wl->dtim_period);
1078                         if (ret < 0)
1079                                 goto out_sleep;
1080                 }
1081         }
1082
1083         if (changed & BSS_CHANGED_ASSOC) {
1084                 if (bss_conf->assoc) {
1085                         wl->beacon_int = bss_conf->beacon_int;
1086
1087                         skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1088                         if (!skb)
1089                                 goto out_sleep;
1090
1091                         ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
1092                                                       skb->data,
1093                                                       skb->len);
1094                         dev_kfree_skb(skb);
1095                         if (ret < 0)
1096                                 goto out_sleep;
1097
1098                         ret = wl1251_acx_aid(wl, bss_conf->aid);
1099                         if (ret < 0)
1100                                 goto out_sleep;
1101                 } else {
1102                         /* use defaults when not associated */
1103                         wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1104                         wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
1105                 }
1106         }
1107         if (changed & BSS_CHANGED_ERP_SLOT) {
1108                 if (bss_conf->use_short_slot)
1109                         ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
1110                 else
1111                         ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
1112                 if (ret < 0) {
1113                         wl1251_warning("Set slot time failed %d", ret);
1114                         goto out_sleep;
1115                 }
1116         }
1117
1118         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1119                 if (bss_conf->use_short_preamble)
1120                         wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1121                 else
1122                         wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1123         }
1124
1125         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1126                 if (bss_conf->use_cts_prot)
1127                         ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1128                 else
1129                         ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1130                 if (ret < 0) {
1131                         wl1251_warning("Set ctsprotect failed %d", ret);
1132                         goto out_sleep;
1133                 }
1134         }
1135
1136         if (changed & BSS_CHANGED_BEACON) {
1137                 beacon = ieee80211_beacon_get(hw, vif);
1138                 if (!beacon)
1139                         goto out_sleep;
1140
1141                 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
1142                                               beacon->len);
1143
1144                 if (ret < 0) {
1145                         dev_kfree_skb(beacon);
1146                         goto out_sleep;
1147                 }
1148
1149                 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
1150                                               beacon->len);
1151
1152                 dev_kfree_skb(beacon);
1153
1154                 if (ret < 0)
1155                         goto out_sleep;
1156
1157                 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1158                                   wl->channel, wl->dtim_period);
1159
1160                 if (ret < 0)
1161                         goto out_sleep;
1162         }
1163
1164 out_sleep:
1165         wl1251_ps_elp_sleep(wl);
1166
1167 out:
1168         mutex_unlock(&wl->mutex);
1169 }
1170
1171
1172 /* can't be const, mac80211 writes to this */
1173 static struct ieee80211_rate wl1251_rates[] = {
1174         { .bitrate = 10,
1175           .hw_value = 0x1,
1176           .hw_value_short = 0x1, },
1177         { .bitrate = 20,
1178           .hw_value = 0x2,
1179           .hw_value_short = 0x2,
1180           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1181         { .bitrate = 55,
1182           .hw_value = 0x4,
1183           .hw_value_short = 0x4,
1184           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1185         { .bitrate = 110,
1186           .hw_value = 0x20,
1187           .hw_value_short = 0x20,
1188           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1189         { .bitrate = 60,
1190           .hw_value = 0x8,
1191           .hw_value_short = 0x8, },
1192         { .bitrate = 90,
1193           .hw_value = 0x10,
1194           .hw_value_short = 0x10, },
1195         { .bitrate = 120,
1196           .hw_value = 0x40,
1197           .hw_value_short = 0x40, },
1198         { .bitrate = 180,
1199           .hw_value = 0x80,
1200           .hw_value_short = 0x80, },
1201         { .bitrate = 240,
1202           .hw_value = 0x200,
1203           .hw_value_short = 0x200, },
1204         { .bitrate = 360,
1205          .hw_value = 0x400,
1206          .hw_value_short = 0x400, },
1207         { .bitrate = 480,
1208           .hw_value = 0x800,
1209           .hw_value_short = 0x800, },
1210         { .bitrate = 540,
1211           .hw_value = 0x1000,
1212           .hw_value_short = 0x1000, },
1213 };
1214
1215 /* can't be const, mac80211 writes to this */
1216 static struct ieee80211_channel wl1251_channels[] = {
1217         { .hw_value = 1, .center_freq = 2412},
1218         { .hw_value = 2, .center_freq = 2417},
1219         { .hw_value = 3, .center_freq = 2422},
1220         { .hw_value = 4, .center_freq = 2427},
1221         { .hw_value = 5, .center_freq = 2432},
1222         { .hw_value = 6, .center_freq = 2437},
1223         { .hw_value = 7, .center_freq = 2442},
1224         { .hw_value = 8, .center_freq = 2447},
1225         { .hw_value = 9, .center_freq = 2452},
1226         { .hw_value = 10, .center_freq = 2457},
1227         { .hw_value = 11, .center_freq = 2462},
1228         { .hw_value = 12, .center_freq = 2467},
1229         { .hw_value = 13, .center_freq = 2472},
1230 };
1231
1232 static int wl1251_op_conf_tx(struct ieee80211_hw *hw,
1233                              struct ieee80211_vif *vif, u16 queue,
1234                              const struct ieee80211_tx_queue_params *params)
1235 {
1236         enum wl1251_acx_ps_scheme ps_scheme;
1237         struct wl1251 *wl = hw->priv;
1238         int ret;
1239
1240         mutex_lock(&wl->mutex);
1241
1242         wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1243
1244         ret = wl1251_ps_elp_wakeup(wl);
1245         if (ret < 0)
1246                 goto out;
1247
1248         /* mac80211 uses units of 32 usec */
1249         ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
1250                                 params->cw_min, params->cw_max,
1251                                 params->aifs, params->txop * 32);
1252         if (ret < 0)
1253                 goto out_sleep;
1254
1255         if (params->uapsd)
1256                 ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
1257         else
1258                 ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
1259
1260         ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
1261                                  CHANNEL_TYPE_EDCF,
1262                                  wl1251_tx_get_queue(queue), ps_scheme,
1263                                  WL1251_ACX_ACK_POLICY_LEGACY);
1264         if (ret < 0)
1265                 goto out_sleep;
1266
1267 out_sleep:
1268         wl1251_ps_elp_sleep(wl);
1269
1270 out:
1271         mutex_unlock(&wl->mutex);
1272
1273         return ret;
1274 }
1275
1276 static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
1277                                 struct survey_info *survey)
1278 {
1279         struct wl1251 *wl = hw->priv;
1280         struct ieee80211_conf *conf = &hw->conf;
1281  
1282         if (idx != 0)
1283                 return -ENOENT;
1284  
1285         survey->channel = conf->channel;
1286         survey->filled = SURVEY_INFO_NOISE_DBM;
1287         survey->noise = wl->noise;
1288  
1289         return 0;
1290 }
1291
1292 /* can't be const, mac80211 writes to this */
1293 static struct ieee80211_supported_band wl1251_band_2ghz = {
1294         .channels = wl1251_channels,
1295         .n_channels = ARRAY_SIZE(wl1251_channels),
1296         .bitrates = wl1251_rates,
1297         .n_bitrates = ARRAY_SIZE(wl1251_rates),
1298 };
1299
1300 static const struct ieee80211_ops wl1251_ops = {
1301         .start = wl1251_op_start,
1302         .stop = wl1251_op_stop,
1303         .add_interface = wl1251_op_add_interface,
1304         .remove_interface = wl1251_op_remove_interface,
1305         .config = wl1251_op_config,
1306         .configure_filter = wl1251_op_configure_filter,
1307         .tx = wl1251_op_tx,
1308         .set_key = wl1251_op_set_key,
1309         .hw_scan = wl1251_op_hw_scan,
1310         .bss_info_changed = wl1251_op_bss_info_changed,
1311         .set_rts_threshold = wl1251_op_set_rts_threshold,
1312         .conf_tx = wl1251_op_conf_tx,
1313         .get_survey = wl1251_op_get_survey,
1314 };
1315
1316 static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
1317 {
1318         unsigned long timeout;
1319
1320         wl1251_reg_write32(wl, EE_ADDR, offset);
1321         wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
1322
1323         /* EE_CTL_READ clears when data is ready */
1324         timeout = jiffies + msecs_to_jiffies(100);
1325         while (1) {
1326                 if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
1327                         break;
1328
1329                 if (time_after(jiffies, timeout))
1330                         return -ETIMEDOUT;
1331
1332                 msleep(1);
1333         }
1334
1335         *data = wl1251_reg_read32(wl, EE_DATA);
1336         return 0;
1337 }
1338
1339 static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
1340                               u8 *data, size_t len)
1341 {
1342         size_t i;
1343         int ret;
1344
1345         wl1251_reg_write32(wl, EE_START, 0);
1346
1347         for (i = 0; i < len; i++) {
1348                 ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
1349                 if (ret < 0)
1350                         return ret;
1351         }
1352
1353         return 0;
1354 }
1355
1356 static int wl1251_read_eeprom_mac(struct wl1251 *wl)
1357 {
1358         u8 mac[ETH_ALEN];
1359         int i, ret;
1360
1361         wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1362
1363         ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
1364         if (ret < 0) {
1365                 wl1251_warning("failed to read MAC address from EEPROM");
1366                 return ret;
1367         }
1368
1369         /* MAC is stored in reverse order */
1370         for (i = 0; i < ETH_ALEN; i++)
1371                 wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
1372
1373         return 0;
1374 }
1375
1376 /* temporary (?) hack for EEPROM dumping
1377  * (it seems this can only be done before fw is running) */
1378 static int wl1251_dump_eeprom(struct wl1251 *wl)
1379 {
1380         int ret;
1381
1382         wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1383
1384         wl->eeprom_dump = kzalloc(1024, GFP_KERNEL);
1385         if (wl->eeprom_dump == NULL) {
1386                 ret = -ENOMEM;
1387                 goto out;
1388         }
1389
1390         ret = wl1251_read_eeprom(wl, 0, wl->eeprom_dump, 1024);
1391         if (ret != 0) {
1392                 wl1251_error("eeprom dump failed: %d", ret);
1393                 kfree(wl->eeprom_dump);
1394                 wl->eeprom_dump = NULL;
1395                 goto out;
1396         }
1397
1398         wl1251_info("eeprom dumped.");
1399
1400 out:
1401         return ret;
1402 }
1403
1404 static int wl1251_register_hw(struct wl1251 *wl)
1405 {
1406         int ret;
1407
1408         if (wl->mac80211_registered)
1409                 return 0;
1410
1411         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1412
1413         ret = ieee80211_register_hw(wl->hw);
1414         if (ret < 0) {
1415                 wl1251_error("unable to register mac80211 hw: %d", ret);
1416                 return ret;
1417         }
1418
1419         wl->mac80211_registered = true;
1420
1421         wl1251_notice("loaded");
1422
1423         return 0;
1424 }
1425
1426 int wl1251_init_ieee80211(struct wl1251 *wl)
1427 {
1428         int ret;
1429
1430         /* The tx descriptor buffer and the TKIP space */
1431         wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
1432                 + WL1251_TKIP_IV_SPACE;
1433
1434         /* unit us */
1435         /* FIXME: find a proper value */
1436         wl->hw->channel_change_time = 10000;
1437
1438         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1439                 IEEE80211_HW_SUPPORTS_PS |
1440                 IEEE80211_HW_BEACON_FILTER |
1441                 IEEE80211_HW_SUPPORTS_UAPSD |
1442                 IEEE80211_HW_SUPPORTS_CQM_RSSI;
1443
1444         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1445                                          BIT(NL80211_IFTYPE_ADHOC);
1446         wl->hw->wiphy->max_scan_ssids = 1;
1447         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
1448
1449         wl->hw->queues = 4;
1450
1451         if (wl->use_eeprom)
1452                 wl1251_read_eeprom_mac(wl);
1453         if (wl->dump_eeprom)
1454                 wl1251_dump_eeprom(wl);
1455
1456         ret = wl1251_register_hw(wl);
1457         if (ret)
1458                 goto out;
1459
1460         wl1251_debugfs_init(wl);
1461         wl1251_notice("initialized");
1462
1463         ret = 0;
1464
1465 out:
1466         return ret;
1467 }
1468 EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
1469
1470 struct ieee80211_hw *wl1251_alloc_hw(void)
1471 {
1472         struct ieee80211_hw *hw;
1473         struct wl1251 *wl;
1474         int i;
1475         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1476
1477         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
1478         if (!hw) {
1479                 wl1251_error("could not alloc ieee80211_hw");
1480                 return ERR_PTR(-ENOMEM);
1481         }
1482
1483         wl = hw->priv;
1484         memset(wl, 0, sizeof(*wl));
1485
1486         wl->hw = hw;
1487
1488         wl->data_in_count = 0;
1489
1490         skb_queue_head_init(&wl->tx_queue);
1491
1492         INIT_DELAYED_WORK(&wl->ps_work, wl1251_ps_work);
1493         INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
1494         wl->channel = WL1251_DEFAULT_CHANNEL;
1495         wl->scanning = false;
1496         wl->default_key = 0;
1497         wl->listen_int = 1;
1498         wl->rx_counter = 0;
1499         wl->rx_handled = 0;
1500         wl->rx_current_buffer = 0;
1501         wl->rx_last_id = 0;
1502         wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1503         wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
1504         wl->elp = false;
1505         wl->station_mode = STATION_ACTIVE_MODE;
1506         wl->psm_requested = false;
1507         wl->tx_queue_stopped = false;
1508         wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
1509         wl->rssi_thold = 0;
1510         wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1511         wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
1512         wl->vif = NULL;
1513
1514         for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1515                 wl->tx_frames[i] = NULL;
1516
1517         wl->next_tx_complete = 0;
1518
1519         INIT_WORK(&wl->irq_work, wl1251_irq_work);
1520         INIT_WORK(&wl->tx_work, wl1251_tx_work);
1521
1522         /*
1523          * In case our MAC address is not correctly set,
1524          * we use a random but Nokia MAC.
1525          */
1526         memcpy(wl->mac_addr, nokia_oui, 3);
1527         get_random_bytes(wl->mac_addr + 3, 3);
1528
1529         wl->state = WL1251_STATE_OFF;
1530         mutex_init(&wl->mutex);
1531
1532         wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1533         wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1534
1535         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1536         if (!wl->rx_descriptor) {
1537                 wl1251_error("could not allocate memory for rx descriptor");
1538                 ieee80211_free_hw(hw);
1539                 return ERR_PTR(-ENOMEM);
1540         }
1541
1542         return hw;
1543 }
1544 EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
1545
1546 int wl1251_free_hw(struct wl1251 *wl)
1547 {
1548         ieee80211_unregister_hw(wl->hw);
1549
1550         wl1251_debugfs_exit(wl);
1551
1552         kfree(wl->target_mem_map);
1553         kfree(wl->data_path);
1554         vfree(wl->fw);
1555         wl->fw = NULL;
1556         kfree(wl->nvs);
1557         wl->nvs = NULL;
1558
1559         kfree(wl->rx_descriptor);
1560         wl->rx_descriptor = NULL;
1561
1562         ieee80211_free_hw(wl->hw);
1563
1564         if (wl->eeprom_dump != NULL)
1565                 kfree(wl->eeprom_dump);
1566
1567         return 0;
1568 }
1569 EXPORT_SYMBOL_GPL(wl1251_free_hw);
1570
1571 MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1572 MODULE_LICENSE("GPL");
1573 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
1574 MODULE_FIRMWARE(WL1251_FW_NAME);