rt2x00dev.c 28 KB

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