2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
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/vmalloc.h>
34 #include <linux/spi/wl12xx.h>
37 #include "wl12xx_80211.h"
38 #include "wl1271_reg.h"
39 #include "wl1271_spi.h"
40 #include "wl1271_event.h"
41 #include "wl1271_tx.h"
42 #include "wl1271_rx.h"
43 #include "wl1271_ps.h"
44 #include "wl1271_init.h"
45 #include "wl1271_debugfs.h"
46 #include "wl1271_cmd.h"
47 #include "wl1271_boot.h"
49 static int wl1271_plt_init(struct wl1271 *wl)
53 ret = wl1271_acx_init_mem_config(wl);
57 ret = wl1271_cmd_data_path(wl, wl->channel, 1);
64 static void wl1271_disable_interrupts(struct wl1271 *wl)
69 static void wl1271_power_off(struct wl1271 *wl)
74 static void wl1271_power_on(struct wl1271 *wl)
79 static void wl1271_fw_status(struct wl1271 *wl, struct wl1271_fw_status *status)
85 * FIXME: Reading the FW status directly from the registers seems to
86 * be the right thing to do, but it doesn't work. And in the
87 * reference driver, there is a workaround called
88 * USE_SDIO_24M_WORKAROUND, which reads the status from memory
89 * instead, so we do the same here.
92 wl1271_spi_mem_read(wl, STATUS_MEM_ADDRESS, status, sizeof(*status));
94 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
95 "drv_rx_counter = %d, tx_results_counter = %d)",
97 status->fw_rx_counter,
98 status->drv_rx_counter,
99 status->tx_results_counter);
101 /* update number of available TX blocks */
102 for (i = 0; i < NUM_TX_QUEUES; i++) {
103 u32 cnt = status->tx_released_blks[i] - wl->tx_blocks_freed[i];
104 wl->tx_blocks_freed[i] = status->tx_released_blks[i];
105 wl->tx_blocks_available += cnt;
109 /* if more blocks are available now, schedule some tx work */
110 if (total && !skb_queue_empty(&wl->tx_queue))
111 ieee80211_queue_work(wl->hw, &wl->tx_work);
113 /* update the host-chipset time offset */
114 wl->time_offset = jiffies_to_usecs(jiffies) - status->fw_localtime;
117 #define WL1271_IRQ_MAX_LOOPS 10
118 static void wl1271_irq_work(struct work_struct *work)
120 u32 intr, ctr = WL1271_IRQ_MAX_LOOPS;
123 container_of(work, struct wl1271, irq_work);
125 mutex_lock(&wl->mutex);
127 wl1271_debug(DEBUG_IRQ, "IRQ work");
129 if (wl->state == WL1271_STATE_OFF)
132 ret = wl1271_ps_elp_wakeup(wl, true);
136 wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
138 intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
140 wl1271_debug(DEBUG_IRQ, "Zero interrupt received.");
144 intr &= WL1271_INTR_MASK;
147 wl1271_fw_status(wl, wl->fw_status);
150 if (intr & (WL1271_ACX_INTR_EVENT_A |
151 WL1271_ACX_INTR_EVENT_B)) {
152 wl1271_debug(DEBUG_IRQ,
153 "WL1271_ACX_INTR_EVENT (0x%x)", intr);
154 if (intr & WL1271_ACX_INTR_EVENT_A)
155 wl1271_event_handle(wl, 0);
157 wl1271_event_handle(wl, 1);
160 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
161 wl1271_debug(DEBUG_IRQ,
162 "WL1271_ACX_INTR_INIT_COMPLETE");
164 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
165 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
167 if (intr & WL1271_ACX_INTR_DATA) {
168 u8 tx_res_cnt = wl->fw_status->tx_results_counter -
169 wl->tx_results_count;
171 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
173 /* check for tx results */
175 wl1271_tx_complete(wl, tx_res_cnt);
177 wl1271_rx(wl, wl->fw_status);
180 intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
181 intr &= WL1271_INTR_MASK;
182 } while (intr && --ctr);
185 wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
186 WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
187 wl1271_ps_elp_sleep(wl);
190 mutex_unlock(&wl->mutex);
193 static irqreturn_t wl1271_irq(int irq, void *cookie)
198 wl1271_debug(DEBUG_IRQ, "IRQ");
202 /* complete the ELP completion */
203 spin_lock_irqsave(&wl->wl_lock, flags);
205 complete(wl->elp_compl);
206 wl->elp_compl = NULL;
209 ieee80211_queue_work(wl->hw, &wl->irq_work);
210 spin_unlock_irqrestore(&wl->wl_lock, flags);
215 static int wl1271_fetch_firmware(struct wl1271 *wl)
217 const struct firmware *fw;
220 ret = request_firmware(&fw, WL1271_FW_NAME, &wl->spi->dev);
223 wl1271_error("could not get firmware: %d", ret);
228 wl1271_error("firmware size is not multiple of 32 bits: %zu",
234 wl->fw_len = fw->size;
235 wl->fw = vmalloc(wl->fw_len);
238 wl1271_error("could not allocate memory for the firmware");
243 memcpy(wl->fw, fw->data, wl->fw_len);
248 release_firmware(fw);
253 static int wl1271_fetch_nvs(struct wl1271 *wl)
255 const struct firmware *fw;
258 ret = request_firmware(&fw, WL1271_NVS_NAME, &wl->spi->dev);
261 wl1271_error("could not get nvs file: %d", ret);
266 wl1271_error("nvs size is not multiple of 32 bits: %zu",
272 wl->nvs_len = fw->size;
273 wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
276 wl1271_error("could not allocate memory for the nvs file");
281 memcpy(wl->nvs, fw->data, wl->nvs_len);
286 release_firmware(fw);
291 static void wl1271_fw_wakeup(struct wl1271 *wl)
295 elp_reg = ELPCTRL_WAKE_UP;
296 wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
299 static int wl1271_setup(struct wl1271 *wl)
301 wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
305 wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
306 if (!wl->tx_res_if) {
307 kfree(wl->fw_status);
311 INIT_WORK(&wl->irq_work, wl1271_irq_work);
312 INIT_WORK(&wl->tx_work, wl1271_tx_work);
316 static int wl1271_chip_wakeup(struct wl1271 *wl)
321 msleep(WL1271_POWER_ON_SLEEP);
322 wl1271_spi_reset(wl);
325 /* We don't need a real memory partition here, because we only want
326 * to use the registers at this point. */
327 wl1271_set_partition(wl,
331 REGISTERS_DOWN_SIZE);
333 /* ELP module wake up */
334 wl1271_fw_wakeup(wl);
336 /* whal_FwCtrl_BootSm() */
338 /* 0. read chip id from CHIP_ID */
339 wl->chip.id = wl1271_reg_read32(wl, CHIP_ID_B);
341 /* 1. check if chip id is valid */
343 switch (wl->chip.id) {
344 case CHIP_ID_1271_PG10:
345 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
348 ret = wl1271_setup(wl);
352 case CHIP_ID_1271_PG20:
353 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
356 ret = wl1271_setup(wl);
361 wl1271_error("unsupported chip id: 0x%x", wl->chip.id);
366 if (wl->fw == NULL) {
367 ret = wl1271_fetch_firmware(wl);
372 /* No NVS from netlink, try to get it from the filesystem */
373 if (wl->nvs == NULL) {
374 ret = wl1271_fetch_nvs(wl);
383 struct wl1271_filter_params {
384 unsigned int filters;
385 unsigned int changed;
387 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
390 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
393 FIF_BCN_PRBRESP_PROMISC | \
397 static void wl1271_filter_work(struct work_struct *work)
400 container_of(work, struct wl1271, filter_work);
401 struct wl1271_filter_params *fp;
406 /* first, get the filter parameters */
407 spin_lock_irqsave(&wl->wl_lock, flags);
408 fp = wl->filter_params;
409 wl->filter_params = NULL;
410 spin_unlock_irqrestore(&wl->wl_lock, flags);
415 /* then, lock the mutex without risk of lock-up */
416 mutex_lock(&wl->mutex);
418 if (wl->state == WL1271_STATE_OFF)
421 ret = wl1271_ps_elp_wakeup(wl, false);
425 /* configure the mc filter regardless of the changed flags */
426 if (fp->filters & FIF_ALLMULTI)
429 ret = wl1271_acx_group_address_tbl(wl, enabled,
430 fp->mc_list, fp->mc_list_length);
434 /* determine, whether supported filter values have changed */
435 if (fp->changed == 0)
438 /* apply configured filters */
439 ret = wl1271_cmd_join(wl);
444 wl1271_ps_elp_sleep(wl);
447 mutex_unlock(&wl->mutex);
451 int wl1271_plt_start(struct wl1271 *wl)
455 mutex_lock(&wl->mutex);
457 wl1271_notice("power up");
459 if (wl->state != WL1271_STATE_OFF) {
460 wl1271_error("cannot go into PLT state because not "
461 "in off state: %d", wl->state);
466 wl->state = WL1271_STATE_PLT;
468 ret = wl1271_chip_wakeup(wl);
472 ret = wl1271_boot(wl);
476 wl1271_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
478 ret = wl1271_plt_init(wl);
483 mutex_unlock(&wl->mutex);
488 int wl1271_plt_stop(struct wl1271 *wl)
492 mutex_lock(&wl->mutex);
494 wl1271_notice("power down");
496 if (wl->state != WL1271_STATE_PLT) {
497 wl1271_error("cannot power down because not in PLT "
498 "state: %d", wl->state);
503 wl1271_disable_interrupts(wl);
504 wl1271_power_off(wl);
506 wl->state = WL1271_STATE_OFF;
509 mutex_unlock(&wl->mutex);
515 static int wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
517 struct wl1271 *wl = hw->priv;
519 skb_queue_tail(&wl->tx_queue, skb);
522 * The chip specific setup must run before the first TX packet -
523 * before that, the tx_work will not be initialized!
526 ieee80211_queue_work(wl->hw, &wl->tx_work);
529 * The workqueue is slow to process the tx_queue and we need stop
530 * the queue here, otherwise the queue will get too long.
532 if (skb_queue_len(&wl->tx_queue) >= WL1271_TX_QUEUE_MAX_LENGTH) {
533 ieee80211_stop_queues(wl->hw);
536 * FIXME: this is racy, the variable is not properly
537 * protected. Maybe fix this by removing the stupid
538 * variable altogether and checking the real queue state?
540 wl->tx_queue_stopped = true;
546 static int wl1271_op_start(struct ieee80211_hw *hw)
548 struct wl1271 *wl = hw->priv;
551 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
553 mutex_lock(&wl->mutex);
555 if (wl->state != WL1271_STATE_OFF) {
556 wl1271_error("cannot start because not in off state: %d",
562 ret = wl1271_chip_wakeup(wl);
566 ret = wl1271_boot(wl);
570 ret = wl1271_hw_init(wl);
574 wl->state = WL1271_STATE_ON;
576 wl1271_info("firmware booted (%s)", wl->chip.fw_ver);
580 wl1271_power_off(wl);
582 mutex_unlock(&wl->mutex);
587 static void wl1271_op_stop(struct ieee80211_hw *hw)
589 struct wl1271 *wl = hw->priv;
595 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
597 /* complete/cancel ongoing work */
598 cancel_work_sync(&wl->filter_work);
599 spin_lock_irqsave(&wl->wl_lock, flags);
600 kfree(wl->filter_params);
601 wl->filter_params = NULL;
602 spin_unlock_irqrestore(&wl->wl_lock, flags);
604 mutex_lock(&wl->mutex);
606 WARN_ON(wl->state != WL1271_STATE_ON);
609 mutex_unlock(&wl->mutex);
610 ieee80211_scan_completed(wl->hw, true);
611 mutex_lock(&wl->mutex);
612 wl->scanning = false;
615 wl->state = WL1271_STATE_OFF;
617 wl1271_disable_interrupts(wl);
619 mutex_unlock(&wl->mutex);
621 cancel_work_sync(&wl->irq_work);
622 cancel_work_sync(&wl->tx_work);
623 cancel_work_sync(&wl->filter_work);
625 mutex_lock(&wl->mutex);
627 /* let's notify MAC80211 about the remaining pending TX frames */
629 wl1271_power_off(wl);
631 memset(wl->bssid, 0, ETH_ALEN);
632 memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
635 wl->bss_type = MAX_BSS_TYPE;
636 wl->band = IEEE80211_BAND_2GHZ;
641 wl->tx_queue_stopped = false;
642 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
643 wl->tx_blocks_available = 0;
644 wl->tx_results_count = 0;
645 wl->tx_packets_count = 0;
646 wl->tx_security_last_seq = 0;
647 wl->tx_security_seq_16 = 0;
648 wl->tx_security_seq_32 = 0;
650 wl->session_counter = 0;
651 for (i = 0; i < NUM_TX_QUEUES; i++)
652 wl->tx_blocks_freed[i] = 0;
654 wl1271_debugfs_reset(wl);
655 mutex_unlock(&wl->mutex);
658 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
659 struct ieee80211_if_init_conf *conf)
661 struct wl1271 *wl = hw->priv;
664 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
665 conf->type, conf->mac_addr);
667 mutex_lock(&wl->mutex);
669 switch (conf->type) {
670 case NL80211_IFTYPE_STATION:
671 wl->bss_type = BSS_TYPE_STA_BSS;
673 case NL80211_IFTYPE_ADHOC:
674 wl->bss_type = BSS_TYPE_IBSS;
681 /* FIXME: what if conf->mac_addr changes? */
684 mutex_unlock(&wl->mutex);
688 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
689 struct ieee80211_if_init_conf *conf)
691 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
695 static int wl1271_op_config_interface(struct ieee80211_hw *hw,
696 struct ieee80211_vif *vif,
697 struct ieee80211_if_conf *conf)
699 struct wl1271 *wl = hw->priv;
700 struct sk_buff *beacon;
703 wl1271_debug(DEBUG_MAC80211, "mac80211 config_interface bssid %pM",
705 wl1271_dump_ascii(DEBUG_MAC80211, "ssid: ", conf->ssid,
708 mutex_lock(&wl->mutex);
710 ret = wl1271_ps_elp_wakeup(wl, false);
714 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
716 ret = wl1271_cmd_build_null_data(wl);
720 wl->ssid_len = conf->ssid_len;
722 memcpy(wl->ssid, conf->ssid, wl->ssid_len);
724 if (wl->bss_type != BSS_TYPE_IBSS) {
725 ret = wl1271_cmd_join(wl);
730 if (conf->changed & IEEE80211_IFCC_BEACON) {
731 beacon = ieee80211_beacon_get(hw, vif);
732 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
733 beacon->data, beacon->len);
736 dev_kfree_skb(beacon);
740 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PROBE_RESPONSE,
741 beacon->data, beacon->len);
743 dev_kfree_skb(beacon);
748 ret = wl1271_cmd_join(wl);
755 wl1271_ps_elp_sleep(wl);
758 mutex_unlock(&wl->mutex);
764 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
766 struct wl1271 *wl = hw->priv;
767 struct ieee80211_conf *conf = &hw->conf;
768 int channel, ret = 0;
770 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
772 wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
774 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
777 mutex_lock(&wl->mutex);
779 wl->band = conf->channel->band;
781 ret = wl1271_ps_elp_wakeup(wl, false);
785 if (channel != wl->channel) {
786 u8 old_channel = wl->channel;
787 wl->channel = channel;
789 ret = wl1271_cmd_join(wl);
791 wl->channel = old_channel;
796 ret = wl1271_cmd_build_null_data(wl);
800 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
801 wl1271_info("psm enabled");
803 wl->psm_requested = true;
806 * We enter PSM only if we're already associated.
807 * If we're not, we'll enter it when joining an SSID,
808 * through the bss_info_changed() hook.
810 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
811 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
813 wl1271_info("psm disabled");
815 wl->psm_requested = false;
818 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE);
821 if (conf->power_level != wl->power_level) {
822 ret = wl1271_acx_tx_power(wl, conf->power_level);
826 wl->power_level = conf->power_level;
830 wl1271_ps_elp_sleep(wl);
833 mutex_unlock(&wl->mutex);
838 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
839 struct dev_addr_list *mc_list)
841 struct wl1271 *wl = hw->priv;
842 struct wl1271_filter_params *fp;
847 * FIXME: we should return a hash that will be passed to
848 * configure_filter() instead of saving everything in the context.
851 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
853 wl1271_error("Out of memory setting filters.");
857 /* update multicast filtering parameters */
858 if (mc_count > ACX_MC_ADDRESS_GROUP_MAX) {
860 fp->filters |= FIF_ALLMULTI;
863 fp->mc_list_length = 0;
864 for (i = 0; i < mc_count; i++) {
865 if (mc_list->da_addrlen == ETH_ALEN) {
866 memcpy(fp->mc_list[fp->mc_list_length],
867 mc_list->da_addr, ETH_ALEN);
868 fp->mc_list_length++;
870 wl1271_warning("Unknown mc address length.");
873 spin_lock_irqsave(&wl->wl_lock, flags);
874 kfree(wl->filter_params);
875 wl->filter_params = fp;
876 spin_unlock_irqrestore(&wl->wl_lock, flags);
881 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
882 unsigned int changed,
883 unsigned int *total, u64 multicast)
885 struct wl1271 *wl = hw->priv;
887 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
889 *total &= WL1271_SUPPORTED_FILTERS;
890 changed &= WL1271_SUPPORTED_FILTERS;
896 * FIXME: for now we are still using a workqueue for filter
897 * configuration, but with the new mac80211, this is not needed,
898 * since configure_filter can now sleep. We now have
899 * prepare_multicast, which needs to be atomic instead.
902 /* store current filter config */
903 wl->filter_params->filters = *total;
904 wl->filter_params->changed = changed;
906 ieee80211_queue_work(wl->hw, &wl->filter_work);
909 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
910 struct ieee80211_vif *vif,
911 struct ieee80211_sta *sta,
912 struct ieee80211_key_conf *key_conf)
914 struct wl1271 *wl = hw->priv;
921 static const u8 bcast_addr[ETH_ALEN] =
922 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
924 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
926 addr = sta ? sta->addr : bcast_addr;
928 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
929 wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
930 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
931 key_conf->alg, key_conf->keyidx,
932 key_conf->keylen, key_conf->flags);
933 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
935 if (is_zero_ether_addr(addr)) {
936 /* We dont support TX only encryption */
941 mutex_lock(&wl->mutex);
943 ret = wl1271_ps_elp_wakeup(wl, false);
947 switch (key_conf->alg) {
951 key_conf->hw_key_idx = key_conf->keyidx;
956 key_conf->hw_key_idx = key_conf->keyidx;
957 tx_seq_32 = wl->tx_security_seq_32;
958 tx_seq_16 = wl->tx_security_seq_16;
963 key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
964 tx_seq_32 = wl->tx_security_seq_32;
965 tx_seq_16 = wl->tx_security_seq_16;
968 wl1271_error("Unknown key algo 0x%x", key_conf->alg);
976 ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
977 key_conf->keyidx, key_type,
978 key_conf->keylen, key_conf->key,
979 addr, tx_seq_32, tx_seq_16);
981 wl1271_error("Could not add or replace key");
987 ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
988 key_conf->keyidx, key_type,
989 key_conf->keylen, key_conf->key,
992 wl1271_error("Could not remove key");
998 wl1271_error("Unsupported key cmd 0x%x", cmd);
1006 wl1271_ps_elp_sleep(wl);
1009 mutex_unlock(&wl->mutex);
1015 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
1016 struct cfg80211_scan_request *req)
1018 struct wl1271 *wl = hw->priv;
1021 size_t ssid_len = 0;
1023 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
1026 ssid = req->ssids[0].ssid;
1027 ssid_len = req->ssids[0].ssid_len;
1030 mutex_lock(&wl->mutex);
1032 ret = wl1271_ps_elp_wakeup(wl, false);
1036 ret = wl1271_cmd_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
1038 wl1271_ps_elp_sleep(wl);
1041 mutex_unlock(&wl->mutex);
1046 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1048 struct wl1271 *wl = hw->priv;
1051 mutex_lock(&wl->mutex);
1053 ret = wl1271_ps_elp_wakeup(wl, false);
1057 ret = wl1271_acx_rts_threshold(wl, (u16) value);
1059 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
1061 wl1271_ps_elp_sleep(wl);
1064 mutex_unlock(&wl->mutex);
1069 static u32 wl1271_enabled_rates_get(struct wl1271 *wl, u64 basic_rate_set)
1071 struct ieee80211_supported_band *band;
1072 u32 enabled_rates = 0;
1075 band = wl->hw->wiphy->bands[wl->band];
1076 for (bit = 0; bit < band->n_bitrates; bit++) {
1077 if (basic_rate_set & 0x1)
1078 enabled_rates |= band->bitrates[bit].hw_value;
1079 basic_rate_set >>= 1;
1082 return enabled_rates;
1085 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
1086 struct ieee80211_vif *vif,
1087 struct ieee80211_bss_conf *bss_conf,
1090 enum wl1271_cmd_ps_mode mode;
1091 struct wl1271 *wl = hw->priv;
1094 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1096 mutex_lock(&wl->mutex);
1098 ret = wl1271_ps_elp_wakeup(wl, false);
1102 if (changed & BSS_CHANGED_ASSOC) {
1103 if (bss_conf->assoc) {
1104 wl->beacon_int = bss_conf->beacon_int;
1105 wl->dtim_period = bss_conf->dtim_period;
1106 wl->aid = bss_conf->aid;
1108 ret = wl1271_cmd_join(wl);
1110 wl1271_warning("Association configuration "
1115 ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
1119 ret = wl1271_acx_aid(wl, wl->aid);
1123 /* If we want to go in PSM but we're not there yet */
1124 if (wl->psm_requested && !wl->psm) {
1125 mode = STATION_POWER_SAVE_MODE;
1126 ret = wl1271_ps_set_mode(wl, mode);
1131 /* use defaults when not associated */
1132 wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
1133 wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
1134 wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1140 if (changed & BSS_CHANGED_ERP_SLOT) {
1141 if (bss_conf->use_short_slot)
1142 ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
1144 ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
1146 wl1271_warning("Set slot time failed %d", ret);
1151 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1152 if (bss_conf->use_short_preamble)
1153 wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1155 wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1158 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1159 if (bss_conf->use_cts_prot)
1160 ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1162 ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1164 wl1271_warning("Set ctsprotect failed %d", ret);
1169 if (changed & BSS_CHANGED_BASIC_RATES) {
1170 wl->basic_rate_set = wl1271_enabled_rates_get(
1171 wl, bss_conf->basic_rates);
1172 ret = wl1271_acx_rate_policies(wl, wl->basic_rate_set);
1175 wl1271_warning("Set rate policies failed %d", ret);
1178 ret = wl1271_cmd_join(wl);
1180 wl1271_warning("Join with new basic rate "
1181 "set failed %d", ret);
1187 wl1271_ps_elp_sleep(wl);
1190 mutex_unlock(&wl->mutex);
1194 /* can't be const, mac80211 writes to this */
1195 static struct ieee80211_rate wl1271_rates[] = {
1198 .hw_value_short = 0x1, },
1201 .hw_value_short = 0x2,
1202 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1205 .hw_value_short = 0x4,
1206 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1209 .hw_value_short = 0x20,
1210 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1213 .hw_value_short = 0x8, },
1216 .hw_value_short = 0x10, },
1219 .hw_value_short = 0x40, },
1222 .hw_value_short = 0x80, },
1225 .hw_value_short = 0x200, },
1228 .hw_value_short = 0x400, },
1231 .hw_value_short = 0x800, },
1234 .hw_value_short = 0x1000, },
1237 /* can't be const, mac80211 writes to this */
1238 static struct ieee80211_channel wl1271_channels[] = {
1239 { .hw_value = 1, .center_freq = 2412},
1240 { .hw_value = 2, .center_freq = 2417},
1241 { .hw_value = 3, .center_freq = 2422},
1242 { .hw_value = 4, .center_freq = 2427},
1243 { .hw_value = 5, .center_freq = 2432},
1244 { .hw_value = 6, .center_freq = 2437},
1245 { .hw_value = 7, .center_freq = 2442},
1246 { .hw_value = 8, .center_freq = 2447},
1247 { .hw_value = 9, .center_freq = 2452},
1248 { .hw_value = 10, .center_freq = 2457},
1249 { .hw_value = 11, .center_freq = 2462},
1250 { .hw_value = 12, .center_freq = 2467},
1251 { .hw_value = 13, .center_freq = 2472},
1254 /* can't be const, mac80211 writes to this */
1255 static struct ieee80211_supported_band wl1271_band_2ghz = {
1256 .channels = wl1271_channels,
1257 .n_channels = ARRAY_SIZE(wl1271_channels),
1258 .bitrates = wl1271_rates,
1259 .n_bitrates = ARRAY_SIZE(wl1271_rates),
1262 static const struct ieee80211_ops wl1271_ops = {
1263 .start = wl1271_op_start,
1264 .stop = wl1271_op_stop,
1265 .add_interface = wl1271_op_add_interface,
1266 .remove_interface = wl1271_op_remove_interface,
1267 .config = wl1271_op_config,
1268 /* .config_interface = wl1271_op_config_interface, */
1269 .prepare_multicast = wl1271_op_prepare_multicast,
1270 .configure_filter = wl1271_op_configure_filter,
1272 .set_key = wl1271_op_set_key,
1273 .hw_scan = wl1271_op_hw_scan,
1274 .bss_info_changed = wl1271_op_bss_info_changed,
1275 .set_rts_threshold = wl1271_op_set_rts_threshold,
1278 static int wl1271_register_hw(struct wl1271 *wl)
1282 if (wl->mac80211_registered)
1285 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1287 ret = ieee80211_register_hw(wl->hw);
1289 wl1271_error("unable to register mac80211 hw: %d", ret);
1293 wl->mac80211_registered = true;
1295 wl1271_notice("loaded");
1300 static int wl1271_init_ieee80211(struct wl1271 *wl)
1302 /* The tx descriptor buffer and the TKIP space. */
1303 wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
1304 sizeof(struct wl1271_tx_hw_descr);
1307 /* FIXME: find a proper value */
1308 wl->hw->channel_change_time = 10000;
1310 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1311 IEEE80211_HW_NOISE_DBM;
1313 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1314 wl->hw->wiphy->max_scan_ssids = 1;
1315 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
1317 SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1322 static void wl1271_device_release(struct device *dev)
1327 static struct platform_device wl1271_device = {
1331 /* device model insists to have a release function */
1333 .release = wl1271_device_release,
1337 #define WL1271_DEFAULT_CHANNEL 0
1338 static int __devinit wl1271_probe(struct spi_device *spi)
1340 struct wl12xx_platform_data *pdata;
1341 struct ieee80211_hw *hw;
1344 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1346 pdata = spi->dev.platform_data;
1348 wl1271_error("no platform data");
1352 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
1354 wl1271_error("could not alloc ieee80211_hw");
1359 memset(wl, 0, sizeof(*wl));
1362 dev_set_drvdata(&spi->dev, wl);
1365 skb_queue_head_init(&wl->tx_queue);
1367 INIT_WORK(&wl->filter_work, wl1271_filter_work);
1368 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
1369 wl->channel = WL1271_DEFAULT_CHANNEL;
1370 wl->scanning = false;
1371 wl->default_key = 0;
1374 wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
1375 wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
1378 wl->psm_requested = false;
1379 wl->tx_queue_stopped = false;
1380 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1381 wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
1382 wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
1383 wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1384 wl->band = IEEE80211_BAND_2GHZ;
1386 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
1387 wl->tx_frames[i] = NULL;
1389 spin_lock_init(&wl->wl_lock);
1392 * In case our MAC address is not correctly set,
1393 * we use a random but Nokia MAC.
1395 memcpy(wl->mac_addr, nokia_oui, 3);
1396 get_random_bytes(wl->mac_addr + 3, 3);
1398 wl->state = WL1271_STATE_OFF;
1399 mutex_init(&wl->mutex);
1401 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1402 if (!wl->rx_descriptor) {
1403 wl1271_error("could not allocate memory for rx descriptor");
1408 /* This is the only SPI value that we need to set here, the rest
1409 * comes from the board-peripherals file */
1410 spi->bits_per_word = 32;
1412 ret = spi_setup(spi);
1414 wl1271_error("spi_setup failed");
1418 wl->set_power = pdata->set_power;
1419 if (!wl->set_power) {
1420 wl1271_error("set power function missing in platform data");
1427 wl1271_error("irq missing in platform data");
1432 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
1434 wl1271_error("request_irq() failed: %d", ret);
1438 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1440 disable_irq(wl->irq);
1442 ret = platform_device_register(&wl1271_device);
1444 wl1271_error("couldn't register platform device");
1447 dev_set_drvdata(&wl1271_device.dev, wl);
1449 ret = wl1271_init_ieee80211(wl);
1453 ret = wl1271_register_hw(wl);
1457 wl1271_debugfs_init(wl);
1459 wl1271_notice("initialized");
1464 platform_device_unregister(&wl1271_device);
1467 free_irq(wl->irq, wl);
1470 kfree(wl->rx_descriptor);
1471 wl->rx_descriptor = NULL;
1473 ieee80211_free_hw(hw);
1478 static int __devexit wl1271_remove(struct spi_device *spi)
1480 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
1482 ieee80211_unregister_hw(wl->hw);
1484 wl1271_debugfs_exit(wl);
1485 platform_device_unregister(&wl1271_device);
1486 free_irq(wl->irq, wl);
1487 kfree(wl->target_mem_map);
1493 kfree(wl->rx_descriptor);
1494 wl->rx_descriptor = NULL;
1496 kfree(wl->fw_status);
1497 kfree(wl->tx_res_if);
1499 ieee80211_free_hw(wl->hw);
1505 static struct spi_driver wl1271_spi_driver = {
1508 .bus = &spi_bus_type,
1509 .owner = THIS_MODULE,
1512 .probe = wl1271_probe,
1513 .remove = __devexit_p(wl1271_remove),
1516 static int __init wl1271_init(void)
1520 ret = spi_register_driver(&wl1271_spi_driver);
1522 wl1271_error("failed to register spi driver: %d", ret);
1530 static void __exit wl1271_exit(void)
1532 spi_unregister_driver(&wl1271_spi_driver);
1534 wl1271_notice("unloaded");
1537 module_init(wl1271_init);
1538 module_exit(wl1271_exit);
1540 MODULE_LICENSE("GPL");
1541 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
1542 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");