rt2x00dev.c 29 KB

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