rt2x00dev.c 27 KB

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