rt2x00dev.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. Copyright (C) 2004 - 2008 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, plcp=%d.\n", rxdesc.signal,
  330. !!(rxdesc.dev_flags & RXDONE_SIGNAL_PLCP));
  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->flag = rxdesc.flags;
  343. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  344. /*
  345. * Send frame to mac80211 & debugfs.
  346. * mac80211 will clean up the skb structure.
  347. */
  348. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  349. ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
  350. /*
  351. * Replace the skb with the freshly allocated one.
  352. */
  353. entry->skb = skb;
  354. entry->flags = 0;
  355. rt2x00dev->ops->lib->clear_entry(entry);
  356. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  357. }
  358. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  359. /*
  360. * Driver initialization handlers.
  361. */
  362. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  363. {
  364. .flags = DEV_RATE_CCK,
  365. .bitrate = 10,
  366. .ratemask = BIT(0),
  367. .plcp = 0x00,
  368. },
  369. {
  370. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  371. .bitrate = 20,
  372. .ratemask = BIT(1),
  373. .plcp = 0x01,
  374. },
  375. {
  376. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  377. .bitrate = 55,
  378. .ratemask = BIT(2),
  379. .plcp = 0x02,
  380. },
  381. {
  382. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  383. .bitrate = 110,
  384. .ratemask = BIT(3),
  385. .plcp = 0x03,
  386. },
  387. {
  388. .flags = DEV_RATE_OFDM,
  389. .bitrate = 60,
  390. .ratemask = BIT(4),
  391. .plcp = 0x0b,
  392. },
  393. {
  394. .flags = DEV_RATE_OFDM,
  395. .bitrate = 90,
  396. .ratemask = BIT(5),
  397. .plcp = 0x0f,
  398. },
  399. {
  400. .flags = DEV_RATE_OFDM,
  401. .bitrate = 120,
  402. .ratemask = BIT(6),
  403. .plcp = 0x0a,
  404. },
  405. {
  406. .flags = DEV_RATE_OFDM,
  407. .bitrate = 180,
  408. .ratemask = BIT(7),
  409. .plcp = 0x0e,
  410. },
  411. {
  412. .flags = DEV_RATE_OFDM,
  413. .bitrate = 240,
  414. .ratemask = BIT(8),
  415. .plcp = 0x09,
  416. },
  417. {
  418. .flags = DEV_RATE_OFDM,
  419. .bitrate = 360,
  420. .ratemask = BIT(9),
  421. .plcp = 0x0d,
  422. },
  423. {
  424. .flags = DEV_RATE_OFDM,
  425. .bitrate = 480,
  426. .ratemask = BIT(10),
  427. .plcp = 0x08,
  428. },
  429. {
  430. .flags = DEV_RATE_OFDM,
  431. .bitrate = 540,
  432. .ratemask = BIT(11),
  433. .plcp = 0x0c,
  434. },
  435. };
  436. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  437. const int channel, const int tx_power,
  438. const int value)
  439. {
  440. entry->center_freq = ieee80211_channel_to_frequency(channel);
  441. entry->hw_value = value;
  442. entry->max_power = tx_power;
  443. entry->max_antenna_gain = 0xff;
  444. }
  445. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  446. const u16 index, const struct rt2x00_rate *rate)
  447. {
  448. entry->flags = 0;
  449. entry->bitrate = rate->bitrate;
  450. entry->hw_value =index;
  451. entry->hw_value_short = index;
  452. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  453. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  454. }
  455. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  456. struct hw_mode_spec *spec)
  457. {
  458. struct ieee80211_hw *hw = rt2x00dev->hw;
  459. struct ieee80211_channel *channels;
  460. struct ieee80211_rate *rates;
  461. unsigned int num_rates;
  462. unsigned int i;
  463. num_rates = 0;
  464. if (spec->supported_rates & SUPPORT_RATE_CCK)
  465. num_rates += 4;
  466. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  467. num_rates += 8;
  468. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  469. if (!channels)
  470. return -ENOMEM;
  471. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  472. if (!rates)
  473. goto exit_free_channels;
  474. /*
  475. * Initialize Rate list.
  476. */
  477. for (i = 0; i < num_rates; i++)
  478. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  479. /*
  480. * Initialize Channel list.
  481. */
  482. for (i = 0; i < spec->num_channels; i++) {
  483. rt2x00lib_channel(&channels[i],
  484. spec->channels[i].channel,
  485. spec->channels_info[i].tx_power1, i);
  486. }
  487. /*
  488. * Intitialize 802.11b, 802.11g
  489. * Rates: CCK, OFDM.
  490. * Channels: 2.4 GHz
  491. */
  492. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  493. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  494. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  495. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  496. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  497. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  498. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  499. }
  500. /*
  501. * Intitialize 802.11a
  502. * Rates: OFDM.
  503. * Channels: OFDM, UNII, HiperLAN2.
  504. */
  505. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  506. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  507. spec->num_channels - 14;
  508. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  509. num_rates - 4;
  510. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  511. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  512. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  513. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  514. }
  515. return 0;
  516. exit_free_channels:
  517. kfree(channels);
  518. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  519. return -ENOMEM;
  520. }
  521. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  522. {
  523. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  524. ieee80211_unregister_hw(rt2x00dev->hw);
  525. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  526. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  527. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  528. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  529. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  530. }
  531. kfree(rt2x00dev->spec.channels_info);
  532. }
  533. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  534. {
  535. struct hw_mode_spec *spec = &rt2x00dev->spec;
  536. int status;
  537. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  538. return 0;
  539. /*
  540. * Initialize HW modes.
  541. */
  542. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  543. if (status)
  544. return status;
  545. /*
  546. * Initialize HW fields.
  547. */
  548. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  549. /*
  550. * Register HW.
  551. */
  552. status = ieee80211_register_hw(rt2x00dev->hw);
  553. if (status) {
  554. rt2x00lib_remove_hw(rt2x00dev);
  555. return status;
  556. }
  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. int retval;
  746. NOTICE(rt2x00dev, "Going to sleep.\n");
  747. /*
  748. * Only continue if mac80211 has open interfaces.
  749. */
  750. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  751. !test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  752. goto exit;
  753. set_bit(DEVICE_STATE_STARTED_SUSPEND, &rt2x00dev->flags);
  754. /*
  755. * Disable radio.
  756. */
  757. rt2x00lib_stop(rt2x00dev);
  758. rt2x00lib_uninitialize(rt2x00dev);
  759. /*
  760. * Suspend/disable extra components.
  761. */
  762. rt2x00leds_suspend(rt2x00dev);
  763. rt2x00debug_deregister(rt2x00dev);
  764. exit:
  765. /*
  766. * Set device mode to sleep for power management,
  767. * on some hardware this call seems to consistently fail.
  768. * From the specifications it is hard to tell why it fails,
  769. * and if this is a "bad thing".
  770. * Overall it is safe to just ignore the failure and
  771. * continue suspending. The only downside is that the
  772. * device will not be in optimal power save mode, but with
  773. * the radio and the other components already disabled the
  774. * device is as good as disabled.
  775. */
  776. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  777. if (retval)
  778. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  779. "continue suspending.\n");
  780. return 0;
  781. }
  782. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  783. static void rt2x00lib_resume_intf(void *data, u8 *mac,
  784. struct ieee80211_vif *vif)
  785. {
  786. struct rt2x00_dev *rt2x00dev = data;
  787. struct rt2x00_intf *intf = vif_to_intf(vif);
  788. spin_lock(&intf->lock);
  789. rt2x00lib_config_intf(rt2x00dev, intf,
  790. vif->type, intf->mac, intf->bssid);
  791. /*
  792. * AP, Ad-hoc, and Mesh Point mode require a new beacon update.
  793. */
  794. if (vif->type == NL80211_IFTYPE_AP ||
  795. vif->type == NL80211_IFTYPE_ADHOC ||
  796. vif->type == NL80211_IFTYPE_MESH_POINT ||
  797. vif->type == NL80211_IFTYPE_WDS)
  798. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  799. spin_unlock(&intf->lock);
  800. }
  801. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  802. {
  803. int retval;
  804. NOTICE(rt2x00dev, "Waking up.\n");
  805. /*
  806. * Restore/enable extra components.
  807. */
  808. rt2x00debug_register(rt2x00dev);
  809. rt2x00leds_resume(rt2x00dev);
  810. /*
  811. * Only continue if mac80211 had open interfaces.
  812. */
  813. if (!test_and_clear_bit(DEVICE_STATE_STARTED_SUSPEND, &rt2x00dev->flags))
  814. return 0;
  815. /*
  816. * Reinitialize device and all active interfaces.
  817. */
  818. retval = rt2x00lib_start(rt2x00dev);
  819. if (retval)
  820. goto exit;
  821. /*
  822. * Reconfigure device.
  823. */
  824. retval = rt2x00mac_config(rt2x00dev->hw, ~0);
  825. if (retval)
  826. goto exit;
  827. /*
  828. * Iterator over each active interface to
  829. * reconfigure the hardware.
  830. */
  831. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  832. rt2x00lib_resume_intf, rt2x00dev);
  833. /*
  834. * We are ready again to receive requests from mac80211.
  835. */
  836. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  837. /*
  838. * It is possible that during that mac80211 has attempted
  839. * to send frames while we were suspending or resuming.
  840. * In that case we have disabled the TX queue and should
  841. * now enable it again
  842. */
  843. ieee80211_wake_queues(rt2x00dev->hw);
  844. /*
  845. * During interface iteration we might have changed the
  846. * delayed_flags, time to handles the event by calling
  847. * the work handler directly.
  848. */
  849. rt2x00lib_intf_scheduled(&rt2x00dev->intf_work);
  850. return 0;
  851. exit:
  852. rt2x00lib_stop(rt2x00dev);
  853. rt2x00lib_uninitialize(rt2x00dev);
  854. rt2x00debug_deregister(rt2x00dev);
  855. return retval;
  856. }
  857. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  858. #endif /* CONFIG_PM */
  859. /*
  860. * rt2x00lib module information.
  861. */
  862. MODULE_AUTHOR(DRV_PROJECT);
  863. MODULE_VERSION(DRV_VERSION);
  864. MODULE_DESCRIPTION("rt2x00 library");
  865. MODULE_LICENSE("GPL");