rt2x00dev.c 24 KB

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