rt2x00dev.c 22 KB

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