rt2x00dev.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 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->noise = rxdesc.noise;
  370. rx_status->flag = rxdesc.flags;
  371. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  372. /*
  373. * Send frame to mac80211 & debugfs.
  374. * mac80211 will clean up the skb structure.
  375. */
  376. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  377. memcpy(IEEE80211_SKB_RXCB(entry->skb), rx_status, sizeof(*rx_status));
  378. ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb);
  379. /*
  380. * Replace the skb with the freshly allocated one.
  381. */
  382. entry->skb = skb;
  383. entry->flags = 0;
  384. rt2x00dev->ops->lib->clear_entry(entry);
  385. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  386. }
  387. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  388. /*
  389. * Driver initialization handlers.
  390. */
  391. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  392. {
  393. .flags = DEV_RATE_CCK,
  394. .bitrate = 10,
  395. .ratemask = BIT(0),
  396. .plcp = 0x00,
  397. .mcs = RATE_MCS(RATE_MODE_CCK, 0),
  398. },
  399. {
  400. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  401. .bitrate = 20,
  402. .ratemask = BIT(1),
  403. .plcp = 0x01,
  404. .mcs = RATE_MCS(RATE_MODE_CCK, 1),
  405. },
  406. {
  407. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  408. .bitrate = 55,
  409. .ratemask = BIT(2),
  410. .plcp = 0x02,
  411. .mcs = RATE_MCS(RATE_MODE_CCK, 2),
  412. },
  413. {
  414. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  415. .bitrate = 110,
  416. .ratemask = BIT(3),
  417. .plcp = 0x03,
  418. .mcs = RATE_MCS(RATE_MODE_CCK, 3),
  419. },
  420. {
  421. .flags = DEV_RATE_OFDM,
  422. .bitrate = 60,
  423. .ratemask = BIT(4),
  424. .plcp = 0x0b,
  425. .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
  426. },
  427. {
  428. .flags = DEV_RATE_OFDM,
  429. .bitrate = 90,
  430. .ratemask = BIT(5),
  431. .plcp = 0x0f,
  432. .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
  433. },
  434. {
  435. .flags = DEV_RATE_OFDM,
  436. .bitrate = 120,
  437. .ratemask = BIT(6),
  438. .plcp = 0x0a,
  439. .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
  440. },
  441. {
  442. .flags = DEV_RATE_OFDM,
  443. .bitrate = 180,
  444. .ratemask = BIT(7),
  445. .plcp = 0x0e,
  446. .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
  447. },
  448. {
  449. .flags = DEV_RATE_OFDM,
  450. .bitrate = 240,
  451. .ratemask = BIT(8),
  452. .plcp = 0x09,
  453. .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
  454. },
  455. {
  456. .flags = DEV_RATE_OFDM,
  457. .bitrate = 360,
  458. .ratemask = BIT(9),
  459. .plcp = 0x0d,
  460. .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
  461. },
  462. {
  463. .flags = DEV_RATE_OFDM,
  464. .bitrate = 480,
  465. .ratemask = BIT(10),
  466. .plcp = 0x08,
  467. .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
  468. },
  469. {
  470. .flags = DEV_RATE_OFDM,
  471. .bitrate = 540,
  472. .ratemask = BIT(11),
  473. .plcp = 0x0c,
  474. .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
  475. },
  476. };
  477. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  478. const int channel, const int tx_power,
  479. const int value)
  480. {
  481. entry->center_freq = ieee80211_channel_to_frequency(channel);
  482. entry->hw_value = value;
  483. entry->max_power = tx_power;
  484. entry->max_antenna_gain = 0xff;
  485. }
  486. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  487. const u16 index, const struct rt2x00_rate *rate)
  488. {
  489. entry->flags = 0;
  490. entry->bitrate = rate->bitrate;
  491. entry->hw_value =index;
  492. entry->hw_value_short = index;
  493. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  494. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  495. }
  496. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  497. struct hw_mode_spec *spec)
  498. {
  499. struct ieee80211_hw *hw = rt2x00dev->hw;
  500. struct ieee80211_channel *channels;
  501. struct ieee80211_rate *rates;
  502. unsigned int num_rates;
  503. unsigned int i;
  504. num_rates = 0;
  505. if (spec->supported_rates & SUPPORT_RATE_CCK)
  506. num_rates += 4;
  507. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  508. num_rates += 8;
  509. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  510. if (!channels)
  511. return -ENOMEM;
  512. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  513. if (!rates)
  514. goto exit_free_channels;
  515. /*
  516. * Initialize Rate list.
  517. */
  518. for (i = 0; i < num_rates; i++)
  519. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  520. /*
  521. * Initialize Channel list.
  522. */
  523. for (i = 0; i < spec->num_channels; i++) {
  524. rt2x00lib_channel(&channels[i],
  525. spec->channels[i].channel,
  526. spec->channels_info[i].tx_power1, i);
  527. }
  528. /*
  529. * Intitialize 802.11b, 802.11g
  530. * Rates: CCK, OFDM.
  531. * Channels: 2.4 GHz
  532. */
  533. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  534. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  535. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  536. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  537. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  538. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  539. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  540. memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
  541. &spec->ht, sizeof(spec->ht));
  542. }
  543. /*
  544. * Intitialize 802.11a
  545. * Rates: OFDM.
  546. * Channels: OFDM, UNII, HiperLAN2.
  547. */
  548. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  549. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  550. spec->num_channels - 14;
  551. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  552. num_rates - 4;
  553. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  554. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  555. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  556. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  557. memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
  558. &spec->ht, sizeof(spec->ht));
  559. }
  560. return 0;
  561. exit_free_channels:
  562. kfree(channels);
  563. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  564. return -ENOMEM;
  565. }
  566. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  567. {
  568. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  569. ieee80211_unregister_hw(rt2x00dev->hw);
  570. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  571. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  572. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  573. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  574. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  575. }
  576. kfree(rt2x00dev->spec.channels_info);
  577. }
  578. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  579. {
  580. struct hw_mode_spec *spec = &rt2x00dev->spec;
  581. int status;
  582. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  583. return 0;
  584. /*
  585. * Initialize HW modes.
  586. */
  587. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  588. if (status)
  589. return status;
  590. /*
  591. * Initialize HW fields.
  592. */
  593. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  594. /*
  595. * Initialize extra TX headroom required.
  596. */
  597. rt2x00dev->hw->extra_tx_headroom =
  598. max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
  599. rt2x00dev->ops->extra_tx_headroom);
  600. /*
  601. * Take TX headroom required for alignment into account.
  602. */
  603. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  604. rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
  605. else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  606. rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
  607. /*
  608. * Register HW.
  609. */
  610. status = ieee80211_register_hw(rt2x00dev->hw);
  611. if (status)
  612. return status;
  613. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  614. return 0;
  615. }
  616. /*
  617. * Initialization/uninitialization handlers.
  618. */
  619. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  620. {
  621. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  622. return;
  623. /*
  624. * Unregister extra components.
  625. */
  626. rt2x00rfkill_unregister(rt2x00dev);
  627. /*
  628. * Allow the HW to uninitialize.
  629. */
  630. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  631. /*
  632. * Free allocated queue entries.
  633. */
  634. rt2x00queue_uninitialize(rt2x00dev);
  635. }
  636. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  637. {
  638. int status;
  639. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  640. return 0;
  641. /*
  642. * Allocate all queue entries.
  643. */
  644. status = rt2x00queue_initialize(rt2x00dev);
  645. if (status)
  646. return status;
  647. /*
  648. * Initialize the device.
  649. */
  650. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  651. if (status) {
  652. rt2x00queue_uninitialize(rt2x00dev);
  653. return status;
  654. }
  655. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  656. /*
  657. * Register the extra components.
  658. */
  659. rt2x00rfkill_register(rt2x00dev);
  660. return 0;
  661. }
  662. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  663. {
  664. int retval;
  665. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  666. return 0;
  667. /*
  668. * If this is the first interface which is added,
  669. * we should load the firmware now.
  670. */
  671. retval = rt2x00lib_load_firmware(rt2x00dev);
  672. if (retval)
  673. return retval;
  674. /*
  675. * Initialize the device.
  676. */
  677. retval = rt2x00lib_initialize(rt2x00dev);
  678. if (retval)
  679. return retval;
  680. rt2x00dev->intf_ap_count = 0;
  681. rt2x00dev->intf_sta_count = 0;
  682. rt2x00dev->intf_associated = 0;
  683. /* Enable the radio */
  684. retval = rt2x00lib_enable_radio(rt2x00dev);
  685. if (retval) {
  686. rt2x00queue_uninitialize(rt2x00dev);
  687. return retval;
  688. }
  689. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  690. return 0;
  691. }
  692. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  693. {
  694. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  695. return;
  696. /*
  697. * Perhaps we can add something smarter here,
  698. * but for now just disabling the radio should do.
  699. */
  700. rt2x00lib_disable_radio(rt2x00dev);
  701. rt2x00dev->intf_ap_count = 0;
  702. rt2x00dev->intf_sta_count = 0;
  703. rt2x00dev->intf_associated = 0;
  704. }
  705. /*
  706. * driver allocation handlers.
  707. */
  708. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  709. {
  710. int retval = -ENOMEM;
  711. mutex_init(&rt2x00dev->csr_mutex);
  712. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  713. /*
  714. * Make room for rt2x00_intf inside the per-interface
  715. * structure ieee80211_vif.
  716. */
  717. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  718. /*
  719. * Determine which operating modes are supported, all modes
  720. * which require beaconing, depend on the availability of
  721. * beacon entries.
  722. */
  723. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  724. if (rt2x00dev->ops->bcn->entry_num > 0)
  725. rt2x00dev->hw->wiphy->interface_modes |=
  726. BIT(NL80211_IFTYPE_ADHOC) |
  727. BIT(NL80211_IFTYPE_AP) |
  728. BIT(NL80211_IFTYPE_MESH_POINT) |
  729. BIT(NL80211_IFTYPE_WDS);
  730. /*
  731. * Let the driver probe the device to detect the capabilities.
  732. */
  733. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  734. if (retval) {
  735. ERROR(rt2x00dev, "Failed to allocate device.\n");
  736. goto exit;
  737. }
  738. /*
  739. * Initialize configuration work.
  740. */
  741. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  742. /*
  743. * Allocate queue array.
  744. */
  745. retval = rt2x00queue_allocate(rt2x00dev);
  746. if (retval)
  747. goto exit;
  748. /*
  749. * Initialize ieee80211 structure.
  750. */
  751. retval = rt2x00lib_probe_hw(rt2x00dev);
  752. if (retval) {
  753. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  754. goto exit;
  755. }
  756. /*
  757. * Register extra components.
  758. */
  759. rt2x00link_register(rt2x00dev);
  760. rt2x00leds_register(rt2x00dev);
  761. rt2x00debug_register(rt2x00dev);
  762. return 0;
  763. exit:
  764. rt2x00lib_remove_dev(rt2x00dev);
  765. return retval;
  766. }
  767. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  768. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  769. {
  770. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  771. /*
  772. * Disable radio.
  773. */
  774. rt2x00lib_disable_radio(rt2x00dev);
  775. /*
  776. * Stop all work.
  777. */
  778. cancel_work_sync(&rt2x00dev->intf_work);
  779. /*
  780. * Uninitialize device.
  781. */
  782. rt2x00lib_uninitialize(rt2x00dev);
  783. /*
  784. * Free extra components
  785. */
  786. rt2x00debug_deregister(rt2x00dev);
  787. rt2x00leds_unregister(rt2x00dev);
  788. /*
  789. * Free ieee80211_hw memory.
  790. */
  791. rt2x00lib_remove_hw(rt2x00dev);
  792. /*
  793. * Free firmware image.
  794. */
  795. rt2x00lib_free_firmware(rt2x00dev);
  796. /*
  797. * Free queue structures.
  798. */
  799. rt2x00queue_free(rt2x00dev);
  800. }
  801. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  802. /*
  803. * Device state handlers
  804. */
  805. #ifdef CONFIG_PM
  806. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  807. {
  808. NOTICE(rt2x00dev, "Going to sleep.\n");
  809. /*
  810. * Prevent mac80211 from accessing driver while suspended.
  811. */
  812. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  813. return 0;
  814. /*
  815. * Cleanup as much as possible.
  816. */
  817. rt2x00lib_uninitialize(rt2x00dev);
  818. /*
  819. * Suspend/disable extra components.
  820. */
  821. rt2x00leds_suspend(rt2x00dev);
  822. rt2x00debug_deregister(rt2x00dev);
  823. /*
  824. * Set device mode to sleep for power management,
  825. * on some hardware this call seems to consistently fail.
  826. * From the specifications it is hard to tell why it fails,
  827. * and if this is a "bad thing".
  828. * Overall it is safe to just ignore the failure and
  829. * continue suspending. The only downside is that the
  830. * device will not be in optimal power save mode, but with
  831. * the radio and the other components already disabled the
  832. * device is as good as disabled.
  833. */
  834. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  835. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  836. "continue suspending.\n");
  837. return 0;
  838. }
  839. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  840. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  841. {
  842. NOTICE(rt2x00dev, "Waking up.\n");
  843. /*
  844. * Restore/enable extra components.
  845. */
  846. rt2x00debug_register(rt2x00dev);
  847. rt2x00leds_resume(rt2x00dev);
  848. /*
  849. * We are ready again to receive requests from mac80211.
  850. */
  851. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  852. return 0;
  853. }
  854. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  855. #endif /* CONFIG_PM */
  856. /*
  857. * rt2x00lib module information.
  858. */
  859. MODULE_AUTHOR(DRV_PROJECT);
  860. MODULE_VERSION(DRV_VERSION);
  861. MODULE_DESCRIPTION("rt2x00 library");
  862. MODULE_LICENSE("GPL");