rt2x00dev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. Copyright (C) 2004 - 2009 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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. /*
  26. * Radio control handlers.
  27. */
  28. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  29. {
  30. int status;
  31. /*
  32. * Don't enable the radio twice.
  33. * And check if the hardware button has been disabled.
  34. */
  35. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
  36. test_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags))
  37. return 0;
  38. /*
  39. * Initialize all data queues.
  40. */
  41. rt2x00queue_init_queues(rt2x00dev);
  42. /*
  43. * Enable radio.
  44. */
  45. status =
  46. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
  47. if (status)
  48. return status;
  49. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
  50. rt2x00leds_led_radio(rt2x00dev, true);
  51. rt2x00led_led_activity(rt2x00dev, true);
  52. set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
  53. /*
  54. * Enable RX.
  55. */
  56. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  57. /*
  58. * Start the TX queues.
  59. */
  60. ieee80211_wake_queues(rt2x00dev->hw);
  61. return 0;
  62. }
  63. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  64. {
  65. if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  66. return;
  67. /*
  68. * Stop the TX queues in mac80211.
  69. */
  70. ieee80211_stop_queues(rt2x00dev->hw);
  71. rt2x00queue_stop_queues(rt2x00dev);
  72. /*
  73. * Disable RX.
  74. */
  75. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  76. /*
  77. * Disable radio.
  78. */
  79. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  80. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  81. rt2x00led_led_activity(rt2x00dev, false);
  82. rt2x00leds_led_radio(rt2x00dev, false);
  83. }
  84. void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  85. {
  86. /*
  87. * When we are disabling the RX, we should also stop the link tuner.
  88. */
  89. if (state == STATE_RADIO_RX_OFF)
  90. rt2x00link_stop_tuner(rt2x00dev);
  91. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  92. /*
  93. * When we are enabling the RX, we should also start the link tuner.
  94. */
  95. if (state == STATE_RADIO_RX_ON)
  96. rt2x00link_start_tuner(rt2x00dev);
  97. }
  98. static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
  99. {
  100. struct rt2x00_dev *rt2x00dev =
  101. container_of(work, struct rt2x00_dev, filter_work);
  102. rt2x00dev->ops->lib->config_filter(rt2x00dev, rt2x00dev->packet_filter);
  103. }
  104. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  105. struct ieee80211_vif *vif)
  106. {
  107. struct rt2x00_dev *rt2x00dev = data;
  108. struct rt2x00_intf *intf = vif_to_intf(vif);
  109. struct ieee80211_bss_conf conf;
  110. int delayed_flags;
  111. /*
  112. * Copy all data we need during this action under the protection
  113. * of a spinlock. Otherwise race conditions might occur which results
  114. * into an invalid configuration.
  115. */
  116. spin_lock(&intf->lock);
  117. memcpy(&conf, &vif->bss_conf, sizeof(conf));
  118. delayed_flags = intf->delayed_flags;
  119. intf->delayed_flags = 0;
  120. spin_unlock(&intf->lock);
  121. /*
  122. * It is possible the radio was disabled while the work had been
  123. * scheduled. If that happens we should return here immediately,
  124. * note that in the spinlock protected area above the delayed_flags
  125. * have been cleared correctly.
  126. */
  127. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  128. return;
  129. if (delayed_flags & DELAYED_UPDATE_BEACON)
  130. rt2x00queue_update_beacon(rt2x00dev, vif, true);
  131. if (delayed_flags & DELAYED_CONFIG_ERP)
  132. rt2x00lib_config_erp(rt2x00dev, intf, &conf);
  133. if (delayed_flags & DELAYED_LED_ASSOC)
  134. rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
  135. }
  136. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  137. {
  138. struct rt2x00_dev *rt2x00dev =
  139. container_of(work, struct rt2x00_dev, intf_work);
  140. /*
  141. * Iterate over each interface and perform the
  142. * requested configurations.
  143. */
  144. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  145. rt2x00lib_intf_scheduled_iter,
  146. rt2x00dev);
  147. }
  148. /*
  149. * Interrupt context handlers.
  150. */
  151. static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
  152. struct ieee80211_vif *vif)
  153. {
  154. struct rt2x00_dev *rt2x00dev = data;
  155. struct rt2x00_intf *intf = vif_to_intf(vif);
  156. if (vif->type != NL80211_IFTYPE_AP &&
  157. vif->type != NL80211_IFTYPE_ADHOC &&
  158. vif->type != NL80211_IFTYPE_MESH_POINT &&
  159. vif->type != NL80211_IFTYPE_WDS)
  160. return;
  161. /*
  162. * Clean up the beacon skb.
  163. */
  164. rt2x00queue_free_skb(rt2x00dev, intf->beacon->skb);
  165. intf->beacon->skb = NULL;
  166. spin_lock(&intf->lock);
  167. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  168. spin_unlock(&intf->lock);
  169. }
  170. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  171. {
  172. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  173. return;
  174. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  175. rt2x00lib_beacondone_iter,
  176. rt2x00dev);
  177. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  178. }
  179. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  180. void rt2x00lib_txdone(struct queue_entry *entry,
  181. struct txdone_entry_desc *txdesc)
  182. {
  183. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  184. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  185. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  186. enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
  187. unsigned int header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  188. u8 rate_idx, rate_flags;
  189. /*
  190. * Unmap the skb.
  191. */
  192. rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
  193. /*
  194. * Remove L2 padding which was added during
  195. */
  196. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  197. rt2x00queue_payload_align(entry->skb, true, header_length);
  198. /*
  199. * If the IV/EIV data was stripped from the frame before it was
  200. * passed to the hardware, we should now reinsert it again because
  201. * mac80211 will expect the the same data to be present it the
  202. * frame as it was passed to us.
  203. */
  204. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
  205. rt2x00crypto_tx_insert_iv(entry->skb, header_length);
  206. /*
  207. * Send frame to debugfs immediately, after this call is completed
  208. * we are going to overwrite the skb->cb array.
  209. */
  210. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
  211. /*
  212. * Update TX statistics.
  213. */
  214. rt2x00dev->link.qual.tx_success +=
  215. test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  216. test_bit(TXDONE_UNKNOWN, &txdesc->flags);
  217. rt2x00dev->link.qual.tx_failed +=
  218. test_bit(TXDONE_FAILURE, &txdesc->flags);
  219. rate_idx = skbdesc->tx_rate_idx;
  220. rate_flags = skbdesc->tx_rate_flags;
  221. /*
  222. * Initialize TX status
  223. */
  224. memset(&tx_info->status, 0, sizeof(tx_info->status));
  225. tx_info->status.ack_signal = 0;
  226. tx_info->status.rates[0].idx = rate_idx;
  227. tx_info->status.rates[0].flags = rate_flags;
  228. tx_info->status.rates[0].count = txdesc->retry + 1;
  229. tx_info->status.rates[1].idx = -1; /* terminate */
  230. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  231. if (test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  232. test_bit(TXDONE_UNKNOWN, &txdesc->flags))
  233. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  234. else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
  235. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  236. }
  237. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  238. if (test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  239. test_bit(TXDONE_UNKNOWN, &txdesc->flags))
  240. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  241. else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
  242. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  243. }
  244. /*
  245. * Only send the status report to mac80211 when TX status was
  246. * requested by it. If this was a extra frame coming through
  247. * a mac80211 library call (RTS/CTS) then we should not send the
  248. * status report back.
  249. */
  250. if (tx_info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  251. ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb);
  252. else
  253. dev_kfree_skb_irq(entry->skb);
  254. /*
  255. * Make this entry available for reuse.
  256. */
  257. entry->skb = NULL;
  258. entry->flags = 0;
  259. rt2x00dev->ops->lib->clear_entry(entry);
  260. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  261. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  262. /*
  263. * If the data queue was below the threshold before the txdone
  264. * handler we must make sure the packet queue in the mac80211 stack
  265. * is reenabled when the txdone handler has finished.
  266. */
  267. if (!rt2x00queue_threshold(entry->queue))
  268. ieee80211_wake_queue(rt2x00dev->hw, qid);
  269. }
  270. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  271. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  272. struct rxdone_entry_desc *rxdesc)
  273. {
  274. struct ieee80211_supported_band *sband;
  275. const struct rt2x00_rate *rate;
  276. unsigned int i;
  277. int signal;
  278. int type;
  279. /*
  280. * For non-HT rates the MCS value needs to contain the
  281. * actually used rate modulation (CCK or OFDM).
  282. */
  283. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  284. signal = RATE_MCS(rxdesc->rate_mode, rxdesc->signal);
  285. else
  286. signal = rxdesc->signal;
  287. type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  288. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  289. for (i = 0; i < sband->n_bitrates; i++) {
  290. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  291. if (((type == RXDONE_SIGNAL_PLCP) &&
  292. (rate->plcp == signal)) ||
  293. ((type == RXDONE_SIGNAL_BITRATE) &&
  294. (rate->bitrate == signal)) ||
  295. ((type == RXDONE_SIGNAL_MCS) &&
  296. (rate->mcs == signal))) {
  297. return i;
  298. }
  299. }
  300. WARNING(rt2x00dev, "Frame received with unrecognized signal, "
  301. "signal=0x%.4x, type=%d.\n", signal, type);
  302. return 0;
  303. }
  304. void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev,
  305. struct queue_entry *entry)
  306. {
  307. struct rxdone_entry_desc rxdesc;
  308. struct sk_buff *skb;
  309. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  310. unsigned int header_length;
  311. bool l2pad;
  312. int rate_idx;
  313. /*
  314. * Allocate a new sk_buffer. If no new buffer available, drop the
  315. * received frame and reuse the existing buffer.
  316. */
  317. skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry);
  318. if (!skb)
  319. return;
  320. /*
  321. * Unmap the skb.
  322. */
  323. rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
  324. /*
  325. * Extract the RXD details.
  326. */
  327. memset(&rxdesc, 0, sizeof(rxdesc));
  328. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  329. /* Trim buffer to correct size */
  330. skb_trim(entry->skb, rxdesc.size);
  331. /*
  332. * The data behind the ieee80211 header must be
  333. * aligned on a 4 byte boundary.
  334. */
  335. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  336. l2pad = !!(rxdesc.dev_flags & RXDONE_L2PAD);
  337. /*
  338. * Hardware might have stripped the IV/EIV/ICV data,
  339. * in that case it is possible that the data was
  340. * provided seperately (through hardware descriptor)
  341. * in which case we should reinsert the data into the frame.
  342. */
  343. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  344. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  345. rt2x00crypto_rx_insert_iv(entry->skb, l2pad, header_length,
  346. &rxdesc);
  347. else
  348. rt2x00queue_payload_align(entry->skb, l2pad, header_length);
  349. /*
  350. * Check if the frame was received using HT. In that case,
  351. * the rate is the MCS index and should be passed to mac80211
  352. * directly. Otherwise we need to translate the signal to
  353. * the correct bitrate index.
  354. */
  355. if (rxdesc.rate_mode == RATE_MODE_CCK ||
  356. rxdesc.rate_mode == RATE_MODE_OFDM) {
  357. rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
  358. } else {
  359. rxdesc.flags |= RX_FLAG_HT;
  360. rate_idx = rxdesc.signal;
  361. }
  362. /*
  363. * Update extra components
  364. */
  365. rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
  366. rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
  367. rx_status->mactime = rxdesc.timestamp;
  368. rx_status->rate_idx = rate_idx;
  369. rx_status->qual = rt2x00link_calculate_signal(rt2x00dev, rxdesc.rssi);
  370. rx_status->signal = rxdesc.rssi;
  371. rx_status->noise = rxdesc.noise;
  372. rx_status->flag = rxdesc.flags;
  373. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  374. /*
  375. * Send frame to mac80211 & debugfs.
  376. * mac80211 will clean up the skb structure.
  377. */
  378. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  379. memcpy(IEEE80211_SKB_RXCB(entry->skb), rx_status, sizeof(*rx_status));
  380. ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb);
  381. /*
  382. * Replace the skb with the freshly allocated one.
  383. */
  384. entry->skb = skb;
  385. entry->flags = 0;
  386. rt2x00dev->ops->lib->clear_entry(entry);
  387. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  388. }
  389. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  390. /*
  391. * Driver initialization handlers.
  392. */
  393. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  394. {
  395. .flags = DEV_RATE_CCK,
  396. .bitrate = 10,
  397. .ratemask = BIT(0),
  398. .plcp = 0x00,
  399. .mcs = RATE_MCS(RATE_MODE_CCK, 0),
  400. },
  401. {
  402. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  403. .bitrate = 20,
  404. .ratemask = BIT(1),
  405. .plcp = 0x01,
  406. .mcs = RATE_MCS(RATE_MODE_CCK, 1),
  407. },
  408. {
  409. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  410. .bitrate = 55,
  411. .ratemask = BIT(2),
  412. .plcp = 0x02,
  413. .mcs = RATE_MCS(RATE_MODE_CCK, 2),
  414. },
  415. {
  416. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  417. .bitrate = 110,
  418. .ratemask = BIT(3),
  419. .plcp = 0x03,
  420. .mcs = RATE_MCS(RATE_MODE_CCK, 3),
  421. },
  422. {
  423. .flags = DEV_RATE_OFDM,
  424. .bitrate = 60,
  425. .ratemask = BIT(4),
  426. .plcp = 0x0b,
  427. .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
  428. },
  429. {
  430. .flags = DEV_RATE_OFDM,
  431. .bitrate = 90,
  432. .ratemask = BIT(5),
  433. .plcp = 0x0f,
  434. .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
  435. },
  436. {
  437. .flags = DEV_RATE_OFDM,
  438. .bitrate = 120,
  439. .ratemask = BIT(6),
  440. .plcp = 0x0a,
  441. .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
  442. },
  443. {
  444. .flags = DEV_RATE_OFDM,
  445. .bitrate = 180,
  446. .ratemask = BIT(7),
  447. .plcp = 0x0e,
  448. .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
  449. },
  450. {
  451. .flags = DEV_RATE_OFDM,
  452. .bitrate = 240,
  453. .ratemask = BIT(8),
  454. .plcp = 0x09,
  455. .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
  456. },
  457. {
  458. .flags = DEV_RATE_OFDM,
  459. .bitrate = 360,
  460. .ratemask = BIT(9),
  461. .plcp = 0x0d,
  462. .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
  463. },
  464. {
  465. .flags = DEV_RATE_OFDM,
  466. .bitrate = 480,
  467. .ratemask = BIT(10),
  468. .plcp = 0x08,
  469. .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
  470. },
  471. {
  472. .flags = DEV_RATE_OFDM,
  473. .bitrate = 540,
  474. .ratemask = BIT(11),
  475. .plcp = 0x0c,
  476. .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
  477. },
  478. };
  479. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  480. const int channel, const int tx_power,
  481. const int value)
  482. {
  483. entry->center_freq = ieee80211_channel_to_frequency(channel);
  484. entry->hw_value = value;
  485. entry->max_power = tx_power;
  486. entry->max_antenna_gain = 0xff;
  487. }
  488. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  489. const u16 index, const struct rt2x00_rate *rate)
  490. {
  491. entry->flags = 0;
  492. entry->bitrate = rate->bitrate;
  493. entry->hw_value =index;
  494. entry->hw_value_short = index;
  495. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  496. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  497. }
  498. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  499. struct hw_mode_spec *spec)
  500. {
  501. struct ieee80211_hw *hw = rt2x00dev->hw;
  502. struct ieee80211_channel *channels;
  503. struct ieee80211_rate *rates;
  504. unsigned int num_rates;
  505. unsigned int i;
  506. num_rates = 0;
  507. if (spec->supported_rates & SUPPORT_RATE_CCK)
  508. num_rates += 4;
  509. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  510. num_rates += 8;
  511. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  512. if (!channels)
  513. return -ENOMEM;
  514. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  515. if (!rates)
  516. goto exit_free_channels;
  517. /*
  518. * Initialize Rate list.
  519. */
  520. for (i = 0; i < num_rates; i++)
  521. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  522. /*
  523. * Initialize Channel list.
  524. */
  525. for (i = 0; i < spec->num_channels; i++) {
  526. rt2x00lib_channel(&channels[i],
  527. spec->channels[i].channel,
  528. spec->channels_info[i].tx_power1, i);
  529. }
  530. /*
  531. * Intitialize 802.11b, 802.11g
  532. * Rates: CCK, OFDM.
  533. * Channels: 2.4 GHz
  534. */
  535. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  536. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  537. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  538. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  539. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  540. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  541. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  542. memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
  543. &spec->ht, sizeof(spec->ht));
  544. }
  545. /*
  546. * Intitialize 802.11a
  547. * Rates: OFDM.
  548. * Channels: OFDM, UNII, HiperLAN2.
  549. */
  550. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  551. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  552. spec->num_channels - 14;
  553. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  554. num_rates - 4;
  555. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  556. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  557. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  558. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  559. memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
  560. &spec->ht, sizeof(spec->ht));
  561. }
  562. return 0;
  563. exit_free_channels:
  564. kfree(channels);
  565. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  566. return -ENOMEM;
  567. }
  568. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  569. {
  570. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  571. ieee80211_unregister_hw(rt2x00dev->hw);
  572. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  573. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  574. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  575. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  576. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  577. }
  578. kfree(rt2x00dev->spec.channels_info);
  579. }
  580. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  581. {
  582. struct hw_mode_spec *spec = &rt2x00dev->spec;
  583. int status;
  584. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  585. return 0;
  586. /*
  587. * Initialize HW modes.
  588. */
  589. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  590. if (status)
  591. return status;
  592. /*
  593. * Initialize HW fields.
  594. */
  595. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  596. /*
  597. * Register HW.
  598. */
  599. status = ieee80211_register_hw(rt2x00dev->hw);
  600. if (status)
  601. return status;
  602. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  603. return 0;
  604. }
  605. /*
  606. * Initialization/uninitialization handlers.
  607. */
  608. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  609. {
  610. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  611. return;
  612. /*
  613. * Unregister extra components.
  614. */
  615. rt2x00rfkill_unregister(rt2x00dev);
  616. /*
  617. * Allow the HW to uninitialize.
  618. */
  619. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  620. /*
  621. * Free allocated queue entries.
  622. */
  623. rt2x00queue_uninitialize(rt2x00dev);
  624. }
  625. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  626. {
  627. int status;
  628. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  629. return 0;
  630. /*
  631. * Allocate all queue entries.
  632. */
  633. status = rt2x00queue_initialize(rt2x00dev);
  634. if (status)
  635. return status;
  636. /*
  637. * Initialize the device.
  638. */
  639. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  640. if (status) {
  641. rt2x00queue_uninitialize(rt2x00dev);
  642. return status;
  643. }
  644. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  645. /*
  646. * Register the extra components.
  647. */
  648. rt2x00rfkill_register(rt2x00dev);
  649. return 0;
  650. }
  651. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  652. {
  653. int retval;
  654. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  655. return 0;
  656. /*
  657. * If this is the first interface which is added,
  658. * we should load the firmware now.
  659. */
  660. retval = rt2x00lib_load_firmware(rt2x00dev);
  661. if (retval)
  662. return retval;
  663. /*
  664. * Initialize the device.
  665. */
  666. retval = rt2x00lib_initialize(rt2x00dev);
  667. if (retval)
  668. return retval;
  669. rt2x00dev->intf_ap_count = 0;
  670. rt2x00dev->intf_sta_count = 0;
  671. rt2x00dev->intf_associated = 0;
  672. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  673. return 0;
  674. }
  675. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  676. {
  677. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  678. return;
  679. /*
  680. * Perhaps we can add something smarter here,
  681. * but for now just disabling the radio should do.
  682. */
  683. rt2x00lib_disable_radio(rt2x00dev);
  684. rt2x00dev->intf_ap_count = 0;
  685. rt2x00dev->intf_sta_count = 0;
  686. rt2x00dev->intf_associated = 0;
  687. }
  688. /*
  689. * driver allocation handlers.
  690. */
  691. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  692. {
  693. int retval = -ENOMEM;
  694. mutex_init(&rt2x00dev->csr_mutex);
  695. /*
  696. * Make room for rt2x00_intf inside the per-interface
  697. * structure ieee80211_vif.
  698. */
  699. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  700. /*
  701. * Determine which operating modes are supported, all modes
  702. * which require beaconing, depend on the availability of
  703. * beacon entries.
  704. */
  705. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  706. if (rt2x00dev->ops->bcn->entry_num > 0)
  707. rt2x00dev->hw->wiphy->interface_modes |=
  708. BIT(NL80211_IFTYPE_ADHOC) |
  709. BIT(NL80211_IFTYPE_AP) |
  710. BIT(NL80211_IFTYPE_MESH_POINT) |
  711. BIT(NL80211_IFTYPE_WDS);
  712. /*
  713. * Let the driver probe the device to detect the capabilities.
  714. */
  715. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  716. if (retval) {
  717. ERROR(rt2x00dev, "Failed to allocate device.\n");
  718. goto exit;
  719. }
  720. /*
  721. * Initialize configuration work.
  722. */
  723. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  724. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  725. /*
  726. * Allocate queue array.
  727. */
  728. retval = rt2x00queue_allocate(rt2x00dev);
  729. if (retval)
  730. goto exit;
  731. /*
  732. * Initialize ieee80211 structure.
  733. */
  734. retval = rt2x00lib_probe_hw(rt2x00dev);
  735. if (retval) {
  736. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  737. goto exit;
  738. }
  739. /*
  740. * Register extra components.
  741. */
  742. rt2x00link_register(rt2x00dev);
  743. rt2x00leds_register(rt2x00dev);
  744. rt2x00rfkill_allocate(rt2x00dev);
  745. rt2x00debug_register(rt2x00dev);
  746. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  747. return 0;
  748. exit:
  749. rt2x00lib_remove_dev(rt2x00dev);
  750. return retval;
  751. }
  752. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  753. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  754. {
  755. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  756. /*
  757. * Disable radio.
  758. */
  759. rt2x00lib_disable_radio(rt2x00dev);
  760. /*
  761. * Uninitialize device.
  762. */
  763. rt2x00lib_uninitialize(rt2x00dev);
  764. /*
  765. * Free extra components
  766. */
  767. rt2x00debug_deregister(rt2x00dev);
  768. rt2x00rfkill_free(rt2x00dev);
  769. rt2x00leds_unregister(rt2x00dev);
  770. /*
  771. * Free ieee80211_hw memory.
  772. */
  773. rt2x00lib_remove_hw(rt2x00dev);
  774. /*
  775. * Free firmware image.
  776. */
  777. rt2x00lib_free_firmware(rt2x00dev);
  778. /*
  779. * Free queue structures.
  780. */
  781. rt2x00queue_free(rt2x00dev);
  782. }
  783. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  784. /*
  785. * Device state handlers
  786. */
  787. #ifdef CONFIG_PM
  788. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  789. {
  790. NOTICE(rt2x00dev, "Going to sleep.\n");
  791. /*
  792. * Prevent mac80211 from accessing driver while suspended.
  793. */
  794. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  795. return 0;
  796. /*
  797. * Cleanup as much as possible.
  798. */
  799. rt2x00lib_uninitialize(rt2x00dev);
  800. /*
  801. * Suspend/disable extra components.
  802. */
  803. rt2x00leds_suspend(rt2x00dev);
  804. rt2x00debug_deregister(rt2x00dev);
  805. /*
  806. * Set device mode to sleep for power management,
  807. * on some hardware this call seems to consistently fail.
  808. * From the specifications it is hard to tell why it fails,
  809. * and if this is a "bad thing".
  810. * Overall it is safe to just ignore the failure and
  811. * continue suspending. The only downside is that the
  812. * device will not be in optimal power save mode, but with
  813. * the radio and the other components already disabled the
  814. * device is as good as disabled.
  815. */
  816. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  817. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  818. "continue suspending.\n");
  819. return 0;
  820. }
  821. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  822. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  823. {
  824. NOTICE(rt2x00dev, "Waking up.\n");
  825. /*
  826. * Restore/enable extra components.
  827. */
  828. rt2x00debug_register(rt2x00dev);
  829. rt2x00leds_resume(rt2x00dev);
  830. /*
  831. * We are ready again to receive requests from mac80211.
  832. */
  833. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  834. return 0;
  835. }
  836. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  837. #endif /* CONFIG_PM */
  838. /*
  839. * rt2x00lib module information.
  840. */
  841. MODULE_AUTHOR(DRV_PROJECT);
  842. MODULE_VERSION(DRV_VERSION);
  843. MODULE_DESCRIPTION("rt2x00 library");
  844. MODULE_LICENSE("GPL");