blob: 0d6000205548f96498441cd785a1ee6ba11fc9ce [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
2 * Copyright (c) 2008 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* mac80211 and PCI callbacks */
18
19#include <linux/nl80211.h>
20#include "core.h"
21
22#define ATH_PCI_VERSION "0.1"
23
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070024static char *dev_info = "ath9k";
25
26MODULE_AUTHOR("Atheros Communications");
27MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
28MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
29MODULE_LICENSE("Dual BSD/GPL");
30
31static struct pci_device_id ath_pci_id_table[] __devinitdata = {
32 { PCI_VDEVICE(ATHEROS, 0x0023) }, /* PCI */
33 { PCI_VDEVICE(ATHEROS, 0x0024) }, /* PCI-E */
34 { PCI_VDEVICE(ATHEROS, 0x0027) }, /* PCI */
35 { PCI_VDEVICE(ATHEROS, 0x0029) }, /* PCI */
36 { PCI_VDEVICE(ATHEROS, 0x002A) }, /* PCI-E */
37 { 0 }
38};
39
40static int ath_get_channel(struct ath_softc *sc,
41 struct ieee80211_channel *chan)
42{
43 int i;
44
45 for (i = 0; i < sc->sc_ah->ah_nchan; i++) {
46 if (sc->sc_ah->ah_channels[i].channel == chan->center_freq)
47 return i;
48 }
49
50 return -1;
51}
52
53static u32 ath_get_extchanmode(struct ath_softc *sc,
54 struct ieee80211_channel *chan)
55{
56 u32 chanmode = 0;
57 u8 ext_chan_offset = sc->sc_ht_info.ext_chan_offset;
58 enum ath9k_ht_macmode tx_chan_width = sc->sc_ht_info.tx_chan_width;
59
60 switch (chan->band) {
61 case IEEE80211_BAND_2GHZ:
Johannes Bergd9fe60d2008-10-09 12:13:49 +020062 if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_NONE) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070063 (tx_chan_width == ATH9K_HT_MACMODE_20))
64 chanmode = CHANNEL_G_HT20;
Johannes Bergd9fe60d2008-10-09 12:13:49 +020065 if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066 (tx_chan_width == ATH9K_HT_MACMODE_2040))
67 chanmode = CHANNEL_G_HT40PLUS;
Johannes Bergd9fe60d2008-10-09 12:13:49 +020068 if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070069 (tx_chan_width == ATH9K_HT_MACMODE_2040))
70 chanmode = CHANNEL_G_HT40MINUS;
71 break;
72 case IEEE80211_BAND_5GHZ:
Johannes Bergd9fe60d2008-10-09 12:13:49 +020073 if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_NONE) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070074 (tx_chan_width == ATH9K_HT_MACMODE_20))
75 chanmode = CHANNEL_A_HT20;
Johannes Bergd9fe60d2008-10-09 12:13:49 +020076 if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070077 (tx_chan_width == ATH9K_HT_MACMODE_2040))
78 chanmode = CHANNEL_A_HT40PLUS;
Johannes Bergd9fe60d2008-10-09 12:13:49 +020079 if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070080 (tx_chan_width == ATH9K_HT_MACMODE_2040))
81 chanmode = CHANNEL_A_HT40MINUS;
82 break;
83 default:
84 break;
85 }
86
87 return chanmode;
88}
89
90
91static int ath_setkey_tkip(struct ath_softc *sc,
92 struct ieee80211_key_conf *key,
93 struct ath9k_keyval *hk,
94 const u8 *addr)
95{
96 u8 *key_rxmic = NULL;
97 u8 *key_txmic = NULL;
98
99 key_txmic = key->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
100 key_rxmic = key->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
101
102 if (addr == NULL) {
103 /* Group key installation */
104 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
105 return ath_keyset(sc, key->keyidx, hk, addr);
106 }
107 if (!sc->sc_splitmic) {
108 /*
109 * data key goes at first index,
110 * the hal handles the MIC keys at index+64.
111 */
112 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
113 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
114 return ath_keyset(sc, key->keyidx, hk, addr);
115 }
116 /*
117 * TX key goes at first index, RX key at +32.
118 * The hal handles the MIC keys at index+64.
119 */
120 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
121 if (!ath_keyset(sc, key->keyidx, hk, NULL)) {
122 /* Txmic entry failed. No need to proceed further */
123 DPRINTF(sc, ATH_DBG_KEYCACHE,
124 "%s Setting TX MIC Key Failed\n", __func__);
125 return 0;
126 }
127
128 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
129 /* XXX delete tx key on failure? */
130 return ath_keyset(sc, key->keyidx+32, hk, addr);
131}
132
133static int ath_key_config(struct ath_softc *sc,
134 const u8 *addr,
135 struct ieee80211_key_conf *key)
136{
137 struct ieee80211_vif *vif;
138 struct ath9k_keyval hk;
139 const u8 *mac = NULL;
140 int ret = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +0200141 enum nl80211_iftype opmode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700142
143 memset(&hk, 0, sizeof(hk));
144
145 switch (key->alg) {
146 case ALG_WEP:
147 hk.kv_type = ATH9K_CIPHER_WEP;
148 break;
149 case ALG_TKIP:
150 hk.kv_type = ATH9K_CIPHER_TKIP;
151 break;
152 case ALG_CCMP:
153 hk.kv_type = ATH9K_CIPHER_AES_CCM;
154 break;
155 default:
156 return -EINVAL;
157 }
158
159 hk.kv_len = key->keylen;
160 memcpy(hk.kv_val, key->key, key->keylen);
161
162 if (!sc->sc_vaps[0])
163 return -EIO;
164
Sujith5640b082008-10-29 10:16:06 +0530165 vif = sc->sc_vaps[0];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700166 opmode = vif->type;
167
168 /*
169 * Strategy:
170 * For _M_STA mc tx, we will not setup a key at all since we never
171 * tx mc.
172 * _M_STA mc rx, we will use the keyID.
173 * for _M_IBSS mc tx, we will use the keyID, and no macaddr.
174 * for _M_IBSS mc rx, we will alloc a slot and plumb the mac of the
175 * peer node. BUT we will plumb a cleartext key so that we can do
176 * perSta default key table lookup in software.
177 */
178 if (is_broadcast_ether_addr(addr)) {
179 switch (opmode) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200180 case NL80211_IFTYPE_STATION:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700181 /* default key: could be group WPA key
182 * or could be static WEP key */
183 mac = NULL;
184 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200185 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700186 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200187 case NL80211_IFTYPE_AP:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700188 break;
189 default:
190 ASSERT(0);
191 break;
192 }
193 } else {
194 mac = addr;
195 }
196
197 if (key->alg == ALG_TKIP)
198 ret = ath_setkey_tkip(sc, key, &hk, mac);
199 else
200 ret = ath_keyset(sc, key->keyidx, &hk, mac);
201
202 if (!ret)
203 return -EIO;
204
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700205 return 0;
206}
207
208static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
209{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700210 int freeslot;
211
Sujithff9b6622008-08-14 13:27:16 +0530212 freeslot = (key->keyidx >= 4) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700213 ath_key_reset(sc, key->keyidx, freeslot);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700214}
215
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200216static void setup_ht_cap(struct ieee80211_sta_ht_cap *ht_info)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700217{
Sujith60653672008-08-14 13:28:02 +0530218#define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
219#define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700220
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200221 ht_info->ht_supported = true;
222 ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
223 IEEE80211_HT_CAP_SM_PS |
224 IEEE80211_HT_CAP_SGI_40 |
225 IEEE80211_HT_CAP_DSSSCCK40;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700226
Sujith60653672008-08-14 13:28:02 +0530227 ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
228 ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200229 /* set up supported mcs set */
230 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
231 ht_info->mcs.rx_mask[0] = 0xff;
232 ht_info->mcs.rx_mask[1] = 0xff;
233 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700234}
235
236static int ath_rate2idx(struct ath_softc *sc, int rate)
237{
238 int i = 0, cur_band, n_rates;
239 struct ieee80211_hw *hw = sc->hw;
240
241 cur_band = hw->conf.channel->band;
242 n_rates = sc->sbands[cur_band].n_bitrates;
243
244 for (i = 0; i < n_rates; i++) {
245 if (sc->sbands[cur_band].bitrates[i].bitrate == rate)
246 break;
247 }
248
249 /*
250 * NB:mac80211 validates rx rate index against the supported legacy rate
251 * index only (should be done against ht rates also), return the highest
252 * legacy rate index for rx rate which does not match any one of the
253 * supported basic and extended rates to make mac80211 happy.
254 * The following hack will be cleaned up once the issue with
255 * the rx rate index validation in mac80211 is fixed.
256 */
257 if (i == n_rates)
258 return n_rates - 1;
259 return i;
260}
261
262static void ath9k_rx_prepare(struct ath_softc *sc,
263 struct sk_buff *skb,
264 struct ath_recv_status *status,
265 struct ieee80211_rx_status *rx_status)
266{
267 struct ieee80211_hw *hw = sc->hw;
268 struct ieee80211_channel *curchan = hw->conf.channel;
269
270 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
271
272 rx_status->mactime = status->tsf;
273 rx_status->band = curchan->band;
274 rx_status->freq = curchan->center_freq;
Luis R. Rodriguez6f255422008-10-03 15:45:27 -0700275 rx_status->noise = sc->sc_ani.sc_noise_floor;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700276 rx_status->signal = rx_status->noise + status->rssi;
277 rx_status->rate_idx = ath_rate2idx(sc, (status->rateKbps / 100));
278 rx_status->antenna = status->antenna;
Luis R. Rodriguez6f255422008-10-03 15:45:27 -0700279
Luis R. Rodriguezc49d1542008-10-13 14:08:09 -0700280 /* at 45 you will be able to use MCS 15 reliably. A more elaborate
281 * scheme can be used here but it requires tables of SNR/throughput for
282 * each possible mode used. */
283 rx_status->qual = status->rssi * 100 / 45;
284
285 /* rssi can be more than 45 though, anything above that
286 * should be considered at 100% */
287 if (rx_status->qual > 100)
288 rx_status->qual = 100;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700289
290 if (status->flags & ATH_RX_MIC_ERROR)
291 rx_status->flag |= RX_FLAG_MMIC_ERROR;
292 if (status->flags & ATH_RX_FCS_ERROR)
293 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
294
295 rx_status->flag |= RX_FLAG_TSFT;
296}
297
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530298static void ath9k_ht_conf(struct ath_softc *sc,
299 struct ieee80211_bss_conf *bss_conf)
300{
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530301 struct ath_ht_info *ht_info = &sc->sc_ht_info;
302
Johannes Bergae5eb022008-10-14 16:58:37 +0200303 if (sc->hw->conf.ht.enabled) {
304 ht_info->ext_chan_offset = bss_conf->ht.secondary_channel_offset;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530305
Johannes Bergae5eb022008-10-14 16:58:37 +0200306 if (bss_conf->ht.width_40_ok)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530307 ht_info->tx_chan_width = ATH9K_HT_MACMODE_2040;
308 else
309 ht_info->tx_chan_width = ATH9K_HT_MACMODE_20;
310
311 ath9k_hw_set11nmac2040(sc->sc_ah, ht_info->tx_chan_width);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530312 }
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530313}
314
315static void ath9k_bss_assoc_info(struct ath_softc *sc,
Sujith5640b082008-10-29 10:16:06 +0530316 struct ieee80211_vif *vif,
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530317 struct ieee80211_bss_conf *bss_conf)
318{
319 struct ieee80211_hw *hw = sc->hw;
320 struct ieee80211_channel *curchan = hw->conf.channel;
Sujith5640b082008-10-29 10:16:06 +0530321 struct ath_vap *avp = (void *)vif->drv_priv;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530322 int pos;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530323
324 if (bss_conf->assoc) {
325 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Bss Info ASSOC %d\n",
326 __func__,
327 bss_conf->aid);
328
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530329 /* New association, store aid */
330 if (avp->av_opmode == ATH9K_M_STA) {
331 sc->sc_curaid = bss_conf->aid;
332 ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
333 sc->sc_curaid);
334 }
335
336 /* Configure the beacon */
337 ath_beacon_config(sc, 0);
338 sc->sc_flags |= SC_OP_BEACONS;
339
340 /* Reset rssi stats */
341 sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
342 sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
343 sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
344 sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
345
346 /* Update chainmask */
Johannes Bergae5eb022008-10-14 16:58:37 +0200347 ath_update_chainmask(sc, hw->conf.ht.enabled);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530348
349 DPRINTF(sc, ATH_DBG_CONFIG,
Johannes Berge1749612008-10-27 15:59:26 -0700350 "%s: bssid %pM aid 0x%x\n",
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530351 __func__,
Johannes Berge1749612008-10-27 15:59:26 -0700352 sc->sc_curbssid, sc->sc_curaid);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530353
354 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
355 __func__,
356 curchan->center_freq);
357
358 pos = ath_get_channel(sc, curchan);
359 if (pos == -1) {
360 DPRINTF(sc, ATH_DBG_FATAL,
361 "%s: Invalid channel\n", __func__);
362 return;
363 }
364
Johannes Bergae5eb022008-10-14 16:58:37 +0200365 if (hw->conf.ht.enabled)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530366 sc->sc_ah->ah_channels[pos].chanmode =
367 ath_get_extchanmode(sc, curchan);
368 else
369 sc->sc_ah->ah_channels[pos].chanmode =
370 (curchan->band == IEEE80211_BAND_2GHZ) ?
371 CHANNEL_G : CHANNEL_A;
372
373 /* set h/w channel */
374 if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
375 DPRINTF(sc, ATH_DBG_FATAL,
376 "%s: Unable to set channel\n",
377 __func__);
378
379 ath_rate_newstate(sc, avp);
380 /* Update ratectrl about the new state */
381 ath_rc_node_update(hw, avp->rc_node);
Luis R. Rodriguez6f255422008-10-03 15:45:27 -0700382
383 /* Start ANI */
384 mod_timer(&sc->sc_ani.timer,
385 jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
386
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530387 } else {
388 DPRINTF(sc, ATH_DBG_CONFIG,
389 "%s: Bss Info DISSOC\n", __func__);
390 sc->sc_curaid = 0;
391 }
392}
393
394void ath_get_beaconconfig(struct ath_softc *sc,
395 int if_id,
396 struct ath_beacon_config *conf)
397{
398 struct ieee80211_hw *hw = sc->hw;
399
400 /* fill in beacon config data */
401
402 conf->beacon_interval = hw->conf.beacon_int;
403 conf->listen_interval = 100;
404 conf->dtim_count = 1;
405 conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
406}
407
408void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
Sujith528f0c62008-10-29 10:14:26 +0530409 struct ath_xmit_status *tx_status)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530410{
411 struct ieee80211_hw *hw = sc->hw;
412 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
413
414 DPRINTF(sc, ATH_DBG_XMIT,
415 "%s: TX complete: skb: %p\n", __func__, skb);
416
Johannes Berge6a98542008-10-21 12:40:02 +0200417 ieee80211_tx_info_clear_status(tx_info);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530418 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
419 tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
Johannes Berge6a98542008-10-21 12:40:02 +0200420 /* free driver's private data area of tx_info, XXX: HACK! */
421 if (tx_info->control.vif != NULL)
422 kfree(tx_info->control.vif);
423 tx_info->control.vif = NULL;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530424 }
425
426 if (tx_status->flags & ATH_TX_BAR) {
427 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
428 tx_status->flags &= ~ATH_TX_BAR;
429 }
430
Johannes Berge6a98542008-10-21 12:40:02 +0200431 if (!(tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530432 /* Frame was ACKed */
433 tx_info->flags |= IEEE80211_TX_STAT_ACK;
434 }
435
Johannes Berge6a98542008-10-21 12:40:02 +0200436 tx_info->status.rates[0].count = tx_status->retries + 1;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530437
438 ieee80211_tx_status(hw, skb);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530439}
440
441int _ath_rx_indicate(struct ath_softc *sc,
442 struct sk_buff *skb,
443 struct ath_recv_status *status,
444 u16 keyix)
445{
446 struct ieee80211_hw *hw = sc->hw;
447 struct ath_node *an = NULL;
448 struct ieee80211_rx_status rx_status;
449 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
450 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
451 int padsize;
452 enum ATH_RX_TYPE st;
453
454 /* see if any padding is done by the hw and remove it */
455 if (hdrlen & 3) {
456 padsize = hdrlen % 4;
457 memmove(skb->data + padsize, skb->data, hdrlen);
458 skb_pull(skb, padsize);
459 }
460
461 /* Prepare rx status */
462 ath9k_rx_prepare(sc, skb, status, &rx_status);
463
464 if (!(keyix == ATH9K_RXKEYIX_INVALID) &&
465 !(status->flags & ATH_RX_DECRYPT_ERROR)) {
466 rx_status.flag |= RX_FLAG_DECRYPTED;
467 } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
468 && !(status->flags & ATH_RX_DECRYPT_ERROR)
469 && skb->len >= hdrlen + 4) {
470 keyix = skb->data[hdrlen + 3] >> 6;
471
472 if (test_bit(keyix, sc->sc_keymap))
473 rx_status.flag |= RX_FLAG_DECRYPTED;
474 }
475
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530476 if (an) {
477 ath_rx_input(sc, an,
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530478 skb, status, &st);
479 }
480 if (!an || (st != ATH_RX_CONSUMED))
481 __ieee80211_rx(hw, skb, &rx_status);
482
483 return 0;
484}
485
Sujith102e0572008-10-29 10:15:16 +0530486int ath_rx_subframe(struct ath_node *an, struct sk_buff *skb,
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530487 struct ath_recv_status *status)
488{
489 struct ath_softc *sc = an->an_sc;
490 struct ieee80211_hw *hw = sc->hw;
491 struct ieee80211_rx_status rx_status;
492
493 /* Prepare rx status */
494 ath9k_rx_prepare(sc, skb, status, &rx_status);
495 if (!(status->flags & ATH_RX_DECRYPT_ERROR))
496 rx_status.flag |= RX_FLAG_DECRYPTED;
497
498 __ieee80211_rx(hw, skb, &rx_status);
499
500 return 0;
501}
502
503/********************************/
504/* LED functions */
505/********************************/
506
507static void ath_led_brightness(struct led_classdev *led_cdev,
508 enum led_brightness brightness)
509{
510 struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
511 struct ath_softc *sc = led->sc;
512
513 switch (brightness) {
514 case LED_OFF:
515 if (led->led_type == ATH_LED_ASSOC ||
516 led->led_type == ATH_LED_RADIO)
517 sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
518 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
519 (led->led_type == ATH_LED_RADIO) ? 1 :
520 !!(sc->sc_flags & SC_OP_LED_ASSOCIATED));
521 break;
522 case LED_FULL:
523 if (led->led_type == ATH_LED_ASSOC)
524 sc->sc_flags |= SC_OP_LED_ASSOCIATED;
525 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
526 break;
527 default:
528 break;
529 }
530}
531
532static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
533 char *trigger)
534{
535 int ret;
536
537 led->sc = sc;
538 led->led_cdev.name = led->name;
539 led->led_cdev.default_trigger = trigger;
540 led->led_cdev.brightness_set = ath_led_brightness;
541
542 ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
543 if (ret)
544 DPRINTF(sc, ATH_DBG_FATAL,
545 "Failed to register led:%s", led->name);
546 else
547 led->registered = 1;
548 return ret;
549}
550
551static void ath_unregister_led(struct ath_led *led)
552{
553 if (led->registered) {
554 led_classdev_unregister(&led->led_cdev);
555 led->registered = 0;
556 }
557}
558
559static void ath_deinit_leds(struct ath_softc *sc)
560{
561 ath_unregister_led(&sc->assoc_led);
562 sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
563 ath_unregister_led(&sc->tx_led);
564 ath_unregister_led(&sc->rx_led);
565 ath_unregister_led(&sc->radio_led);
566 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
567}
568
569static void ath_init_leds(struct ath_softc *sc)
570{
571 char *trigger;
572 int ret;
573
574 /* Configure gpio 1 for output */
575 ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
576 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
577 /* LED off, active low */
578 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
579
580 trigger = ieee80211_get_radio_led_name(sc->hw);
581 snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
582 "ath9k-%s:radio", wiphy_name(sc->hw->wiphy));
583 ret = ath_register_led(sc, &sc->radio_led, trigger);
584 sc->radio_led.led_type = ATH_LED_RADIO;
585 if (ret)
586 goto fail;
587
588 trigger = ieee80211_get_assoc_led_name(sc->hw);
589 snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
590 "ath9k-%s:assoc", wiphy_name(sc->hw->wiphy));
591 ret = ath_register_led(sc, &sc->assoc_led, trigger);
592 sc->assoc_led.led_type = ATH_LED_ASSOC;
593 if (ret)
594 goto fail;
595
596 trigger = ieee80211_get_tx_led_name(sc->hw);
597 snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
598 "ath9k-%s:tx", wiphy_name(sc->hw->wiphy));
599 ret = ath_register_led(sc, &sc->tx_led, trigger);
600 sc->tx_led.led_type = ATH_LED_TX;
601 if (ret)
602 goto fail;
603
604 trigger = ieee80211_get_rx_led_name(sc->hw);
605 snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
606 "ath9k-%s:rx", wiphy_name(sc->hw->wiphy));
607 ret = ath_register_led(sc, &sc->rx_led, trigger);
608 sc->rx_led.led_type = ATH_LED_RX;
609 if (ret)
610 goto fail;
611
612 return;
613
614fail:
615 ath_deinit_leds(sc);
616}
617
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530618#ifdef CONFIG_RFKILL
Sujith9c84b792008-10-29 10:17:13 +0530619
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530620/*******************/
621/* Rfkill */
622/*******************/
623
624static void ath_radio_enable(struct ath_softc *sc)
625{
626 struct ath_hal *ah = sc->sc_ah;
627 int status;
628
629 spin_lock_bh(&sc->sc_resetlock);
630 if (!ath9k_hw_reset(ah, ah->ah_curchan,
631 sc->sc_ht_info.tx_chan_width,
632 sc->sc_tx_chainmask,
633 sc->sc_rx_chainmask,
634 sc->sc_ht_extprotspacing,
635 false, &status)) {
636 DPRINTF(sc, ATH_DBG_FATAL,
637 "%s: unable to reset channel %u (%uMhz) "
638 "flags 0x%x hal status %u\n", __func__,
639 ath9k_hw_mhz2ieee(ah,
640 ah->ah_curchan->channel,
641 ah->ah_curchan->channelFlags),
642 ah->ah_curchan->channel,
643 ah->ah_curchan->channelFlags, status);
644 }
645 spin_unlock_bh(&sc->sc_resetlock);
646
647 ath_update_txpow(sc);
648 if (ath_startrecv(sc) != 0) {
649 DPRINTF(sc, ATH_DBG_FATAL,
650 "%s: unable to restart recv logic\n", __func__);
651 return;
652 }
653
654 if (sc->sc_flags & SC_OP_BEACONS)
655 ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
656
657 /* Re-Enable interrupts */
658 ath9k_hw_set_interrupts(ah, sc->sc_imask);
659
660 /* Enable LED */
661 ath9k_hw_cfg_output(ah, ATH_LED_PIN,
662 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
663 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
664
665 ieee80211_wake_queues(sc->hw);
666}
667
668static void ath_radio_disable(struct ath_softc *sc)
669{
670 struct ath_hal *ah = sc->sc_ah;
671 int status;
672
673
674 ieee80211_stop_queues(sc->hw);
675
676 /* Disable LED */
677 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
678 ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
679
680 /* Disable interrupts */
681 ath9k_hw_set_interrupts(ah, 0);
682
683 ath_draintxq(sc, false); /* clear pending tx frames */
684 ath_stoprecv(sc); /* turn off frame recv */
685 ath_flushrecv(sc); /* flush recv queue */
686
687 spin_lock_bh(&sc->sc_resetlock);
688 if (!ath9k_hw_reset(ah, ah->ah_curchan,
689 sc->sc_ht_info.tx_chan_width,
690 sc->sc_tx_chainmask,
691 sc->sc_rx_chainmask,
692 sc->sc_ht_extprotspacing,
693 false, &status)) {
694 DPRINTF(sc, ATH_DBG_FATAL,
695 "%s: unable to reset channel %u (%uMhz) "
696 "flags 0x%x hal status %u\n", __func__,
697 ath9k_hw_mhz2ieee(ah,
698 ah->ah_curchan->channel,
699 ah->ah_curchan->channelFlags),
700 ah->ah_curchan->channel,
701 ah->ah_curchan->channelFlags, status);
702 }
703 spin_unlock_bh(&sc->sc_resetlock);
704
705 ath9k_hw_phy_disable(ah);
706 ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
707}
708
709static bool ath_is_rfkill_set(struct ath_softc *sc)
710{
711 struct ath_hal *ah = sc->sc_ah;
712
713 return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
714 ah->ah_rfkill_polarity;
715}
716
717/* h/w rfkill poll function */
718static void ath_rfkill_poll(struct work_struct *work)
719{
720 struct ath_softc *sc = container_of(work, struct ath_softc,
721 rf_kill.rfkill_poll.work);
722 bool radio_on;
723
724 if (sc->sc_flags & SC_OP_INVALID)
725 return;
726
727 radio_on = !ath_is_rfkill_set(sc);
728
729 /*
730 * enable/disable radio only when there is a
731 * state change in RF switch
732 */
733 if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
734 enum rfkill_state state;
735
736 if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
737 state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
738 : RFKILL_STATE_HARD_BLOCKED;
739 } else if (radio_on) {
740 ath_radio_enable(sc);
741 state = RFKILL_STATE_UNBLOCKED;
742 } else {
743 ath_radio_disable(sc);
744 state = RFKILL_STATE_HARD_BLOCKED;
745 }
746
747 if (state == RFKILL_STATE_HARD_BLOCKED)
748 sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
749 else
750 sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
751
752 rfkill_force_state(sc->rf_kill.rfkill, state);
753 }
754
755 queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
756 msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
757}
758
759/* s/w rfkill handler */
760static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
761{
762 struct ath_softc *sc = data;
763
764 switch (state) {
765 case RFKILL_STATE_SOFT_BLOCKED:
766 if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
767 SC_OP_RFKILL_SW_BLOCKED)))
768 ath_radio_disable(sc);
769 sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
770 return 0;
771 case RFKILL_STATE_UNBLOCKED:
772 if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
773 sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
774 if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
775 DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
776 "radio as it is disabled by h/w \n");
777 return -EPERM;
778 }
779 ath_radio_enable(sc);
780 }
781 return 0;
782 default:
783 return -EINVAL;
784 }
785}
786
787/* Init s/w rfkill */
788static int ath_init_sw_rfkill(struct ath_softc *sc)
789{
790 sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
791 RFKILL_TYPE_WLAN);
792 if (!sc->rf_kill.rfkill) {
793 DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
794 return -ENOMEM;
795 }
796
797 snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
798 "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
799 sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
800 sc->rf_kill.rfkill->data = sc;
801 sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
802 sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
803 sc->rf_kill.rfkill->user_claim_unsupported = 1;
804
805 return 0;
806}
807
808/* Deinitialize rfkill */
809static void ath_deinit_rfkill(struct ath_softc *sc)
810{
811 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
812 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
813
814 if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
815 rfkill_unregister(sc->rf_kill.rfkill);
816 sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
817 sc->rf_kill.rfkill = NULL;
818 }
819}
Sujith9c84b792008-10-29 10:17:13 +0530820
821static int ath_start_rfkill_poll(struct ath_softc *sc)
822{
823 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
824 queue_delayed_work(sc->hw->workqueue,
825 &sc->rf_kill.rfkill_poll, 0);
826
827 if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
828 if (rfkill_register(sc->rf_kill.rfkill)) {
829 DPRINTF(sc, ATH_DBG_FATAL,
830 "Unable to register rfkill\n");
831 rfkill_free(sc->rf_kill.rfkill);
832
833 /* Deinitialize the device */
834 if (sc->pdev->irq)
835 free_irq(sc->pdev->irq, sc);
836 ath_detach(sc);
837 pci_iounmap(sc->pdev, sc->mem);
838 pci_release_region(sc->pdev, 0);
839 pci_disable_device(sc->pdev);
840 ieee80211_free_hw(hw);
841 return -EIO;
842 } else {
843 sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
844 }
845 }
846
847 return 0;
848}
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530849#endif /* CONFIG_RFKILL */
850
Sujith9c84b792008-10-29 10:17:13 +0530851static void ath_detach(struct ath_softc *sc)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530852{
853 struct ieee80211_hw *hw = sc->hw;
Sujith9c84b792008-10-29 10:17:13 +0530854 int i = 0;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530855
856 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach ATH hw\n", __func__);
857
Sujith9c84b792008-10-29 10:17:13 +0530858 ieee80211_unregister_hw(hw);
859
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530860 ath_deinit_leds(sc);
861
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530862#ifdef CONFIG_RFKILL
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530863 ath_deinit_rfkill(sc);
864#endif
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530865 ath_rate_control_unregister();
Sujith9c84b792008-10-29 10:17:13 +0530866 ath_rate_detach(sc->sc_rc);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530867
868 ath_rx_cleanup(sc);
869 ath_tx_cleanup(sc);
870
Sujith9c84b792008-10-29 10:17:13 +0530871 tasklet_kill(&sc->intr_tq);
872 tasklet_kill(&sc->bcon_tasklet);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530873
Sujith9c84b792008-10-29 10:17:13 +0530874 if (!(sc->sc_flags & SC_OP_INVALID))
875 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530876
Sujith9c84b792008-10-29 10:17:13 +0530877 /* cleanup tx queues */
878 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
879 if (ATH_TXQ_SETUP(sc, i))
880 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
881
882 ath9k_hw_detach(sc->sc_ah);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530883}
884
Sujith9c84b792008-10-29 10:17:13 +0530885static int ath_attach(u16 devid, struct ath_softc *sc)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530886{
887 struct ieee80211_hw *hw = sc->hw;
888 int error = 0;
889
890 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach ATH hw\n", __func__);
891
892 error = ath_init(devid, sc);
893 if (error != 0)
894 return error;
895
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530896 /* get mac address from hardware and set in mac80211 */
897
898 SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
899
Sujith9c84b792008-10-29 10:17:13 +0530900 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
901 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
902 IEEE80211_HW_SIGNAL_DBM |
903 IEEE80211_HW_AMPDU_AGGREGATION;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530904
Sujith9c84b792008-10-29 10:17:13 +0530905 hw->wiphy->interface_modes =
906 BIT(NL80211_IFTYPE_AP) |
907 BIT(NL80211_IFTYPE_STATION) |
908 BIT(NL80211_IFTYPE_ADHOC);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530909
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530910 hw->queues = 4;
Sujith528f0c62008-10-29 10:14:26 +0530911 hw->sta_data_size = sizeof(struct ath_node);
Sujith5640b082008-10-29 10:16:06 +0530912 hw->vif_data_size = sizeof(struct ath_vap);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530913
914 /* Register rate control */
915 hw->rate_control_algorithm = "ath9k_rate_control";
916 error = ath_rate_control_register();
917 if (error != 0) {
918 DPRINTF(sc, ATH_DBG_FATAL,
919 "%s: Unable to register rate control "
920 "algorithm:%d\n", __func__, error);
921 ath_rate_control_unregister();
922 goto bad;
923 }
924
Sujith9c84b792008-10-29 10:17:13 +0530925 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
926 setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
927 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
928 setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
929 }
930
931 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &sc->sbands[IEEE80211_BAND_2GHZ];
932 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
933 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
934 &sc->sbands[IEEE80211_BAND_5GHZ];
935
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530936 error = ieee80211_register_hw(hw);
937 if (error != 0) {
938 ath_rate_control_unregister();
939 goto bad;
940 }
941
942 /* Initialize LED control */
943 ath_init_leds(sc);
944
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530945#ifdef CONFIG_RFKILL
946 /* Initialze h/w Rfkill */
947 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
948 INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
949
950 /* Initialize s/w rfkill */
951 if (ath_init_sw_rfkill(sc))
952 goto detach;
953#endif
954
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530955 /* initialize tx/rx engine */
956
957 error = ath_tx_init(sc, ATH_TXBUF);
958 if (error != 0)
959 goto detach;
960
961 error = ath_rx_init(sc, ATH_RXBUF);
962 if (error != 0)
963 goto detach;
964
965 return 0;
966detach:
967 ath_detach(sc);
968bad:
969 return error;
970}
971
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700972static int ath9k_start(struct ieee80211_hw *hw)
973{
974 struct ath_softc *sc = hw->priv;
975 struct ieee80211_channel *curchan = hw->conf.channel;
976 int error = 0, pos;
977
978 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Starting driver with "
979 "initial channel: %d MHz\n", __func__, curchan->center_freq);
980
981 /* setup initial channel */
982
983 pos = ath_get_channel(sc, curchan);
984 if (pos == -1) {
985 DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid channel\n", __func__);
Sujith9c84b792008-10-29 10:17:13 +0530986 error = -EINVAL;
987 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700988 }
989
990 sc->sc_ah->ah_channels[pos].chanmode =
991 (curchan->band == IEEE80211_BAND_2GHZ) ? CHANNEL_G : CHANNEL_A;
992
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700993 error = ath_open(sc, &sc->sc_ah->ah_channels[pos]);
994 if (error) {
995 DPRINTF(sc, ATH_DBG_FATAL,
996 "%s: Unable to complete ath_open\n", __func__);
Sujith9c84b792008-10-29 10:17:13 +0530997 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700998 }
999
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301000#ifdef CONFIG_RFKILL
Sujith9c84b792008-10-29 10:17:13 +05301001 error = ath_start_rfkill_poll(sc);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301002#endif
1003
Sujith9c84b792008-10-29 10:17:13 +05301004exit:
1005 return error;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001006}
1007
1008static int ath9k_tx(struct ieee80211_hw *hw,
1009 struct sk_buff *skb)
1010{
Jouni Malinen147583c2008-08-11 14:01:50 +03001011 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sujith528f0c62008-10-29 10:14:26 +05301012 struct ath_softc *sc = hw->priv;
1013 struct ath_tx_control txctl;
1014 int hdrlen, padsize;
1015
1016 memset(&txctl, 0, sizeof(struct ath_tx_control));
Jouni Malinen147583c2008-08-11 14:01:50 +03001017
1018 /*
1019 * As a temporary workaround, assign seq# here; this will likely need
1020 * to be cleaned up to work better with Beacon transmission and virtual
1021 * BSSes.
1022 */
1023 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1024 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1025 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
1026 sc->seq_no += 0x10;
1027 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1028 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
1029 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001030
1031 /* Add the padding after the header if this is not already done */
1032 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1033 if (hdrlen & 3) {
1034 padsize = hdrlen % 4;
1035 if (skb_headroom(skb) < padsize)
1036 return -1;
1037 skb_push(skb, padsize);
1038 memmove(skb->data, skb->data + padsize, hdrlen);
1039 }
1040
Sujith528f0c62008-10-29 10:14:26 +05301041 /* Check if a tx queue is available */
1042
1043 txctl.txq = ath_test_get_txq(sc, skb);
1044 if (!txctl.txq)
1045 goto exit;
1046
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001047 DPRINTF(sc, ATH_DBG_XMIT, "%s: transmitting packet, skb: %p\n",
1048 __func__,
1049 skb);
1050
Sujith528f0c62008-10-29 10:14:26 +05301051 if (ath_tx_start(sc, skb, &txctl) != 0) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001052 DPRINTF(sc, ATH_DBG_XMIT, "%s: TX failed\n", __func__);
Sujith528f0c62008-10-29 10:14:26 +05301053 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001054 }
1055
1056 return 0;
Sujith528f0c62008-10-29 10:14:26 +05301057exit:
1058 dev_kfree_skb_any(skb);
1059 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001060}
1061
1062static void ath9k_stop(struct ieee80211_hw *hw)
1063{
1064 struct ath_softc *sc = hw->priv;
Sujith9c84b792008-10-29 10:17:13 +05301065
1066 if (sc->sc_flags & SC_OP_INVALID) {
1067 DPRINTF(sc, ATH_DBG_ANY, "%s: Device not present\n", __func__);
1068 return;
1069 }
1070
1071 ath_stop(sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001072
1073 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Driver halt\n", __func__);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001074}
1075
1076static int ath9k_add_interface(struct ieee80211_hw *hw,
1077 struct ieee80211_if_init_conf *conf)
1078{
1079 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05301080 struct ath_vap *avp = (void *)conf->vif->drv_priv;
1081 int ic_opmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001082
1083 /* Support only vap for now */
1084
1085 if (sc->sc_nvaps)
1086 return -ENOBUFS;
1087
1088 switch (conf->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001089 case NL80211_IFTYPE_STATION:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001090 ic_opmode = ATH9K_M_STA;
1091 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02001092 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001093 ic_opmode = ATH9K_M_IBSS;
1094 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02001095 case NL80211_IFTYPE_AP:
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001096 ic_opmode = ATH9K_M_HOSTAP;
1097 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001098 default:
1099 DPRINTF(sc, ATH_DBG_FATAL,
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001100 "%s: Interface type %d not yet supported\n",
1101 __func__, conf->type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001102 return -EOPNOTSUPP;
1103 }
1104
1105 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach a VAP of type: %d\n",
1106 __func__,
1107 ic_opmode);
1108
Sujith5640b082008-10-29 10:16:06 +05301109 /* Set the VAP opmode */
1110 avp->av_opmode = ic_opmode;
1111 avp->av_bslot = -1;
1112
1113 if (ic_opmode == ATH9K_M_HOSTAP)
1114 ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
1115
1116 sc->sc_vaps[0] = conf->vif;
1117 sc->sc_nvaps++;
1118
1119 /* Set the device opmode */
1120 sc->sc_ah->ah_opmode = ic_opmode;
1121
1122 /* default VAP configuration */
1123 avp->av_config.av_fixed_rateset = IEEE80211_FIXED_RATE_NONE;
1124 avp->av_config.av_fixed_retryset = 0x03030303;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001125
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07001126 if (conf->type == NL80211_IFTYPE_AP) {
1127 /* TODO: is this a suitable place to start ANI for AP mode? */
1128 /* Start ANI */
1129 mod_timer(&sc->sc_ani.timer,
1130 jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
1131 }
1132
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001133 return 0;
1134}
1135
1136static void ath9k_remove_interface(struct ieee80211_hw *hw,
1137 struct ieee80211_if_init_conf *conf)
1138{
1139 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05301140 struct ath_vap *avp = (void *)conf->vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001141
1142 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach VAP\n", __func__);
1143
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001144#ifdef CONFIG_SLOW_ANT_DIV
1145 ath_slow_ant_div_stop(&sc->sc_antdiv);
1146#endif
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07001147 /* Stop ANI */
1148 del_timer_sync(&sc->sc_ani.timer);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001149
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001150 /* Reclaim beacon resources */
Sujithb4696c8b2008-08-11 14:04:52 +05301151 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
1152 sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001153 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
1154 ath_beacon_return(sc, avp);
1155 }
1156
Sujith672840a2008-08-11 14:05:08 +05301157 sc->sc_flags &= ~SC_OP_BEACONS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001158
Sujith5640b082008-10-29 10:16:06 +05301159 sc->sc_vaps[0] = NULL;
1160 sc->sc_nvaps--;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001161}
1162
Johannes Berge8975582008-10-09 12:18:51 +02001163static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001164{
1165 struct ath_softc *sc = hw->priv;
1166 struct ieee80211_channel *curchan = hw->conf.channel;
Johannes Berge8975582008-10-09 12:18:51 +02001167 struct ieee80211_conf *conf = &hw->conf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001168 int pos;
1169
1170 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
1171 __func__,
1172 curchan->center_freq);
1173
Johannes Bergae5eb022008-10-14 16:58:37 +02001174 /* Update chainmask */
1175 ath_update_chainmask(sc, conf->ht.enabled);
1176
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001177 pos = ath_get_channel(sc, curchan);
1178 if (pos == -1) {
1179 DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid channel\n", __func__);
1180 return -EINVAL;
1181 }
1182
1183 sc->sc_ah->ah_channels[pos].chanmode =
Sujith86b89ee2008-08-07 10:54:57 +05301184 (curchan->band == IEEE80211_BAND_2GHZ) ?
1185 CHANNEL_G : CHANNEL_A;
1186
Johannes Bergae5eb022008-10-14 16:58:37 +02001187 if (sc->sc_curaid && hw->conf.ht.enabled)
Sujith86b89ee2008-08-07 10:54:57 +05301188 sc->sc_ah->ah_channels[pos].chanmode =
1189 ath_get_extchanmode(sc, curchan);
1190
Luis R. Rodriguez5c020dc2008-10-22 13:28:45 -07001191 if (changed & IEEE80211_CONF_CHANGE_POWER)
1192 sc->sc_config.txpowlimit = 2 * conf->power_level;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001193
1194 /* set h/w channel */
1195 if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
1196 DPRINTF(sc, ATH_DBG_FATAL, "%s: Unable to set channel\n",
1197 __func__);
1198
1199 return 0;
1200}
1201
1202static int ath9k_config_interface(struct ieee80211_hw *hw,
1203 struct ieee80211_vif *vif,
1204 struct ieee80211_if_conf *conf)
1205{
1206 struct ath_softc *sc = hw->priv;
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001207 struct ath_hal *ah = sc->sc_ah;
Sujith5640b082008-10-29 10:16:06 +05301208 struct ath_vap *avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001209 u32 rfilt = 0;
1210 int error, i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001211
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001212 /* TODO: Need to decide which hw opmode to use for multi-interface
1213 * cases */
Johannes Berg05c914f2008-09-11 00:01:58 +02001214 if (vif->type == NL80211_IFTYPE_AP &&
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001215 ah->ah_opmode != ATH9K_M_HOSTAP) {
1216 ah->ah_opmode = ATH9K_M_HOSTAP;
1217 ath9k_hw_setopmode(ah);
1218 ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
1219 /* Request full reset to get hw opmode changed properly */
1220 sc->sc_flags |= SC_OP_FULL_RESET;
1221 }
1222
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001223 if ((conf->changed & IEEE80211_IFCC_BSSID) &&
1224 !is_zero_ether_addr(conf->bssid)) {
1225 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001226 case NL80211_IFTYPE_STATION:
1227 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001228 /* Update ratectrl about the new state */
1229 ath_rate_newstate(sc, avp);
1230
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001231 /* Set BSSID */
1232 memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
1233 sc->sc_curaid = 0;
1234 ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
1235 sc->sc_curaid);
1236
1237 /* Set aggregation protection mode parameters */
1238 sc->sc_config.ath_aggr_prot = 0;
1239
1240 /*
1241 * Reset our TSF so that its value is lower than the
1242 * beacon that we are trying to catch.
1243 * Only then hw will update its TSF register with the
1244 * new beacon. Reset the TSF before setting the BSSID
1245 * to avoid allowing in any frames that would update
1246 * our TSF only to have us clear it
1247 * immediately thereafter.
1248 */
1249 ath9k_hw_reset_tsf(sc->sc_ah);
1250
1251 /* Disable BMISS interrupt when we're not associated */
1252 ath9k_hw_set_interrupts(sc->sc_ah,
1253 sc->sc_imask &
1254 ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS));
1255 sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
1256
1257 DPRINTF(sc, ATH_DBG_CONFIG,
Johannes Berge1749612008-10-27 15:59:26 -07001258 "%s: RX filter 0x%x bssid %pM aid 0x%x\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001259 __func__, rfilt,
Johannes Berge1749612008-10-27 15:59:26 -07001260 sc->sc_curbssid, sc->sc_curaid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001261
1262 /* need to reconfigure the beacon */
Sujith672840a2008-08-11 14:05:08 +05301263 sc->sc_flags &= ~SC_OP_BEACONS ;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001264
1265 break;
1266 default:
1267 break;
1268 }
1269 }
1270
1271 if ((conf->changed & IEEE80211_IFCC_BEACON) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001272 ((vif->type == NL80211_IFTYPE_ADHOC) ||
1273 (vif->type == NL80211_IFTYPE_AP))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001274 /*
1275 * Allocate and setup the beacon frame.
1276 *
1277 * Stop any previous beacon DMA. This may be
1278 * necessary, for example, when an ibss merge
1279 * causes reconfiguration; we may be called
1280 * with beacon transmission active.
1281 */
1282 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
1283
1284 error = ath_beacon_alloc(sc, 0);
1285 if (error != 0)
1286 return error;
1287
1288 ath_beacon_sync(sc, 0);
1289 }
1290
1291 /* Check for WLAN_CAPABILITY_PRIVACY ? */
Sujith5640b082008-10-29 10:16:06 +05301292 if ((avp->av_opmode != ATH9K_M_STA)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001293 for (i = 0; i < IEEE80211_WEP_NKID; i++)
1294 if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
1295 ath9k_hw_keysetmac(sc->sc_ah,
1296 (u16)i,
1297 sc->sc_curbssid);
1298 }
1299
1300 /* Only legacy IBSS for now */
Johannes Berg05c914f2008-09-11 00:01:58 +02001301 if (vif->type == NL80211_IFTYPE_ADHOC)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001302 ath_update_chainmask(sc, 0);
1303
1304 return 0;
1305}
1306
1307#define SUPPORTED_FILTERS \
1308 (FIF_PROMISC_IN_BSS | \
1309 FIF_ALLMULTI | \
1310 FIF_CONTROL | \
1311 FIF_OTHER_BSS | \
1312 FIF_BCN_PRBRESP_PROMISC | \
1313 FIF_FCSFAIL)
1314
Sujith7dcfdcd2008-08-11 14:03:13 +05301315/* FIXME: sc->sc_full_reset ? */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001316static void ath9k_configure_filter(struct ieee80211_hw *hw,
1317 unsigned int changed_flags,
1318 unsigned int *total_flags,
1319 int mc_count,
1320 struct dev_mc_list *mclist)
1321{
1322 struct ath_softc *sc = hw->priv;
Sujith7dcfdcd2008-08-11 14:03:13 +05301323 u32 rfilt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001324
1325 changed_flags &= SUPPORTED_FILTERS;
1326 *total_flags &= SUPPORTED_FILTERS;
1327
Sujith7dcfdcd2008-08-11 14:03:13 +05301328 sc->rx_filter = *total_flags;
1329 rfilt = ath_calcrxfilter(sc);
1330 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
1331
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001332 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
1333 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
Sujith7dcfdcd2008-08-11 14:03:13 +05301334 ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001335 }
Sujith7dcfdcd2008-08-11 14:03:13 +05301336
1337 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set HW RX filter: 0x%x\n",
1338 __func__, sc->rx_filter);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001339}
1340
1341static void ath9k_sta_notify(struct ieee80211_hw *hw,
1342 struct ieee80211_vif *vif,
1343 enum sta_notify_cmd cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02001344 struct ieee80211_sta *sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001345{
1346 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001347
1348 switch (cmd) {
1349 case STA_NOTIFY_ADD:
Sujith5640b082008-10-29 10:16:06 +05301350 ath_node_attach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001351 break;
1352 case STA_NOTIFY_REMOVE:
Sujithb5aa9bf2008-10-29 10:13:31 +05301353 ath_node_detach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001354 break;
1355 default:
1356 break;
1357 }
1358}
1359
1360static int ath9k_conf_tx(struct ieee80211_hw *hw,
1361 u16 queue,
1362 const struct ieee80211_tx_queue_params *params)
1363{
1364 struct ath_softc *sc = hw->priv;
Sujithea9880f2008-08-07 10:53:10 +05301365 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001366 int ret = 0, qnum;
1367
1368 if (queue >= WME_NUM_AC)
1369 return 0;
1370
1371 qi.tqi_aifs = params->aifs;
1372 qi.tqi_cwmin = params->cw_min;
1373 qi.tqi_cwmax = params->cw_max;
1374 qi.tqi_burstTime = params->txop;
1375 qnum = ath_get_hal_qnum(queue, sc);
1376
1377 DPRINTF(sc, ATH_DBG_CONFIG,
1378 "%s: Configure tx [queue/halq] [%d/%d], "
1379 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1380 __func__,
1381 queue,
1382 qnum,
1383 params->aifs,
1384 params->cw_min,
1385 params->cw_max,
1386 params->txop);
1387
1388 ret = ath_txq_update(sc, qnum, &qi);
1389 if (ret)
1390 DPRINTF(sc, ATH_DBG_FATAL,
1391 "%s: TXQ Update failed\n", __func__);
1392
1393 return ret;
1394}
1395
1396static int ath9k_set_key(struct ieee80211_hw *hw,
1397 enum set_key_cmd cmd,
1398 const u8 *local_addr,
1399 const u8 *addr,
1400 struct ieee80211_key_conf *key)
1401{
1402 struct ath_softc *sc = hw->priv;
1403 int ret = 0;
1404
1405 DPRINTF(sc, ATH_DBG_KEYCACHE, " %s: Set HW Key\n", __func__);
1406
1407 switch (cmd) {
1408 case SET_KEY:
1409 ret = ath_key_config(sc, addr, key);
1410 if (!ret) {
1411 set_bit(key->keyidx, sc->sc_keymap);
1412 key->hw_key_idx = key->keyidx;
1413 /* push IV and Michael MIC generation to stack */
1414 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Senthil Balasubramanian1b961752008-09-01 19:45:21 +05301415 if (key->alg == ALG_TKIP)
1416 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001417 }
1418 break;
1419 case DISABLE_KEY:
1420 ath_key_delete(sc, key);
1421 clear_bit(key->keyidx, sc->sc_keymap);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001422 break;
1423 default:
1424 ret = -EINVAL;
1425 }
1426
1427 return ret;
1428}
1429
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001430static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1431 struct ieee80211_vif *vif,
1432 struct ieee80211_bss_conf *bss_conf,
1433 u32 changed)
1434{
1435 struct ath_softc *sc = hw->priv;
1436
1437 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1438 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed PREAMBLE %d\n",
1439 __func__,
1440 bss_conf->use_short_preamble);
1441 if (bss_conf->use_short_preamble)
Sujith672840a2008-08-11 14:05:08 +05301442 sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001443 else
Sujith672840a2008-08-11 14:05:08 +05301444 sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001445 }
1446
1447 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1448 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed CTS PROT %d\n",
1449 __func__,
1450 bss_conf->use_cts_prot);
1451 if (bss_conf->use_cts_prot &&
1452 hw->conf.channel->band != IEEE80211_BAND_5GHZ)
Sujith672840a2008-08-11 14:05:08 +05301453 sc->sc_flags |= SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001454 else
Sujith672840a2008-08-11 14:05:08 +05301455 sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001456 }
1457
1458 if (changed & BSS_CHANGED_HT) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001459 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed HT\n",
1460 __func__);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001461 ath9k_ht_conf(sc, bss_conf);
1462 }
1463
1464 if (changed & BSS_CHANGED_ASSOC) {
1465 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed ASSOC %d\n",
1466 __func__,
1467 bss_conf->assoc);
Sujith5640b082008-10-29 10:16:06 +05301468 ath9k_bss_assoc_info(sc, vif, bss_conf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001469 }
1470}
1471
1472static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
1473{
1474 u64 tsf;
1475 struct ath_softc *sc = hw->priv;
1476 struct ath_hal *ah = sc->sc_ah;
1477
1478 tsf = ath9k_hw_gettsf64(ah);
1479
1480 return tsf;
1481}
1482
1483static void ath9k_reset_tsf(struct ieee80211_hw *hw)
1484{
1485 struct ath_softc *sc = hw->priv;
1486 struct ath_hal *ah = sc->sc_ah;
1487
1488 ath9k_hw_reset_tsf(ah);
1489}
1490
1491static int ath9k_ampdu_action(struct ieee80211_hw *hw,
1492 enum ieee80211_ampdu_mlme_action action,
Johannes Berg17741cd2008-09-11 00:02:02 +02001493 struct ieee80211_sta *sta,
1494 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001495{
1496 struct ath_softc *sc = hw->priv;
1497 int ret = 0;
1498
1499 switch (action) {
1500 case IEEE80211_AMPDU_RX_START:
Sujithb5aa9bf2008-10-29 10:13:31 +05301501 ret = ath_rx_aggr_start(sc, sta, tid, ssn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001502 if (ret < 0)
1503 DPRINTF(sc, ATH_DBG_FATAL,
1504 "%s: Unable to start RX aggregation\n",
1505 __func__);
1506 break;
1507 case IEEE80211_AMPDU_RX_STOP:
Sujithb5aa9bf2008-10-29 10:13:31 +05301508 ret = ath_rx_aggr_stop(sc, sta, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001509 if (ret < 0)
1510 DPRINTF(sc, ATH_DBG_FATAL,
1511 "%s: Unable to stop RX aggregation\n",
1512 __func__);
1513 break;
1514 case IEEE80211_AMPDU_TX_START:
Sujithb5aa9bf2008-10-29 10:13:31 +05301515 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001516 if (ret < 0)
1517 DPRINTF(sc, ATH_DBG_FATAL,
1518 "%s: Unable to start TX aggregation\n",
1519 __func__);
1520 else
Johannes Berg17741cd2008-09-11 00:02:02 +02001521 ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001522 break;
1523 case IEEE80211_AMPDU_TX_STOP:
Sujithb5aa9bf2008-10-29 10:13:31 +05301524 ret = ath_tx_aggr_stop(sc, sta, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001525 if (ret < 0)
1526 DPRINTF(sc, ATH_DBG_FATAL,
1527 "%s: Unable to stop TX aggregation\n",
1528 __func__);
1529
Johannes Berg17741cd2008-09-11 00:02:02 +02001530 ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001531 break;
1532 default:
1533 DPRINTF(sc, ATH_DBG_FATAL,
1534 "%s: Unknown AMPDU action\n", __func__);
1535 }
1536
1537 return ret;
1538}
1539
Johannes Berg4233df62008-10-13 13:35:05 +02001540static int ath9k_no_fragmentation(struct ieee80211_hw *hw, u32 value)
1541{
1542 return -EOPNOTSUPP;
1543}
1544
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001545static struct ieee80211_ops ath9k_ops = {
1546 .tx = ath9k_tx,
1547 .start = ath9k_start,
1548 .stop = ath9k_stop,
1549 .add_interface = ath9k_add_interface,
1550 .remove_interface = ath9k_remove_interface,
1551 .config = ath9k_config,
1552 .config_interface = ath9k_config_interface,
1553 .configure_filter = ath9k_configure_filter,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001554 .sta_notify = ath9k_sta_notify,
1555 .conf_tx = ath9k_conf_tx,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001556 .bss_info_changed = ath9k_bss_info_changed,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001557 .set_key = ath9k_set_key,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001558 .get_tsf = ath9k_get_tsf,
1559 .reset_tsf = ath9k_reset_tsf,
Johannes Berg4233df62008-10-13 13:35:05 +02001560 .ampdu_action = ath9k_ampdu_action,
1561 .set_frag_threshold = ath9k_no_fragmentation,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001562};
1563
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001564static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1565{
1566 void __iomem *mem;
1567 struct ath_softc *sc;
1568 struct ieee80211_hw *hw;
1569 const char *athname;
1570 u8 csz;
1571 u32 val;
1572 int ret = 0;
1573
1574 if (pci_enable_device(pdev))
1575 return -EIO;
1576
1577 /* XXX 32-bit addressing only */
1578 if (pci_set_dma_mask(pdev, 0xffffffff)) {
1579 printk(KERN_ERR "ath_pci: 32-bit DMA not available\n");
1580 ret = -ENODEV;
1581 goto bad;
1582 }
1583
1584 /*
1585 * Cache line size is used to size and align various
1586 * structures used to communicate with the hardware.
1587 */
1588 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
1589 if (csz == 0) {
1590 /*
1591 * Linux 2.4.18 (at least) writes the cache line size
1592 * register as a 16-bit wide register which is wrong.
1593 * We must have this setup properly for rx buffer
1594 * DMA to work so force a reasonable value here if it
1595 * comes up zero.
1596 */
1597 csz = L1_CACHE_BYTES / sizeof(u32);
1598 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
1599 }
1600 /*
1601 * The default setting of latency timer yields poor results,
1602 * set it to the value used by other systems. It may be worth
1603 * tweaking this setting more.
1604 */
1605 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
1606
1607 pci_set_master(pdev);
1608
1609 /*
1610 * Disable the RETRY_TIMEOUT register (0x41) to keep
1611 * PCI Tx retries from interfering with C3 CPU state.
1612 */
1613 pci_read_config_dword(pdev, 0x40, &val);
1614 if ((val & 0x0000ff00) != 0)
1615 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1616
1617 ret = pci_request_region(pdev, 0, "ath9k");
1618 if (ret) {
1619 dev_err(&pdev->dev, "PCI memory region reserve error\n");
1620 ret = -ENODEV;
1621 goto bad;
1622 }
1623
1624 mem = pci_iomap(pdev, 0, 0);
1625 if (!mem) {
1626 printk(KERN_ERR "PCI memory map error\n") ;
1627 ret = -EIO;
1628 goto bad1;
1629 }
1630
1631 hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
1632 if (hw == NULL) {
1633 printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
1634 goto bad2;
1635 }
1636
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001637 SET_IEEE80211_DEV(hw, &pdev->dev);
1638 pci_set_drvdata(pdev, hw);
1639
1640 sc = hw->priv;
1641 sc->hw = hw;
1642 sc->pdev = pdev;
1643 sc->mem = mem;
1644
1645 if (ath_attach(id->device, sc) != 0) {
1646 ret = -ENODEV;
1647 goto bad3;
1648 }
1649
1650 /* setup interrupt service routine */
1651
1652 if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
1653 printk(KERN_ERR "%s: request_irq failed\n",
1654 wiphy_name(hw->wiphy));
1655 ret = -EIO;
1656 goto bad4;
1657 }
1658
1659 athname = ath9k_hw_probe(id->vendor, id->device);
1660
1661 printk(KERN_INFO "%s: %s: mem=0x%lx, irq=%d\n",
1662 wiphy_name(hw->wiphy),
1663 athname ? athname : "Atheros ???",
1664 (unsigned long)mem, pdev->irq);
1665
1666 return 0;
1667bad4:
1668 ath_detach(sc);
1669bad3:
1670 ieee80211_free_hw(hw);
1671bad2:
1672 pci_iounmap(pdev, mem);
1673bad1:
1674 pci_release_region(pdev, 0);
1675bad:
1676 pci_disable_device(pdev);
1677 return ret;
1678}
1679
1680static void ath_pci_remove(struct pci_dev *pdev)
1681{
1682 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1683 struct ath_softc *sc = hw->priv;
1684
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001685 ath_detach(sc);
Sujith9c84b792008-10-29 10:17:13 +05301686 if (pdev->irq)
1687 free_irq(pdev->irq, sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001688 pci_iounmap(pdev, sc->mem);
1689 pci_release_region(pdev, 0);
1690 pci_disable_device(pdev);
1691 ieee80211_free_hw(hw);
1692}
1693
1694#ifdef CONFIG_PM
1695
1696static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1697{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05301698 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1699 struct ath_softc *sc = hw->priv;
1700
1701 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301702
1703#ifdef CONFIG_RFKILL
1704 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1705 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
1706#endif
1707
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001708 pci_save_state(pdev);
1709 pci_disable_device(pdev);
1710 pci_set_power_state(pdev, 3);
1711
1712 return 0;
1713}
1714
1715static int ath_pci_resume(struct pci_dev *pdev)
1716{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05301717 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1718 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001719 u32 val;
1720 int err;
1721
1722 err = pci_enable_device(pdev);
1723 if (err)
1724 return err;
1725 pci_restore_state(pdev);
1726 /*
1727 * Suspend/Resume resets the PCI configuration space, so we have to
1728 * re-disable the RETRY_TIMEOUT register (0x41) to keep
1729 * PCI Tx retries from interfering with C3 CPU state
1730 */
1731 pci_read_config_dword(pdev, 0x40, &val);
1732 if ((val & 0x0000ff00) != 0)
1733 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1734
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05301735 /* Enable LED */
1736 ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
1737 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1738 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
1739
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301740#ifdef CONFIG_RFKILL
1741 /*
1742 * check the h/w rfkill state on resume
1743 * and start the rfkill poll timer
1744 */
1745 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1746 queue_delayed_work(sc->hw->workqueue,
1747 &sc->rf_kill.rfkill_poll, 0);
1748#endif
1749
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001750 return 0;
1751}
1752
1753#endif /* CONFIG_PM */
1754
1755MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
1756
1757static struct pci_driver ath_pci_driver = {
1758 .name = "ath9k",
1759 .id_table = ath_pci_id_table,
1760 .probe = ath_pci_probe,
1761 .remove = ath_pci_remove,
1762#ifdef CONFIG_PM
1763 .suspend = ath_pci_suspend,
1764 .resume = ath_pci_resume,
1765#endif /* CONFIG_PM */
1766};
1767
1768static int __init init_ath_pci(void)
1769{
1770 printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
1771
1772 if (pci_register_driver(&ath_pci_driver) < 0) {
1773 printk(KERN_ERR
1774 "ath_pci: No devices found, driver not installed.\n");
1775 pci_unregister_driver(&ath_pci_driver);
1776 return -ENODEV;
1777 }
1778
1779 return 0;
1780}
1781module_init(init_ath_pci);
1782
1783static void __exit exit_ath_pci(void)
1784{
1785 pci_unregister_driver(&ath_pci_driver);
1786 printk(KERN_INFO "%s: driver unloaded\n", dev_info);
1787}
1788module_exit(exit_ath_pci);