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 void wl1271_conf_init(struct wl1271
*wl
)
51 struct conf_drv_settings conf
= {
53 .per_threshold
= 7500,
54 .max_scan_compensation_time
= 120000,
55 .nfs_sample_interval
= 400,
58 .probe_req_compensation
= 170,
59 .scan_window_compensation
= 50,
61 .beacon_miss_threshold
= 60,
62 .rate_adaptation_threshold
= CONF_HW_BIT_RATE_12MBPS
,
63 .rate_adaptation_snr
= 0
66 .rx_msdu_life_time
= 512000,
67 .packet_detection_threshold
= 0,
68 .ps_poll_timeout
= 15,
70 .rts_threshold
= 2347,
71 .rx_cca_threshold
= 0xFFEF,
72 .irq_blk_threshold
= 0,
73 .irq_pkt_threshold
= USHORT_MAX
,
75 .queue_type
= CONF_RX_QUEUE_TYPE_LOW_PRIORITY
,
78 .tx_energy_detection
= 0,
81 CONF_TX_RATE_MASK_UNSPECIFIED
,
82 .short_retry_limit
= 10,
83 .long_retry_limit
= 10,
106 .aifsn
= CONF_TX_AIFS_PIFS
,
113 .aifsn
= CONF_TX_AIFS_PIFS
,
121 .channel_type
= CONF_CHANNEL_TYPE_DCF
,
122 .tsid
= CONF_TX_AC_BE
,
123 .ps_scheme
= CONF_PS_SCHEME_LEGACY
,
124 .ack_policy
= CONF_ACK_POLICY_LEGACY
,
129 .channel_type
= CONF_CHANNEL_TYPE_DCF
,
130 .tsid
= CONF_TX_AC_BE
,
131 .ps_scheme
= CONF_PS_SCHEME_LEGACY
,
132 .ack_policy
= CONF_ACK_POLICY_LEGACY
,
137 .channel_type
= CONF_CHANNEL_TYPE_DCF
,
138 .tsid
= CONF_TX_AC_BE
,
139 .ps_scheme
= CONF_PS_SCHEME_LEGACY
,
140 .ack_policy
= CONF_ACK_POLICY_LEGACY
,
145 .channel_type
= CONF_CHANNEL_TYPE_DCF
,
146 .tsid
= CONF_TX_AC_BE
,
147 .ps_scheme
= CONF_PS_SCHEME_LEGACY
,
148 .ack_policy
= CONF_ACK_POLICY_LEGACY
,
153 .channel_type
= CONF_CHANNEL_TYPE_DCF
,
154 .tsid
= CONF_TX_AC_BE
,
155 .ps_scheme
= CONF_PS_SCHEME_LEGACY
,
156 .ack_policy
= CONF_ACK_POLICY_LEGACY
,
161 .channel_type
= CONF_CHANNEL_TYPE_DCF
,
162 .tsid
= CONF_TX_AC_BE
,
163 .ps_scheme
= CONF_PS_SCHEME_LEGACY
,
164 .ack_policy
= CONF_ACK_POLICY_LEGACY
,
169 .channel_type
= CONF_CHANNEL_TYPE_DCF
,
170 .tsid
= CONF_TX_AC_BE
,
171 .ps_scheme
= CONF_PS_SCHEME_LEGACY
,
172 .ack_policy
= CONF_ACK_POLICY_LEGACY
,
176 .frag_threshold
= IEEE80211_MAX_FRAG_THRESHOLD
,
177 .tx_compl_timeout
= 5,
178 .tx_compl_threshold
= 5
181 .wake_up_event
= CONF_WAKE_UP_EVENT_DTIM
,
182 .listen_interval
= 0,
183 .bcn_filt_mode
= CONF_BCN_FILT_MODE_ENABLED
,
184 .bcn_filt_ie_count
= 1,
187 .ie
= WLAN_EID_CHANNEL_SWITCH
,
189 CONF_BCN_RULE_PASS_ON_APPEARANCE
,
192 .synch_fail_thold
= 5,
193 .bss_lose_timeout
= 100,
194 .beacon_rx_timeout
= 10000,
195 .broadcast_timeout
= 20000,
196 .rx_broadcast_in_ps
= 1,
197 .ps_poll_threshold
= 4,
198 .sig_trigger_count
= 2,
203 .metric
= CONF_TRIG_METRIC_RSSI_BEACON
,
204 .type
= CONF_TRIG_EVENT_TYPE_EDGE
,
205 .direction
= CONF_TRIG_EVENT_DIR_LOW
,
213 .metric
= CONF_TRIG_METRIC_RSSI_BEACON
,
214 .type
= CONF_TRIG_EVENT_TYPE_EDGE
,
215 .direction
= CONF_TRIG_EVENT_DIR_HIGH
,
222 .rssi_bcn_avg_weight
= 10,
223 .rssi_pkt_avg_weight
= 10,
224 .snr_bcn_avg_weight
= 10,
225 .snr_pkt_avg_weight
= 10
231 * This function applies the default configuration to the driver. This
232 * function is invoked upon driver load (spi probe.)
234 * The configuration is stored in a run-time structure in order to
235 * facilitate for run-time adjustment of any of the parameters. Making
236 * changes to the configuration structure will apply the new values on
237 * the next interface up (wl1271_op_start.)
240 /* apply driver default configuration */
241 memcpy(&wl
->conf
, &conf
, sizeof(conf
));
245 static int wl1271_plt_init(struct wl1271
*wl
)
249 ret
= wl1271_acx_init_mem_config(wl
);
253 ret
= wl1271_cmd_data_path(wl
, wl
->channel
, 1);
260 static void wl1271_disable_interrupts(struct wl1271
*wl
)
262 disable_irq(wl
->irq
);
265 static void wl1271_power_off(struct wl1271
*wl
)
267 wl
->set_power(false);
270 static void wl1271_power_on(struct wl1271
*wl
)
275 static void wl1271_fw_status(struct wl1271
*wl
,
276 struct wl1271_fw_status
*status
)
281 wl1271_spi_read(wl
, FW_STATUS_ADDR
, status
,
282 sizeof(*status
), false);
284 wl1271_debug(DEBUG_IRQ
, "intr: 0x%x (fw_rx_counter = %d, "
285 "drv_rx_counter = %d, tx_results_counter = %d)",
287 status
->fw_rx_counter
,
288 status
->drv_rx_counter
,
289 status
->tx_results_counter
);
291 /* update number of available TX blocks */
292 for (i
= 0; i
< NUM_TX_QUEUES
; i
++) {
293 u32 cnt
= status
->tx_released_blks
[i
] - wl
->tx_blocks_freed
[i
];
294 wl
->tx_blocks_freed
[i
] = status
->tx_released_blks
[i
];
295 wl
->tx_blocks_available
+= cnt
;
299 /* if more blocks are available now, schedule some tx work */
300 if (total
&& !skb_queue_empty(&wl
->tx_queue
))
301 ieee80211_queue_work(wl
->hw
, &wl
->tx_work
);
303 /* update the host-chipset time offset */
304 wl
->time_offset
= jiffies_to_usecs(jiffies
) - status
->fw_localtime
;
307 static void wl1271_irq_work(struct work_struct
*work
)
312 container_of(work
, struct wl1271
, irq_work
);
314 mutex_lock(&wl
->mutex
);
316 wl1271_debug(DEBUG_IRQ
, "IRQ work");
318 if (wl
->state
== WL1271_STATE_OFF
)
321 ret
= wl1271_ps_elp_wakeup(wl
, true);
325 wl1271_spi_write32(wl
, ACX_REG_INTERRUPT_MASK
, WL1271_ACX_INTR_ALL
);
327 wl1271_fw_status(wl
, wl
->fw_status
);
328 intr
= wl
->fw_status
->intr
;
330 wl1271_debug(DEBUG_IRQ
, "Zero interrupt received.");
334 intr
&= WL1271_INTR_MASK
;
336 if (intr
& (WL1271_ACX_INTR_EVENT_A
|
337 WL1271_ACX_INTR_EVENT_B
)) {
338 wl1271_debug(DEBUG_IRQ
,
339 "WL1271_ACX_INTR_EVENT (0x%x)", intr
);
340 if (intr
& WL1271_ACX_INTR_EVENT_A
)
341 wl1271_event_handle(wl
, 0);
343 wl1271_event_handle(wl
, 1);
346 if (intr
& WL1271_ACX_INTR_INIT_COMPLETE
)
347 wl1271_debug(DEBUG_IRQ
,
348 "WL1271_ACX_INTR_INIT_COMPLETE");
350 if (intr
& WL1271_ACX_INTR_HW_AVAILABLE
)
351 wl1271_debug(DEBUG_IRQ
, "WL1271_ACX_INTR_HW_AVAILABLE");
353 if (intr
& WL1271_ACX_INTR_DATA
) {
354 u8 tx_res_cnt
= wl
->fw_status
->tx_results_counter
-
355 wl
->tx_results_count
;
357 wl1271_debug(DEBUG_IRQ
, "WL1271_ACX_INTR_DATA");
359 /* check for tx results */
361 wl1271_tx_complete(wl
, tx_res_cnt
);
363 wl1271_rx(wl
, wl
->fw_status
);
367 wl1271_spi_write32(wl
, ACX_REG_INTERRUPT_MASK
,
368 WL1271_ACX_INTR_ALL
& ~(WL1271_INTR_MASK
));
369 wl1271_ps_elp_sleep(wl
);
372 mutex_unlock(&wl
->mutex
);
375 static irqreturn_t
wl1271_irq(int irq
, void *cookie
)
380 wl1271_debug(DEBUG_IRQ
, "IRQ");
384 /* complete the ELP completion */
385 spin_lock_irqsave(&wl
->wl_lock
, flags
);
387 complete(wl
->elp_compl
);
388 wl
->elp_compl
= NULL
;
391 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
392 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
397 static int wl1271_fetch_firmware(struct wl1271
*wl
)
399 const struct firmware
*fw
;
402 ret
= request_firmware(&fw
, WL1271_FW_NAME
, &wl
->spi
->dev
);
405 wl1271_error("could not get firmware: %d", ret
);
410 wl1271_error("firmware size is not multiple of 32 bits: %zu",
416 wl
->fw_len
= fw
->size
;
417 wl
->fw
= vmalloc(wl
->fw_len
);
420 wl1271_error("could not allocate memory for the firmware");
425 memcpy(wl
->fw
, fw
->data
, wl
->fw_len
);
430 release_firmware(fw
);
435 static int wl1271_fetch_nvs(struct wl1271
*wl
)
437 const struct firmware
*fw
;
440 ret
= request_firmware(&fw
, WL1271_NVS_NAME
, &wl
->spi
->dev
);
443 wl1271_error("could not get nvs file: %d", ret
);
448 wl1271_error("nvs size is not multiple of 32 bits: %zu",
454 wl
->nvs_len
= fw
->size
;
455 wl
->nvs
= kmalloc(wl
->nvs_len
, GFP_KERNEL
);
458 wl1271_error("could not allocate memory for the nvs file");
463 memcpy(wl
->nvs
, fw
->data
, wl
->nvs_len
);
468 release_firmware(fw
);
473 static void wl1271_fw_wakeup(struct wl1271
*wl
)
477 elp_reg
= ELPCTRL_WAKE_UP
;
478 wl1271_raw_write32(wl
, HW_ACCESS_ELP_CTRL_REG_ADDR
, elp_reg
);
481 static int wl1271_setup(struct wl1271
*wl
)
483 wl
->fw_status
= kmalloc(sizeof(*wl
->fw_status
), GFP_KERNEL
);
487 wl
->tx_res_if
= kmalloc(sizeof(*wl
->tx_res_if
), GFP_KERNEL
);
488 if (!wl
->tx_res_if
) {
489 kfree(wl
->fw_status
);
493 INIT_WORK(&wl
->irq_work
, wl1271_irq_work
);
494 INIT_WORK(&wl
->tx_work
, wl1271_tx_work
);
498 static int wl1271_chip_wakeup(struct wl1271
*wl
)
500 struct wl1271_partition_set partition
;
504 msleep(WL1271_POWER_ON_SLEEP
);
505 wl1271_spi_reset(wl
);
508 /* We don't need a real memory partition here, because we only want
509 * to use the registers at this point. */
510 memset(&partition
, 0, sizeof(partition
));
511 partition
.reg
.start
= REGISTERS_BASE
;
512 partition
.reg
.size
= REGISTERS_DOWN_SIZE
;
513 wl1271_set_partition(wl
, &partition
);
515 /* ELP module wake up */
516 wl1271_fw_wakeup(wl
);
518 /* whal_FwCtrl_BootSm() */
520 /* 0. read chip id from CHIP_ID */
521 wl
->chip
.id
= wl1271_spi_read32(wl
, CHIP_ID_B
);
523 /* 1. check if chip id is valid */
525 switch (wl
->chip
.id
) {
526 case CHIP_ID_1271_PG10
:
527 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
530 ret
= wl1271_setup(wl
);
534 case CHIP_ID_1271_PG20
:
535 wl1271_debug(DEBUG_BOOT
, "chip id 0x%x (1271 PG20)",
538 ret
= wl1271_setup(wl
);
543 wl1271_error("unsupported chip id: 0x%x", wl
->chip
.id
);
548 if (wl
->fw
== NULL
) {
549 ret
= wl1271_fetch_firmware(wl
);
554 /* No NVS from netlink, try to get it from the filesystem */
555 if (wl
->nvs
== NULL
) {
556 ret
= wl1271_fetch_nvs(wl
);
565 struct wl1271_filter_params
{
566 unsigned int filters
;
567 unsigned int changed
;
569 u8 mc_list
[ACX_MC_ADDRESS_GROUP_MAX
][ETH_ALEN
];
572 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
575 FIF_BCN_PRBRESP_PROMISC | \
579 static void wl1271_filter_work(struct work_struct
*work
)
582 container_of(work
, struct wl1271
, filter_work
);
583 struct wl1271_filter_params
*fp
;
588 /* first, get the filter parameters */
589 spin_lock_irqsave(&wl
->wl_lock
, flags
);
590 fp
= wl
->filter_params
;
591 wl
->filter_params
= NULL
;
592 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
597 /* then, lock the mutex without risk of lock-up */
598 mutex_lock(&wl
->mutex
);
600 if (wl
->state
== WL1271_STATE_OFF
)
603 ret
= wl1271_ps_elp_wakeup(wl
, false);
607 /* configure the mc filter regardless of the changed flags */
608 if (fp
->filters
& FIF_ALLMULTI
)
611 ret
= wl1271_acx_group_address_tbl(wl
, enabled
,
612 fp
->mc_list
, fp
->mc_list_length
);
616 /* determine, whether supported filter values have changed */
617 if (fp
->changed
== 0)
620 /* apply configured filters */
621 ret
= wl1271_acx_rx_config(wl
, wl
->rx_config
, wl
->rx_filter
);
626 wl1271_ps_elp_sleep(wl
);
629 mutex_unlock(&wl
->mutex
);
633 int wl1271_plt_start(struct wl1271
*wl
)
637 mutex_lock(&wl
->mutex
);
639 wl1271_notice("power up");
641 if (wl
->state
!= WL1271_STATE_OFF
) {
642 wl1271_error("cannot go into PLT state because not "
643 "in off state: %d", wl
->state
);
648 wl
->state
= WL1271_STATE_PLT
;
650 ret
= wl1271_chip_wakeup(wl
);
654 ret
= wl1271_boot(wl
);
658 wl1271_notice("firmware booted in PLT mode (%s)", wl
->chip
.fw_ver
);
660 ret
= wl1271_plt_init(wl
);
665 mutex_unlock(&wl
->mutex
);
670 int wl1271_plt_stop(struct wl1271
*wl
)
674 mutex_lock(&wl
->mutex
);
676 wl1271_notice("power down");
678 if (wl
->state
!= WL1271_STATE_PLT
) {
679 wl1271_error("cannot power down because not in PLT "
680 "state: %d", wl
->state
);
685 wl1271_disable_interrupts(wl
);
686 wl1271_power_off(wl
);
688 wl
->state
= WL1271_STATE_OFF
;
691 mutex_unlock(&wl
->mutex
);
697 static int wl1271_op_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
699 struct wl1271
*wl
= hw
->priv
;
701 skb_queue_tail(&wl
->tx_queue
, skb
);
704 * The chip specific setup must run before the first TX packet -
705 * before that, the tx_work will not be initialized!
708 ieee80211_queue_work(wl
->hw
, &wl
->tx_work
);
711 * The workqueue is slow to process the tx_queue and we need stop
712 * the queue here, otherwise the queue will get too long.
714 if (skb_queue_len(&wl
->tx_queue
) >= WL1271_TX_QUEUE_MAX_LENGTH
) {
715 ieee80211_stop_queues(wl
->hw
);
718 * FIXME: this is racy, the variable is not properly
719 * protected. Maybe fix this by removing the stupid
720 * variable altogether and checking the real queue state?
722 wl
->tx_queue_stopped
= true;
728 static int wl1271_op_start(struct ieee80211_hw
*hw
)
730 struct wl1271
*wl
= hw
->priv
;
733 wl1271_debug(DEBUG_MAC80211
, "mac80211 start");
735 mutex_lock(&wl
->mutex
);
737 if (wl
->state
!= WL1271_STATE_OFF
) {
738 wl1271_error("cannot start because not in off state: %d",
744 ret
= wl1271_chip_wakeup(wl
);
748 ret
= wl1271_boot(wl
);
752 ret
= wl1271_hw_init(wl
);
756 wl
->state
= WL1271_STATE_ON
;
758 wl1271_info("firmware booted (%s)", wl
->chip
.fw_ver
);
762 wl1271_power_off(wl
);
764 mutex_unlock(&wl
->mutex
);
769 static void wl1271_op_stop(struct ieee80211_hw
*hw
)
771 struct wl1271
*wl
= hw
->priv
;
777 wl1271_debug(DEBUG_MAC80211
, "mac80211 stop");
779 /* complete/cancel ongoing work */
780 cancel_work_sync(&wl
->filter_work
);
781 spin_lock_irqsave(&wl
->wl_lock
, flags
);
782 kfree(wl
->filter_params
);
783 wl
->filter_params
= NULL
;
784 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
786 mutex_lock(&wl
->mutex
);
788 WARN_ON(wl
->state
!= WL1271_STATE_ON
);
791 mutex_unlock(&wl
->mutex
);
792 ieee80211_scan_completed(wl
->hw
, true);
793 mutex_lock(&wl
->mutex
);
794 wl
->scanning
= false;
797 wl
->state
= WL1271_STATE_OFF
;
799 wl1271_disable_interrupts(wl
);
801 mutex_unlock(&wl
->mutex
);
803 cancel_work_sync(&wl
->irq_work
);
804 cancel_work_sync(&wl
->tx_work
);
805 cancel_work_sync(&wl
->filter_work
);
807 mutex_lock(&wl
->mutex
);
809 /* let's notify MAC80211 about the remaining pending TX frames */
811 wl1271_power_off(wl
);
813 memset(wl
->bssid
, 0, ETH_ALEN
);
814 memset(wl
->ssid
, 0, IW_ESSID_MAX_SIZE
+ 1);
816 wl
->bss_type
= MAX_BSS_TYPE
;
817 wl
->band
= IEEE80211_BAND_2GHZ
;
822 wl
->tx_queue_stopped
= false;
823 wl
->power_level
= WL1271_DEFAULT_POWER_LEVEL
;
824 wl
->tx_blocks_available
= 0;
825 wl
->tx_results_count
= 0;
826 wl
->tx_packets_count
= 0;
827 wl
->tx_security_last_seq
= 0;
828 wl
->tx_security_seq_16
= 0;
829 wl
->tx_security_seq_32
= 0;
831 wl
->session_counter
= 0;
834 for (i
= 0; i
< NUM_TX_QUEUES
; i
++)
835 wl
->tx_blocks_freed
[i
] = 0;
837 wl1271_debugfs_reset(wl
);
838 mutex_unlock(&wl
->mutex
);
841 static int wl1271_op_add_interface(struct ieee80211_hw
*hw
,
842 struct ieee80211_if_init_conf
*conf
)
844 struct wl1271
*wl
= hw
->priv
;
847 wl1271_debug(DEBUG_MAC80211
, "mac80211 add interface type %d mac %pM",
848 conf
->type
, conf
->mac_addr
);
850 mutex_lock(&wl
->mutex
);
858 switch (conf
->type
) {
859 case NL80211_IFTYPE_STATION
:
860 wl
->bss_type
= BSS_TYPE_STA_BSS
;
862 case NL80211_IFTYPE_ADHOC
:
863 wl
->bss_type
= BSS_TYPE_IBSS
;
870 /* FIXME: what if conf->mac_addr changes? */
873 mutex_unlock(&wl
->mutex
);
877 static void wl1271_op_remove_interface(struct ieee80211_hw
*hw
,
878 struct ieee80211_if_init_conf
*conf
)
880 struct wl1271
*wl
= hw
->priv
;
882 mutex_lock(&wl
->mutex
);
883 wl1271_debug(DEBUG_MAC80211
, "mac80211 remove interface");
885 mutex_unlock(&wl
->mutex
);
889 static int wl1271_op_config_interface(struct ieee80211_hw
*hw
,
890 struct ieee80211_vif
*vif
,
891 struct ieee80211_if_conf
*conf
)
893 struct wl1271
*wl
= hw
->priv
;
894 struct sk_buff
*beacon
;
897 wl1271_debug(DEBUG_MAC80211
, "mac80211 config_interface bssid %pM",
899 wl1271_dump_ascii(DEBUG_MAC80211
, "ssid: ", conf
->ssid
,
902 mutex_lock(&wl
->mutex
);
904 ret
= wl1271_ps_elp_wakeup(wl
, false);
908 if (memcmp(wl
->bssid
, conf
->bssid
, ETH_ALEN
)) {
909 wl1271_debug(DEBUG_MAC80211
, "bssid changed");
911 memcpy(wl
->bssid
, conf
->bssid
, ETH_ALEN
);
913 ret
= wl1271_cmd_join(wl
);
918 ret
= wl1271_cmd_build_null_data(wl
);
922 wl
->ssid_len
= conf
->ssid_len
;
924 memcpy(wl
->ssid
, conf
->ssid
, wl
->ssid_len
);
926 if (conf
->changed
& IEEE80211_IFCC_BEACON
) {
927 beacon
= ieee80211_beacon_get(hw
, vif
);
928 ret
= wl1271_cmd_template_set(wl
, CMD_TEMPL_BEACON
,
929 beacon
->data
, beacon
->len
);
932 dev_kfree_skb(beacon
);
936 ret
= wl1271_cmd_template_set(wl
, CMD_TEMPL_PROBE_RESPONSE
,
937 beacon
->data
, beacon
->len
);
939 dev_kfree_skb(beacon
);
946 wl1271_ps_elp_sleep(wl
);
949 mutex_unlock(&wl
->mutex
);
955 static int wl1271_op_config(struct ieee80211_hw
*hw
, u32 changed
)
957 struct wl1271
*wl
= hw
->priv
;
958 struct ieee80211_conf
*conf
= &hw
->conf
;
959 int channel
, ret
= 0;
961 channel
= ieee80211_frequency_to_channel(conf
->channel
->center_freq
);
963 wl1271_debug(DEBUG_MAC80211
, "mac80211 config ch %d psm %s power %d",
965 conf
->flags
& IEEE80211_CONF_PS
? "on" : "off",
968 mutex_lock(&wl
->mutex
);
970 wl
->band
= conf
->channel
->band
;
972 ret
= wl1271_ps_elp_wakeup(wl
, false);
976 if (channel
!= wl
->channel
) {
978 * We assume that the stack will configure the right channel
979 * before associating, so we don't need to send a join
980 * command here. We will join the right channel when the
983 wl
->channel
= channel
;
986 ret
= wl1271_cmd_build_null_data(wl
);
990 if (conf
->flags
& IEEE80211_CONF_PS
&& !wl
->psm_requested
) {
991 wl1271_info("psm enabled");
993 wl
->psm_requested
= true;
996 * We enter PSM only if we're already associated.
997 * If we're not, we'll enter it when joining an SSID,
998 * through the bss_info_changed() hook.
1000 ret
= wl1271_ps_set_mode(wl
, STATION_POWER_SAVE_MODE
);
1001 } else if (!(conf
->flags
& IEEE80211_CONF_PS
) &&
1002 wl
->psm_requested
) {
1003 wl1271_info("psm disabled");
1005 wl
->psm_requested
= false;
1008 ret
= wl1271_ps_set_mode(wl
, STATION_ACTIVE_MODE
);
1011 if (conf
->power_level
!= wl
->power_level
) {
1012 ret
= wl1271_acx_tx_power(wl
, conf
->power_level
);
1016 wl
->power_level
= conf
->power_level
;
1020 wl1271_ps_elp_sleep(wl
);
1023 mutex_unlock(&wl
->mutex
);
1028 static u64
wl1271_op_prepare_multicast(struct ieee80211_hw
*hw
, int mc_count
,
1029 struct dev_addr_list
*mc_list
)
1031 struct wl1271
*wl
= hw
->priv
;
1032 struct wl1271_filter_params
*fp
;
1033 unsigned long flags
;
1037 * FIXME: we should return a hash that will be passed to
1038 * configure_filter() instead of saving everything in the context.
1041 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
1043 wl1271_error("Out of memory setting filters.");
1047 /* update multicast filtering parameters */
1048 if (mc_count
> ACX_MC_ADDRESS_GROUP_MAX
) {
1050 fp
->filters
|= FIF_ALLMULTI
;
1053 fp
->mc_list_length
= 0;
1054 for (i
= 0; i
< mc_count
; i
++) {
1055 if (mc_list
->da_addrlen
== ETH_ALEN
) {
1056 memcpy(fp
->mc_list
[fp
->mc_list_length
],
1057 mc_list
->da_addr
, ETH_ALEN
);
1058 fp
->mc_list_length
++;
1060 wl1271_warning("Unknown mc address length.");
1063 /* FIXME: We still need to set our filters properly */
1065 spin_lock_irqsave(&wl
->wl_lock
, flags
);
1066 kfree(wl
->filter_params
);
1067 wl
->filter_params
= fp
;
1068 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
1073 static void wl1271_op_configure_filter(struct ieee80211_hw
*hw
,
1074 unsigned int changed
,
1075 unsigned int *total
, u64 multicast
)
1077 struct wl1271
*wl
= hw
->priv
;
1079 wl1271_debug(DEBUG_MAC80211
, "mac80211 configure filter");
1081 *total
&= WL1271_SUPPORTED_FILTERS
;
1082 changed
&= WL1271_SUPPORTED_FILTERS
;
1088 * FIXME: for now we are still using a workqueue for filter
1089 * configuration, but with the new mac80211, this is not needed,
1090 * since configure_filter can now sleep. We now have
1091 * prepare_multicast, which needs to be atomic instead.
1094 /* store current filter config */
1095 wl
->filter_params
->filters
= *total
;
1096 wl
->filter_params
->changed
= changed
;
1098 ieee80211_queue_work(wl
->hw
, &wl
->filter_work
);
1101 static int wl1271_op_set_key(struct ieee80211_hw
*hw
, enum set_key_cmd cmd
,
1102 struct ieee80211_vif
*vif
,
1103 struct ieee80211_sta
*sta
,
1104 struct ieee80211_key_conf
*key_conf
)
1106 struct wl1271
*wl
= hw
->priv
;
1113 static const u8 bcast_addr
[ETH_ALEN
] =
1114 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1116 wl1271_debug(DEBUG_MAC80211
, "mac80211 set key");
1118 addr
= sta
? sta
->addr
: bcast_addr
;
1120 wl1271_debug(DEBUG_CRYPT
, "CMD: 0x%x", cmd
);
1121 wl1271_dump(DEBUG_CRYPT
, "ADDR: ", addr
, ETH_ALEN
);
1122 wl1271_debug(DEBUG_CRYPT
, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
1123 key_conf
->alg
, key_conf
->keyidx
,
1124 key_conf
->keylen
, key_conf
->flags
);
1125 wl1271_dump(DEBUG_CRYPT
, "KEY: ", key_conf
->key
, key_conf
->keylen
);
1127 if (is_zero_ether_addr(addr
)) {
1128 /* We dont support TX only encryption */
1133 mutex_lock(&wl
->mutex
);
1135 ret
= wl1271_ps_elp_wakeup(wl
, false);
1139 switch (key_conf
->alg
) {
1143 key_conf
->hw_key_idx
= key_conf
->keyidx
;
1146 key_type
= KEY_TKIP
;
1148 key_conf
->hw_key_idx
= key_conf
->keyidx
;
1149 tx_seq_32
= wl
->tx_security_seq_32
;
1150 tx_seq_16
= wl
->tx_security_seq_16
;
1155 key_conf
->flags
|= IEEE80211_KEY_FLAG_GENERATE_IV
;
1156 tx_seq_32
= wl
->tx_security_seq_32
;
1157 tx_seq_16
= wl
->tx_security_seq_16
;
1160 wl1271_error("Unknown key algo 0x%x", key_conf
->alg
);
1168 ret
= wl1271_cmd_set_key(wl
, KEY_ADD_OR_REPLACE
,
1169 key_conf
->keyidx
, key_type
,
1170 key_conf
->keylen
, key_conf
->key
,
1171 addr
, tx_seq_32
, tx_seq_16
);
1173 wl1271_error("Could not add or replace key");
1179 ret
= wl1271_cmd_set_key(wl
, KEY_REMOVE
,
1180 key_conf
->keyidx
, key_type
,
1181 key_conf
->keylen
, key_conf
->key
,
1184 wl1271_error("Could not remove key");
1190 wl1271_error("Unsupported key cmd 0x%x", cmd
);
1198 wl1271_ps_elp_sleep(wl
);
1201 mutex_unlock(&wl
->mutex
);
1207 static int wl1271_op_hw_scan(struct ieee80211_hw
*hw
,
1208 struct cfg80211_scan_request
*req
)
1210 struct wl1271
*wl
= hw
->priv
;
1213 size_t ssid_len
= 0;
1215 wl1271_debug(DEBUG_MAC80211
, "mac80211 hw scan");
1218 ssid
= req
->ssids
[0].ssid
;
1219 ssid_len
= req
->ssids
[0].ssid_len
;
1222 mutex_lock(&wl
->mutex
);
1224 ret
= wl1271_ps_elp_wakeup(wl
, false);
1228 ret
= wl1271_cmd_scan(hw
->priv
, ssid
, ssid_len
, 1, 0, 13, 3);
1230 wl1271_ps_elp_sleep(wl
);
1233 mutex_unlock(&wl
->mutex
);
1238 static int wl1271_op_set_rts_threshold(struct ieee80211_hw
*hw
, u32 value
)
1240 struct wl1271
*wl
= hw
->priv
;
1243 mutex_lock(&wl
->mutex
);
1245 ret
= wl1271_ps_elp_wakeup(wl
, false);
1249 ret
= wl1271_acx_rts_threshold(wl
, (u16
) value
);
1251 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret
);
1253 wl1271_ps_elp_sleep(wl
);
1256 mutex_unlock(&wl
->mutex
);
1261 static u32
wl1271_enabled_rates_get(struct wl1271
*wl
, u64 basic_rate_set
)
1263 struct ieee80211_supported_band
*band
;
1264 u32 enabled_rates
= 0;
1267 band
= wl
->hw
->wiphy
->bands
[wl
->band
];
1268 for (bit
= 0; bit
< band
->n_bitrates
; bit
++) {
1269 if (basic_rate_set
& 0x1)
1270 enabled_rates
|= band
->bitrates
[bit
].hw_value
;
1271 basic_rate_set
>>= 1;
1274 return enabled_rates
;
1277 static void wl1271_op_bss_info_changed(struct ieee80211_hw
*hw
,
1278 struct ieee80211_vif
*vif
,
1279 struct ieee80211_bss_conf
*bss_conf
,
1282 enum wl1271_cmd_ps_mode mode
;
1283 struct wl1271
*wl
= hw
->priv
;
1286 wl1271_debug(DEBUG_MAC80211
, "mac80211 bss info changed");
1288 mutex_lock(&wl
->mutex
);
1290 ret
= wl1271_ps_elp_wakeup(wl
, false);
1294 if (changed
& BSS_CHANGED_ASSOC
) {
1295 if (bss_conf
->assoc
) {
1296 wl
->aid
= bss_conf
->aid
;
1299 * with wl1271, we don't need to update the
1300 * beacon_int and dtim_period, because the firmware
1301 * updates it by itself when the first beacon is
1302 * received after a join.
1304 ret
= wl1271_cmd_build_ps_poll(wl
, wl
->aid
);
1308 ret
= wl1271_acx_aid(wl
, wl
->aid
);
1312 /* If we want to go in PSM but we're not there yet */
1313 if (wl
->psm_requested
&& !wl
->psm
) {
1314 mode
= STATION_POWER_SAVE_MODE
;
1315 ret
= wl1271_ps_set_mode(wl
, mode
);
1320 /* use defaults when not associated */
1321 wl
->basic_rate_set
= WL1271_DEFAULT_BASIC_RATE_SET
;
1327 if (changed
& BSS_CHANGED_ERP_SLOT
) {
1328 if (bss_conf
->use_short_slot
)
1329 ret
= wl1271_acx_slot(wl
, SLOT_TIME_SHORT
);
1331 ret
= wl1271_acx_slot(wl
, SLOT_TIME_LONG
);
1333 wl1271_warning("Set slot time failed %d", ret
);
1338 if (changed
& BSS_CHANGED_ERP_PREAMBLE
) {
1339 if (bss_conf
->use_short_preamble
)
1340 wl1271_acx_set_preamble(wl
, ACX_PREAMBLE_SHORT
);
1342 wl1271_acx_set_preamble(wl
, ACX_PREAMBLE_LONG
);
1345 if (changed
& BSS_CHANGED_ERP_CTS_PROT
) {
1346 if (bss_conf
->use_cts_prot
)
1347 ret
= wl1271_acx_cts_protect(wl
, CTSPROTECT_ENABLE
);
1349 ret
= wl1271_acx_cts_protect(wl
, CTSPROTECT_DISABLE
);
1351 wl1271_warning("Set ctsprotect failed %d", ret
);
1356 if (changed
& BSS_CHANGED_BASIC_RATES
) {
1357 wl
->basic_rate_set
= wl1271_enabled_rates_get(
1358 wl
, bss_conf
->basic_rates
);
1360 ret
= wl1271_acx_rate_policies(wl
, wl
->basic_rate_set
);
1362 wl1271_warning("Set rate policies failed %d", ret
);
1368 wl1271_ps_elp_sleep(wl
);
1371 mutex_unlock(&wl
->mutex
);
1375 /* can't be const, mac80211 writes to this */
1376 static struct ieee80211_rate wl1271_rates
[] = {
1378 .hw_value
= CONF_HW_BIT_RATE_1MBPS
,
1379 .hw_value_short
= CONF_HW_BIT_RATE_1MBPS
, },
1381 .hw_value
= CONF_HW_BIT_RATE_2MBPS
,
1382 .hw_value_short
= CONF_HW_BIT_RATE_2MBPS
,
1383 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
1385 .hw_value
= CONF_HW_BIT_RATE_5_5MBPS
,
1386 .hw_value_short
= CONF_HW_BIT_RATE_5_5MBPS
,
1387 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
1389 .hw_value
= CONF_HW_BIT_RATE_11MBPS
,
1390 .hw_value_short
= CONF_HW_BIT_RATE_11MBPS
,
1391 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
1393 .hw_value
= CONF_HW_BIT_RATE_6MBPS
,
1394 .hw_value_short
= CONF_HW_BIT_RATE_6MBPS
, },
1396 .hw_value
= CONF_HW_BIT_RATE_9MBPS
,
1397 .hw_value_short
= CONF_HW_BIT_RATE_9MBPS
, },
1399 .hw_value
= CONF_HW_BIT_RATE_12MBPS
,
1400 .hw_value_short
= CONF_HW_BIT_RATE_12MBPS
, },
1402 .hw_value
= CONF_HW_BIT_RATE_18MBPS
,
1403 .hw_value_short
= CONF_HW_BIT_RATE_18MBPS
, },
1405 .hw_value
= CONF_HW_BIT_RATE_24MBPS
,
1406 .hw_value_short
= CONF_HW_BIT_RATE_24MBPS
, },
1408 .hw_value
= CONF_HW_BIT_RATE_36MBPS
,
1409 .hw_value_short
= CONF_HW_BIT_RATE_36MBPS
, },
1411 .hw_value
= CONF_HW_BIT_RATE_48MBPS
,
1412 .hw_value_short
= CONF_HW_BIT_RATE_48MBPS
, },
1414 .hw_value
= CONF_HW_BIT_RATE_54MBPS
,
1415 .hw_value_short
= CONF_HW_BIT_RATE_54MBPS
, },
1418 /* can't be const, mac80211 writes to this */
1419 static struct ieee80211_channel wl1271_channels
[] = {
1420 { .hw_value
= 1, .center_freq
= 2412},
1421 { .hw_value
= 2, .center_freq
= 2417},
1422 { .hw_value
= 3, .center_freq
= 2422},
1423 { .hw_value
= 4, .center_freq
= 2427},
1424 { .hw_value
= 5, .center_freq
= 2432},
1425 { .hw_value
= 6, .center_freq
= 2437},
1426 { .hw_value
= 7, .center_freq
= 2442},
1427 { .hw_value
= 8, .center_freq
= 2447},
1428 { .hw_value
= 9, .center_freq
= 2452},
1429 { .hw_value
= 10, .center_freq
= 2457},
1430 { .hw_value
= 11, .center_freq
= 2462},
1431 { .hw_value
= 12, .center_freq
= 2467},
1432 { .hw_value
= 13, .center_freq
= 2472},
1435 /* can't be const, mac80211 writes to this */
1436 static struct ieee80211_supported_band wl1271_band_2ghz
= {
1437 .channels
= wl1271_channels
,
1438 .n_channels
= ARRAY_SIZE(wl1271_channels
),
1439 .bitrates
= wl1271_rates
,
1440 .n_bitrates
= ARRAY_SIZE(wl1271_rates
),
1443 static const struct ieee80211_ops wl1271_ops
= {
1444 .start
= wl1271_op_start
,
1445 .stop
= wl1271_op_stop
,
1446 .add_interface
= wl1271_op_add_interface
,
1447 .remove_interface
= wl1271_op_remove_interface
,
1448 .config
= wl1271_op_config
,
1449 /* .config_interface = wl1271_op_config_interface, */
1450 .prepare_multicast
= wl1271_op_prepare_multicast
,
1451 .configure_filter
= wl1271_op_configure_filter
,
1453 .set_key
= wl1271_op_set_key
,
1454 .hw_scan
= wl1271_op_hw_scan
,
1455 .bss_info_changed
= wl1271_op_bss_info_changed
,
1456 .set_rts_threshold
= wl1271_op_set_rts_threshold
,
1459 static int wl1271_register_hw(struct wl1271
*wl
)
1463 if (wl
->mac80211_registered
)
1466 SET_IEEE80211_PERM_ADDR(wl
->hw
, wl
->mac_addr
);
1468 ret
= ieee80211_register_hw(wl
->hw
);
1470 wl1271_error("unable to register mac80211 hw: %d", ret
);
1474 wl
->mac80211_registered
= true;
1476 wl1271_notice("loaded");
1481 static int wl1271_init_ieee80211(struct wl1271
*wl
)
1483 /* The tx descriptor buffer and the TKIP space. */
1484 wl
->hw
->extra_tx_headroom
= WL1271_TKIP_IV_SPACE
+
1485 sizeof(struct wl1271_tx_hw_descr
);
1488 /* FIXME: find a proper value */
1489 wl
->hw
->channel_change_time
= 10000;
1491 wl
->hw
->flags
= IEEE80211_HW_SIGNAL_DBM
|
1492 IEEE80211_HW_NOISE_DBM
|
1493 IEEE80211_HW_BEACON_FILTER
;
1495 wl
->hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
);
1496 wl
->hw
->wiphy
->max_scan_ssids
= 1;
1497 wl
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = &wl1271_band_2ghz
;
1499 SET_IEEE80211_DEV(wl
->hw
, &wl
->spi
->dev
);
1504 static void wl1271_device_release(struct device
*dev
)
1509 static struct platform_device wl1271_device
= {
1513 /* device model insists to have a release function */
1515 .release
= wl1271_device_release
,
1519 #define WL1271_DEFAULT_CHANNEL 0
1520 static int __devinit
wl1271_probe(struct spi_device
*spi
)
1522 struct wl12xx_platform_data
*pdata
;
1523 struct ieee80211_hw
*hw
;
1526 static const u8 nokia_oui
[3] = {0x00, 0x1f, 0xdf};
1528 pdata
= spi
->dev
.platform_data
;
1530 wl1271_error("no platform data");
1534 hw
= ieee80211_alloc_hw(sizeof(*wl
), &wl1271_ops
);
1536 wl1271_error("could not alloc ieee80211_hw");
1541 memset(wl
, 0, sizeof(*wl
));
1544 dev_set_drvdata(&spi
->dev
, wl
);
1547 skb_queue_head_init(&wl
->tx_queue
);
1549 INIT_WORK(&wl
->filter_work
, wl1271_filter_work
);
1550 INIT_DELAYED_WORK(&wl
->elp_work
, wl1271_elp_work
);
1551 wl
->channel
= WL1271_DEFAULT_CHANNEL
;
1552 wl
->scanning
= false;
1553 wl
->default_key
= 0;
1555 wl
->rx_config
= WL1271_DEFAULT_RX_CONFIG
;
1556 wl
->rx_filter
= WL1271_DEFAULT_RX_FILTER
;
1559 wl
->psm_requested
= false;
1560 wl
->tx_queue_stopped
= false;
1561 wl
->power_level
= WL1271_DEFAULT_POWER_LEVEL
;
1562 wl
->basic_rate_set
= WL1271_DEFAULT_BASIC_RATE_SET
;
1563 wl
->band
= IEEE80211_BAND_2GHZ
;
1567 for (i
= 0; i
< ACX_TX_DESCRIPTORS
; i
++)
1568 wl
->tx_frames
[i
] = NULL
;
1570 spin_lock_init(&wl
->wl_lock
);
1573 * In case our MAC address is not correctly set,
1574 * we use a random but Nokia MAC.
1576 memcpy(wl
->mac_addr
, nokia_oui
, 3);
1577 get_random_bytes(wl
->mac_addr
+ 3, 3);
1579 wl
->state
= WL1271_STATE_OFF
;
1580 mutex_init(&wl
->mutex
);
1582 wl
->rx_descriptor
= kmalloc(sizeof(*wl
->rx_descriptor
), GFP_KERNEL
);
1583 if (!wl
->rx_descriptor
) {
1584 wl1271_error("could not allocate memory for rx descriptor");
1589 /* This is the only SPI value that we need to set here, the rest
1590 * comes from the board-peripherals file */
1591 spi
->bits_per_word
= 32;
1593 ret
= spi_setup(spi
);
1595 wl1271_error("spi_setup failed");
1599 wl
->set_power
= pdata
->set_power
;
1600 if (!wl
->set_power
) {
1601 wl1271_error("set power function missing in platform data");
1608 wl1271_error("irq missing in platform data");
1613 ret
= request_irq(wl
->irq
, wl1271_irq
, 0, DRIVER_NAME
, wl
);
1615 wl1271_error("request_irq() failed: %d", ret
);
1619 set_irq_type(wl
->irq
, IRQ_TYPE_EDGE_RISING
);
1621 disable_irq(wl
->irq
);
1623 ret
= platform_device_register(&wl1271_device
);
1625 wl1271_error("couldn't register platform device");
1628 dev_set_drvdata(&wl1271_device
.dev
, wl
);
1630 /* Apply default driver configuration. */
1631 wl1271_conf_init(wl
);
1633 ret
= wl1271_init_ieee80211(wl
);
1637 ret
= wl1271_register_hw(wl
);
1641 wl1271_debugfs_init(wl
);
1643 wl1271_notice("initialized");
1648 platform_device_unregister(&wl1271_device
);
1651 free_irq(wl
->irq
, wl
);
1654 kfree(wl
->rx_descriptor
);
1655 wl
->rx_descriptor
= NULL
;
1657 ieee80211_free_hw(hw
);
1662 static int __devexit
wl1271_remove(struct spi_device
*spi
)
1664 struct wl1271
*wl
= dev_get_drvdata(&spi
->dev
);
1666 ieee80211_unregister_hw(wl
->hw
);
1668 wl1271_debugfs_exit(wl
);
1669 platform_device_unregister(&wl1271_device
);
1670 free_irq(wl
->irq
, wl
);
1671 kfree(wl
->target_mem_map
);
1677 kfree(wl
->rx_descriptor
);
1678 wl
->rx_descriptor
= NULL
;
1680 kfree(wl
->fw_status
);
1681 kfree(wl
->tx_res_if
);
1683 ieee80211_free_hw(wl
->hw
);
1689 static struct spi_driver wl1271_spi_driver
= {
1692 .bus
= &spi_bus_type
,
1693 .owner
= THIS_MODULE
,
1696 .probe
= wl1271_probe
,
1697 .remove
= __devexit_p(wl1271_remove
),
1700 static int __init
wl1271_init(void)
1704 ret
= spi_register_driver(&wl1271_spi_driver
);
1706 wl1271_error("failed to register spi driver: %d", ret
);
1714 static void __exit
wl1271_exit(void)
1716 spi_unregister_driver(&wl1271_spi_driver
);
1718 wl1271_notice("unloaded");
1721 module_init(wl1271_init
);
1722 module_exit(wl1271_exit
);
1724 MODULE_LICENSE("GPL");
1725 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
1726 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");