rt2x00dev.c 24 KB

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