rt2x00dev.c 26 KB

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