rt2x00dev.c 24 KB

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