rt2x00dev.c 25 KB

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