rt2x00dev.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 generic device routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. /*
  26. * Radio control handlers.
  27. */
  28. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  29. {
  30. int status;
  31. /*
  32. * Don't enable the radio twice.
  33. * And check if the hardware button has been disabled.
  34. */
  35. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  36. 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. struct ieee80211_bss_conf conf;
  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. memcpy(&conf, &vif->bss_conf, sizeof(conf));
  111. delayed_flags = intf->delayed_flags;
  112. intf->delayed_flags = 0;
  113. spin_unlock(&intf->lock);
  114. /*
  115. * It is possible the radio was disabled while the work had been
  116. * scheduled. If that happens we should return here immediately,
  117. * note that in the spinlock protected area above the delayed_flags
  118. * have been cleared correctly.
  119. */
  120. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  121. return;
  122. if (delayed_flags & DELAYED_UPDATE_BEACON)
  123. rt2x00queue_update_beacon(rt2x00dev, vif, true);
  124. if (delayed_flags & DELAYED_CONFIG_ERP)
  125. rt2x00lib_config_erp(rt2x00dev, intf, &conf);
  126. if (delayed_flags & DELAYED_LED_ASSOC)
  127. rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
  128. }
  129. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  130. {
  131. struct rt2x00_dev *rt2x00dev =
  132. container_of(work, struct rt2x00_dev, intf_work);
  133. /*
  134. * Iterate over each interface and perform the
  135. * requested configurations.
  136. */
  137. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  138. rt2x00lib_intf_scheduled_iter,
  139. rt2x00dev);
  140. }
  141. /*
  142. * Interrupt context handlers.
  143. */
  144. static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
  145. struct ieee80211_vif *vif)
  146. {
  147. struct rt2x00_intf *intf = vif_to_intf(vif);
  148. if (vif->type != NL80211_IFTYPE_AP &&
  149. vif->type != NL80211_IFTYPE_ADHOC &&
  150. vif->type != NL80211_IFTYPE_MESH_POINT &&
  151. vif->type != NL80211_IFTYPE_WDS)
  152. return;
  153. spin_lock(&intf->lock);
  154. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  155. spin_unlock(&intf->lock);
  156. }
  157. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  158. {
  159. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  160. return;
  161. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  162. rt2x00lib_beacondone_iter,
  163. rt2x00dev);
  164. ieee80211_queue_work(rt2x00dev->hw, &rt2x00dev->intf_work);
  165. }
  166. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  167. void rt2x00lib_txdone(struct queue_entry *entry,
  168. struct txdone_entry_desc *txdesc)
  169. {
  170. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  171. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  172. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  173. enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
  174. unsigned int header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  175. u8 rate_idx, rate_flags, retry_rates;
  176. unsigned int i;
  177. /*
  178. * Unmap the skb.
  179. */
  180. rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
  181. /*
  182. * Remove L2 padding which was added during
  183. */
  184. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  185. rt2x00queue_payload_align(entry->skb, true, header_length);
  186. /*
  187. * If the IV/EIV data was stripped from the frame before it was
  188. * passed to the hardware, we should now reinsert it again because
  189. * mac80211 will expect the the same data to be present it the
  190. * frame as it was passed to us.
  191. */
  192. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
  193. rt2x00crypto_tx_insert_iv(entry->skb, header_length);
  194. /*
  195. * Send frame to debugfs immediately, after this call is completed
  196. * we are going to overwrite the skb->cb array.
  197. */
  198. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
  199. /*
  200. * Update TX statistics.
  201. */
  202. rt2x00dev->link.qual.tx_success +=
  203. test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  204. test_bit(TXDONE_UNKNOWN, &txdesc->flags);
  205. rt2x00dev->link.qual.tx_failed +=
  206. test_bit(TXDONE_FAILURE, &txdesc->flags);
  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 (test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  230. test_bit(TXDONE_UNKNOWN, &txdesc->flags))
  231. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  232. else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
  233. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  234. }
  235. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  236. if (test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  237. test_bit(TXDONE_UNKNOWN, &txdesc->flags))
  238. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  239. else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
  240. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  241. }
  242. /*
  243. * Only send the status report to mac80211 when TX status was
  244. * requested by it. If this was a extra frame coming through
  245. * a mac80211 library call (RTS/CTS) then we should not send the
  246. * status report back.
  247. */
  248. if (tx_info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  249. ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb);
  250. else
  251. dev_kfree_skb_irq(entry->skb);
  252. /*
  253. * Make this entry available for reuse.
  254. */
  255. entry->skb = NULL;
  256. entry->flags = 0;
  257. rt2x00dev->ops->lib->clear_entry(entry);
  258. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  259. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  260. /*
  261. * If the data queue was below the threshold before the txdone
  262. * handler we must make sure the packet queue in the mac80211 stack
  263. * is reenabled when the txdone handler has finished.
  264. */
  265. if (!rt2x00queue_threshold(entry->queue))
  266. ieee80211_wake_queue(rt2x00dev->hw, qid);
  267. }
  268. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  269. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  270. struct rxdone_entry_desc *rxdesc)
  271. {
  272. struct ieee80211_supported_band *sband;
  273. const struct rt2x00_rate *rate;
  274. unsigned int i;
  275. int signal;
  276. int type;
  277. /*
  278. * For non-HT rates the MCS value needs to contain the
  279. * actually used rate modulation (CCK or OFDM).
  280. */
  281. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  282. signal = RATE_MCS(rxdesc->rate_mode, rxdesc->signal);
  283. else
  284. signal = rxdesc->signal;
  285. type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  286. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  287. for (i = 0; i < sband->n_bitrates; i++) {
  288. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  289. if (((type == RXDONE_SIGNAL_PLCP) &&
  290. (rate->plcp == signal)) ||
  291. ((type == RXDONE_SIGNAL_BITRATE) &&
  292. (rate->bitrate == signal)) ||
  293. ((type == RXDONE_SIGNAL_MCS) &&
  294. (rate->mcs == signal))) {
  295. return i;
  296. }
  297. }
  298. WARNING(rt2x00dev, "Frame received with unrecognized signal, "
  299. "signal=0x%.4x, type=%d.\n", signal, type);
  300. return 0;
  301. }
  302. void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev,
  303. struct queue_entry *entry)
  304. {
  305. struct rxdone_entry_desc rxdesc;
  306. struct sk_buff *skb;
  307. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  308. unsigned int header_length;
  309. bool l2pad;
  310. int rate_idx;
  311. /*
  312. * Allocate a new sk_buffer. If no new buffer available, drop the
  313. * received frame and reuse the existing buffer.
  314. */
  315. skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry);
  316. if (!skb)
  317. return;
  318. /*
  319. * Unmap the skb.
  320. */
  321. rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
  322. /*
  323. * Extract the RXD details.
  324. */
  325. memset(&rxdesc, 0, sizeof(rxdesc));
  326. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  327. /* Trim buffer to correct size */
  328. skb_trim(entry->skb, rxdesc.size);
  329. /*
  330. * The data behind the ieee80211 header must be
  331. * aligned on a 4 byte boundary.
  332. */
  333. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  334. l2pad = !!(rxdesc.dev_flags & RXDONE_L2PAD);
  335. /*
  336. * Hardware might have stripped the IV/EIV/ICV data,
  337. * in that case it is possible that the data was
  338. * provided seperately (through hardware descriptor)
  339. * in which case we should reinsert the data into the frame.
  340. */
  341. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  342. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  343. rt2x00crypto_rx_insert_iv(entry->skb, l2pad, header_length,
  344. &rxdesc);
  345. else
  346. rt2x00queue_payload_align(entry->skb, l2pad, header_length);
  347. /*
  348. * Check if the frame was received using HT. In that case,
  349. * the rate is the MCS index and should be passed to mac80211
  350. * directly. Otherwise we need to translate the signal to
  351. * the correct bitrate index.
  352. */
  353. if (rxdesc.rate_mode == RATE_MODE_CCK ||
  354. rxdesc.rate_mode == RATE_MODE_OFDM) {
  355. rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
  356. } else {
  357. rxdesc.flags |= RX_FLAG_HT;
  358. rate_idx = rxdesc.signal;
  359. }
  360. /*
  361. * Update extra components
  362. */
  363. rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
  364. rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
  365. rx_status->mactime = rxdesc.timestamp;
  366. rx_status->rate_idx = rate_idx;
  367. rx_status->qual = rt2x00link_calculate_signal(rt2x00dev, rxdesc.rssi);
  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. * Register HW.
  596. */
  597. status = ieee80211_register_hw(rt2x00dev->hw);
  598. if (status)
  599. return status;
  600. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  601. return 0;
  602. }
  603. /*
  604. * Initialization/uninitialization handlers.
  605. */
  606. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  607. {
  608. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  609. return;
  610. /*
  611. * Unregister extra components.
  612. */
  613. rt2x00rfkill_unregister(rt2x00dev);
  614. /*
  615. * Allow the HW to uninitialize.
  616. */
  617. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  618. /*
  619. * Free allocated queue entries.
  620. */
  621. rt2x00queue_uninitialize(rt2x00dev);
  622. }
  623. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  624. {
  625. int status;
  626. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  627. return 0;
  628. /*
  629. * Allocate all queue entries.
  630. */
  631. status = rt2x00queue_initialize(rt2x00dev);
  632. if (status)
  633. return status;
  634. /*
  635. * Initialize the device.
  636. */
  637. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  638. if (status) {
  639. rt2x00queue_uninitialize(rt2x00dev);
  640. return status;
  641. }
  642. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  643. /*
  644. * Register the extra components.
  645. */
  646. rt2x00rfkill_register(rt2x00dev);
  647. return 0;
  648. }
  649. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  650. {
  651. int retval;
  652. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  653. return 0;
  654. /*
  655. * If this is the first interface which is added,
  656. * we should load the firmware now.
  657. */
  658. retval = rt2x00lib_load_firmware(rt2x00dev);
  659. if (retval)
  660. return retval;
  661. /*
  662. * Initialize the device.
  663. */
  664. retval = rt2x00lib_initialize(rt2x00dev);
  665. if (retval)
  666. return retval;
  667. rt2x00dev->intf_ap_count = 0;
  668. rt2x00dev->intf_sta_count = 0;
  669. rt2x00dev->intf_associated = 0;
  670. /* Enable the radio */
  671. retval = rt2x00lib_enable_radio(rt2x00dev);
  672. if (retval) {
  673. rt2x00queue_uninitialize(rt2x00dev);
  674. return retval;
  675. }
  676. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  677. return 0;
  678. }
  679. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  680. {
  681. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  682. return;
  683. /*
  684. * Perhaps we can add something smarter here,
  685. * but for now just disabling the radio should do.
  686. */
  687. rt2x00lib_disable_radio(rt2x00dev);
  688. rt2x00dev->intf_ap_count = 0;
  689. rt2x00dev->intf_sta_count = 0;
  690. rt2x00dev->intf_associated = 0;
  691. }
  692. /*
  693. * driver allocation handlers.
  694. */
  695. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  696. {
  697. int retval = -ENOMEM;
  698. mutex_init(&rt2x00dev->csr_mutex);
  699. /*
  700. * Make room for rt2x00_intf inside the per-interface
  701. * structure ieee80211_vif.
  702. */
  703. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  704. /*
  705. * Determine which operating modes are supported, all modes
  706. * which require beaconing, depend on the availability of
  707. * beacon entries.
  708. */
  709. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  710. if (rt2x00dev->ops->bcn->entry_num > 0)
  711. rt2x00dev->hw->wiphy->interface_modes |=
  712. BIT(NL80211_IFTYPE_ADHOC) |
  713. BIT(NL80211_IFTYPE_AP) |
  714. BIT(NL80211_IFTYPE_MESH_POINT) |
  715. BIT(NL80211_IFTYPE_WDS);
  716. /*
  717. * Let the driver probe the device to detect the capabilities.
  718. */
  719. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  720. if (retval) {
  721. ERROR(rt2x00dev, "Failed to allocate device.\n");
  722. goto exit;
  723. }
  724. /*
  725. * Initialize configuration work.
  726. */
  727. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  728. /*
  729. * Allocate queue array.
  730. */
  731. retval = rt2x00queue_allocate(rt2x00dev);
  732. if (retval)
  733. goto exit;
  734. /*
  735. * Initialize ieee80211 structure.
  736. */
  737. retval = rt2x00lib_probe_hw(rt2x00dev);
  738. if (retval) {
  739. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  740. goto exit;
  741. }
  742. /*
  743. * Register extra components.
  744. */
  745. rt2x00link_register(rt2x00dev);
  746. rt2x00leds_register(rt2x00dev);
  747. rt2x00debug_register(rt2x00dev);
  748. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  749. return 0;
  750. exit:
  751. rt2x00lib_remove_dev(rt2x00dev);
  752. return retval;
  753. }
  754. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  755. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  756. {
  757. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  758. /*
  759. * Disable radio.
  760. */
  761. rt2x00lib_disable_radio(rt2x00dev);
  762. /*
  763. * Stop all work.
  764. */
  765. cancel_work_sync(&rt2x00dev->intf_work);
  766. /*
  767. * Uninitialize device.
  768. */
  769. rt2x00lib_uninitialize(rt2x00dev);
  770. /*
  771. * Free extra components
  772. */
  773. rt2x00debug_deregister(rt2x00dev);
  774. rt2x00leds_unregister(rt2x00dev);
  775. /*
  776. * Free ieee80211_hw memory.
  777. */
  778. rt2x00lib_remove_hw(rt2x00dev);
  779. /*
  780. * Free firmware image.
  781. */
  782. rt2x00lib_free_firmware(rt2x00dev);
  783. /*
  784. * Free queue structures.
  785. */
  786. rt2x00queue_free(rt2x00dev);
  787. }
  788. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  789. /*
  790. * Device state handlers
  791. */
  792. #ifdef CONFIG_PM
  793. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  794. {
  795. NOTICE(rt2x00dev, "Going to sleep.\n");
  796. /*
  797. * Prevent mac80211 from accessing driver while suspended.
  798. */
  799. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  800. return 0;
  801. /*
  802. * Cleanup as much as possible.
  803. */
  804. rt2x00lib_uninitialize(rt2x00dev);
  805. /*
  806. * Suspend/disable extra components.
  807. */
  808. rt2x00leds_suspend(rt2x00dev);
  809. rt2x00debug_deregister(rt2x00dev);
  810. /*
  811. * Set device mode to sleep for power management,
  812. * on some hardware this call seems to consistently fail.
  813. * From the specifications it is hard to tell why it fails,
  814. * and if this is a "bad thing".
  815. * Overall it is safe to just ignore the failure and
  816. * continue suspending. The only downside is that the
  817. * device will not be in optimal power save mode, but with
  818. * the radio and the other components already disabled the
  819. * device is as good as disabled.
  820. */
  821. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  822. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  823. "continue suspending.\n");
  824. return 0;
  825. }
  826. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  827. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  828. {
  829. NOTICE(rt2x00dev, "Waking up.\n");
  830. /*
  831. * Restore/enable extra components.
  832. */
  833. rt2x00debug_register(rt2x00dev);
  834. rt2x00leds_resume(rt2x00dev);
  835. /*
  836. * We are ready again to receive requests from mac80211.
  837. */
  838. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  839. return 0;
  840. }
  841. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  842. #endif /* CONFIG_PM */
  843. /*
  844. * rt2x00lib module information.
  845. */
  846. MODULE_AUTHOR(DRV_PROJECT);
  847. MODULE_VERSION(DRV_VERSION);
  848. MODULE_DESCRIPTION("rt2x00 library");
  849. MODULE_LICENSE("GPL");