rt2x00dev.c 24 KB

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