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