blob: 0194e44034e001afd2bdb3b54e6c11a288e25806 [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
619/*******************/
620/* Rfkill */
621/*******************/
622
623static void ath_radio_enable(struct ath_softc *sc)
624{
625 struct ath_hal *ah = sc->sc_ah;
626 int status;
627
628 spin_lock_bh(&sc->sc_resetlock);
629 if (!ath9k_hw_reset(ah, ah->ah_curchan,
630 sc->sc_ht_info.tx_chan_width,
631 sc->sc_tx_chainmask,
632 sc->sc_rx_chainmask,
633 sc->sc_ht_extprotspacing,
634 false, &status)) {
635 DPRINTF(sc, ATH_DBG_FATAL,
636 "%s: unable to reset channel %u (%uMhz) "
637 "flags 0x%x hal status %u\n", __func__,
638 ath9k_hw_mhz2ieee(ah,
639 ah->ah_curchan->channel,
640 ah->ah_curchan->channelFlags),
641 ah->ah_curchan->channel,
642 ah->ah_curchan->channelFlags, status);
643 }
644 spin_unlock_bh(&sc->sc_resetlock);
645
646 ath_update_txpow(sc);
647 if (ath_startrecv(sc) != 0) {
648 DPRINTF(sc, ATH_DBG_FATAL,
649 "%s: unable to restart recv logic\n", __func__);
650 return;
651 }
652
653 if (sc->sc_flags & SC_OP_BEACONS)
654 ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
655
656 /* Re-Enable interrupts */
657 ath9k_hw_set_interrupts(ah, sc->sc_imask);
658
659 /* Enable LED */
660 ath9k_hw_cfg_output(ah, ATH_LED_PIN,
661 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
662 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
663
664 ieee80211_wake_queues(sc->hw);
665}
666
667static void ath_radio_disable(struct ath_softc *sc)
668{
669 struct ath_hal *ah = sc->sc_ah;
670 int status;
671
672
673 ieee80211_stop_queues(sc->hw);
674
675 /* Disable LED */
676 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
677 ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
678
679 /* Disable interrupts */
680 ath9k_hw_set_interrupts(ah, 0);
681
682 ath_draintxq(sc, false); /* clear pending tx frames */
683 ath_stoprecv(sc); /* turn off frame recv */
684 ath_flushrecv(sc); /* flush recv queue */
685
686 spin_lock_bh(&sc->sc_resetlock);
687 if (!ath9k_hw_reset(ah, ah->ah_curchan,
688 sc->sc_ht_info.tx_chan_width,
689 sc->sc_tx_chainmask,
690 sc->sc_rx_chainmask,
691 sc->sc_ht_extprotspacing,
692 false, &status)) {
693 DPRINTF(sc, ATH_DBG_FATAL,
694 "%s: unable to reset channel %u (%uMhz) "
695 "flags 0x%x hal status %u\n", __func__,
696 ath9k_hw_mhz2ieee(ah,
697 ah->ah_curchan->channel,
698 ah->ah_curchan->channelFlags),
699 ah->ah_curchan->channel,
700 ah->ah_curchan->channelFlags, status);
701 }
702 spin_unlock_bh(&sc->sc_resetlock);
703
704 ath9k_hw_phy_disable(ah);
705 ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
706}
707
708static bool ath_is_rfkill_set(struct ath_softc *sc)
709{
710 struct ath_hal *ah = sc->sc_ah;
711
712 return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
713 ah->ah_rfkill_polarity;
714}
715
716/* h/w rfkill poll function */
717static void ath_rfkill_poll(struct work_struct *work)
718{
719 struct ath_softc *sc = container_of(work, struct ath_softc,
720 rf_kill.rfkill_poll.work);
721 bool radio_on;
722
723 if (sc->sc_flags & SC_OP_INVALID)
724 return;
725
726 radio_on = !ath_is_rfkill_set(sc);
727
728 /*
729 * enable/disable radio only when there is a
730 * state change in RF switch
731 */
732 if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
733 enum rfkill_state state;
734
735 if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
736 state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
737 : RFKILL_STATE_HARD_BLOCKED;
738 } else if (radio_on) {
739 ath_radio_enable(sc);
740 state = RFKILL_STATE_UNBLOCKED;
741 } else {
742 ath_radio_disable(sc);
743 state = RFKILL_STATE_HARD_BLOCKED;
744 }
745
746 if (state == RFKILL_STATE_HARD_BLOCKED)
747 sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
748 else
749 sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
750
751 rfkill_force_state(sc->rf_kill.rfkill, state);
752 }
753
754 queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
755 msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
756}
757
758/* s/w rfkill handler */
759static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
760{
761 struct ath_softc *sc = data;
762
763 switch (state) {
764 case RFKILL_STATE_SOFT_BLOCKED:
765 if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
766 SC_OP_RFKILL_SW_BLOCKED)))
767 ath_radio_disable(sc);
768 sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
769 return 0;
770 case RFKILL_STATE_UNBLOCKED:
771 if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
772 sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
773 if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
774 DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
775 "radio as it is disabled by h/w \n");
776 return -EPERM;
777 }
778 ath_radio_enable(sc);
779 }
780 return 0;
781 default:
782 return -EINVAL;
783 }
784}
785
786/* Init s/w rfkill */
787static int ath_init_sw_rfkill(struct ath_softc *sc)
788{
789 sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
790 RFKILL_TYPE_WLAN);
791 if (!sc->rf_kill.rfkill) {
792 DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
793 return -ENOMEM;
794 }
795
796 snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
797 "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
798 sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
799 sc->rf_kill.rfkill->data = sc;
800 sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
801 sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
802 sc->rf_kill.rfkill->user_claim_unsupported = 1;
803
804 return 0;
805}
806
807/* Deinitialize rfkill */
808static void ath_deinit_rfkill(struct ath_softc *sc)
809{
810 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
811 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
812
813 if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
814 rfkill_unregister(sc->rf_kill.rfkill);
815 sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
816 sc->rf_kill.rfkill = NULL;
817 }
818}
819#endif /* CONFIG_RFKILL */
820
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530821static int ath_detach(struct ath_softc *sc)
822{
823 struct ieee80211_hw *hw = sc->hw;
824
825 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach ATH hw\n", __func__);
826
827 /* Deinit LED control */
828 ath_deinit_leds(sc);
829
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530830#ifdef CONFIG_RFKILL
831 /* deinit rfkill */
832 ath_deinit_rfkill(sc);
833#endif
834
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530835 /* Unregister hw */
836
837 ieee80211_unregister_hw(hw);
838
839 /* unregister Rate control */
840 ath_rate_control_unregister();
841
842 /* tx/rx cleanup */
843
844 ath_rx_cleanup(sc);
845 ath_tx_cleanup(sc);
846
847 /* Deinit */
848
849 ath_deinit(sc);
850
851 return 0;
852}
853
854static int ath_attach(u16 devid,
855 struct ath_softc *sc)
856{
857 struct ieee80211_hw *hw = sc->hw;
858 int error = 0;
859
860 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach ATH hw\n", __func__);
861
862 error = ath_init(devid, sc);
863 if (error != 0)
864 return error;
865
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530866 /* get mac address from hardware and set in mac80211 */
867
868 SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
869
870 /* setup channels and rates */
871
872 sc->sbands[IEEE80211_BAND_2GHZ].channels =
873 sc->channels[IEEE80211_BAND_2GHZ];
874 sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
875 sc->rates[IEEE80211_BAND_2GHZ];
876 sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
877
878 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
879 /* Setup HT capabilities for 2.4Ghz*/
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200880 setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530881
882 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
883 &sc->sbands[IEEE80211_BAND_2GHZ];
884
885 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
886 sc->sbands[IEEE80211_BAND_5GHZ].channels =
887 sc->channels[IEEE80211_BAND_5GHZ];
888 sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
889 sc->rates[IEEE80211_BAND_5GHZ];
890 sc->sbands[IEEE80211_BAND_5GHZ].band =
891 IEEE80211_BAND_5GHZ;
892
893 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
894 /* Setup HT capabilities for 5Ghz*/
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200895 setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530896
897 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
898 &sc->sbands[IEEE80211_BAND_5GHZ];
899 }
900
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530901 hw->queues = 4;
Sujith528f0c62008-10-29 10:14:26 +0530902 hw->sta_data_size = sizeof(struct ath_node);
Sujith5640b082008-10-29 10:16:06 +0530903 hw->vif_data_size = sizeof(struct ath_vap);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530904
905 /* Register rate control */
906 hw->rate_control_algorithm = "ath9k_rate_control";
907 error = ath_rate_control_register();
908 if (error != 0) {
909 DPRINTF(sc, ATH_DBG_FATAL,
910 "%s: Unable to register rate control "
911 "algorithm:%d\n", __func__, error);
912 ath_rate_control_unregister();
913 goto bad;
914 }
915
916 error = ieee80211_register_hw(hw);
917 if (error != 0) {
918 ath_rate_control_unregister();
919 goto bad;
920 }
921
922 /* Initialize LED control */
923 ath_init_leds(sc);
924
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530925#ifdef CONFIG_RFKILL
926 /* Initialze h/w Rfkill */
927 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
928 INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
929
930 /* Initialize s/w rfkill */
931 if (ath_init_sw_rfkill(sc))
932 goto detach;
933#endif
934
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530935 /* initialize tx/rx engine */
936
937 error = ath_tx_init(sc, ATH_TXBUF);
938 if (error != 0)
939 goto detach;
940
941 error = ath_rx_init(sc, ATH_RXBUF);
942 if (error != 0)
943 goto detach;
944
945 return 0;
946detach:
947 ath_detach(sc);
948bad:
949 return error;
950}
951
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700952static int ath9k_start(struct ieee80211_hw *hw)
953{
954 struct ath_softc *sc = hw->priv;
955 struct ieee80211_channel *curchan = hw->conf.channel;
956 int error = 0, pos;
957
958 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Starting driver with "
959 "initial channel: %d MHz\n", __func__, curchan->center_freq);
960
961 /* setup initial channel */
962
963 pos = ath_get_channel(sc, curchan);
964 if (pos == -1) {
965 DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid channel\n", __func__);
966 return -EINVAL;
967 }
968
969 sc->sc_ah->ah_channels[pos].chanmode =
970 (curchan->band == IEEE80211_BAND_2GHZ) ? CHANNEL_G : CHANNEL_A;
971
972 /* open ath_dev */
973 error = ath_open(sc, &sc->sc_ah->ah_channels[pos]);
974 if (error) {
975 DPRINTF(sc, ATH_DBG_FATAL,
976 "%s: Unable to complete ath_open\n", __func__);
977 return error;
978 }
979
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +0530980#ifdef CONFIG_RFKILL
981 /* Start rfkill polling */
982 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
983 queue_delayed_work(sc->hw->workqueue,
984 &sc->rf_kill.rfkill_poll, 0);
985
986 if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
987 if (rfkill_register(sc->rf_kill.rfkill)) {
988 DPRINTF(sc, ATH_DBG_FATAL,
989 "Unable to register rfkill\n");
990 rfkill_free(sc->rf_kill.rfkill);
991
992 /* Deinitialize the device */
993 if (sc->pdev->irq)
994 free_irq(sc->pdev->irq, sc);
995 ath_detach(sc);
996 pci_iounmap(sc->pdev, sc->mem);
997 pci_release_region(sc->pdev, 0);
998 pci_disable_device(sc->pdev);
999 ieee80211_free_hw(hw);
1000 return -EIO;
1001 } else {
1002 sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
1003 }
1004 }
1005#endif
1006
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001007 ieee80211_wake_queues(hw);
1008 return 0;
1009}
1010
1011static int ath9k_tx(struct ieee80211_hw *hw,
1012 struct sk_buff *skb)
1013{
Jouni Malinen147583c2008-08-11 14:01:50 +03001014 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sujith528f0c62008-10-29 10:14:26 +05301015 struct ath_softc *sc = hw->priv;
1016 struct ath_tx_control txctl;
1017 int hdrlen, padsize;
1018
1019 memset(&txctl, 0, sizeof(struct ath_tx_control));
Jouni Malinen147583c2008-08-11 14:01:50 +03001020
1021 /*
1022 * As a temporary workaround, assign seq# here; this will likely need
1023 * to be cleaned up to work better with Beacon transmission and virtual
1024 * BSSes.
1025 */
1026 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1027 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1028 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
1029 sc->seq_no += 0x10;
1030 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1031 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
1032 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001033
1034 /* Add the padding after the header if this is not already done */
1035 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1036 if (hdrlen & 3) {
1037 padsize = hdrlen % 4;
1038 if (skb_headroom(skb) < padsize)
1039 return -1;
1040 skb_push(skb, padsize);
1041 memmove(skb->data, skb->data + padsize, hdrlen);
1042 }
1043
Sujith528f0c62008-10-29 10:14:26 +05301044 /* Check if a tx queue is available */
1045
1046 txctl.txq = ath_test_get_txq(sc, skb);
1047 if (!txctl.txq)
1048 goto exit;
1049
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001050 DPRINTF(sc, ATH_DBG_XMIT, "%s: transmitting packet, skb: %p\n",
1051 __func__,
1052 skb);
1053
Sujith528f0c62008-10-29 10:14:26 +05301054 if (ath_tx_start(sc, skb, &txctl) != 0) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001055 DPRINTF(sc, ATH_DBG_XMIT, "%s: TX failed\n", __func__);
Sujith528f0c62008-10-29 10:14:26 +05301056 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001057 }
1058
1059 return 0;
Sujith528f0c62008-10-29 10:14:26 +05301060exit:
1061 dev_kfree_skb_any(skb);
1062 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001063}
1064
1065static void ath9k_stop(struct ieee80211_hw *hw)
1066{
1067 struct ath_softc *sc = hw->priv;
1068 int error;
1069
1070 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Driver halt\n", __func__);
1071
1072 error = ath_suspend(sc);
1073 if (error)
1074 DPRINTF(sc, ATH_DBG_CONFIG,
1075 "%s: Device is no longer present\n", __func__);
1076
1077 ieee80211_stop_queues(hw);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301078
1079#ifdef CONFIG_RFKILL
1080 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1081 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
1082#endif
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001083}
1084
1085static int ath9k_add_interface(struct ieee80211_hw *hw,
1086 struct ieee80211_if_init_conf *conf)
1087{
1088 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05301089 struct ath_vap *avp = (void *)conf->vif->drv_priv;
1090 int ic_opmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001091
1092 /* Support only vap for now */
1093
1094 if (sc->sc_nvaps)
1095 return -ENOBUFS;
1096
1097 switch (conf->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001098 case NL80211_IFTYPE_STATION:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001099 ic_opmode = ATH9K_M_STA;
1100 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02001101 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001102 ic_opmode = ATH9K_M_IBSS;
1103 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02001104 case NL80211_IFTYPE_AP:
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001105 ic_opmode = ATH9K_M_HOSTAP;
1106 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001107 default:
1108 DPRINTF(sc, ATH_DBG_FATAL,
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001109 "%s: Interface type %d not yet supported\n",
1110 __func__, conf->type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001111 return -EOPNOTSUPP;
1112 }
1113
1114 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach a VAP of type: %d\n",
1115 __func__,
1116 ic_opmode);
1117
Sujith5640b082008-10-29 10:16:06 +05301118 /* Set the VAP opmode */
1119 avp->av_opmode = ic_opmode;
1120 avp->av_bslot = -1;
1121
1122 if (ic_opmode == ATH9K_M_HOSTAP)
1123 ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
1124
1125 sc->sc_vaps[0] = conf->vif;
1126 sc->sc_nvaps++;
1127
1128 /* Set the device opmode */
1129 sc->sc_ah->ah_opmode = ic_opmode;
1130
1131 /* default VAP configuration */
1132 avp->av_config.av_fixed_rateset = IEEE80211_FIXED_RATE_NONE;
1133 avp->av_config.av_fixed_retryset = 0x03030303;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001134
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07001135 if (conf->type == NL80211_IFTYPE_AP) {
1136 /* TODO: is this a suitable place to start ANI for AP mode? */
1137 /* Start ANI */
1138 mod_timer(&sc->sc_ani.timer,
1139 jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
1140 }
1141
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001142 return 0;
1143}
1144
1145static void ath9k_remove_interface(struct ieee80211_hw *hw,
1146 struct ieee80211_if_init_conf *conf)
1147{
1148 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05301149 struct ath_vap *avp = (void *)conf->vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001150
1151 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach VAP\n", __func__);
1152
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001153#ifdef CONFIG_SLOW_ANT_DIV
1154 ath_slow_ant_div_stop(&sc->sc_antdiv);
1155#endif
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07001156 /* Stop ANI */
1157 del_timer_sync(&sc->sc_ani.timer);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001158
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001159 /* Reclaim beacon resources */
Sujithb4696c8b2008-08-11 14:04:52 +05301160 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
1161 sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001162 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
1163 ath_beacon_return(sc, avp);
1164 }
1165
Sujith672840a2008-08-11 14:05:08 +05301166 sc->sc_flags &= ~SC_OP_BEACONS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001167
Sujith5640b082008-10-29 10:16:06 +05301168 sc->sc_vaps[0] = NULL;
1169 sc->sc_nvaps--;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001170}
1171
Johannes Berge8975582008-10-09 12:18:51 +02001172static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001173{
1174 struct ath_softc *sc = hw->priv;
1175 struct ieee80211_channel *curchan = hw->conf.channel;
Johannes Berge8975582008-10-09 12:18:51 +02001176 struct ieee80211_conf *conf = &hw->conf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001177 int pos;
1178
1179 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
1180 __func__,
1181 curchan->center_freq);
1182
Johannes Bergae5eb022008-10-14 16:58:37 +02001183 /* Update chainmask */
1184 ath_update_chainmask(sc, conf->ht.enabled);
1185
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001186 pos = ath_get_channel(sc, curchan);
1187 if (pos == -1) {
1188 DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid channel\n", __func__);
1189 return -EINVAL;
1190 }
1191
1192 sc->sc_ah->ah_channels[pos].chanmode =
Sujith86b89ee2008-08-07 10:54:57 +05301193 (curchan->band == IEEE80211_BAND_2GHZ) ?
1194 CHANNEL_G : CHANNEL_A;
1195
Johannes Bergae5eb022008-10-14 16:58:37 +02001196 if (sc->sc_curaid && hw->conf.ht.enabled)
Sujith86b89ee2008-08-07 10:54:57 +05301197 sc->sc_ah->ah_channels[pos].chanmode =
1198 ath_get_extchanmode(sc, curchan);
1199
Luis R. Rodriguez5c020dc2008-10-22 13:28:45 -07001200 if (changed & IEEE80211_CONF_CHANGE_POWER)
1201 sc->sc_config.txpowlimit = 2 * conf->power_level;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001202
1203 /* set h/w channel */
1204 if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
1205 DPRINTF(sc, ATH_DBG_FATAL, "%s: Unable to set channel\n",
1206 __func__);
1207
1208 return 0;
1209}
1210
1211static int ath9k_config_interface(struct ieee80211_hw *hw,
1212 struct ieee80211_vif *vif,
1213 struct ieee80211_if_conf *conf)
1214{
1215 struct ath_softc *sc = hw->priv;
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001216 struct ath_hal *ah = sc->sc_ah;
Sujith5640b082008-10-29 10:16:06 +05301217 struct ath_vap *avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001218 u32 rfilt = 0;
1219 int error, i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001220
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001221 /* TODO: Need to decide which hw opmode to use for multi-interface
1222 * cases */
Johannes Berg05c914f2008-09-11 00:01:58 +02001223 if (vif->type == NL80211_IFTYPE_AP &&
Jouni Malinen2ad67de2008-08-11 14:01:47 +03001224 ah->ah_opmode != ATH9K_M_HOSTAP) {
1225 ah->ah_opmode = ATH9K_M_HOSTAP;
1226 ath9k_hw_setopmode(ah);
1227 ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
1228 /* Request full reset to get hw opmode changed properly */
1229 sc->sc_flags |= SC_OP_FULL_RESET;
1230 }
1231
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001232 if ((conf->changed & IEEE80211_IFCC_BSSID) &&
1233 !is_zero_ether_addr(conf->bssid)) {
1234 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001235 case NL80211_IFTYPE_STATION:
1236 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001237 /* Update ratectrl about the new state */
1238 ath_rate_newstate(sc, avp);
1239
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001240 /* Set BSSID */
1241 memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
1242 sc->sc_curaid = 0;
1243 ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
1244 sc->sc_curaid);
1245
1246 /* Set aggregation protection mode parameters */
1247 sc->sc_config.ath_aggr_prot = 0;
1248
1249 /*
1250 * Reset our TSF so that its value is lower than the
1251 * beacon that we are trying to catch.
1252 * Only then hw will update its TSF register with the
1253 * new beacon. Reset the TSF before setting the BSSID
1254 * to avoid allowing in any frames that would update
1255 * our TSF only to have us clear it
1256 * immediately thereafter.
1257 */
1258 ath9k_hw_reset_tsf(sc->sc_ah);
1259
1260 /* Disable BMISS interrupt when we're not associated */
1261 ath9k_hw_set_interrupts(sc->sc_ah,
1262 sc->sc_imask &
1263 ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS));
1264 sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
1265
1266 DPRINTF(sc, ATH_DBG_CONFIG,
Johannes Berge1749612008-10-27 15:59:26 -07001267 "%s: RX filter 0x%x bssid %pM aid 0x%x\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001268 __func__, rfilt,
Johannes Berge1749612008-10-27 15:59:26 -07001269 sc->sc_curbssid, sc->sc_curaid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001270
1271 /* need to reconfigure the beacon */
Sujith672840a2008-08-11 14:05:08 +05301272 sc->sc_flags &= ~SC_OP_BEACONS ;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001273
1274 break;
1275 default:
1276 break;
1277 }
1278 }
1279
1280 if ((conf->changed & IEEE80211_IFCC_BEACON) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001281 ((vif->type == NL80211_IFTYPE_ADHOC) ||
1282 (vif->type == NL80211_IFTYPE_AP))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001283 /*
1284 * Allocate and setup the beacon frame.
1285 *
1286 * Stop any previous beacon DMA. This may be
1287 * necessary, for example, when an ibss merge
1288 * causes reconfiguration; we may be called
1289 * with beacon transmission active.
1290 */
1291 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
1292
1293 error = ath_beacon_alloc(sc, 0);
1294 if (error != 0)
1295 return error;
1296
1297 ath_beacon_sync(sc, 0);
1298 }
1299
1300 /* Check for WLAN_CAPABILITY_PRIVACY ? */
Sujith5640b082008-10-29 10:16:06 +05301301 if ((avp->av_opmode != ATH9K_M_STA)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001302 for (i = 0; i < IEEE80211_WEP_NKID; i++)
1303 if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
1304 ath9k_hw_keysetmac(sc->sc_ah,
1305 (u16)i,
1306 sc->sc_curbssid);
1307 }
1308
1309 /* Only legacy IBSS for now */
Johannes Berg05c914f2008-09-11 00:01:58 +02001310 if (vif->type == NL80211_IFTYPE_ADHOC)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001311 ath_update_chainmask(sc, 0);
1312
1313 return 0;
1314}
1315
1316#define SUPPORTED_FILTERS \
1317 (FIF_PROMISC_IN_BSS | \
1318 FIF_ALLMULTI | \
1319 FIF_CONTROL | \
1320 FIF_OTHER_BSS | \
1321 FIF_BCN_PRBRESP_PROMISC | \
1322 FIF_FCSFAIL)
1323
Sujith7dcfdcd2008-08-11 14:03:13 +05301324/* FIXME: sc->sc_full_reset ? */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001325static void ath9k_configure_filter(struct ieee80211_hw *hw,
1326 unsigned int changed_flags,
1327 unsigned int *total_flags,
1328 int mc_count,
1329 struct dev_mc_list *mclist)
1330{
1331 struct ath_softc *sc = hw->priv;
Sujith7dcfdcd2008-08-11 14:03:13 +05301332 u32 rfilt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001333
1334 changed_flags &= SUPPORTED_FILTERS;
1335 *total_flags &= SUPPORTED_FILTERS;
1336
Sujith7dcfdcd2008-08-11 14:03:13 +05301337 sc->rx_filter = *total_flags;
1338 rfilt = ath_calcrxfilter(sc);
1339 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
1340
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001341 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
1342 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
Sujith7dcfdcd2008-08-11 14:03:13 +05301343 ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001344 }
Sujith7dcfdcd2008-08-11 14:03:13 +05301345
1346 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set HW RX filter: 0x%x\n",
1347 __func__, sc->rx_filter);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001348}
1349
1350static void ath9k_sta_notify(struct ieee80211_hw *hw,
1351 struct ieee80211_vif *vif,
1352 enum sta_notify_cmd cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02001353 struct ieee80211_sta *sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001354{
1355 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001356
1357 switch (cmd) {
1358 case STA_NOTIFY_ADD:
Sujith5640b082008-10-29 10:16:06 +05301359 ath_node_attach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001360 break;
1361 case STA_NOTIFY_REMOVE:
Sujithb5aa9bf2008-10-29 10:13:31 +05301362 ath_node_detach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001363 break;
1364 default:
1365 break;
1366 }
1367}
1368
1369static int ath9k_conf_tx(struct ieee80211_hw *hw,
1370 u16 queue,
1371 const struct ieee80211_tx_queue_params *params)
1372{
1373 struct ath_softc *sc = hw->priv;
Sujithea9880f2008-08-07 10:53:10 +05301374 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001375 int ret = 0, qnum;
1376
1377 if (queue >= WME_NUM_AC)
1378 return 0;
1379
1380 qi.tqi_aifs = params->aifs;
1381 qi.tqi_cwmin = params->cw_min;
1382 qi.tqi_cwmax = params->cw_max;
1383 qi.tqi_burstTime = params->txop;
1384 qnum = ath_get_hal_qnum(queue, sc);
1385
1386 DPRINTF(sc, ATH_DBG_CONFIG,
1387 "%s: Configure tx [queue/halq] [%d/%d], "
1388 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1389 __func__,
1390 queue,
1391 qnum,
1392 params->aifs,
1393 params->cw_min,
1394 params->cw_max,
1395 params->txop);
1396
1397 ret = ath_txq_update(sc, qnum, &qi);
1398 if (ret)
1399 DPRINTF(sc, ATH_DBG_FATAL,
1400 "%s: TXQ Update failed\n", __func__);
1401
1402 return ret;
1403}
1404
1405static int ath9k_set_key(struct ieee80211_hw *hw,
1406 enum set_key_cmd cmd,
1407 const u8 *local_addr,
1408 const u8 *addr,
1409 struct ieee80211_key_conf *key)
1410{
1411 struct ath_softc *sc = hw->priv;
1412 int ret = 0;
1413
1414 DPRINTF(sc, ATH_DBG_KEYCACHE, " %s: Set HW Key\n", __func__);
1415
1416 switch (cmd) {
1417 case SET_KEY:
1418 ret = ath_key_config(sc, addr, key);
1419 if (!ret) {
1420 set_bit(key->keyidx, sc->sc_keymap);
1421 key->hw_key_idx = key->keyidx;
1422 /* push IV and Michael MIC generation to stack */
1423 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Senthil Balasubramanian1b961752008-09-01 19:45:21 +05301424 if (key->alg == ALG_TKIP)
1425 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001426 }
1427 break;
1428 case DISABLE_KEY:
1429 ath_key_delete(sc, key);
1430 clear_bit(key->keyidx, sc->sc_keymap);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001431 break;
1432 default:
1433 ret = -EINVAL;
1434 }
1435
1436 return ret;
1437}
1438
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001439static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1440 struct ieee80211_vif *vif,
1441 struct ieee80211_bss_conf *bss_conf,
1442 u32 changed)
1443{
1444 struct ath_softc *sc = hw->priv;
1445
1446 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1447 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed PREAMBLE %d\n",
1448 __func__,
1449 bss_conf->use_short_preamble);
1450 if (bss_conf->use_short_preamble)
Sujith672840a2008-08-11 14:05:08 +05301451 sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001452 else
Sujith672840a2008-08-11 14:05:08 +05301453 sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001454 }
1455
1456 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1457 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed CTS PROT %d\n",
1458 __func__,
1459 bss_conf->use_cts_prot);
1460 if (bss_conf->use_cts_prot &&
1461 hw->conf.channel->band != IEEE80211_BAND_5GHZ)
Sujith672840a2008-08-11 14:05:08 +05301462 sc->sc_flags |= SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001463 else
Sujith672840a2008-08-11 14:05:08 +05301464 sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001465 }
1466
1467 if (changed & BSS_CHANGED_HT) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001468 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed HT\n",
1469 __func__);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001470 ath9k_ht_conf(sc, bss_conf);
1471 }
1472
1473 if (changed & BSS_CHANGED_ASSOC) {
1474 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed ASSOC %d\n",
1475 __func__,
1476 bss_conf->assoc);
Sujith5640b082008-10-29 10:16:06 +05301477 ath9k_bss_assoc_info(sc, vif, bss_conf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001478 }
1479}
1480
1481static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
1482{
1483 u64 tsf;
1484 struct ath_softc *sc = hw->priv;
1485 struct ath_hal *ah = sc->sc_ah;
1486
1487 tsf = ath9k_hw_gettsf64(ah);
1488
1489 return tsf;
1490}
1491
1492static void ath9k_reset_tsf(struct ieee80211_hw *hw)
1493{
1494 struct ath_softc *sc = hw->priv;
1495 struct ath_hal *ah = sc->sc_ah;
1496
1497 ath9k_hw_reset_tsf(ah);
1498}
1499
1500static int ath9k_ampdu_action(struct ieee80211_hw *hw,
1501 enum ieee80211_ampdu_mlme_action action,
Johannes Berg17741cd2008-09-11 00:02:02 +02001502 struct ieee80211_sta *sta,
1503 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001504{
1505 struct ath_softc *sc = hw->priv;
1506 int ret = 0;
1507
1508 switch (action) {
1509 case IEEE80211_AMPDU_RX_START:
Sujithb5aa9bf2008-10-29 10:13:31 +05301510 ret = ath_rx_aggr_start(sc, sta, tid, ssn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001511 if (ret < 0)
1512 DPRINTF(sc, ATH_DBG_FATAL,
1513 "%s: Unable to start RX aggregation\n",
1514 __func__);
1515 break;
1516 case IEEE80211_AMPDU_RX_STOP:
Sujithb5aa9bf2008-10-29 10:13:31 +05301517 ret = ath_rx_aggr_stop(sc, sta, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001518 if (ret < 0)
1519 DPRINTF(sc, ATH_DBG_FATAL,
1520 "%s: Unable to stop RX aggregation\n",
1521 __func__);
1522 break;
1523 case IEEE80211_AMPDU_TX_START:
Sujithb5aa9bf2008-10-29 10:13:31 +05301524 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001525 if (ret < 0)
1526 DPRINTF(sc, ATH_DBG_FATAL,
1527 "%s: Unable to start TX aggregation\n",
1528 __func__);
1529 else
Johannes Berg17741cd2008-09-11 00:02:02 +02001530 ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001531 break;
1532 case IEEE80211_AMPDU_TX_STOP:
Sujithb5aa9bf2008-10-29 10:13:31 +05301533 ret = ath_tx_aggr_stop(sc, sta, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001534 if (ret < 0)
1535 DPRINTF(sc, ATH_DBG_FATAL,
1536 "%s: Unable to stop TX aggregation\n",
1537 __func__);
1538
Johannes Berg17741cd2008-09-11 00:02:02 +02001539 ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001540 break;
1541 default:
1542 DPRINTF(sc, ATH_DBG_FATAL,
1543 "%s: Unknown AMPDU action\n", __func__);
1544 }
1545
1546 return ret;
1547}
1548
Johannes Berg4233df62008-10-13 13:35:05 +02001549static int ath9k_no_fragmentation(struct ieee80211_hw *hw, u32 value)
1550{
1551 return -EOPNOTSUPP;
1552}
1553
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001554static struct ieee80211_ops ath9k_ops = {
1555 .tx = ath9k_tx,
1556 .start = ath9k_start,
1557 .stop = ath9k_stop,
1558 .add_interface = ath9k_add_interface,
1559 .remove_interface = ath9k_remove_interface,
1560 .config = ath9k_config,
1561 .config_interface = ath9k_config_interface,
1562 .configure_filter = ath9k_configure_filter,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001563 .sta_notify = ath9k_sta_notify,
1564 .conf_tx = ath9k_conf_tx,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001565 .bss_info_changed = ath9k_bss_info_changed,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001566 .set_key = ath9k_set_key,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001567 .get_tsf = ath9k_get_tsf,
1568 .reset_tsf = ath9k_reset_tsf,
Johannes Berg4233df62008-10-13 13:35:05 +02001569 .ampdu_action = ath9k_ampdu_action,
1570 .set_frag_threshold = ath9k_no_fragmentation,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001571};
1572
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001573static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1574{
1575 void __iomem *mem;
1576 struct ath_softc *sc;
1577 struct ieee80211_hw *hw;
1578 const char *athname;
1579 u8 csz;
1580 u32 val;
1581 int ret = 0;
1582
1583 if (pci_enable_device(pdev))
1584 return -EIO;
1585
1586 /* XXX 32-bit addressing only */
1587 if (pci_set_dma_mask(pdev, 0xffffffff)) {
1588 printk(KERN_ERR "ath_pci: 32-bit DMA not available\n");
1589 ret = -ENODEV;
1590 goto bad;
1591 }
1592
1593 /*
1594 * Cache line size is used to size and align various
1595 * structures used to communicate with the hardware.
1596 */
1597 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
1598 if (csz == 0) {
1599 /*
1600 * Linux 2.4.18 (at least) writes the cache line size
1601 * register as a 16-bit wide register which is wrong.
1602 * We must have this setup properly for rx buffer
1603 * DMA to work so force a reasonable value here if it
1604 * comes up zero.
1605 */
1606 csz = L1_CACHE_BYTES / sizeof(u32);
1607 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
1608 }
1609 /*
1610 * The default setting of latency timer yields poor results,
1611 * set it to the value used by other systems. It may be worth
1612 * tweaking this setting more.
1613 */
1614 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
1615
1616 pci_set_master(pdev);
1617
1618 /*
1619 * Disable the RETRY_TIMEOUT register (0x41) to keep
1620 * PCI Tx retries from interfering with C3 CPU state.
1621 */
1622 pci_read_config_dword(pdev, 0x40, &val);
1623 if ((val & 0x0000ff00) != 0)
1624 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1625
1626 ret = pci_request_region(pdev, 0, "ath9k");
1627 if (ret) {
1628 dev_err(&pdev->dev, "PCI memory region reserve error\n");
1629 ret = -ENODEV;
1630 goto bad;
1631 }
1632
1633 mem = pci_iomap(pdev, 0, 0);
1634 if (!mem) {
1635 printk(KERN_ERR "PCI memory map error\n") ;
1636 ret = -EIO;
1637 goto bad1;
1638 }
1639
1640 hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
1641 if (hw == NULL) {
1642 printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
1643 goto bad2;
1644 }
1645
Sujith19b73c72008-08-14 13:28:20 +05301646 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Jouni Malinene022edb2008-08-22 17:31:33 +03001647 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Sujith19b73c72008-08-14 13:28:20 +05301648 IEEE80211_HW_SIGNAL_DBM |
Sujith8b30b1f2008-10-24 09:55:27 +05301649 IEEE80211_HW_NOISE_DBM |
1650 IEEE80211_HW_AMPDU_AGGREGATION;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001651
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001652 hw->wiphy->interface_modes =
1653 BIT(NL80211_IFTYPE_AP) |
1654 BIT(NL80211_IFTYPE_STATION) |
1655 BIT(NL80211_IFTYPE_ADHOC);
1656
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001657 SET_IEEE80211_DEV(hw, &pdev->dev);
1658 pci_set_drvdata(pdev, hw);
1659
1660 sc = hw->priv;
1661 sc->hw = hw;
1662 sc->pdev = pdev;
1663 sc->mem = mem;
1664
1665 if (ath_attach(id->device, sc) != 0) {
1666 ret = -ENODEV;
1667 goto bad3;
1668 }
1669
1670 /* setup interrupt service routine */
1671
1672 if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
1673 printk(KERN_ERR "%s: request_irq failed\n",
1674 wiphy_name(hw->wiphy));
1675 ret = -EIO;
1676 goto bad4;
1677 }
1678
1679 athname = ath9k_hw_probe(id->vendor, id->device);
1680
1681 printk(KERN_INFO "%s: %s: mem=0x%lx, irq=%d\n",
1682 wiphy_name(hw->wiphy),
1683 athname ? athname : "Atheros ???",
1684 (unsigned long)mem, pdev->irq);
1685
1686 return 0;
1687bad4:
1688 ath_detach(sc);
1689bad3:
1690 ieee80211_free_hw(hw);
1691bad2:
1692 pci_iounmap(pdev, mem);
1693bad1:
1694 pci_release_region(pdev, 0);
1695bad:
1696 pci_disable_device(pdev);
1697 return ret;
1698}
1699
1700static void ath_pci_remove(struct pci_dev *pdev)
1701{
1702 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1703 struct ath_softc *sc = hw->priv;
Senthil Balasubramanian6115e852008-09-22 14:22:39 +05301704 enum ath9k_int status;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001705
Senthil Balasubramanian6115e852008-09-22 14:22:39 +05301706 if (pdev->irq) {
1707 ath9k_hw_set_interrupts(sc->sc_ah, 0);
1708 /* clear the ISR */
1709 ath9k_hw_getisr(sc->sc_ah, &status);
David S. Millerb262e602008-10-01 06:12:56 -07001710 sc->sc_flags |= SC_OP_INVALID;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001711 free_irq(pdev->irq, sc);
Senthil Balasubramanian6115e852008-09-22 14:22:39 +05301712 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001713 ath_detach(sc);
Senthil Balasubramanian6115e852008-09-22 14:22:39 +05301714
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001715 pci_iounmap(pdev, sc->mem);
1716 pci_release_region(pdev, 0);
1717 pci_disable_device(pdev);
1718 ieee80211_free_hw(hw);
1719}
1720
1721#ifdef CONFIG_PM
1722
1723static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1724{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05301725 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1726 struct ath_softc *sc = hw->priv;
1727
1728 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301729
1730#ifdef CONFIG_RFKILL
1731 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1732 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
1733#endif
1734
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001735 pci_save_state(pdev);
1736 pci_disable_device(pdev);
1737 pci_set_power_state(pdev, 3);
1738
1739 return 0;
1740}
1741
1742static int ath_pci_resume(struct pci_dev *pdev)
1743{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05301744 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1745 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001746 u32 val;
1747 int err;
1748
1749 err = pci_enable_device(pdev);
1750 if (err)
1751 return err;
1752 pci_restore_state(pdev);
1753 /*
1754 * Suspend/Resume resets the PCI configuration space, so we have to
1755 * re-disable the RETRY_TIMEOUT register (0x41) to keep
1756 * PCI Tx retries from interfering with C3 CPU state
1757 */
1758 pci_read_config_dword(pdev, 0x40, &val);
1759 if ((val & 0x0000ff00) != 0)
1760 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1761
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05301762 /* Enable LED */
1763 ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
1764 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1765 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
1766
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301767#ifdef CONFIG_RFKILL
1768 /*
1769 * check the h/w rfkill state on resume
1770 * and start the rfkill poll timer
1771 */
1772 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1773 queue_delayed_work(sc->hw->workqueue,
1774 &sc->rf_kill.rfkill_poll, 0);
1775#endif
1776
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001777 return 0;
1778}
1779
1780#endif /* CONFIG_PM */
1781
1782MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
1783
1784static struct pci_driver ath_pci_driver = {
1785 .name = "ath9k",
1786 .id_table = ath_pci_id_table,
1787 .probe = ath_pci_probe,
1788 .remove = ath_pci_remove,
1789#ifdef CONFIG_PM
1790 .suspend = ath_pci_suspend,
1791 .resume = ath_pci_resume,
1792#endif /* CONFIG_PM */
1793};
1794
1795static int __init init_ath_pci(void)
1796{
1797 printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
1798
1799 if (pci_register_driver(&ath_pci_driver) < 0) {
1800 printk(KERN_ERR
1801 "ath_pci: No devices found, driver not installed.\n");
1802 pci_unregister_driver(&ath_pci_driver);
1803 return -ENODEV;
1804 }
1805
1806 return 0;
1807}
1808module_init(init_ath_pci);
1809
1810static void __exit exit_ath_pci(void)
1811{
1812 pci_unregister_driver(&ath_pci_driver);
1813 printk(KERN_INFO "%s: driver unloaded\n", dev_info);
1814}
1815module_exit(exit_ath_pci);