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