rt2x00dev.c 27 KB

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