rt2x00dev.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  3. Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  4. <http://rt2x00.serialmonkey.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the
  15. Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /*
  19. Module: rt2x00lib
  20. Abstract: rt2x00 generic device routines.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include "rt2x00.h"
  26. #include "rt2x00lib.h"
  27. /*
  28. * Radio control handlers.
  29. */
  30. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  31. {
  32. int status;
  33. /*
  34. * Don't enable the radio twice.
  35. * And check if the hardware button has been disabled.
  36. */
  37. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  38. return 0;
  39. /*
  40. * Initialize all data queues.
  41. */
  42. rt2x00queue_init_queues(rt2x00dev);
  43. /*
  44. * Enable radio.
  45. */
  46. status =
  47. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
  48. if (status)
  49. return status;
  50. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
  51. rt2x00leds_led_radio(rt2x00dev, true);
  52. rt2x00led_led_activity(rt2x00dev, true);
  53. set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
  54. /*
  55. * Enable RX.
  56. */
  57. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_RX_ON);
  58. rt2x00link_start_tuner(rt2x00dev);
  59. /*
  60. * Start watchdog monitoring.
  61. */
  62. rt2x00link_start_watchdog(rt2x00dev);
  63. /*
  64. * Start the TX queues.
  65. */
  66. ieee80211_wake_queues(rt2x00dev->hw);
  67. return 0;
  68. }
  69. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  70. {
  71. if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  72. return;
  73. /*
  74. * Stop the TX queues in mac80211.
  75. */
  76. ieee80211_stop_queues(rt2x00dev->hw);
  77. rt2x00queue_stop_queues(rt2x00dev);
  78. /*
  79. * Stop watchdog monitoring.
  80. */
  81. rt2x00link_stop_watchdog(rt2x00dev);
  82. /*
  83. * Disable RX.
  84. */
  85. rt2x00link_stop_tuner(rt2x00dev);
  86. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_RX_OFF);
  87. /*
  88. * Disable radio.
  89. */
  90. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  91. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  92. rt2x00led_led_activity(rt2x00dev, false);
  93. rt2x00leds_led_radio(rt2x00dev, false);
  94. }
  95. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  96. struct ieee80211_vif *vif)
  97. {
  98. struct rt2x00_dev *rt2x00dev = data;
  99. struct rt2x00_intf *intf = vif_to_intf(vif);
  100. int delayed_flags;
  101. /*
  102. * Copy all data we need during this action under the protection
  103. * of a spinlock. Otherwise race conditions might occur which results
  104. * into an invalid configuration.
  105. */
  106. spin_lock(&intf->lock);
  107. delayed_flags = intf->delayed_flags;
  108. intf->delayed_flags = 0;
  109. spin_unlock(&intf->lock);
  110. /*
  111. * It is possible the radio was disabled while the work had been
  112. * scheduled. If that happens we should return here immediately,
  113. * note that in the spinlock protected area above the delayed_flags
  114. * have been cleared correctly.
  115. */
  116. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  117. return;
  118. if (delayed_flags & DELAYED_UPDATE_BEACON)
  119. rt2x00queue_update_beacon(rt2x00dev, vif, true);
  120. }
  121. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  122. {
  123. struct rt2x00_dev *rt2x00dev =
  124. container_of(work, struct rt2x00_dev, intf_work);
  125. /*
  126. * Iterate over each interface and perform the
  127. * requested configurations.
  128. */
  129. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  130. rt2x00lib_intf_scheduled_iter,
  131. rt2x00dev);
  132. }
  133. /*
  134. * Interrupt context handlers.
  135. */
  136. static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
  137. struct ieee80211_vif *vif)
  138. {
  139. struct rt2x00_dev *rt2x00dev = data;
  140. struct sk_buff *skb;
  141. /*
  142. * Only AP mode interfaces do broad- and multicast buffering
  143. */
  144. if (vif->type != NL80211_IFTYPE_AP)
  145. return;
  146. /*
  147. * Send out buffered broad- and multicast frames
  148. */
  149. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  150. while (skb) {
  151. rt2x00mac_tx(rt2x00dev->hw, skb);
  152. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  153. }
  154. }
  155. static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
  156. struct ieee80211_vif *vif)
  157. {
  158. struct rt2x00_dev *rt2x00dev = data;
  159. if (vif->type != NL80211_IFTYPE_AP &&
  160. vif->type != NL80211_IFTYPE_ADHOC &&
  161. vif->type != NL80211_IFTYPE_MESH_POINT &&
  162. vif->type != NL80211_IFTYPE_WDS)
  163. return;
  164. rt2x00queue_update_beacon(rt2x00dev, vif, true);
  165. }
  166. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  167. {
  168. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  169. return;
  170. /* send buffered bc/mc frames out for every bssid */
  171. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  172. rt2x00lib_bc_buffer_iter,
  173. rt2x00dev);
  174. /*
  175. * Devices with pre tbtt interrupt don't need to update the beacon
  176. * here as they will fetch the next beacon directly prior to
  177. * transmission.
  178. */
  179. if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
  180. return;
  181. /* fetch next beacon */
  182. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  183. rt2x00lib_beaconupdate_iter,
  184. rt2x00dev);
  185. }
  186. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  187. void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
  188. {
  189. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  190. return;
  191. /* fetch next beacon */
  192. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  193. rt2x00lib_beaconupdate_iter,
  194. rt2x00dev);
  195. }
  196. EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
  197. void rt2x00lib_dmadone(struct queue_entry *entry)
  198. {
  199. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  200. rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
  201. }
  202. EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
  203. void rt2x00lib_txdone(struct queue_entry *entry,
  204. struct txdone_entry_desc *txdesc)
  205. {
  206. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  207. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  208. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  209. enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
  210. unsigned int header_length, i;
  211. u8 rate_idx, rate_flags, retry_rates;
  212. u8 skbdesc_flags = skbdesc->flags;
  213. bool success;
  214. /*
  215. * Unmap the skb.
  216. */
  217. rt2x00queue_unmap_skb(entry);
  218. /*
  219. * Remove the extra tx headroom from the skb.
  220. */
  221. skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  222. /*
  223. * Signal that the TX descriptor is no longer in the skb.
  224. */
  225. skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
  226. /*
  227. * Determine the length of 802.11 header.
  228. */
  229. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  230. /*
  231. * Remove L2 padding which was added during
  232. */
  233. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  234. rt2x00queue_remove_l2pad(entry->skb, header_length);
  235. /*
  236. * If the IV/EIV data was stripped from the frame before it was
  237. * passed to the hardware, we should now reinsert it again because
  238. * mac80211 will expect the same data to be present it the
  239. * frame as it was passed to us.
  240. */
  241. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
  242. rt2x00crypto_tx_insert_iv(entry->skb, header_length);
  243. /*
  244. * Send frame to debugfs immediately, after this call is completed
  245. * we are going to overwrite the skb->cb array.
  246. */
  247. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
  248. /*
  249. * Determine if the frame has been successfully transmitted.
  250. */
  251. success =
  252. test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  253. test_bit(TXDONE_UNKNOWN, &txdesc->flags);
  254. /*
  255. * Update TX statistics.
  256. */
  257. rt2x00dev->link.qual.tx_success += success;
  258. rt2x00dev->link.qual.tx_failed += !success;
  259. rate_idx = skbdesc->tx_rate_idx;
  260. rate_flags = skbdesc->tx_rate_flags;
  261. retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
  262. (txdesc->retry + 1) : 1;
  263. /*
  264. * Initialize TX status
  265. */
  266. memset(&tx_info->status, 0, sizeof(tx_info->status));
  267. tx_info->status.ack_signal = 0;
  268. /*
  269. * Frame was send with retries, hardware tried
  270. * different rates to send out the frame, at each
  271. * retry it lowered the rate 1 step except when the
  272. * lowest rate was used.
  273. */
  274. for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
  275. tx_info->status.rates[i].idx = rate_idx - i;
  276. tx_info->status.rates[i].flags = rate_flags;
  277. if (rate_idx - i == 0) {
  278. /*
  279. * The lowest rate (index 0) was used until the
  280. * number of max retries was reached.
  281. */
  282. tx_info->status.rates[i].count = retry_rates - i;
  283. i++;
  284. break;
  285. }
  286. tx_info->status.rates[i].count = 1;
  287. }
  288. if (i < (IEEE80211_TX_MAX_RATES - 1))
  289. tx_info->status.rates[i].idx = -1; /* terminate */
  290. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  291. if (success)
  292. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  293. else
  294. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  295. }
  296. /*
  297. * Every single frame has it's own tx status, hence report
  298. * every frame as ampdu of size 1.
  299. *
  300. * TODO: if we can find out how many frames were aggregated
  301. * by the hw we could provide the real ampdu_len to mac80211
  302. * which would allow the rc algorithm to better decide on
  303. * which rates are suitable.
  304. */
  305. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  306. tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
  307. tx_info->status.ampdu_len = 1;
  308. tx_info->status.ampdu_ack_len = success ? 1 : 0;
  309. }
  310. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  311. if (success)
  312. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  313. else
  314. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  315. }
  316. /*
  317. * Only send the status report to mac80211 when it's a frame
  318. * that originated in mac80211. If this was a extra frame coming
  319. * through a mac80211 library call (RTS/CTS) then we should not
  320. * send the status report back.
  321. */
  322. if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
  323. if (test_bit(DRIVER_REQUIRE_TASKLET_CONTEXT, &rt2x00dev->flags))
  324. ieee80211_tx_status(rt2x00dev->hw, entry->skb);
  325. else
  326. ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
  327. } else
  328. dev_kfree_skb_any(entry->skb);
  329. /*
  330. * Make this entry available for reuse.
  331. */
  332. entry->skb = NULL;
  333. entry->flags = 0;
  334. rt2x00dev->ops->lib->clear_entry(entry);
  335. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  336. /*
  337. * If the data queue was below the threshold before the txdone
  338. * handler we must make sure the packet queue in the mac80211 stack
  339. * is reenabled when the txdone handler has finished.
  340. */
  341. if (!rt2x00queue_threshold(entry->queue))
  342. ieee80211_wake_queue(rt2x00dev->hw, qid);
  343. }
  344. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  345. void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
  346. {
  347. struct txdone_entry_desc txdesc;
  348. txdesc.flags = 0;
  349. __set_bit(status, &txdesc.flags);
  350. txdesc.retry = 0;
  351. rt2x00lib_txdone(entry, &txdesc);
  352. }
  353. EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
  354. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  355. struct rxdone_entry_desc *rxdesc)
  356. {
  357. struct ieee80211_supported_band *sband;
  358. const struct rt2x00_rate *rate;
  359. unsigned int i;
  360. int signal = rxdesc->signal;
  361. int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  362. switch (rxdesc->rate_mode) {
  363. case RATE_MODE_CCK:
  364. case RATE_MODE_OFDM:
  365. /*
  366. * For non-HT rates the MCS value needs to contain the
  367. * actually used rate modulation (CCK or OFDM).
  368. */
  369. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  370. signal = RATE_MCS(rxdesc->rate_mode, signal);
  371. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  372. for (i = 0; i < sband->n_bitrates; i++) {
  373. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  374. if (((type == RXDONE_SIGNAL_PLCP) &&
  375. (rate->plcp == signal)) ||
  376. ((type == RXDONE_SIGNAL_BITRATE) &&
  377. (rate->bitrate == signal)) ||
  378. ((type == RXDONE_SIGNAL_MCS) &&
  379. (rate->mcs == signal))) {
  380. return i;
  381. }
  382. }
  383. break;
  384. case RATE_MODE_HT_MIX:
  385. case RATE_MODE_HT_GREENFIELD:
  386. if (signal >= 0 && signal <= 76)
  387. return signal;
  388. break;
  389. default:
  390. break;
  391. }
  392. WARNING(rt2x00dev, "Frame received with unrecognized signal, "
  393. "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
  394. rxdesc->rate_mode, signal, type);
  395. return 0;
  396. }
  397. void rt2x00lib_rxdone(struct queue_entry *entry)
  398. {
  399. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  400. struct rxdone_entry_desc rxdesc;
  401. struct sk_buff *skb;
  402. struct ieee80211_rx_status *rx_status;
  403. unsigned int header_length;
  404. int rate_idx;
  405. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  406. !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  407. goto submit_entry;
  408. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  409. goto submit_entry;
  410. /*
  411. * Allocate a new sk_buffer. If no new buffer available, drop the
  412. * received frame and reuse the existing buffer.
  413. */
  414. skb = rt2x00queue_alloc_rxskb(entry);
  415. if (!skb)
  416. goto submit_entry;
  417. /*
  418. * Unmap the skb.
  419. */
  420. rt2x00queue_unmap_skb(entry);
  421. /*
  422. * Extract the RXD details.
  423. */
  424. memset(&rxdesc, 0, sizeof(rxdesc));
  425. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  426. /*
  427. * The data behind the ieee80211 header must be
  428. * aligned on a 4 byte boundary.
  429. */
  430. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  431. /*
  432. * Hardware might have stripped the IV/EIV/ICV data,
  433. * in that case it is possible that the data was
  434. * provided separately (through hardware descriptor)
  435. * in which case we should reinsert the data into the frame.
  436. */
  437. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  438. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  439. rt2x00crypto_rx_insert_iv(entry->skb, header_length,
  440. &rxdesc);
  441. else if (header_length &&
  442. (rxdesc.size > header_length) &&
  443. (rxdesc.dev_flags & RXDONE_L2PAD))
  444. rt2x00queue_remove_l2pad(entry->skb, header_length);
  445. else
  446. rt2x00queue_align_payload(entry->skb, header_length);
  447. /* Trim buffer to correct size */
  448. skb_trim(entry->skb, rxdesc.size);
  449. /*
  450. * Translate the signal to the correct bitrate index.
  451. */
  452. rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
  453. if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
  454. rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
  455. rxdesc.flags |= RX_FLAG_HT;
  456. /*
  457. * Update extra components
  458. */
  459. rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
  460. rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
  461. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  462. /*
  463. * Initialize RX status information, and send frame
  464. * to mac80211.
  465. */
  466. rx_status = IEEE80211_SKB_RXCB(entry->skb);
  467. rx_status->mactime = rxdesc.timestamp;
  468. rx_status->band = rt2x00dev->curr_band;
  469. rx_status->freq = rt2x00dev->curr_freq;
  470. rx_status->rate_idx = rate_idx;
  471. rx_status->signal = rxdesc.rssi;
  472. rx_status->flag = rxdesc.flags;
  473. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  474. ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
  475. /*
  476. * Replace the skb with the freshly allocated one.
  477. */
  478. entry->skb = skb;
  479. submit_entry:
  480. entry->flags = 0;
  481. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  482. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
  483. test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) {
  484. rt2x00dev->ops->lib->clear_entry(entry);
  485. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  486. }
  487. }
  488. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  489. /*
  490. * Driver initialization handlers.
  491. */
  492. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  493. {
  494. .flags = DEV_RATE_CCK,
  495. .bitrate = 10,
  496. .ratemask = BIT(0),
  497. .plcp = 0x00,
  498. .mcs = RATE_MCS(RATE_MODE_CCK, 0),
  499. },
  500. {
  501. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  502. .bitrate = 20,
  503. .ratemask = BIT(1),
  504. .plcp = 0x01,
  505. .mcs = RATE_MCS(RATE_MODE_CCK, 1),
  506. },
  507. {
  508. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  509. .bitrate = 55,
  510. .ratemask = BIT(2),
  511. .plcp = 0x02,
  512. .mcs = RATE_MCS(RATE_MODE_CCK, 2),
  513. },
  514. {
  515. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  516. .bitrate = 110,
  517. .ratemask = BIT(3),
  518. .plcp = 0x03,
  519. .mcs = RATE_MCS(RATE_MODE_CCK, 3),
  520. },
  521. {
  522. .flags = DEV_RATE_OFDM,
  523. .bitrate = 60,
  524. .ratemask = BIT(4),
  525. .plcp = 0x0b,
  526. .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
  527. },
  528. {
  529. .flags = DEV_RATE_OFDM,
  530. .bitrate = 90,
  531. .ratemask = BIT(5),
  532. .plcp = 0x0f,
  533. .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
  534. },
  535. {
  536. .flags = DEV_RATE_OFDM,
  537. .bitrate = 120,
  538. .ratemask = BIT(6),
  539. .plcp = 0x0a,
  540. .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
  541. },
  542. {
  543. .flags = DEV_RATE_OFDM,
  544. .bitrate = 180,
  545. .ratemask = BIT(7),
  546. .plcp = 0x0e,
  547. .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
  548. },
  549. {
  550. .flags = DEV_RATE_OFDM,
  551. .bitrate = 240,
  552. .ratemask = BIT(8),
  553. .plcp = 0x09,
  554. .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
  555. },
  556. {
  557. .flags = DEV_RATE_OFDM,
  558. .bitrate = 360,
  559. .ratemask = BIT(9),
  560. .plcp = 0x0d,
  561. .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
  562. },
  563. {
  564. .flags = DEV_RATE_OFDM,
  565. .bitrate = 480,
  566. .ratemask = BIT(10),
  567. .plcp = 0x08,
  568. .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
  569. },
  570. {
  571. .flags = DEV_RATE_OFDM,
  572. .bitrate = 540,
  573. .ratemask = BIT(11),
  574. .plcp = 0x0c,
  575. .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
  576. },
  577. };
  578. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  579. const int channel, const int tx_power,
  580. const int value)
  581. {
  582. entry->center_freq = ieee80211_channel_to_frequency(channel);
  583. entry->hw_value = value;
  584. entry->max_power = tx_power;
  585. entry->max_antenna_gain = 0xff;
  586. }
  587. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  588. const u16 index, const struct rt2x00_rate *rate)
  589. {
  590. entry->flags = 0;
  591. entry->bitrate = rate->bitrate;
  592. entry->hw_value = index;
  593. entry->hw_value_short = index;
  594. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  595. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  596. }
  597. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  598. struct hw_mode_spec *spec)
  599. {
  600. struct ieee80211_hw *hw = rt2x00dev->hw;
  601. struct ieee80211_channel *channels;
  602. struct ieee80211_rate *rates;
  603. unsigned int num_rates;
  604. unsigned int i;
  605. num_rates = 0;
  606. if (spec->supported_rates & SUPPORT_RATE_CCK)
  607. num_rates += 4;
  608. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  609. num_rates += 8;
  610. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  611. if (!channels)
  612. return -ENOMEM;
  613. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  614. if (!rates)
  615. goto exit_free_channels;
  616. /*
  617. * Initialize Rate list.
  618. */
  619. for (i = 0; i < num_rates; i++)
  620. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  621. /*
  622. * Initialize Channel list.
  623. */
  624. for (i = 0; i < spec->num_channels; i++) {
  625. rt2x00lib_channel(&channels[i],
  626. spec->channels[i].channel,
  627. spec->channels_info[i].max_power, i);
  628. }
  629. /*
  630. * Intitialize 802.11b, 802.11g
  631. * Rates: CCK, OFDM.
  632. * Channels: 2.4 GHz
  633. */
  634. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  635. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  636. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  637. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  638. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  639. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  640. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  641. memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
  642. &spec->ht, sizeof(spec->ht));
  643. }
  644. /*
  645. * Intitialize 802.11a
  646. * Rates: OFDM.
  647. * Channels: OFDM, UNII, HiperLAN2.
  648. */
  649. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  650. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  651. spec->num_channels - 14;
  652. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  653. num_rates - 4;
  654. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  655. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  656. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  657. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  658. memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
  659. &spec->ht, sizeof(spec->ht));
  660. }
  661. return 0;
  662. exit_free_channels:
  663. kfree(channels);
  664. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  665. return -ENOMEM;
  666. }
  667. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  668. {
  669. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  670. ieee80211_unregister_hw(rt2x00dev->hw);
  671. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  672. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  673. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  674. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  675. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  676. }
  677. kfree(rt2x00dev->spec.channels_info);
  678. }
  679. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  680. {
  681. struct hw_mode_spec *spec = &rt2x00dev->spec;
  682. int status;
  683. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  684. return 0;
  685. /*
  686. * Initialize HW modes.
  687. */
  688. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  689. if (status)
  690. return status;
  691. /*
  692. * Initialize HW fields.
  693. */
  694. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  695. /*
  696. * Initialize extra TX headroom required.
  697. */
  698. rt2x00dev->hw->extra_tx_headroom =
  699. max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
  700. rt2x00dev->ops->extra_tx_headroom);
  701. /*
  702. * Take TX headroom required for alignment into account.
  703. */
  704. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  705. rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
  706. else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  707. rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
  708. /*
  709. * Allocate tx status FIFO for driver use.
  710. */
  711. if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags) &&
  712. rt2x00dev->ops->lib->txstatus_tasklet) {
  713. /*
  714. * Allocate txstatus fifo and tasklet, we use a size of 512
  715. * for the kfifo which is big enough to store 512/4=128 tx
  716. * status reports. In the worst case (tx status for all tx
  717. * queues gets reported before we've got a chance to handle
  718. * them) 24*4=384 tx status reports need to be cached.
  719. */
  720. status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
  721. GFP_KERNEL);
  722. if (status)
  723. return status;
  724. /* tasklet for processing the tx status reports. */
  725. tasklet_init(&rt2x00dev->txstatus_tasklet,
  726. rt2x00dev->ops->lib->txstatus_tasklet,
  727. (unsigned long)rt2x00dev);
  728. }
  729. /*
  730. * Register HW.
  731. */
  732. status = ieee80211_register_hw(rt2x00dev->hw);
  733. if (status)
  734. return status;
  735. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  736. return 0;
  737. }
  738. /*
  739. * Initialization/uninitialization handlers.
  740. */
  741. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  742. {
  743. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  744. return;
  745. /*
  746. * Unregister extra components.
  747. */
  748. rt2x00rfkill_unregister(rt2x00dev);
  749. /*
  750. * Allow the HW to uninitialize.
  751. */
  752. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  753. /*
  754. * Free allocated queue entries.
  755. */
  756. rt2x00queue_uninitialize(rt2x00dev);
  757. }
  758. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  759. {
  760. int status;
  761. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  762. return 0;
  763. /*
  764. * Allocate all queue entries.
  765. */
  766. status = rt2x00queue_initialize(rt2x00dev);
  767. if (status)
  768. return status;
  769. /*
  770. * Initialize the device.
  771. */
  772. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  773. if (status) {
  774. rt2x00queue_uninitialize(rt2x00dev);
  775. return status;
  776. }
  777. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  778. /*
  779. * Register the extra components.
  780. */
  781. rt2x00rfkill_register(rt2x00dev);
  782. return 0;
  783. }
  784. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  785. {
  786. int retval;
  787. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  788. return 0;
  789. /*
  790. * If this is the first interface which is added,
  791. * we should load the firmware now.
  792. */
  793. retval = rt2x00lib_load_firmware(rt2x00dev);
  794. if (retval)
  795. return retval;
  796. /*
  797. * Initialize the device.
  798. */
  799. retval = rt2x00lib_initialize(rt2x00dev);
  800. if (retval)
  801. return retval;
  802. rt2x00dev->intf_ap_count = 0;
  803. rt2x00dev->intf_sta_count = 0;
  804. rt2x00dev->intf_associated = 0;
  805. /* Enable the radio */
  806. retval = rt2x00lib_enable_radio(rt2x00dev);
  807. if (retval)
  808. return retval;
  809. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  810. return 0;
  811. }
  812. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  813. {
  814. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  815. return;
  816. /*
  817. * Perhaps we can add something smarter here,
  818. * but for now just disabling the radio should do.
  819. */
  820. rt2x00lib_disable_radio(rt2x00dev);
  821. rt2x00dev->intf_ap_count = 0;
  822. rt2x00dev->intf_sta_count = 0;
  823. rt2x00dev->intf_associated = 0;
  824. }
  825. /*
  826. * driver allocation handlers.
  827. */
  828. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  829. {
  830. int retval = -ENOMEM;
  831. mutex_init(&rt2x00dev->csr_mutex);
  832. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  833. /*
  834. * Make room for rt2x00_intf inside the per-interface
  835. * structure ieee80211_vif.
  836. */
  837. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  838. /*
  839. * Determine which operating modes are supported, all modes
  840. * which require beaconing, depend on the availability of
  841. * beacon entries.
  842. */
  843. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  844. if (rt2x00dev->ops->bcn->entry_num > 0)
  845. rt2x00dev->hw->wiphy->interface_modes |=
  846. BIT(NL80211_IFTYPE_ADHOC) |
  847. BIT(NL80211_IFTYPE_AP) |
  848. BIT(NL80211_IFTYPE_MESH_POINT) |
  849. BIT(NL80211_IFTYPE_WDS);
  850. /*
  851. * Initialize configuration work.
  852. */
  853. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  854. /*
  855. * Let the driver probe the device to detect the capabilities.
  856. */
  857. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  858. if (retval) {
  859. ERROR(rt2x00dev, "Failed to allocate device.\n");
  860. goto exit;
  861. }
  862. /*
  863. * Allocate queue array.
  864. */
  865. retval = rt2x00queue_allocate(rt2x00dev);
  866. if (retval)
  867. goto exit;
  868. /*
  869. * Initialize ieee80211 structure.
  870. */
  871. retval = rt2x00lib_probe_hw(rt2x00dev);
  872. if (retval) {
  873. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  874. goto exit;
  875. }
  876. /*
  877. * Register extra components.
  878. */
  879. rt2x00link_register(rt2x00dev);
  880. rt2x00leds_register(rt2x00dev);
  881. rt2x00debug_register(rt2x00dev);
  882. return 0;
  883. exit:
  884. rt2x00lib_remove_dev(rt2x00dev);
  885. return retval;
  886. }
  887. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  888. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  889. {
  890. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  891. /*
  892. * Disable radio.
  893. */
  894. rt2x00lib_disable_radio(rt2x00dev);
  895. /*
  896. * Stop all work.
  897. */
  898. cancel_work_sync(&rt2x00dev->intf_work);
  899. cancel_work_sync(&rt2x00dev->rxdone_work);
  900. cancel_work_sync(&rt2x00dev->txdone_work);
  901. /*
  902. * Free the tx status fifo.
  903. */
  904. kfifo_free(&rt2x00dev->txstatus_fifo);
  905. /*
  906. * Kill the tx status tasklet.
  907. */
  908. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  909. /*
  910. * Uninitialize device.
  911. */
  912. rt2x00lib_uninitialize(rt2x00dev);
  913. /*
  914. * Free extra components
  915. */
  916. rt2x00debug_deregister(rt2x00dev);
  917. rt2x00leds_unregister(rt2x00dev);
  918. /*
  919. * Free ieee80211_hw memory.
  920. */
  921. rt2x00lib_remove_hw(rt2x00dev);
  922. /*
  923. * Free firmware image.
  924. */
  925. rt2x00lib_free_firmware(rt2x00dev);
  926. /*
  927. * Free queue structures.
  928. */
  929. rt2x00queue_free(rt2x00dev);
  930. }
  931. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  932. /*
  933. * Device state handlers
  934. */
  935. #ifdef CONFIG_PM
  936. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  937. {
  938. NOTICE(rt2x00dev, "Going to sleep.\n");
  939. /*
  940. * Prevent mac80211 from accessing driver while suspended.
  941. */
  942. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  943. return 0;
  944. /*
  945. * Cleanup as much as possible.
  946. */
  947. rt2x00lib_uninitialize(rt2x00dev);
  948. /*
  949. * Suspend/disable extra components.
  950. */
  951. rt2x00leds_suspend(rt2x00dev);
  952. rt2x00debug_deregister(rt2x00dev);
  953. /*
  954. * Set device mode to sleep for power management,
  955. * on some hardware this call seems to consistently fail.
  956. * From the specifications it is hard to tell why it fails,
  957. * and if this is a "bad thing".
  958. * Overall it is safe to just ignore the failure and
  959. * continue suspending. The only downside is that the
  960. * device will not be in optimal power save mode, but with
  961. * the radio and the other components already disabled the
  962. * device is as good as disabled.
  963. */
  964. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  965. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  966. "continue suspending.\n");
  967. return 0;
  968. }
  969. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  970. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  971. {
  972. NOTICE(rt2x00dev, "Waking up.\n");
  973. /*
  974. * Restore/enable extra components.
  975. */
  976. rt2x00debug_register(rt2x00dev);
  977. rt2x00leds_resume(rt2x00dev);
  978. /*
  979. * We are ready again to receive requests from mac80211.
  980. */
  981. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  982. return 0;
  983. }
  984. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  985. #endif /* CONFIG_PM */
  986. /*
  987. * rt2x00lib module information.
  988. */
  989. MODULE_AUTHOR(DRV_PROJECT);
  990. MODULE_VERSION(DRV_VERSION);
  991. MODULE_DESCRIPTION("rt2x00 library");
  992. MODULE_LICENSE("GPL");