rt2x00dev.c 24 KB

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