main.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. /*
  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. /* mac80211 and PCI callbacks */
  17. #include <linux/nl80211.h>
  18. #include "core.h"
  19. #define ATH_PCI_VERSION "0.1"
  20. #define IEEE80211_HTCAP_MAXRXAMPDU_FACTOR 13
  21. static char *dev_info = "ath9k";
  22. MODULE_AUTHOR("Atheros Communications");
  23. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  24. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  25. MODULE_LICENSE("Dual BSD/GPL");
  26. static struct pci_device_id ath_pci_id_table[] __devinitdata = {
  27. { PCI_VDEVICE(ATHEROS, 0x0023) }, /* PCI */
  28. { PCI_VDEVICE(ATHEROS, 0x0024) }, /* PCI-E */
  29. { PCI_VDEVICE(ATHEROS, 0x0027) }, /* PCI */
  30. { PCI_VDEVICE(ATHEROS, 0x0029) }, /* PCI */
  31. { PCI_VDEVICE(ATHEROS, 0x002A) }, /* PCI-E */
  32. { 0 }
  33. };
  34. static int ath_get_channel(struct ath_softc *sc,
  35. struct ieee80211_channel *chan)
  36. {
  37. int i;
  38. for (i = 0; i < sc->sc_ah->ah_nchan; i++) {
  39. if (sc->sc_ah->ah_channels[i].channel == chan->center_freq)
  40. return i;
  41. }
  42. return -1;
  43. }
  44. static u32 ath_get_extchanmode(struct ath_softc *sc,
  45. struct ieee80211_channel *chan)
  46. {
  47. u32 chanmode = 0;
  48. u8 ext_chan_offset = sc->sc_ht_info.ext_chan_offset;
  49. enum ath9k_ht_macmode tx_chan_width = sc->sc_ht_info.tx_chan_width;
  50. switch (chan->band) {
  51. case IEEE80211_BAND_2GHZ:
  52. if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_NONE) &&
  53. (tx_chan_width == ATH9K_HT_MACMODE_20))
  54. chanmode = CHANNEL_G_HT20;
  55. if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE) &&
  56. (tx_chan_width == ATH9K_HT_MACMODE_2040))
  57. chanmode = CHANNEL_G_HT40PLUS;
  58. if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW) &&
  59. (tx_chan_width == ATH9K_HT_MACMODE_2040))
  60. chanmode = CHANNEL_G_HT40MINUS;
  61. break;
  62. case IEEE80211_BAND_5GHZ:
  63. if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_NONE) &&
  64. (tx_chan_width == ATH9K_HT_MACMODE_20))
  65. chanmode = CHANNEL_A_HT20;
  66. if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE) &&
  67. (tx_chan_width == ATH9K_HT_MACMODE_2040))
  68. chanmode = CHANNEL_A_HT40PLUS;
  69. if ((ext_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW) &&
  70. (tx_chan_width == ATH9K_HT_MACMODE_2040))
  71. chanmode = CHANNEL_A_HT40MINUS;
  72. break;
  73. default:
  74. break;
  75. }
  76. return chanmode;
  77. }
  78. static int ath_setkey_tkip(struct ath_softc *sc,
  79. struct ieee80211_key_conf *key,
  80. struct ath9k_keyval *hk,
  81. const u8 *addr)
  82. {
  83. u8 *key_rxmic = NULL;
  84. u8 *key_txmic = NULL;
  85. key_txmic = key->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  86. key_rxmic = key->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  87. if (addr == NULL) {
  88. /* Group key installation */
  89. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  90. return ath_keyset(sc, key->keyidx, hk, addr);
  91. }
  92. if (!sc->sc_splitmic) {
  93. /*
  94. * data key goes at first index,
  95. * the hal handles the MIC keys at index+64.
  96. */
  97. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  98. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  99. return ath_keyset(sc, key->keyidx, hk, addr);
  100. }
  101. /*
  102. * TX key goes at first index, RX key at +32.
  103. * The hal handles the MIC keys at index+64.
  104. */
  105. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  106. if (!ath_keyset(sc, key->keyidx, hk, NULL)) {
  107. /* Txmic entry failed. No need to proceed further */
  108. DPRINTF(sc, ATH_DBG_KEYCACHE,
  109. "%s Setting TX MIC Key Failed\n", __func__);
  110. return 0;
  111. }
  112. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  113. /* XXX delete tx key on failure? */
  114. return ath_keyset(sc, key->keyidx+32, hk, addr);
  115. }
  116. static int ath_key_config(struct ath_softc *sc,
  117. const u8 *addr,
  118. struct ieee80211_key_conf *key)
  119. {
  120. struct ieee80211_vif *vif;
  121. struct ath9k_keyval hk;
  122. const u8 *mac = NULL;
  123. int ret = 0;
  124. enum nl80211_iftype opmode;
  125. memset(&hk, 0, sizeof(hk));
  126. switch (key->alg) {
  127. case ALG_WEP:
  128. hk.kv_type = ATH9K_CIPHER_WEP;
  129. break;
  130. case ALG_TKIP:
  131. hk.kv_type = ATH9K_CIPHER_TKIP;
  132. break;
  133. case ALG_CCMP:
  134. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. hk.kv_len = key->keylen;
  140. memcpy(hk.kv_val, key->key, key->keylen);
  141. if (!sc->sc_vaps[0])
  142. return -EIO;
  143. vif = sc->sc_vaps[0]->av_if_data;
  144. opmode = vif->type;
  145. /*
  146. * Strategy:
  147. * For _M_STA mc tx, we will not setup a key at all since we never
  148. * tx mc.
  149. * _M_STA mc rx, we will use the keyID.
  150. * for _M_IBSS mc tx, we will use the keyID, and no macaddr.
  151. * for _M_IBSS mc rx, we will alloc a slot and plumb the mac of the
  152. * peer node. BUT we will plumb a cleartext key so that we can do
  153. * perSta default key table lookup in software.
  154. */
  155. if (is_broadcast_ether_addr(addr)) {
  156. switch (opmode) {
  157. case NL80211_IFTYPE_STATION:
  158. /* default key: could be group WPA key
  159. * or could be static WEP key */
  160. mac = NULL;
  161. break;
  162. case NL80211_IFTYPE_ADHOC:
  163. break;
  164. case NL80211_IFTYPE_AP:
  165. break;
  166. default:
  167. ASSERT(0);
  168. break;
  169. }
  170. } else {
  171. mac = addr;
  172. }
  173. if (key->alg == ALG_TKIP)
  174. ret = ath_setkey_tkip(sc, key, &hk, mac);
  175. else
  176. ret = ath_keyset(sc, key->keyidx, &hk, mac);
  177. if (!ret)
  178. return -EIO;
  179. return 0;
  180. }
  181. static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
  182. {
  183. int freeslot;
  184. freeslot = (key->keyidx >= 4) ? 1 : 0;
  185. ath_key_reset(sc, key->keyidx, freeslot);
  186. }
  187. static void setup_ht_cap(struct ieee80211_sta_ht_cap *ht_info)
  188. {
  189. #define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
  190. #define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
  191. ht_info->ht_supported = true;
  192. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  193. IEEE80211_HT_CAP_SM_PS |
  194. IEEE80211_HT_CAP_SGI_40 |
  195. IEEE80211_HT_CAP_DSSSCCK40;
  196. ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
  197. ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
  198. /* set up supported mcs set */
  199. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  200. ht_info->mcs.rx_mask[0] = 0xff;
  201. ht_info->mcs.rx_mask[1] = 0xff;
  202. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  203. }
  204. static int ath_rate2idx(struct ath_softc *sc, int rate)
  205. {
  206. int i = 0, cur_band, n_rates;
  207. struct ieee80211_hw *hw = sc->hw;
  208. cur_band = hw->conf.channel->band;
  209. n_rates = sc->sbands[cur_band].n_bitrates;
  210. for (i = 0; i < n_rates; i++) {
  211. if (sc->sbands[cur_band].bitrates[i].bitrate == rate)
  212. break;
  213. }
  214. /*
  215. * NB:mac80211 validates rx rate index against the supported legacy rate
  216. * index only (should be done against ht rates also), return the highest
  217. * legacy rate index for rx rate which does not match any one of the
  218. * supported basic and extended rates to make mac80211 happy.
  219. * The following hack will be cleaned up once the issue with
  220. * the rx rate index validation in mac80211 is fixed.
  221. */
  222. if (i == n_rates)
  223. return n_rates - 1;
  224. return i;
  225. }
  226. static void ath9k_rx_prepare(struct ath_softc *sc,
  227. struct sk_buff *skb,
  228. struct ath_recv_status *status,
  229. struct ieee80211_rx_status *rx_status)
  230. {
  231. struct ieee80211_hw *hw = sc->hw;
  232. struct ieee80211_channel *curchan = hw->conf.channel;
  233. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  234. rx_status->mactime = status->tsf;
  235. rx_status->band = curchan->band;
  236. rx_status->freq = curchan->center_freq;
  237. rx_status->noise = sc->sc_ani.sc_noise_floor;
  238. rx_status->signal = rx_status->noise + status->rssi;
  239. rx_status->rate_idx = ath_rate2idx(sc, (status->rateKbps / 100));
  240. rx_status->antenna = status->antenna;
  241. /* at 45 you will be able to use MCS 15 reliably. A more elaborate
  242. * scheme can be used here but it requires tables of SNR/throughput for
  243. * each possible mode used. */
  244. rx_status->qual = status->rssi * 100 / 45;
  245. /* rssi can be more than 45 though, anything above that
  246. * should be considered at 100% */
  247. if (rx_status->qual > 100)
  248. rx_status->qual = 100;
  249. if (status->flags & ATH_RX_MIC_ERROR)
  250. rx_status->flag |= RX_FLAG_MMIC_ERROR;
  251. if (status->flags & ATH_RX_FCS_ERROR)
  252. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  253. rx_status->flag |= RX_FLAG_TSFT;
  254. }
  255. static u8 parse_mpdudensity(u8 mpdudensity)
  256. {
  257. /*
  258. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  259. * 0 for no restriction
  260. * 1 for 1/4 us
  261. * 2 for 1/2 us
  262. * 3 for 1 us
  263. * 4 for 2 us
  264. * 5 for 4 us
  265. * 6 for 8 us
  266. * 7 for 16 us
  267. */
  268. switch (mpdudensity) {
  269. case 0:
  270. return 0;
  271. case 1:
  272. case 2:
  273. case 3:
  274. /* Our lower layer calculations limit our precision to
  275. 1 microsecond */
  276. return 1;
  277. case 4:
  278. return 2;
  279. case 5:
  280. return 4;
  281. case 6:
  282. return 8;
  283. case 7:
  284. return 16;
  285. default:
  286. return 0;
  287. }
  288. }
  289. static void ath9k_ht_conf(struct ath_softc *sc,
  290. struct ieee80211_bss_conf *bss_conf)
  291. {
  292. struct ath_ht_info *ht_info = &sc->sc_ht_info;
  293. if (sc->hw->conf.ht.enabled) {
  294. ht_info->ext_chan_offset = bss_conf->ht.secondary_channel_offset;
  295. if (bss_conf->ht.width_40_ok)
  296. ht_info->tx_chan_width = ATH9K_HT_MACMODE_2040;
  297. else
  298. ht_info->tx_chan_width = ATH9K_HT_MACMODE_20;
  299. ath9k_hw_set11nmac2040(sc->sc_ah, ht_info->tx_chan_width);
  300. }
  301. }
  302. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  303. struct ieee80211_bss_conf *bss_conf)
  304. {
  305. struct ieee80211_hw *hw = sc->hw;
  306. struct ieee80211_channel *curchan = hw->conf.channel;
  307. struct ath_vap *avp;
  308. int pos;
  309. if (bss_conf->assoc) {
  310. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Bss Info ASSOC %d\n",
  311. __func__,
  312. bss_conf->aid);
  313. avp = sc->sc_vaps[0];
  314. if (avp == NULL) {
  315. DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
  316. __func__);
  317. return;
  318. }
  319. /* New association, store aid */
  320. if (avp->av_opmode == ATH9K_M_STA) {
  321. sc->sc_curaid = bss_conf->aid;
  322. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  323. sc->sc_curaid);
  324. }
  325. /* Configure the beacon */
  326. ath_beacon_config(sc, 0);
  327. sc->sc_flags |= SC_OP_BEACONS;
  328. /* Reset rssi stats */
  329. sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
  330. sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
  331. sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
  332. sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
  333. /* Update chainmask */
  334. ath_update_chainmask(sc, hw->conf.ht.enabled);
  335. DPRINTF(sc, ATH_DBG_CONFIG,
  336. "%s: bssid %pM aid 0x%x\n",
  337. __func__,
  338. sc->sc_curbssid, sc->sc_curaid);
  339. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
  340. __func__,
  341. curchan->center_freq);
  342. pos = ath_get_channel(sc, curchan);
  343. if (pos == -1) {
  344. DPRINTF(sc, ATH_DBG_FATAL,
  345. "%s: Invalid channel\n", __func__);
  346. return;
  347. }
  348. if (hw->conf.ht.enabled)
  349. sc->sc_ah->ah_channels[pos].chanmode =
  350. ath_get_extchanmode(sc, curchan);
  351. else
  352. sc->sc_ah->ah_channels[pos].chanmode =
  353. (curchan->band == IEEE80211_BAND_2GHZ) ?
  354. CHANNEL_G : CHANNEL_A;
  355. /* set h/w channel */
  356. if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
  357. DPRINTF(sc, ATH_DBG_FATAL,
  358. "%s: Unable to set channel\n",
  359. __func__);
  360. ath_rate_newstate(sc, avp);
  361. /* Update ratectrl about the new state */
  362. ath_rc_node_update(hw, avp->rc_node);
  363. /* Start ANI */
  364. mod_timer(&sc->sc_ani.timer,
  365. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  366. } else {
  367. DPRINTF(sc, ATH_DBG_CONFIG,
  368. "%s: Bss Info DISSOC\n", __func__);
  369. sc->sc_curaid = 0;
  370. }
  371. }
  372. void ath_get_beaconconfig(struct ath_softc *sc,
  373. int if_id,
  374. struct ath_beacon_config *conf)
  375. {
  376. struct ieee80211_hw *hw = sc->hw;
  377. /* fill in beacon config data */
  378. conf->beacon_interval = hw->conf.beacon_int;
  379. conf->listen_interval = 100;
  380. conf->dtim_count = 1;
  381. conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
  382. }
  383. void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
  384. struct ath_xmit_status *tx_status, struct ath_node *an)
  385. {
  386. struct ieee80211_hw *hw = sc->hw;
  387. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  388. DPRINTF(sc, ATH_DBG_XMIT,
  389. "%s: TX complete: skb: %p\n", __func__, skb);
  390. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
  391. tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  392. /* free driver's private data area of tx_info */
  393. if (tx_info->driver_data[0] != NULL)
  394. kfree(tx_info->driver_data[0]);
  395. tx_info->driver_data[0] = NULL;
  396. }
  397. if (tx_status->flags & ATH_TX_BAR) {
  398. tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  399. tx_status->flags &= ~ATH_TX_BAR;
  400. }
  401. if (tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY)) {
  402. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  403. /* Frame was not ACKed, but an ACK was expected */
  404. tx_info->status.excessive_retries = 1;
  405. }
  406. } else {
  407. /* Frame was ACKed */
  408. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  409. }
  410. tx_info->status.retry_count = tx_status->retries;
  411. ieee80211_tx_status(hw, skb);
  412. if (an)
  413. ath_node_put(sc, an, ATH9K_BH_STATUS_CHANGE);
  414. }
  415. int _ath_rx_indicate(struct ath_softc *sc,
  416. struct sk_buff *skb,
  417. struct ath_recv_status *status,
  418. u16 keyix)
  419. {
  420. struct ieee80211_hw *hw = sc->hw;
  421. struct ath_node *an = NULL;
  422. struct ieee80211_rx_status rx_status;
  423. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  424. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  425. int padsize;
  426. enum ATH_RX_TYPE st;
  427. /* see if any padding is done by the hw and remove it */
  428. if (hdrlen & 3) {
  429. padsize = hdrlen % 4;
  430. memmove(skb->data + padsize, skb->data, hdrlen);
  431. skb_pull(skb, padsize);
  432. }
  433. /* Prepare rx status */
  434. ath9k_rx_prepare(sc, skb, status, &rx_status);
  435. if (!(keyix == ATH9K_RXKEYIX_INVALID) &&
  436. !(status->flags & ATH_RX_DECRYPT_ERROR)) {
  437. rx_status.flag |= RX_FLAG_DECRYPTED;
  438. } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
  439. && !(status->flags & ATH_RX_DECRYPT_ERROR)
  440. && skb->len >= hdrlen + 4) {
  441. keyix = skb->data[hdrlen + 3] >> 6;
  442. if (test_bit(keyix, sc->sc_keymap))
  443. rx_status.flag |= RX_FLAG_DECRYPTED;
  444. }
  445. spin_lock_bh(&sc->node_lock);
  446. an = ath_node_find(sc, hdr->addr2);
  447. spin_unlock_bh(&sc->node_lock);
  448. if (an) {
  449. ath_rx_input(sc, an,
  450. skb, status, &st);
  451. }
  452. if (!an || (st != ATH_RX_CONSUMED))
  453. __ieee80211_rx(hw, skb, &rx_status);
  454. return 0;
  455. }
  456. int ath_rx_subframe(struct ath_node *an,
  457. struct sk_buff *skb,
  458. struct ath_recv_status *status)
  459. {
  460. struct ath_softc *sc = an->an_sc;
  461. struct ieee80211_hw *hw = sc->hw;
  462. struct ieee80211_rx_status rx_status;
  463. /* Prepare rx status */
  464. ath9k_rx_prepare(sc, skb, status, &rx_status);
  465. if (!(status->flags & ATH_RX_DECRYPT_ERROR))
  466. rx_status.flag |= RX_FLAG_DECRYPTED;
  467. __ieee80211_rx(hw, skb, &rx_status);
  468. return 0;
  469. }
  470. /********************************/
  471. /* LED functions */
  472. /********************************/
  473. static void ath_led_brightness(struct led_classdev *led_cdev,
  474. enum led_brightness brightness)
  475. {
  476. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  477. struct ath_softc *sc = led->sc;
  478. switch (brightness) {
  479. case LED_OFF:
  480. if (led->led_type == ATH_LED_ASSOC ||
  481. led->led_type == ATH_LED_RADIO)
  482. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  483. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
  484. (led->led_type == ATH_LED_RADIO) ? 1 :
  485. !!(sc->sc_flags & SC_OP_LED_ASSOCIATED));
  486. break;
  487. case LED_FULL:
  488. if (led->led_type == ATH_LED_ASSOC)
  489. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  490. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
  491. break;
  492. default:
  493. break;
  494. }
  495. }
  496. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  497. char *trigger)
  498. {
  499. int ret;
  500. led->sc = sc;
  501. led->led_cdev.name = led->name;
  502. led->led_cdev.default_trigger = trigger;
  503. led->led_cdev.brightness_set = ath_led_brightness;
  504. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  505. if (ret)
  506. DPRINTF(sc, ATH_DBG_FATAL,
  507. "Failed to register led:%s", led->name);
  508. else
  509. led->registered = 1;
  510. return ret;
  511. }
  512. static void ath_unregister_led(struct ath_led *led)
  513. {
  514. if (led->registered) {
  515. led_classdev_unregister(&led->led_cdev);
  516. led->registered = 0;
  517. }
  518. }
  519. static void ath_deinit_leds(struct ath_softc *sc)
  520. {
  521. ath_unregister_led(&sc->assoc_led);
  522. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  523. ath_unregister_led(&sc->tx_led);
  524. ath_unregister_led(&sc->rx_led);
  525. ath_unregister_led(&sc->radio_led);
  526. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  527. }
  528. static void ath_init_leds(struct ath_softc *sc)
  529. {
  530. char *trigger;
  531. int ret;
  532. /* Configure gpio 1 for output */
  533. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  534. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  535. /* LED off, active low */
  536. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  537. trigger = ieee80211_get_radio_led_name(sc->hw);
  538. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  539. "ath9k-%s:radio", wiphy_name(sc->hw->wiphy));
  540. ret = ath_register_led(sc, &sc->radio_led, trigger);
  541. sc->radio_led.led_type = ATH_LED_RADIO;
  542. if (ret)
  543. goto fail;
  544. trigger = ieee80211_get_assoc_led_name(sc->hw);
  545. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  546. "ath9k-%s:assoc", wiphy_name(sc->hw->wiphy));
  547. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  548. sc->assoc_led.led_type = ATH_LED_ASSOC;
  549. if (ret)
  550. goto fail;
  551. trigger = ieee80211_get_tx_led_name(sc->hw);
  552. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  553. "ath9k-%s:tx", wiphy_name(sc->hw->wiphy));
  554. ret = ath_register_led(sc, &sc->tx_led, trigger);
  555. sc->tx_led.led_type = ATH_LED_TX;
  556. if (ret)
  557. goto fail;
  558. trigger = ieee80211_get_rx_led_name(sc->hw);
  559. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  560. "ath9k-%s:rx", wiphy_name(sc->hw->wiphy));
  561. ret = ath_register_led(sc, &sc->rx_led, trigger);
  562. sc->rx_led.led_type = ATH_LED_RX;
  563. if (ret)
  564. goto fail;
  565. return;
  566. fail:
  567. ath_deinit_leds(sc);
  568. }
  569. #ifdef CONFIG_RFKILL
  570. /*******************/
  571. /* Rfkill */
  572. /*******************/
  573. static void ath_radio_enable(struct ath_softc *sc)
  574. {
  575. struct ath_hal *ah = sc->sc_ah;
  576. int status;
  577. spin_lock_bh(&sc->sc_resetlock);
  578. if (!ath9k_hw_reset(ah, ah->ah_curchan,
  579. sc->sc_ht_info.tx_chan_width,
  580. sc->sc_tx_chainmask,
  581. sc->sc_rx_chainmask,
  582. sc->sc_ht_extprotspacing,
  583. false, &status)) {
  584. DPRINTF(sc, ATH_DBG_FATAL,
  585. "%s: unable to reset channel %u (%uMhz) "
  586. "flags 0x%x hal status %u\n", __func__,
  587. ath9k_hw_mhz2ieee(ah,
  588. ah->ah_curchan->channel,
  589. ah->ah_curchan->channelFlags),
  590. ah->ah_curchan->channel,
  591. ah->ah_curchan->channelFlags, status);
  592. }
  593. spin_unlock_bh(&sc->sc_resetlock);
  594. ath_update_txpow(sc);
  595. if (ath_startrecv(sc) != 0) {
  596. DPRINTF(sc, ATH_DBG_FATAL,
  597. "%s: unable to restart recv logic\n", __func__);
  598. return;
  599. }
  600. if (sc->sc_flags & SC_OP_BEACONS)
  601. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  602. /* Re-Enable interrupts */
  603. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  604. /* Enable LED */
  605. ath9k_hw_cfg_output(ah, ATH_LED_PIN,
  606. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  607. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
  608. ieee80211_wake_queues(sc->hw);
  609. }
  610. static void ath_radio_disable(struct ath_softc *sc)
  611. {
  612. struct ath_hal *ah = sc->sc_ah;
  613. int status;
  614. ieee80211_stop_queues(sc->hw);
  615. /* Disable LED */
  616. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
  617. ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
  618. /* Disable interrupts */
  619. ath9k_hw_set_interrupts(ah, 0);
  620. ath_draintxq(sc, false); /* clear pending tx frames */
  621. ath_stoprecv(sc); /* turn off frame recv */
  622. ath_flushrecv(sc); /* flush recv queue */
  623. spin_lock_bh(&sc->sc_resetlock);
  624. if (!ath9k_hw_reset(ah, ah->ah_curchan,
  625. sc->sc_ht_info.tx_chan_width,
  626. sc->sc_tx_chainmask,
  627. sc->sc_rx_chainmask,
  628. sc->sc_ht_extprotspacing,
  629. false, &status)) {
  630. DPRINTF(sc, ATH_DBG_FATAL,
  631. "%s: unable to reset channel %u (%uMhz) "
  632. "flags 0x%x hal status %u\n", __func__,
  633. ath9k_hw_mhz2ieee(ah,
  634. ah->ah_curchan->channel,
  635. ah->ah_curchan->channelFlags),
  636. ah->ah_curchan->channel,
  637. ah->ah_curchan->channelFlags, status);
  638. }
  639. spin_unlock_bh(&sc->sc_resetlock);
  640. ath9k_hw_phy_disable(ah);
  641. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  642. }
  643. static bool ath_is_rfkill_set(struct ath_softc *sc)
  644. {
  645. struct ath_hal *ah = sc->sc_ah;
  646. return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
  647. ah->ah_rfkill_polarity;
  648. }
  649. /* h/w rfkill poll function */
  650. static void ath_rfkill_poll(struct work_struct *work)
  651. {
  652. struct ath_softc *sc = container_of(work, struct ath_softc,
  653. rf_kill.rfkill_poll.work);
  654. bool radio_on;
  655. if (sc->sc_flags & SC_OP_INVALID)
  656. return;
  657. radio_on = !ath_is_rfkill_set(sc);
  658. /*
  659. * enable/disable radio only when there is a
  660. * state change in RF switch
  661. */
  662. if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
  663. enum rfkill_state state;
  664. if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
  665. state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
  666. : RFKILL_STATE_HARD_BLOCKED;
  667. } else if (radio_on) {
  668. ath_radio_enable(sc);
  669. state = RFKILL_STATE_UNBLOCKED;
  670. } else {
  671. ath_radio_disable(sc);
  672. state = RFKILL_STATE_HARD_BLOCKED;
  673. }
  674. if (state == RFKILL_STATE_HARD_BLOCKED)
  675. sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
  676. else
  677. sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
  678. rfkill_force_state(sc->rf_kill.rfkill, state);
  679. }
  680. queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
  681. msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
  682. }
  683. /* s/w rfkill handler */
  684. static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
  685. {
  686. struct ath_softc *sc = data;
  687. switch (state) {
  688. case RFKILL_STATE_SOFT_BLOCKED:
  689. if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
  690. SC_OP_RFKILL_SW_BLOCKED)))
  691. ath_radio_disable(sc);
  692. sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
  693. return 0;
  694. case RFKILL_STATE_UNBLOCKED:
  695. if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
  696. sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
  697. if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
  698. DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
  699. "radio as it is disabled by h/w \n");
  700. return -EPERM;
  701. }
  702. ath_radio_enable(sc);
  703. }
  704. return 0;
  705. default:
  706. return -EINVAL;
  707. }
  708. }
  709. /* Init s/w rfkill */
  710. static int ath_init_sw_rfkill(struct ath_softc *sc)
  711. {
  712. sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
  713. RFKILL_TYPE_WLAN);
  714. if (!sc->rf_kill.rfkill) {
  715. DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
  716. return -ENOMEM;
  717. }
  718. snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
  719. "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
  720. sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
  721. sc->rf_kill.rfkill->data = sc;
  722. sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
  723. sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
  724. sc->rf_kill.rfkill->user_claim_unsupported = 1;
  725. return 0;
  726. }
  727. /* Deinitialize rfkill */
  728. static void ath_deinit_rfkill(struct ath_softc *sc)
  729. {
  730. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  731. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  732. if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
  733. rfkill_unregister(sc->rf_kill.rfkill);
  734. sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
  735. sc->rf_kill.rfkill = NULL;
  736. }
  737. }
  738. #endif /* CONFIG_RFKILL */
  739. static int ath_detach(struct ath_softc *sc)
  740. {
  741. struct ieee80211_hw *hw = sc->hw;
  742. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach ATH hw\n", __func__);
  743. /* Deinit LED control */
  744. ath_deinit_leds(sc);
  745. #ifdef CONFIG_RFKILL
  746. /* deinit rfkill */
  747. ath_deinit_rfkill(sc);
  748. #endif
  749. /* Unregister hw */
  750. ieee80211_unregister_hw(hw);
  751. /* unregister Rate control */
  752. ath_rate_control_unregister();
  753. /* tx/rx cleanup */
  754. ath_rx_cleanup(sc);
  755. ath_tx_cleanup(sc);
  756. /* Deinit */
  757. ath_deinit(sc);
  758. return 0;
  759. }
  760. static int ath_attach(u16 devid,
  761. struct ath_softc *sc)
  762. {
  763. struct ieee80211_hw *hw = sc->hw;
  764. int error = 0;
  765. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach ATH hw\n", __func__);
  766. error = ath_init(devid, sc);
  767. if (error != 0)
  768. return error;
  769. /* Init nodes */
  770. INIT_LIST_HEAD(&sc->node_list);
  771. spin_lock_init(&sc->node_lock);
  772. /* get mac address from hardware and set in mac80211 */
  773. SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
  774. /* setup channels and rates */
  775. sc->sbands[IEEE80211_BAND_2GHZ].channels =
  776. sc->channels[IEEE80211_BAND_2GHZ];
  777. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  778. sc->rates[IEEE80211_BAND_2GHZ];
  779. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  780. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  781. /* Setup HT capabilities for 2.4Ghz*/
  782. setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  783. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  784. &sc->sbands[IEEE80211_BAND_2GHZ];
  785. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
  786. sc->sbands[IEEE80211_BAND_5GHZ].channels =
  787. sc->channels[IEEE80211_BAND_5GHZ];
  788. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  789. sc->rates[IEEE80211_BAND_5GHZ];
  790. sc->sbands[IEEE80211_BAND_5GHZ].band =
  791. IEEE80211_BAND_5GHZ;
  792. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  793. /* Setup HT capabilities for 5Ghz*/
  794. setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  795. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  796. &sc->sbands[IEEE80211_BAND_5GHZ];
  797. }
  798. /* FIXME: Have to figure out proper hw init values later */
  799. hw->queues = 4;
  800. hw->ampdu_queues = 1;
  801. /* Register rate control */
  802. hw->rate_control_algorithm = "ath9k_rate_control";
  803. error = ath_rate_control_register();
  804. if (error != 0) {
  805. DPRINTF(sc, ATH_DBG_FATAL,
  806. "%s: Unable to register rate control "
  807. "algorithm:%d\n", __func__, error);
  808. ath_rate_control_unregister();
  809. goto bad;
  810. }
  811. error = ieee80211_register_hw(hw);
  812. if (error != 0) {
  813. ath_rate_control_unregister();
  814. goto bad;
  815. }
  816. /* Initialize LED control */
  817. ath_init_leds(sc);
  818. #ifdef CONFIG_RFKILL
  819. /* Initialze h/w Rfkill */
  820. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  821. INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
  822. /* Initialize s/w rfkill */
  823. if (ath_init_sw_rfkill(sc))
  824. goto detach;
  825. #endif
  826. /* initialize tx/rx engine */
  827. error = ath_tx_init(sc, ATH_TXBUF);
  828. if (error != 0)
  829. goto detach;
  830. error = ath_rx_init(sc, ATH_RXBUF);
  831. if (error != 0)
  832. goto detach;
  833. return 0;
  834. detach:
  835. ath_detach(sc);
  836. bad:
  837. return error;
  838. }
  839. static int ath9k_start(struct ieee80211_hw *hw)
  840. {
  841. struct ath_softc *sc = hw->priv;
  842. struct ieee80211_channel *curchan = hw->conf.channel;
  843. int error = 0, pos;
  844. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Starting driver with "
  845. "initial channel: %d MHz\n", __func__, curchan->center_freq);
  846. /* setup initial channel */
  847. pos = ath_get_channel(sc, curchan);
  848. if (pos == -1) {
  849. DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid channel\n", __func__);
  850. return -EINVAL;
  851. }
  852. sc->sc_ah->ah_channels[pos].chanmode =
  853. (curchan->band == IEEE80211_BAND_2GHZ) ? CHANNEL_G : CHANNEL_A;
  854. /* open ath_dev */
  855. error = ath_open(sc, &sc->sc_ah->ah_channels[pos]);
  856. if (error) {
  857. DPRINTF(sc, ATH_DBG_FATAL,
  858. "%s: Unable to complete ath_open\n", __func__);
  859. return error;
  860. }
  861. #ifdef CONFIG_RFKILL
  862. /* Start rfkill polling */
  863. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  864. queue_delayed_work(sc->hw->workqueue,
  865. &sc->rf_kill.rfkill_poll, 0);
  866. if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
  867. if (rfkill_register(sc->rf_kill.rfkill)) {
  868. DPRINTF(sc, ATH_DBG_FATAL,
  869. "Unable to register rfkill\n");
  870. rfkill_free(sc->rf_kill.rfkill);
  871. /* Deinitialize the device */
  872. if (sc->pdev->irq)
  873. free_irq(sc->pdev->irq, sc);
  874. ath_detach(sc);
  875. pci_iounmap(sc->pdev, sc->mem);
  876. pci_release_region(sc->pdev, 0);
  877. pci_disable_device(sc->pdev);
  878. ieee80211_free_hw(hw);
  879. return -EIO;
  880. } else {
  881. sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
  882. }
  883. }
  884. #endif
  885. ieee80211_wake_queues(hw);
  886. return 0;
  887. }
  888. static int ath9k_tx(struct ieee80211_hw *hw,
  889. struct sk_buff *skb)
  890. {
  891. struct ath_softc *sc = hw->priv;
  892. int hdrlen, padsize;
  893. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  894. /*
  895. * As a temporary workaround, assign seq# here; this will likely need
  896. * to be cleaned up to work better with Beacon transmission and virtual
  897. * BSSes.
  898. */
  899. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  900. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  901. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  902. sc->seq_no += 0x10;
  903. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  904. hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
  905. }
  906. /* Add the padding after the header if this is not already done */
  907. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  908. if (hdrlen & 3) {
  909. padsize = hdrlen % 4;
  910. if (skb_headroom(skb) < padsize)
  911. return -1;
  912. skb_push(skb, padsize);
  913. memmove(skb->data, skb->data + padsize, hdrlen);
  914. }
  915. DPRINTF(sc, ATH_DBG_XMIT, "%s: transmitting packet, skb: %p\n",
  916. __func__,
  917. skb);
  918. if (ath_tx_start(sc, skb) != 0) {
  919. DPRINTF(sc, ATH_DBG_XMIT, "%s: TX failed\n", __func__);
  920. dev_kfree_skb_any(skb);
  921. /* FIXME: Check for proper return value from ATH_DEV */
  922. return 0;
  923. }
  924. return 0;
  925. }
  926. static void ath9k_stop(struct ieee80211_hw *hw)
  927. {
  928. struct ath_softc *sc = hw->priv;
  929. int error;
  930. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Driver halt\n", __func__);
  931. error = ath_suspend(sc);
  932. if (error)
  933. DPRINTF(sc, ATH_DBG_CONFIG,
  934. "%s: Device is no longer present\n", __func__);
  935. ieee80211_stop_queues(hw);
  936. #ifdef CONFIG_RFKILL
  937. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  938. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  939. #endif
  940. }
  941. static int ath9k_add_interface(struct ieee80211_hw *hw,
  942. struct ieee80211_if_init_conf *conf)
  943. {
  944. struct ath_softc *sc = hw->priv;
  945. int error, ic_opmode = 0;
  946. /* Support only vap for now */
  947. if (sc->sc_nvaps)
  948. return -ENOBUFS;
  949. switch (conf->type) {
  950. case NL80211_IFTYPE_STATION:
  951. ic_opmode = ATH9K_M_STA;
  952. break;
  953. case NL80211_IFTYPE_ADHOC:
  954. ic_opmode = ATH9K_M_IBSS;
  955. break;
  956. case NL80211_IFTYPE_AP:
  957. ic_opmode = ATH9K_M_HOSTAP;
  958. break;
  959. default:
  960. DPRINTF(sc, ATH_DBG_FATAL,
  961. "%s: Interface type %d not yet supported\n",
  962. __func__, conf->type);
  963. return -EOPNOTSUPP;
  964. }
  965. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach a VAP of type: %d\n",
  966. __func__,
  967. ic_opmode);
  968. error = ath_vap_attach(sc, 0, conf->vif, ic_opmode);
  969. if (error) {
  970. DPRINTF(sc, ATH_DBG_FATAL,
  971. "%s: Unable to attach vap, error: %d\n",
  972. __func__, error);
  973. return error;
  974. }
  975. if (conf->type == NL80211_IFTYPE_AP) {
  976. /* TODO: is this a suitable place to start ANI for AP mode? */
  977. /* Start ANI */
  978. mod_timer(&sc->sc_ani.timer,
  979. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  980. }
  981. return 0;
  982. }
  983. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  984. struct ieee80211_if_init_conf *conf)
  985. {
  986. struct ath_softc *sc = hw->priv;
  987. struct ath_vap *avp;
  988. int error;
  989. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach VAP\n", __func__);
  990. avp = sc->sc_vaps[0];
  991. if (avp == NULL) {
  992. DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
  993. __func__);
  994. return;
  995. }
  996. #ifdef CONFIG_SLOW_ANT_DIV
  997. ath_slow_ant_div_stop(&sc->sc_antdiv);
  998. #endif
  999. /* Stop ANI */
  1000. del_timer_sync(&sc->sc_ani.timer);
  1001. /* Update ratectrl */
  1002. ath_rate_newstate(sc, avp);
  1003. /* Reclaim beacon resources */
  1004. if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
  1005. sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
  1006. ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
  1007. ath_beacon_return(sc, avp);
  1008. }
  1009. /* Set interrupt mask */
  1010. sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1011. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask & ~ATH9K_INT_GLOBAL);
  1012. sc->sc_flags &= ~SC_OP_BEACONS;
  1013. error = ath_vap_detach(sc, 0);
  1014. if (error)
  1015. DPRINTF(sc, ATH_DBG_FATAL,
  1016. "%s: Unable to detach vap, error: %d\n",
  1017. __func__, error);
  1018. }
  1019. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1020. {
  1021. struct ath_softc *sc = hw->priv;
  1022. struct ieee80211_channel *curchan = hw->conf.channel;
  1023. struct ieee80211_conf *conf = &hw->conf;
  1024. int pos;
  1025. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
  1026. __func__,
  1027. curchan->center_freq);
  1028. /* Update chainmask */
  1029. ath_update_chainmask(sc, conf->ht.enabled);
  1030. pos = ath_get_channel(sc, curchan);
  1031. if (pos == -1) {
  1032. DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid channel\n", __func__);
  1033. return -EINVAL;
  1034. }
  1035. sc->sc_ah->ah_channels[pos].chanmode =
  1036. (curchan->band == IEEE80211_BAND_2GHZ) ?
  1037. CHANNEL_G : CHANNEL_A;
  1038. if (sc->sc_curaid && hw->conf.ht.enabled)
  1039. sc->sc_ah->ah_channels[pos].chanmode =
  1040. ath_get_extchanmode(sc, curchan);
  1041. sc->sc_config.txpowlimit = 2 * conf->power_level;
  1042. /* set h/w channel */
  1043. if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
  1044. DPRINTF(sc, ATH_DBG_FATAL, "%s: Unable to set channel\n",
  1045. __func__);
  1046. return 0;
  1047. }
  1048. static int ath9k_config_interface(struct ieee80211_hw *hw,
  1049. struct ieee80211_vif *vif,
  1050. struct ieee80211_if_conf *conf)
  1051. {
  1052. struct ath_softc *sc = hw->priv;
  1053. struct ath_hal *ah = sc->sc_ah;
  1054. struct ath_vap *avp;
  1055. u32 rfilt = 0;
  1056. int error, i;
  1057. avp = sc->sc_vaps[0];
  1058. if (avp == NULL) {
  1059. DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
  1060. __func__);
  1061. return -EINVAL;
  1062. }
  1063. /* TODO: Need to decide which hw opmode to use for multi-interface
  1064. * cases */
  1065. if (vif->type == NL80211_IFTYPE_AP &&
  1066. ah->ah_opmode != ATH9K_M_HOSTAP) {
  1067. ah->ah_opmode = ATH9K_M_HOSTAP;
  1068. ath9k_hw_setopmode(ah);
  1069. ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
  1070. /* Request full reset to get hw opmode changed properly */
  1071. sc->sc_flags |= SC_OP_FULL_RESET;
  1072. }
  1073. if ((conf->changed & IEEE80211_IFCC_BSSID) &&
  1074. !is_zero_ether_addr(conf->bssid)) {
  1075. switch (vif->type) {
  1076. case NL80211_IFTYPE_STATION:
  1077. case NL80211_IFTYPE_ADHOC:
  1078. /* Update ratectrl about the new state */
  1079. ath_rate_newstate(sc, avp);
  1080. /* Set BSSID */
  1081. memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
  1082. sc->sc_curaid = 0;
  1083. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  1084. sc->sc_curaid);
  1085. /* Set aggregation protection mode parameters */
  1086. sc->sc_config.ath_aggr_prot = 0;
  1087. /*
  1088. * Reset our TSF so that its value is lower than the
  1089. * beacon that we are trying to catch.
  1090. * Only then hw will update its TSF register with the
  1091. * new beacon. Reset the TSF before setting the BSSID
  1092. * to avoid allowing in any frames that would update
  1093. * our TSF only to have us clear it
  1094. * immediately thereafter.
  1095. */
  1096. ath9k_hw_reset_tsf(sc->sc_ah);
  1097. /* Disable BMISS interrupt when we're not associated */
  1098. ath9k_hw_set_interrupts(sc->sc_ah,
  1099. sc->sc_imask &
  1100. ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS));
  1101. sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1102. DPRINTF(sc, ATH_DBG_CONFIG,
  1103. "%s: RX filter 0x%x bssid %pM aid 0x%x\n",
  1104. __func__, rfilt,
  1105. sc->sc_curbssid, sc->sc_curaid);
  1106. /* need to reconfigure the beacon */
  1107. sc->sc_flags &= ~SC_OP_BEACONS ;
  1108. break;
  1109. default:
  1110. break;
  1111. }
  1112. }
  1113. if ((conf->changed & IEEE80211_IFCC_BEACON) &&
  1114. ((vif->type == NL80211_IFTYPE_ADHOC) ||
  1115. (vif->type == NL80211_IFTYPE_AP))) {
  1116. /*
  1117. * Allocate and setup the beacon frame.
  1118. *
  1119. * Stop any previous beacon DMA. This may be
  1120. * necessary, for example, when an ibss merge
  1121. * causes reconfiguration; we may be called
  1122. * with beacon transmission active.
  1123. */
  1124. ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
  1125. error = ath_beacon_alloc(sc, 0);
  1126. if (error != 0)
  1127. return error;
  1128. ath_beacon_sync(sc, 0);
  1129. }
  1130. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  1131. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  1132. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  1133. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  1134. ath9k_hw_keysetmac(sc->sc_ah,
  1135. (u16)i,
  1136. sc->sc_curbssid);
  1137. }
  1138. /* Only legacy IBSS for now */
  1139. if (vif->type == NL80211_IFTYPE_ADHOC)
  1140. ath_update_chainmask(sc, 0);
  1141. return 0;
  1142. }
  1143. #define SUPPORTED_FILTERS \
  1144. (FIF_PROMISC_IN_BSS | \
  1145. FIF_ALLMULTI | \
  1146. FIF_CONTROL | \
  1147. FIF_OTHER_BSS | \
  1148. FIF_BCN_PRBRESP_PROMISC | \
  1149. FIF_FCSFAIL)
  1150. /* FIXME: sc->sc_full_reset ? */
  1151. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1152. unsigned int changed_flags,
  1153. unsigned int *total_flags,
  1154. int mc_count,
  1155. struct dev_mc_list *mclist)
  1156. {
  1157. struct ath_softc *sc = hw->priv;
  1158. u32 rfilt;
  1159. changed_flags &= SUPPORTED_FILTERS;
  1160. *total_flags &= SUPPORTED_FILTERS;
  1161. sc->rx_filter = *total_flags;
  1162. rfilt = ath_calcrxfilter(sc);
  1163. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1164. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1165. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1166. ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
  1167. }
  1168. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set HW RX filter: 0x%x\n",
  1169. __func__, sc->rx_filter);
  1170. }
  1171. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  1172. struct ieee80211_vif *vif,
  1173. enum sta_notify_cmd cmd,
  1174. struct ieee80211_sta *sta)
  1175. {
  1176. struct ath_softc *sc = hw->priv;
  1177. struct ath_node *an;
  1178. unsigned long flags;
  1179. spin_lock_irqsave(&sc->node_lock, flags);
  1180. an = ath_node_find(sc, sta->addr);
  1181. spin_unlock_irqrestore(&sc->node_lock, flags);
  1182. switch (cmd) {
  1183. case STA_NOTIFY_ADD:
  1184. spin_lock_irqsave(&sc->node_lock, flags);
  1185. if (!an) {
  1186. ath_node_attach(sc, sta->addr, 0);
  1187. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach a node: %pM\n",
  1188. __func__, sta->addr);
  1189. } else {
  1190. ath_node_get(sc, sta->addr);
  1191. }
  1192. /* XXX: Is this right? Can the capabilities change? */
  1193. an = ath_node_find(sc, sta->addr);
  1194. an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
  1195. sta->ht_cap.ampdu_factor);
  1196. an->mpdudensity =
  1197. parse_mpdudensity(sta->ht_cap.ampdu_density);
  1198. spin_unlock_irqrestore(&sc->node_lock, flags);
  1199. break;
  1200. case STA_NOTIFY_REMOVE:
  1201. if (!an)
  1202. DPRINTF(sc, ATH_DBG_FATAL,
  1203. "%s: Removal of a non-existent node\n",
  1204. __func__);
  1205. else {
  1206. ath_node_put(sc, an, ATH9K_BH_STATUS_INTACT);
  1207. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Put a node: %pM\n",
  1208. __func__,
  1209. sta->addr);
  1210. }
  1211. break;
  1212. default:
  1213. break;
  1214. }
  1215. }
  1216. static int ath9k_conf_tx(struct ieee80211_hw *hw,
  1217. u16 queue,
  1218. const struct ieee80211_tx_queue_params *params)
  1219. {
  1220. struct ath_softc *sc = hw->priv;
  1221. struct ath9k_tx_queue_info qi;
  1222. int ret = 0, qnum;
  1223. if (queue >= WME_NUM_AC)
  1224. return 0;
  1225. qi.tqi_aifs = params->aifs;
  1226. qi.tqi_cwmin = params->cw_min;
  1227. qi.tqi_cwmax = params->cw_max;
  1228. qi.tqi_burstTime = params->txop;
  1229. qnum = ath_get_hal_qnum(queue, sc);
  1230. DPRINTF(sc, ATH_DBG_CONFIG,
  1231. "%s: Configure tx [queue/halq] [%d/%d], "
  1232. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1233. __func__,
  1234. queue,
  1235. qnum,
  1236. params->aifs,
  1237. params->cw_min,
  1238. params->cw_max,
  1239. params->txop);
  1240. ret = ath_txq_update(sc, qnum, &qi);
  1241. if (ret)
  1242. DPRINTF(sc, ATH_DBG_FATAL,
  1243. "%s: TXQ Update failed\n", __func__);
  1244. return ret;
  1245. }
  1246. static int ath9k_set_key(struct ieee80211_hw *hw,
  1247. enum set_key_cmd cmd,
  1248. const u8 *local_addr,
  1249. const u8 *addr,
  1250. struct ieee80211_key_conf *key)
  1251. {
  1252. struct ath_softc *sc = hw->priv;
  1253. int ret = 0;
  1254. DPRINTF(sc, ATH_DBG_KEYCACHE, " %s: Set HW Key\n", __func__);
  1255. switch (cmd) {
  1256. case SET_KEY:
  1257. ret = ath_key_config(sc, addr, key);
  1258. if (!ret) {
  1259. set_bit(key->keyidx, sc->sc_keymap);
  1260. key->hw_key_idx = key->keyidx;
  1261. /* push IV and Michael MIC generation to stack */
  1262. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1263. if (key->alg == ALG_TKIP)
  1264. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1265. }
  1266. break;
  1267. case DISABLE_KEY:
  1268. ath_key_delete(sc, key);
  1269. clear_bit(key->keyidx, sc->sc_keymap);
  1270. break;
  1271. default:
  1272. ret = -EINVAL;
  1273. }
  1274. return ret;
  1275. }
  1276. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  1277. struct ieee80211_vif *vif,
  1278. struct ieee80211_bss_conf *bss_conf,
  1279. u32 changed)
  1280. {
  1281. struct ath_softc *sc = hw->priv;
  1282. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1283. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed PREAMBLE %d\n",
  1284. __func__,
  1285. bss_conf->use_short_preamble);
  1286. if (bss_conf->use_short_preamble)
  1287. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  1288. else
  1289. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  1290. }
  1291. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1292. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed CTS PROT %d\n",
  1293. __func__,
  1294. bss_conf->use_cts_prot);
  1295. if (bss_conf->use_cts_prot &&
  1296. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  1297. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  1298. else
  1299. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  1300. }
  1301. if (changed & BSS_CHANGED_HT) {
  1302. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed HT\n",
  1303. __func__);
  1304. ath9k_ht_conf(sc, bss_conf);
  1305. }
  1306. if (changed & BSS_CHANGED_ASSOC) {
  1307. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed ASSOC %d\n",
  1308. __func__,
  1309. bss_conf->assoc);
  1310. ath9k_bss_assoc_info(sc, bss_conf);
  1311. }
  1312. }
  1313. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  1314. {
  1315. u64 tsf;
  1316. struct ath_softc *sc = hw->priv;
  1317. struct ath_hal *ah = sc->sc_ah;
  1318. tsf = ath9k_hw_gettsf64(ah);
  1319. return tsf;
  1320. }
  1321. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  1322. {
  1323. struct ath_softc *sc = hw->priv;
  1324. struct ath_hal *ah = sc->sc_ah;
  1325. ath9k_hw_reset_tsf(ah);
  1326. }
  1327. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  1328. enum ieee80211_ampdu_mlme_action action,
  1329. struct ieee80211_sta *sta,
  1330. u16 tid, u16 *ssn)
  1331. {
  1332. struct ath_softc *sc = hw->priv;
  1333. int ret = 0;
  1334. switch (action) {
  1335. case IEEE80211_AMPDU_RX_START:
  1336. ret = ath_rx_aggr_start(sc, sta->addr, tid, ssn);
  1337. if (ret < 0)
  1338. DPRINTF(sc, ATH_DBG_FATAL,
  1339. "%s: Unable to start RX aggregation\n",
  1340. __func__);
  1341. break;
  1342. case IEEE80211_AMPDU_RX_STOP:
  1343. ret = ath_rx_aggr_stop(sc, sta->addr, tid);
  1344. if (ret < 0)
  1345. DPRINTF(sc, ATH_DBG_FATAL,
  1346. "%s: Unable to stop RX aggregation\n",
  1347. __func__);
  1348. break;
  1349. case IEEE80211_AMPDU_TX_START:
  1350. ret = ath_tx_aggr_start(sc, sta->addr, tid, ssn);
  1351. if (ret < 0)
  1352. DPRINTF(sc, ATH_DBG_FATAL,
  1353. "%s: Unable to start TX aggregation\n",
  1354. __func__);
  1355. else
  1356. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  1357. break;
  1358. case IEEE80211_AMPDU_TX_STOP:
  1359. ret = ath_tx_aggr_stop(sc, sta->addr, tid);
  1360. if (ret < 0)
  1361. DPRINTF(sc, ATH_DBG_FATAL,
  1362. "%s: Unable to stop TX aggregation\n",
  1363. __func__);
  1364. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  1365. break;
  1366. default:
  1367. DPRINTF(sc, ATH_DBG_FATAL,
  1368. "%s: Unknown AMPDU action\n", __func__);
  1369. }
  1370. return ret;
  1371. }
  1372. static int ath9k_no_fragmentation(struct ieee80211_hw *hw, u32 value)
  1373. {
  1374. return -EOPNOTSUPP;
  1375. }
  1376. static struct ieee80211_ops ath9k_ops = {
  1377. .tx = ath9k_tx,
  1378. .start = ath9k_start,
  1379. .stop = ath9k_stop,
  1380. .add_interface = ath9k_add_interface,
  1381. .remove_interface = ath9k_remove_interface,
  1382. .config = ath9k_config,
  1383. .config_interface = ath9k_config_interface,
  1384. .configure_filter = ath9k_configure_filter,
  1385. .get_stats = NULL,
  1386. .sta_notify = ath9k_sta_notify,
  1387. .conf_tx = ath9k_conf_tx,
  1388. .get_tx_stats = NULL,
  1389. .bss_info_changed = ath9k_bss_info_changed,
  1390. .set_tim = NULL,
  1391. .set_key = ath9k_set_key,
  1392. .hw_scan = NULL,
  1393. .get_tkip_seq = NULL,
  1394. .set_rts_threshold = NULL,
  1395. .set_frag_threshold = NULL,
  1396. .get_tsf = ath9k_get_tsf,
  1397. .reset_tsf = ath9k_reset_tsf,
  1398. .tx_last_beacon = NULL,
  1399. .ampdu_action = ath9k_ampdu_action,
  1400. .set_frag_threshold = ath9k_no_fragmentation,
  1401. };
  1402. static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1403. {
  1404. void __iomem *mem;
  1405. struct ath_softc *sc;
  1406. struct ieee80211_hw *hw;
  1407. const char *athname;
  1408. u8 csz;
  1409. u32 val;
  1410. int ret = 0;
  1411. if (pci_enable_device(pdev))
  1412. return -EIO;
  1413. /* XXX 32-bit addressing only */
  1414. if (pci_set_dma_mask(pdev, 0xffffffff)) {
  1415. printk(KERN_ERR "ath_pci: 32-bit DMA not available\n");
  1416. ret = -ENODEV;
  1417. goto bad;
  1418. }
  1419. /*
  1420. * Cache line size is used to size and align various
  1421. * structures used to communicate with the hardware.
  1422. */
  1423. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  1424. if (csz == 0) {
  1425. /*
  1426. * Linux 2.4.18 (at least) writes the cache line size
  1427. * register as a 16-bit wide register which is wrong.
  1428. * We must have this setup properly for rx buffer
  1429. * DMA to work so force a reasonable value here if it
  1430. * comes up zero.
  1431. */
  1432. csz = L1_CACHE_BYTES / sizeof(u32);
  1433. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  1434. }
  1435. /*
  1436. * The default setting of latency timer yields poor results,
  1437. * set it to the value used by other systems. It may be worth
  1438. * tweaking this setting more.
  1439. */
  1440. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  1441. pci_set_master(pdev);
  1442. /*
  1443. * Disable the RETRY_TIMEOUT register (0x41) to keep
  1444. * PCI Tx retries from interfering with C3 CPU state.
  1445. */
  1446. pci_read_config_dword(pdev, 0x40, &val);
  1447. if ((val & 0x0000ff00) != 0)
  1448. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1449. ret = pci_request_region(pdev, 0, "ath9k");
  1450. if (ret) {
  1451. dev_err(&pdev->dev, "PCI memory region reserve error\n");
  1452. ret = -ENODEV;
  1453. goto bad;
  1454. }
  1455. mem = pci_iomap(pdev, 0, 0);
  1456. if (!mem) {
  1457. printk(KERN_ERR "PCI memory map error\n") ;
  1458. ret = -EIO;
  1459. goto bad1;
  1460. }
  1461. hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
  1462. if (hw == NULL) {
  1463. printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
  1464. goto bad2;
  1465. }
  1466. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1467. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1468. IEEE80211_HW_SIGNAL_DBM |
  1469. IEEE80211_HW_NOISE_DBM;
  1470. hw->wiphy->interface_modes =
  1471. BIT(NL80211_IFTYPE_AP) |
  1472. BIT(NL80211_IFTYPE_STATION) |
  1473. BIT(NL80211_IFTYPE_ADHOC);
  1474. SET_IEEE80211_DEV(hw, &pdev->dev);
  1475. pci_set_drvdata(pdev, hw);
  1476. sc = hw->priv;
  1477. sc->hw = hw;
  1478. sc->pdev = pdev;
  1479. sc->mem = mem;
  1480. if (ath_attach(id->device, sc) != 0) {
  1481. ret = -ENODEV;
  1482. goto bad3;
  1483. }
  1484. /* setup interrupt service routine */
  1485. if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
  1486. printk(KERN_ERR "%s: request_irq failed\n",
  1487. wiphy_name(hw->wiphy));
  1488. ret = -EIO;
  1489. goto bad4;
  1490. }
  1491. athname = ath9k_hw_probe(id->vendor, id->device);
  1492. printk(KERN_INFO "%s: %s: mem=0x%lx, irq=%d\n",
  1493. wiphy_name(hw->wiphy),
  1494. athname ? athname : "Atheros ???",
  1495. (unsigned long)mem, pdev->irq);
  1496. return 0;
  1497. bad4:
  1498. ath_detach(sc);
  1499. bad3:
  1500. ieee80211_free_hw(hw);
  1501. bad2:
  1502. pci_iounmap(pdev, mem);
  1503. bad1:
  1504. pci_release_region(pdev, 0);
  1505. bad:
  1506. pci_disable_device(pdev);
  1507. return ret;
  1508. }
  1509. static void ath_pci_remove(struct pci_dev *pdev)
  1510. {
  1511. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  1512. struct ath_softc *sc = hw->priv;
  1513. enum ath9k_int status;
  1514. if (pdev->irq) {
  1515. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  1516. /* clear the ISR */
  1517. ath9k_hw_getisr(sc->sc_ah, &status);
  1518. sc->sc_flags |= SC_OP_INVALID;
  1519. free_irq(pdev->irq, sc);
  1520. }
  1521. ath_detach(sc);
  1522. pci_iounmap(pdev, sc->mem);
  1523. pci_release_region(pdev, 0);
  1524. pci_disable_device(pdev);
  1525. ieee80211_free_hw(hw);
  1526. }
  1527. #ifdef CONFIG_PM
  1528. static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1529. {
  1530. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  1531. struct ath_softc *sc = hw->priv;
  1532. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  1533. #ifdef CONFIG_RFKILL
  1534. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1535. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1536. #endif
  1537. pci_save_state(pdev);
  1538. pci_disable_device(pdev);
  1539. pci_set_power_state(pdev, 3);
  1540. return 0;
  1541. }
  1542. static int ath_pci_resume(struct pci_dev *pdev)
  1543. {
  1544. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  1545. struct ath_softc *sc = hw->priv;
  1546. u32 val;
  1547. int err;
  1548. err = pci_enable_device(pdev);
  1549. if (err)
  1550. return err;
  1551. pci_restore_state(pdev);
  1552. /*
  1553. * Suspend/Resume resets the PCI configuration space, so we have to
  1554. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  1555. * PCI Tx retries from interfering with C3 CPU state
  1556. */
  1557. pci_read_config_dword(pdev, 0x40, &val);
  1558. if ((val & 0x0000ff00) != 0)
  1559. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1560. /* Enable LED */
  1561. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  1562. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  1563. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  1564. #ifdef CONFIG_RFKILL
  1565. /*
  1566. * check the h/w rfkill state on resume
  1567. * and start the rfkill poll timer
  1568. */
  1569. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1570. queue_delayed_work(sc->hw->workqueue,
  1571. &sc->rf_kill.rfkill_poll, 0);
  1572. #endif
  1573. return 0;
  1574. }
  1575. #endif /* CONFIG_PM */
  1576. MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
  1577. static struct pci_driver ath_pci_driver = {
  1578. .name = "ath9k",
  1579. .id_table = ath_pci_id_table,
  1580. .probe = ath_pci_probe,
  1581. .remove = ath_pci_remove,
  1582. #ifdef CONFIG_PM
  1583. .suspend = ath_pci_suspend,
  1584. .resume = ath_pci_resume,
  1585. #endif /* CONFIG_PM */
  1586. };
  1587. static int __init init_ath_pci(void)
  1588. {
  1589. printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
  1590. if (pci_register_driver(&ath_pci_driver) < 0) {
  1591. printk(KERN_ERR
  1592. "ath_pci: No devices found, driver not installed.\n");
  1593. pci_unregister_driver(&ath_pci_driver);
  1594. return -ENODEV;
  1595. }
  1596. return 0;
  1597. }
  1598. module_init(init_ath_pci);
  1599. static void __exit exit_ath_pci(void)
  1600. {
  1601. pci_unregister_driver(&ath_pci_driver);
  1602. printk(KERN_INFO "%s: driver unloaded\n", dev_info);
  1603. }
  1604. module_exit(exit_ath_pci);