rt2x00dev.c 28 KB

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