rt2x00dev.c 24 KB

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