rt2x00dev.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. /*
  2. Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
  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. /*
  22. * Set enviroment defines for rt2x00.h
  23. */
  24. #define DRV_NAME "rt2x00lib"
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include "rt2x00.h"
  28. #include "rt2x00lib.h"
  29. /*
  30. * Ring handler.
  31. */
  32. struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
  33. const unsigned int queue)
  34. {
  35. int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
  36. /*
  37. * Check if we are requesting a reqular TX ring,
  38. * or if we are requesting a Beacon or Atim ring.
  39. * For Atim rings, we should check if it is supported.
  40. */
  41. if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
  42. return &rt2x00dev->tx[queue];
  43. if (!rt2x00dev->bcn || !beacon)
  44. return NULL;
  45. if (queue == IEEE80211_TX_QUEUE_BEACON)
  46. return &rt2x00dev->bcn[0];
  47. else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  48. return &rt2x00dev->bcn[1];
  49. return NULL;
  50. }
  51. EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
  52. /*
  53. * Link tuning handlers
  54. */
  55. static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
  56. {
  57. rt2x00_clear_link(&rt2x00dev->link);
  58. /*
  59. * Reset the link tuner.
  60. */
  61. rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
  62. queue_delayed_work(rt2x00dev->hw->workqueue,
  63. &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
  64. }
  65. static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
  66. {
  67. cancel_delayed_work_sync(&rt2x00dev->link.work);
  68. }
  69. void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
  70. {
  71. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  72. return;
  73. rt2x00lib_stop_link_tuner(rt2x00dev);
  74. rt2x00lib_start_link_tuner(rt2x00dev);
  75. }
  76. /*
  77. * Radio control handlers.
  78. */
  79. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  80. {
  81. int status;
  82. /*
  83. * Don't enable the radio twice.
  84. * And check if the hardware button has been disabled.
  85. */
  86. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  87. test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
  88. return 0;
  89. /*
  90. * Enable radio.
  91. */
  92. status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
  93. STATE_RADIO_ON);
  94. if (status)
  95. return status;
  96. __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
  97. /*
  98. * Enable RX.
  99. */
  100. rt2x00lib_toggle_rx(rt2x00dev, 1);
  101. /*
  102. * Start the TX queues.
  103. */
  104. ieee80211_start_queues(rt2x00dev->hw);
  105. return 0;
  106. }
  107. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  108. {
  109. if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  110. return;
  111. /*
  112. * Stop all scheduled work.
  113. */
  114. if (work_pending(&rt2x00dev->beacon_work))
  115. cancel_work_sync(&rt2x00dev->beacon_work);
  116. if (work_pending(&rt2x00dev->filter_work))
  117. cancel_work_sync(&rt2x00dev->filter_work);
  118. /*
  119. * Stop the TX queues.
  120. */
  121. ieee80211_stop_queues(rt2x00dev->hw);
  122. /*
  123. * Disable RX.
  124. */
  125. rt2x00lib_toggle_rx(rt2x00dev, 0);
  126. /*
  127. * Disable radio.
  128. */
  129. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  130. }
  131. void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, int enable)
  132. {
  133. enum dev_state state = enable ? STATE_RADIO_RX_ON : STATE_RADIO_RX_OFF;
  134. /*
  135. * When we are disabling the RX, we should also stop the link tuner.
  136. */
  137. if (!enable)
  138. rt2x00lib_stop_link_tuner(rt2x00dev);
  139. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  140. /*
  141. * When we are enabling the RX, we should also start the link tuner.
  142. */
  143. if (enable && is_interface_present(&rt2x00dev->interface))
  144. rt2x00lib_start_link_tuner(rt2x00dev);
  145. }
  146. static void rt2x00lib_precalculate_link_signal(struct link *link)
  147. {
  148. if (link->rx_failed || link->rx_success)
  149. link->rx_percentage =
  150. (link->rx_success * 100) /
  151. (link->rx_failed + link->rx_success);
  152. else
  153. link->rx_percentage = 50;
  154. if (link->tx_failed || link->tx_success)
  155. link->tx_percentage =
  156. (link->tx_success * 100) /
  157. (link->tx_failed + link->tx_success);
  158. else
  159. link->tx_percentage = 50;
  160. link->rx_success = 0;
  161. link->rx_failed = 0;
  162. link->tx_success = 0;
  163. link->tx_failed = 0;
  164. }
  165. static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
  166. int rssi)
  167. {
  168. int rssi_percentage = 0;
  169. int signal;
  170. /*
  171. * We need a positive value for the RSSI.
  172. */
  173. if (rssi < 0)
  174. rssi += rt2x00dev->rssi_offset;
  175. /*
  176. * Calculate the different percentages,
  177. * which will be used for the signal.
  178. */
  179. if (rt2x00dev->rssi_offset)
  180. rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
  181. /*
  182. * Add the individual percentages and use the WEIGHT
  183. * defines to calculate the current link signal.
  184. */
  185. signal = ((WEIGHT_RSSI * rssi_percentage) +
  186. (WEIGHT_TX * rt2x00dev->link.tx_percentage) +
  187. (WEIGHT_RX * rt2x00dev->link.rx_percentage)) / 100;
  188. return (signal > 100) ? 100 : signal;
  189. }
  190. static void rt2x00lib_link_tuner(struct work_struct *work)
  191. {
  192. struct rt2x00_dev *rt2x00dev =
  193. container_of(work, struct rt2x00_dev, link.work.work);
  194. /*
  195. * When the radio is shutting down we should
  196. * immediately cease all link tuning.
  197. */
  198. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  199. return;
  200. /*
  201. * Update statistics.
  202. */
  203. rt2x00dev->ops->lib->link_stats(rt2x00dev);
  204. rt2x00dev->low_level_stats.dot11FCSErrorCount +=
  205. rt2x00dev->link.rx_failed;
  206. /*
  207. * Only perform the link tuning when Link tuning
  208. * has been enabled (This could have been disabled from the EEPROM).
  209. */
  210. if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
  211. rt2x00dev->ops->lib->link_tuner(rt2x00dev);
  212. /*
  213. * Precalculate a portion of the link signal which is
  214. * in based on the tx/rx success/failure counters.
  215. */
  216. rt2x00lib_precalculate_link_signal(&rt2x00dev->link);
  217. /*
  218. * Increase tuner counter, and reschedule the next link tuner run.
  219. */
  220. rt2x00dev->link.count++;
  221. queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
  222. LINK_TUNE_INTERVAL);
  223. }
  224. static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
  225. {
  226. struct rt2x00_dev *rt2x00dev =
  227. container_of(work, struct rt2x00_dev, filter_work);
  228. unsigned int filter = rt2x00dev->interface.filter;
  229. /*
  230. * Since we had stored the filter inside interface.filter,
  231. * we should now clear that field. Otherwise the driver will
  232. * assume nothing has changed (*total_flags will be compared
  233. * to interface.filter to determine if any action is required).
  234. */
  235. rt2x00dev->interface.filter = 0;
  236. rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
  237. filter, &filter, 0, NULL);
  238. }
  239. /*
  240. * Interrupt context handlers.
  241. */
  242. static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
  243. {
  244. struct rt2x00_dev *rt2x00dev =
  245. container_of(work, struct rt2x00_dev, beacon_work);
  246. struct data_ring *ring =
  247. rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
  248. struct data_entry *entry = rt2x00_get_data_entry(ring);
  249. struct sk_buff *skb;
  250. skb = ieee80211_beacon_get(rt2x00dev->hw,
  251. rt2x00dev->interface.id,
  252. &entry->tx_status.control);
  253. if (!skb)
  254. return;
  255. rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
  256. &entry->tx_status.control);
  257. dev_kfree_skb(skb);
  258. }
  259. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  260. {
  261. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  262. return;
  263. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
  264. }
  265. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  266. void rt2x00lib_txdone(struct data_entry *entry,
  267. const int status, const int retry)
  268. {
  269. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  270. struct ieee80211_tx_status *tx_status = &entry->tx_status;
  271. struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
  272. int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
  273. int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
  274. status == TX_FAIL_OTHER);
  275. /*
  276. * Update TX statistics.
  277. */
  278. tx_status->flags = 0;
  279. tx_status->ack_signal = 0;
  280. tx_status->excessive_retries = (status == TX_FAIL_RETRY);
  281. tx_status->retry_count = retry;
  282. rt2x00dev->link.tx_success += success;
  283. rt2x00dev->link.tx_failed += retry + fail;
  284. if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
  285. if (success)
  286. tx_status->flags |= IEEE80211_TX_STATUS_ACK;
  287. else
  288. stats->dot11ACKFailureCount++;
  289. }
  290. tx_status->queue_length = entry->ring->stats.limit;
  291. tx_status->queue_number = tx_status->control.queue;
  292. if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  293. if (success)
  294. stats->dot11RTSSuccessCount++;
  295. else
  296. stats->dot11RTSFailureCount++;
  297. }
  298. /*
  299. * Send the tx_status to mac80211,
  300. * that method also cleans up the skb structure.
  301. */
  302. ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
  303. entry->skb = NULL;
  304. }
  305. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  306. void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
  307. struct rxdata_entry_desc *desc)
  308. {
  309. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  310. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  311. struct ieee80211_hw_mode *mode;
  312. struct ieee80211_rate *rate;
  313. unsigned int i;
  314. int val = 0;
  315. /*
  316. * Update RX statistics.
  317. */
  318. mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
  319. for (i = 0; i < mode->num_rates; i++) {
  320. rate = &mode->rates[i];
  321. /*
  322. * When frame was received with an OFDM bitrate,
  323. * the signal is the PLCP value. If it was received with
  324. * a CCK bitrate the signal is the rate in 0.5kbit/s.
  325. */
  326. if (!desc->ofdm)
  327. val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
  328. else
  329. val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
  330. if (val == desc->signal) {
  331. val = rate->val;
  332. break;
  333. }
  334. }
  335. rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
  336. rt2x00dev->link.rx_success++;
  337. rx_status->rate = val;
  338. rx_status->signal =
  339. rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
  340. rx_status->ssi = desc->rssi;
  341. rx_status->flag = desc->flags;
  342. /*
  343. * Send frame to mac80211
  344. */
  345. ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
  346. }
  347. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  348. /*
  349. * TX descriptor initializer
  350. */
  351. void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  352. struct data_desc *txd,
  353. struct ieee80211_hdr *ieee80211hdr,
  354. unsigned int length,
  355. struct ieee80211_tx_control *control)
  356. {
  357. struct txdata_entry_desc desc;
  358. struct data_ring *ring;
  359. int tx_rate;
  360. int bitrate;
  361. int duration;
  362. int residual;
  363. u16 frame_control;
  364. u16 seq_ctrl;
  365. /*
  366. * Make sure the descriptor is properly cleared.
  367. */
  368. memset(&desc, 0x00, sizeof(desc));
  369. /*
  370. * Get ring pointer, if we fail to obtain the
  371. * correct ring, then use the first TX ring.
  372. */
  373. ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
  374. if (!ring)
  375. ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
  376. desc.cw_min = ring->tx_params.cw_min;
  377. desc.cw_max = ring->tx_params.cw_max;
  378. desc.aifs = ring->tx_params.aifs;
  379. /*
  380. * Identify queue
  381. */
  382. if (control->queue < rt2x00dev->hw->queues)
  383. desc.queue = control->queue;
  384. else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  385. control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  386. desc.queue = QUEUE_MGMT;
  387. else
  388. desc.queue = QUEUE_OTHER;
  389. /*
  390. * Read required fields from ieee80211 header.
  391. */
  392. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  393. seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
  394. tx_rate = control->tx_rate;
  395. /*
  396. * Check if this is a RTS/CTS frame
  397. */
  398. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  399. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  400. if (is_rts_frame(frame_control))
  401. __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
  402. if (control->rts_cts_rate)
  403. tx_rate = control->rts_cts_rate;
  404. }
  405. /*
  406. * Check for OFDM
  407. */
  408. if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
  409. __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
  410. /*
  411. * Check if more fragments are pending
  412. */
  413. if (ieee80211_get_morefrag(ieee80211hdr)) {
  414. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  415. __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
  416. }
  417. /*
  418. * Beacons and probe responses require the tsf timestamp
  419. * to be inserted into the frame.
  420. */
  421. if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  422. is_probe_resp(frame_control))
  423. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
  424. /*
  425. * Determine with what IFS priority this frame should be send.
  426. * Set ifs to IFS_SIFS when the this is not the first fragment,
  427. * or this fragment came after RTS/CTS.
  428. */
  429. if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
  430. test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
  431. desc.ifs = IFS_SIFS;
  432. else
  433. desc.ifs = IFS_BACKOFF;
  434. /*
  435. * PLCP setup
  436. * Length calculation depends on OFDM/CCK rate.
  437. */
  438. desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
  439. desc.service = 0x04;
  440. if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
  441. desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
  442. desc.length_low = ((length + FCS_LEN) & 0x3f);
  443. } else {
  444. bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
  445. /*
  446. * Convert length to microseconds.
  447. */
  448. residual = get_duration_res(length + FCS_LEN, bitrate);
  449. duration = get_duration(length + FCS_LEN, bitrate);
  450. if (residual != 0) {
  451. duration++;
  452. /*
  453. * Check if we need to set the Length Extension
  454. */
  455. if (bitrate == 110 && residual <= 3)
  456. desc.service |= 0x80;
  457. }
  458. desc.length_high = (duration >> 8) & 0xff;
  459. desc.length_low = duration & 0xff;
  460. /*
  461. * When preamble is enabled we should set the
  462. * preamble bit for the signal.
  463. */
  464. if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
  465. desc.signal |= 0x08;
  466. }
  467. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
  468. ieee80211hdr, length, control);
  469. }
  470. EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
  471. /*
  472. * Driver initialization handlers.
  473. */
  474. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  475. const int channel, const int tx_power,
  476. const int value)
  477. {
  478. entry->chan = channel;
  479. if (channel <= 14)
  480. entry->freq = 2407 + (5 * channel);
  481. else
  482. entry->freq = 5000 + (5 * channel);
  483. entry->val = value;
  484. entry->flag =
  485. IEEE80211_CHAN_W_IBSS |
  486. IEEE80211_CHAN_W_ACTIVE_SCAN |
  487. IEEE80211_CHAN_W_SCAN;
  488. entry->power_level = tx_power;
  489. entry->antenna_max = 0xff;
  490. }
  491. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  492. const int rate, const int mask,
  493. const int plcp, const int flags)
  494. {
  495. entry->rate = rate;
  496. entry->val =
  497. DEVICE_SET_RATE_FIELD(rate, RATE) |
  498. DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
  499. DEVICE_SET_RATE_FIELD(plcp, PLCP);
  500. entry->flags = flags;
  501. entry->val2 = entry->val;
  502. if (entry->flags & IEEE80211_RATE_PREAMBLE2)
  503. entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
  504. entry->min_rssi_ack = 0;
  505. entry->min_rssi_ack_delta = 0;
  506. }
  507. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  508. struct hw_mode_spec *spec)
  509. {
  510. struct ieee80211_hw *hw = rt2x00dev->hw;
  511. struct ieee80211_hw_mode *hwmodes;
  512. struct ieee80211_channel *channels;
  513. struct ieee80211_rate *rates;
  514. unsigned int i;
  515. unsigned char tx_power;
  516. hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
  517. if (!hwmodes)
  518. goto exit;
  519. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  520. if (!channels)
  521. goto exit_free_modes;
  522. rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
  523. if (!rates)
  524. goto exit_free_channels;
  525. /*
  526. * Initialize Rate list.
  527. */
  528. rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
  529. 0x00, IEEE80211_RATE_CCK);
  530. rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
  531. 0x01, IEEE80211_RATE_CCK_2);
  532. rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
  533. 0x02, IEEE80211_RATE_CCK_2);
  534. rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
  535. 0x03, IEEE80211_RATE_CCK_2);
  536. if (spec->num_rates > 4) {
  537. rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
  538. 0x0b, IEEE80211_RATE_OFDM);
  539. rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
  540. 0x0f, IEEE80211_RATE_OFDM);
  541. rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
  542. 0x0a, IEEE80211_RATE_OFDM);
  543. rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
  544. 0x0e, IEEE80211_RATE_OFDM);
  545. rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
  546. 0x09, IEEE80211_RATE_OFDM);
  547. rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
  548. 0x0d, IEEE80211_RATE_OFDM);
  549. rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
  550. 0x08, IEEE80211_RATE_OFDM);
  551. rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
  552. 0x0c, IEEE80211_RATE_OFDM);
  553. }
  554. /*
  555. * Initialize Channel list.
  556. */
  557. for (i = 0; i < spec->num_channels; i++) {
  558. if (spec->channels[i].channel <= 14)
  559. tx_power = spec->tx_power_bg[i];
  560. else if (spec->tx_power_a)
  561. tx_power = spec->tx_power_a[i];
  562. else
  563. tx_power = spec->tx_power_default;
  564. rt2x00lib_channel(&channels[i],
  565. spec->channels[i].channel, tx_power, i);
  566. }
  567. /*
  568. * Intitialize 802.11b
  569. * Rates: CCK.
  570. * Channels: OFDM.
  571. */
  572. if (spec->num_modes > HWMODE_B) {
  573. hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
  574. hwmodes[HWMODE_B].num_channels = 14;
  575. hwmodes[HWMODE_B].num_rates = 4;
  576. hwmodes[HWMODE_B].channels = channels;
  577. hwmodes[HWMODE_B].rates = rates;
  578. }
  579. /*
  580. * Intitialize 802.11g
  581. * Rates: CCK, OFDM.
  582. * Channels: OFDM.
  583. */
  584. if (spec->num_modes > HWMODE_G) {
  585. hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
  586. hwmodes[HWMODE_G].num_channels = 14;
  587. hwmodes[HWMODE_G].num_rates = spec->num_rates;
  588. hwmodes[HWMODE_G].channels = channels;
  589. hwmodes[HWMODE_G].rates = rates;
  590. }
  591. /*
  592. * Intitialize 802.11a
  593. * Rates: OFDM.
  594. * Channels: OFDM, UNII, HiperLAN2.
  595. */
  596. if (spec->num_modes > HWMODE_A) {
  597. hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
  598. hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
  599. hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
  600. hwmodes[HWMODE_A].channels = &channels[14];
  601. hwmodes[HWMODE_A].rates = &rates[4];
  602. }
  603. if (spec->num_modes > HWMODE_G &&
  604. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
  605. goto exit_free_rates;
  606. if (spec->num_modes > HWMODE_B &&
  607. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
  608. goto exit_free_rates;
  609. if (spec->num_modes > HWMODE_A &&
  610. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
  611. goto exit_free_rates;
  612. rt2x00dev->hwmodes = hwmodes;
  613. return 0;
  614. exit_free_rates:
  615. kfree(rates);
  616. exit_free_channels:
  617. kfree(channels);
  618. exit_free_modes:
  619. kfree(hwmodes);
  620. exit:
  621. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  622. return -ENOMEM;
  623. }
  624. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  625. {
  626. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  627. ieee80211_unregister_hw(rt2x00dev->hw);
  628. if (likely(rt2x00dev->hwmodes)) {
  629. kfree(rt2x00dev->hwmodes->channels);
  630. kfree(rt2x00dev->hwmodes->rates);
  631. kfree(rt2x00dev->hwmodes);
  632. rt2x00dev->hwmodes = NULL;
  633. }
  634. }
  635. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  636. {
  637. struct hw_mode_spec *spec = &rt2x00dev->spec;
  638. int status;
  639. /*
  640. * Initialize HW modes.
  641. */
  642. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  643. if (status)
  644. return status;
  645. /*
  646. * Register HW.
  647. */
  648. status = ieee80211_register_hw(rt2x00dev->hw);
  649. if (status) {
  650. rt2x00lib_remove_hw(rt2x00dev);
  651. return status;
  652. }
  653. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  654. return 0;
  655. }
  656. /*
  657. * Initialization/uninitialization handlers.
  658. */
  659. static int rt2x00lib_alloc_entries(struct data_ring *ring,
  660. const u16 max_entries, const u16 data_size,
  661. const u16 desc_size)
  662. {
  663. struct data_entry *entry;
  664. unsigned int i;
  665. ring->stats.limit = max_entries;
  666. ring->data_size = data_size;
  667. ring->desc_size = desc_size;
  668. /*
  669. * Allocate all ring entries.
  670. */
  671. entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
  672. if (!entry)
  673. return -ENOMEM;
  674. for (i = 0; i < ring->stats.limit; i++) {
  675. entry[i].flags = 0;
  676. entry[i].ring = ring;
  677. entry[i].skb = NULL;
  678. }
  679. ring->entry = entry;
  680. return 0;
  681. }
  682. static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
  683. {
  684. struct data_ring *ring;
  685. /*
  686. * Allocate the RX ring.
  687. */
  688. if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
  689. rt2x00dev->ops->rxd_size))
  690. return -ENOMEM;
  691. /*
  692. * First allocate the TX rings.
  693. */
  694. txring_for_each(rt2x00dev, ring) {
  695. if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
  696. rt2x00dev->ops->txd_size))
  697. return -ENOMEM;
  698. }
  699. if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  700. return 0;
  701. /*
  702. * Allocate the BEACON ring.
  703. */
  704. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
  705. MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
  706. return -ENOMEM;
  707. /*
  708. * Allocate the Atim ring.
  709. */
  710. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
  711. DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
  712. return -ENOMEM;
  713. return 0;
  714. }
  715. static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
  716. {
  717. struct data_ring *ring;
  718. ring_for_each(rt2x00dev, ring) {
  719. kfree(ring->entry);
  720. ring->entry = NULL;
  721. }
  722. }
  723. void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  724. {
  725. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  726. return;
  727. /*
  728. * Unregister rfkill.
  729. */
  730. rt2x00rfkill_unregister(rt2x00dev);
  731. /*
  732. * Allow the HW to uninitialize.
  733. */
  734. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  735. /*
  736. * Free allocated ring entries.
  737. */
  738. rt2x00lib_free_ring_entries(rt2x00dev);
  739. }
  740. int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  741. {
  742. int status;
  743. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  744. return 0;
  745. /*
  746. * Allocate all ring entries.
  747. */
  748. status = rt2x00lib_alloc_ring_entries(rt2x00dev);
  749. if (status) {
  750. ERROR(rt2x00dev, "Ring entries allocation failed.\n");
  751. return status;
  752. }
  753. /*
  754. * Initialize the device.
  755. */
  756. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  757. if (status)
  758. goto exit;
  759. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  760. /*
  761. * Register the rfkill handler.
  762. */
  763. status = rt2x00rfkill_register(rt2x00dev);
  764. if (status)
  765. goto exit_unitialize;
  766. return 0;
  767. exit_unitialize:
  768. rt2x00lib_uninitialize(rt2x00dev);
  769. exit:
  770. rt2x00lib_free_ring_entries(rt2x00dev);
  771. return status;
  772. }
  773. /*
  774. * driver allocation handlers.
  775. */
  776. static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
  777. {
  778. struct data_ring *ring;
  779. /*
  780. * We need the following rings:
  781. * RX: 1
  782. * TX: hw->queues
  783. * Beacon: 1 (if required)
  784. * Atim: 1 (if required)
  785. */
  786. rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
  787. (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
  788. ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
  789. if (!ring) {
  790. ERROR(rt2x00dev, "Ring allocation failed.\n");
  791. return -ENOMEM;
  792. }
  793. /*
  794. * Initialize pointers
  795. */
  796. rt2x00dev->rx = ring;
  797. rt2x00dev->tx = &rt2x00dev->rx[1];
  798. if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  799. rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
  800. /*
  801. * Initialize ring parameters.
  802. * cw_min: 2^5 = 32.
  803. * cw_max: 2^10 = 1024.
  804. */
  805. ring_for_each(rt2x00dev, ring) {
  806. ring->rt2x00dev = rt2x00dev;
  807. ring->tx_params.aifs = 2;
  808. ring->tx_params.cw_min = 5;
  809. ring->tx_params.cw_max = 10;
  810. }
  811. return 0;
  812. }
  813. static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
  814. {
  815. kfree(rt2x00dev->rx);
  816. rt2x00dev->rx = NULL;
  817. rt2x00dev->tx = NULL;
  818. rt2x00dev->bcn = NULL;
  819. }
  820. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  821. {
  822. int retval = -ENOMEM;
  823. /*
  824. * Let the driver probe the device to detect the capabilities.
  825. */
  826. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  827. if (retval) {
  828. ERROR(rt2x00dev, "Failed to allocate device.\n");
  829. goto exit;
  830. }
  831. /*
  832. * Initialize configuration work.
  833. */
  834. INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
  835. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  836. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  837. /*
  838. * Reset current working type.
  839. */
  840. rt2x00dev->interface.type = INVALID_INTERFACE;
  841. /*
  842. * Allocate ring array.
  843. */
  844. retval = rt2x00lib_alloc_rings(rt2x00dev);
  845. if (retval)
  846. goto exit;
  847. /*
  848. * Initialize ieee80211 structure.
  849. */
  850. retval = rt2x00lib_probe_hw(rt2x00dev);
  851. if (retval) {
  852. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  853. goto exit;
  854. }
  855. /*
  856. * Allocatie rfkill.
  857. */
  858. retval = rt2x00rfkill_allocate(rt2x00dev);
  859. if (retval)
  860. goto exit;
  861. /*
  862. * Open the debugfs entry.
  863. */
  864. rt2x00debug_register(rt2x00dev);
  865. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  866. return 0;
  867. exit:
  868. rt2x00lib_remove_dev(rt2x00dev);
  869. return retval;
  870. }
  871. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  872. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  873. {
  874. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  875. /*
  876. * Disable radio.
  877. */
  878. rt2x00lib_disable_radio(rt2x00dev);
  879. /*
  880. * Uninitialize device.
  881. */
  882. rt2x00lib_uninitialize(rt2x00dev);
  883. /*
  884. * Close debugfs entry.
  885. */
  886. rt2x00debug_deregister(rt2x00dev);
  887. /*
  888. * Free rfkill
  889. */
  890. rt2x00rfkill_free(rt2x00dev);
  891. /*
  892. * Free ieee80211_hw memory.
  893. */
  894. rt2x00lib_remove_hw(rt2x00dev);
  895. /*
  896. * Free firmware image.
  897. */
  898. rt2x00lib_free_firmware(rt2x00dev);
  899. /*
  900. * Free ring structures.
  901. */
  902. rt2x00lib_free_rings(rt2x00dev);
  903. }
  904. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  905. /*
  906. * Device state handlers
  907. */
  908. #ifdef CONFIG_PM
  909. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  910. {
  911. int retval;
  912. NOTICE(rt2x00dev, "Going to sleep.\n");
  913. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  914. /*
  915. * Only continue if mac80211 has open interfaces.
  916. */
  917. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  918. goto exit;
  919. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  920. /*
  921. * Disable radio and unitialize all items
  922. * that must be recreated on resume.
  923. */
  924. rt2x00mac_stop(rt2x00dev->hw);
  925. rt2x00lib_uninitialize(rt2x00dev);
  926. rt2x00debug_deregister(rt2x00dev);
  927. exit:
  928. /*
  929. * Set device mode to sleep for power management.
  930. */
  931. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  932. if (retval)
  933. return retval;
  934. return 0;
  935. }
  936. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  937. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  938. {
  939. struct interface *intf = &rt2x00dev->interface;
  940. int retval;
  941. NOTICE(rt2x00dev, "Waking up.\n");
  942. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  943. /*
  944. * Open the debugfs entry.
  945. */
  946. rt2x00debug_register(rt2x00dev);
  947. /*
  948. * Only continue if mac80211 had open interfaces.
  949. */
  950. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  951. return 0;
  952. /*
  953. * Reinitialize device and all active interfaces.
  954. */
  955. retval = rt2x00mac_start(rt2x00dev->hw);
  956. if (retval)
  957. goto exit;
  958. /*
  959. * Reconfigure device.
  960. */
  961. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  962. if (!rt2x00dev->hw->conf.radio_enabled)
  963. rt2x00lib_disable_radio(rt2x00dev);
  964. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  965. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  966. rt2x00lib_config_type(rt2x00dev, intf->type);
  967. /*
  968. * It is possible that during that mac80211 has attempted
  969. * to send frames while we were suspending or resuming.
  970. * In that case we have disabled the TX queue and should
  971. * now enable it again
  972. */
  973. ieee80211_start_queues(rt2x00dev->hw);
  974. /*
  975. * When in Master or Ad-hoc mode,
  976. * restart Beacon transmitting by faking a beacondone event.
  977. */
  978. if (intf->type == IEEE80211_IF_TYPE_AP ||
  979. intf->type == IEEE80211_IF_TYPE_IBSS)
  980. rt2x00lib_beacondone(rt2x00dev);
  981. return 0;
  982. exit:
  983. rt2x00lib_disable_radio(rt2x00dev);
  984. rt2x00lib_uninitialize(rt2x00dev);
  985. rt2x00debug_deregister(rt2x00dev);
  986. return retval;
  987. }
  988. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  989. #endif /* CONFIG_PM */
  990. /*
  991. * rt2x00lib module information.
  992. */
  993. MODULE_AUTHOR(DRV_PROJECT);
  994. MODULE_VERSION(DRV_VERSION);
  995. MODULE_DESCRIPTION("rt2x00 library");
  996. MODULE_LICENSE("GPL");