rt2x00dev.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*
  2. Copyright (C) 2004 - 2007 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. /*
  22. * Set enviroment defines for rt2x00.h
  23. */
  24. #define DRV_NAME "rt2x00lib"
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include "rt2x00.h"
  28. #include "rt2x00lib.h"
  29. /*
  30. * Ring handler.
  31. */
  32. struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
  33. const unsigned int queue)
  34. {
  35. int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
  36. /*
  37. * Check if we are requesting a reqular TX ring,
  38. * or if we are requesting a Beacon or Atim ring.
  39. * For Atim rings, we should check if it is supported.
  40. */
  41. if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
  42. return &rt2x00dev->tx[queue];
  43. if (!rt2x00dev->bcn || !beacon)
  44. return NULL;
  45. if (queue == IEEE80211_TX_QUEUE_BEACON)
  46. return &rt2x00dev->bcn[0];
  47. else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  48. return &rt2x00dev->bcn[1];
  49. return NULL;
  50. }
  51. EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
  52. /*
  53. * Link tuning handlers
  54. */
  55. static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
  56. {
  57. rt2x00_clear_link(&rt2x00dev->link);
  58. /*
  59. * Reset the link tuner.
  60. */
  61. rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
  62. queue_delayed_work(rt2x00dev->hw->workqueue,
  63. &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
  64. }
  65. static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
  66. {
  67. cancel_delayed_work_sync(&rt2x00dev->link.work);
  68. }
  69. void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
  70. {
  71. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  72. return;
  73. rt2x00lib_stop_link_tuner(rt2x00dev);
  74. rt2x00lib_start_link_tuner(rt2x00dev);
  75. }
  76. /*
  77. * Radio control handlers.
  78. */
  79. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  80. {
  81. int status;
  82. /*
  83. * Don't enable the radio twice.
  84. * And check if the hardware button has been disabled.
  85. */
  86. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  87. test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
  88. return 0;
  89. /*
  90. * Enable radio.
  91. */
  92. status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
  93. STATE_RADIO_ON);
  94. if (status)
  95. return status;
  96. __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
  97. /*
  98. * Enable RX.
  99. */
  100. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  101. /*
  102. * Start the TX queues.
  103. */
  104. ieee80211_start_queues(rt2x00dev->hw);
  105. return 0;
  106. }
  107. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  108. {
  109. if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  110. return;
  111. /*
  112. * Stop all scheduled work.
  113. */
  114. if (work_pending(&rt2x00dev->beacon_work))
  115. cancel_work_sync(&rt2x00dev->beacon_work);
  116. if (work_pending(&rt2x00dev->filter_work))
  117. cancel_work_sync(&rt2x00dev->filter_work);
  118. if (work_pending(&rt2x00dev->config_work))
  119. cancel_work_sync(&rt2x00dev->config_work);
  120. /*
  121. * Stop the TX queues.
  122. */
  123. ieee80211_stop_queues(rt2x00dev->hw);
  124. /*
  125. * Disable RX.
  126. */
  127. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  128. /*
  129. * Disable radio.
  130. */
  131. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  132. }
  133. void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  134. {
  135. /*
  136. * When we are disabling the RX, we should also stop the link tuner.
  137. */
  138. if (state == STATE_RADIO_RX_OFF)
  139. rt2x00lib_stop_link_tuner(rt2x00dev);
  140. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  141. /*
  142. * When we are enabling the RX, we should also start the link tuner.
  143. */
  144. if (state == STATE_RADIO_RX_ON &&
  145. is_interface_present(&rt2x00dev->interface))
  146. rt2x00lib_start_link_tuner(rt2x00dev);
  147. }
  148. static void rt2x00lib_precalculate_link_signal(struct link *link)
  149. {
  150. if (link->rx_failed || link->rx_success)
  151. link->rx_percentage =
  152. (link->rx_success * 100) /
  153. (link->rx_failed + link->rx_success);
  154. else
  155. link->rx_percentage = 50;
  156. if (link->tx_failed || link->tx_success)
  157. link->tx_percentage =
  158. (link->tx_success * 100) /
  159. (link->tx_failed + link->tx_success);
  160. else
  161. link->tx_percentage = 50;
  162. link->rx_success = 0;
  163. link->rx_failed = 0;
  164. link->tx_success = 0;
  165. link->tx_failed = 0;
  166. }
  167. static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
  168. int rssi)
  169. {
  170. int rssi_percentage = 0;
  171. int signal;
  172. /*
  173. * We need a positive value for the RSSI.
  174. */
  175. if (rssi < 0)
  176. rssi += rt2x00dev->rssi_offset;
  177. /*
  178. * Calculate the different percentages,
  179. * which will be used for the signal.
  180. */
  181. if (rt2x00dev->rssi_offset)
  182. rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
  183. /*
  184. * Add the individual percentages and use the WEIGHT
  185. * defines to calculate the current link signal.
  186. */
  187. signal = ((WEIGHT_RSSI * rssi_percentage) +
  188. (WEIGHT_TX * rt2x00dev->link.tx_percentage) +
  189. (WEIGHT_RX * rt2x00dev->link.rx_percentage)) / 100;
  190. return (signal > 100) ? 100 : signal;
  191. }
  192. static void rt2x00lib_link_tuner(struct work_struct *work)
  193. {
  194. struct rt2x00_dev *rt2x00dev =
  195. container_of(work, struct rt2x00_dev, link.work.work);
  196. /*
  197. * When the radio is shutting down we should
  198. * immediately cease all link tuning.
  199. */
  200. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  201. return;
  202. /*
  203. * Update statistics.
  204. */
  205. rt2x00dev->ops->lib->link_stats(rt2x00dev);
  206. rt2x00dev->low_level_stats.dot11FCSErrorCount +=
  207. rt2x00dev->link.rx_failed;
  208. /*
  209. * Only perform the link tuning when Link tuning
  210. * has been enabled (This could have been disabled from the EEPROM).
  211. */
  212. if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
  213. rt2x00dev->ops->lib->link_tuner(rt2x00dev);
  214. /*
  215. * Precalculate a portion of the link signal which is
  216. * in based on the tx/rx success/failure counters.
  217. */
  218. rt2x00lib_precalculate_link_signal(&rt2x00dev->link);
  219. /*
  220. * Increase tuner counter, and reschedule the next link tuner run.
  221. */
  222. rt2x00dev->link.count++;
  223. queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
  224. LINK_TUNE_INTERVAL);
  225. }
  226. static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
  227. {
  228. struct rt2x00_dev *rt2x00dev =
  229. container_of(work, struct rt2x00_dev, filter_work);
  230. unsigned int filter = rt2x00dev->interface.filter;
  231. /*
  232. * Since we had stored the filter inside interface.filter,
  233. * we should now clear that field. Otherwise the driver will
  234. * assume nothing has changed (*total_flags will be compared
  235. * to interface.filter to determine if any action is required).
  236. */
  237. rt2x00dev->interface.filter = 0;
  238. rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
  239. filter, &filter, 0, NULL);
  240. }
  241. static void rt2x00lib_configuration_scheduled(struct work_struct *work)
  242. {
  243. struct rt2x00_dev *rt2x00dev =
  244. container_of(work, struct rt2x00_dev, config_work);
  245. int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
  246. rt2x00mac_erp_ie_changed(rt2x00dev->hw,
  247. IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
  248. }
  249. /*
  250. * Interrupt context handlers.
  251. */
  252. static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
  253. {
  254. struct rt2x00_dev *rt2x00dev =
  255. container_of(work, struct rt2x00_dev, beacon_work);
  256. struct data_ring *ring =
  257. rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
  258. struct data_entry *entry = rt2x00_get_data_entry(ring);
  259. struct sk_buff *skb;
  260. skb = ieee80211_beacon_get(rt2x00dev->hw,
  261. rt2x00dev->interface.id,
  262. &entry->tx_status.control);
  263. if (!skb)
  264. return;
  265. rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
  266. &entry->tx_status.control);
  267. dev_kfree_skb(skb);
  268. }
  269. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  270. {
  271. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  272. return;
  273. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
  274. }
  275. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  276. void rt2x00lib_txdone(struct data_entry *entry,
  277. const int status, const int retry)
  278. {
  279. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  280. struct ieee80211_tx_status *tx_status = &entry->tx_status;
  281. struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
  282. int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
  283. int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
  284. status == TX_FAIL_OTHER);
  285. /*
  286. * Update TX statistics.
  287. */
  288. tx_status->flags = 0;
  289. tx_status->ack_signal = 0;
  290. tx_status->excessive_retries = (status == TX_FAIL_RETRY);
  291. tx_status->retry_count = retry;
  292. rt2x00dev->link.tx_success += success;
  293. rt2x00dev->link.tx_failed += retry + fail;
  294. if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
  295. if (success)
  296. tx_status->flags |= IEEE80211_TX_STATUS_ACK;
  297. else
  298. stats->dot11ACKFailureCount++;
  299. }
  300. tx_status->queue_length = entry->ring->stats.limit;
  301. tx_status->queue_number = tx_status->control.queue;
  302. if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  303. if (success)
  304. stats->dot11RTSSuccessCount++;
  305. else
  306. stats->dot11RTSFailureCount++;
  307. }
  308. /*
  309. * Send the tx_status to mac80211,
  310. * that method also cleans up the skb structure.
  311. */
  312. ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
  313. entry->skb = NULL;
  314. }
  315. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  316. void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
  317. struct rxdata_entry_desc *desc)
  318. {
  319. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  320. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  321. struct ieee80211_hw_mode *mode;
  322. struct ieee80211_rate *rate;
  323. unsigned int i;
  324. int val = 0;
  325. /*
  326. * Update RX statistics.
  327. */
  328. mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
  329. for (i = 0; i < mode->num_rates; i++) {
  330. rate = &mode->rates[i];
  331. /*
  332. * When frame was received with an OFDM bitrate,
  333. * the signal is the PLCP value. If it was received with
  334. * a CCK bitrate the signal is the rate in 0.5kbit/s.
  335. */
  336. if (!desc->ofdm)
  337. val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
  338. else
  339. val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
  340. if (val == desc->signal) {
  341. val = rate->val;
  342. break;
  343. }
  344. }
  345. rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
  346. rt2x00dev->link.rx_success++;
  347. rx_status->rate = val;
  348. rx_status->signal =
  349. rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
  350. rx_status->ssi = desc->rssi;
  351. rx_status->flag = desc->flags;
  352. /*
  353. * Send frame to mac80211
  354. */
  355. ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
  356. }
  357. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  358. /*
  359. * TX descriptor initializer
  360. */
  361. void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  362. struct data_desc *txd,
  363. struct ieee80211_hdr *ieee80211hdr,
  364. unsigned int length,
  365. struct ieee80211_tx_control *control)
  366. {
  367. struct txdata_entry_desc desc;
  368. struct data_ring *ring;
  369. int tx_rate;
  370. int bitrate;
  371. int duration;
  372. int residual;
  373. u16 frame_control;
  374. u16 seq_ctrl;
  375. /*
  376. * Make sure the descriptor is properly cleared.
  377. */
  378. memset(&desc, 0x00, sizeof(desc));
  379. /*
  380. * Get ring pointer, if we fail to obtain the
  381. * correct ring, then use the first TX ring.
  382. */
  383. ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
  384. if (!ring)
  385. ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
  386. desc.cw_min = ring->tx_params.cw_min;
  387. desc.cw_max = ring->tx_params.cw_max;
  388. desc.aifs = ring->tx_params.aifs;
  389. /*
  390. * Identify queue
  391. */
  392. if (control->queue < rt2x00dev->hw->queues)
  393. desc.queue = control->queue;
  394. else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  395. control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  396. desc.queue = QUEUE_MGMT;
  397. else
  398. desc.queue = QUEUE_OTHER;
  399. /*
  400. * Read required fields from ieee80211 header.
  401. */
  402. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  403. seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
  404. tx_rate = control->tx_rate;
  405. /*
  406. * Check if this is a RTS/CTS frame
  407. */
  408. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  409. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  410. if (is_rts_frame(frame_control))
  411. __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
  412. if (control->rts_cts_rate)
  413. tx_rate = control->rts_cts_rate;
  414. }
  415. /*
  416. * Check for OFDM
  417. */
  418. if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
  419. __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
  420. /*
  421. * Check if more fragments are pending
  422. */
  423. if (ieee80211_get_morefrag(ieee80211hdr)) {
  424. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  425. __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
  426. }
  427. /*
  428. * Beacons and probe responses require the tsf timestamp
  429. * to be inserted into the frame.
  430. */
  431. if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  432. is_probe_resp(frame_control))
  433. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
  434. /*
  435. * Determine with what IFS priority this frame should be send.
  436. * Set ifs to IFS_SIFS when the this is not the first fragment,
  437. * or this fragment came after RTS/CTS.
  438. */
  439. if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
  440. test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
  441. desc.ifs = IFS_SIFS;
  442. else
  443. desc.ifs = IFS_BACKOFF;
  444. /*
  445. * PLCP setup
  446. * Length calculation depends on OFDM/CCK rate.
  447. */
  448. desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
  449. desc.service = 0x04;
  450. if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
  451. desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
  452. desc.length_low = ((length + FCS_LEN) & 0x3f);
  453. } else {
  454. bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
  455. /*
  456. * Convert length to microseconds.
  457. */
  458. residual = get_duration_res(length + FCS_LEN, bitrate);
  459. duration = get_duration(length + FCS_LEN, bitrate);
  460. if (residual != 0) {
  461. duration++;
  462. /*
  463. * Check if we need to set the Length Extension
  464. */
  465. if (bitrate == 110 && residual <= 30)
  466. desc.service |= 0x80;
  467. }
  468. desc.length_high = (duration >> 8) & 0xff;
  469. desc.length_low = duration & 0xff;
  470. /*
  471. * When preamble is enabled we should set the
  472. * preamble bit for the signal.
  473. */
  474. if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
  475. desc.signal |= 0x08;
  476. }
  477. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
  478. ieee80211hdr, length, control);
  479. }
  480. EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
  481. /*
  482. * Driver initialization handlers.
  483. */
  484. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  485. const int channel, const int tx_power,
  486. const int value)
  487. {
  488. entry->chan = channel;
  489. if (channel <= 14)
  490. entry->freq = 2407 + (5 * channel);
  491. else
  492. entry->freq = 5000 + (5 * channel);
  493. entry->val = value;
  494. entry->flag =
  495. IEEE80211_CHAN_W_IBSS |
  496. IEEE80211_CHAN_W_ACTIVE_SCAN |
  497. IEEE80211_CHAN_W_SCAN;
  498. entry->power_level = tx_power;
  499. entry->antenna_max = 0xff;
  500. }
  501. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  502. const int rate, const int mask,
  503. const int plcp, const int flags)
  504. {
  505. entry->rate = rate;
  506. entry->val =
  507. DEVICE_SET_RATE_FIELD(rate, RATE) |
  508. DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
  509. DEVICE_SET_RATE_FIELD(plcp, PLCP);
  510. entry->flags = flags;
  511. entry->val2 = entry->val;
  512. if (entry->flags & IEEE80211_RATE_PREAMBLE2)
  513. entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
  514. entry->min_rssi_ack = 0;
  515. entry->min_rssi_ack_delta = 0;
  516. }
  517. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  518. struct hw_mode_spec *spec)
  519. {
  520. struct ieee80211_hw *hw = rt2x00dev->hw;
  521. struct ieee80211_hw_mode *hwmodes;
  522. struct ieee80211_channel *channels;
  523. struct ieee80211_rate *rates;
  524. unsigned int i;
  525. unsigned char tx_power;
  526. hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
  527. if (!hwmodes)
  528. goto exit;
  529. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  530. if (!channels)
  531. goto exit_free_modes;
  532. rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
  533. if (!rates)
  534. goto exit_free_channels;
  535. /*
  536. * Initialize Rate list.
  537. */
  538. rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
  539. 0x00, IEEE80211_RATE_CCK);
  540. rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
  541. 0x01, IEEE80211_RATE_CCK_2);
  542. rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
  543. 0x02, IEEE80211_RATE_CCK_2);
  544. rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
  545. 0x03, IEEE80211_RATE_CCK_2);
  546. if (spec->num_rates > 4) {
  547. rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
  548. 0x0b, IEEE80211_RATE_OFDM);
  549. rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
  550. 0x0f, IEEE80211_RATE_OFDM);
  551. rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
  552. 0x0a, IEEE80211_RATE_OFDM);
  553. rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
  554. 0x0e, IEEE80211_RATE_OFDM);
  555. rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
  556. 0x09, IEEE80211_RATE_OFDM);
  557. rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
  558. 0x0d, IEEE80211_RATE_OFDM);
  559. rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
  560. 0x08, IEEE80211_RATE_OFDM);
  561. rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
  562. 0x0c, IEEE80211_RATE_OFDM);
  563. }
  564. /*
  565. * Initialize Channel list.
  566. */
  567. for (i = 0; i < spec->num_channels; i++) {
  568. if (spec->channels[i].channel <= 14)
  569. tx_power = spec->tx_power_bg[i];
  570. else if (spec->tx_power_a)
  571. tx_power = spec->tx_power_a[i];
  572. else
  573. tx_power = spec->tx_power_default;
  574. rt2x00lib_channel(&channels[i],
  575. spec->channels[i].channel, tx_power, i);
  576. }
  577. /*
  578. * Intitialize 802.11b
  579. * Rates: CCK.
  580. * Channels: OFDM.
  581. */
  582. if (spec->num_modes > HWMODE_B) {
  583. hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
  584. hwmodes[HWMODE_B].num_channels = 14;
  585. hwmodes[HWMODE_B].num_rates = 4;
  586. hwmodes[HWMODE_B].channels = channels;
  587. hwmodes[HWMODE_B].rates = rates;
  588. }
  589. /*
  590. * Intitialize 802.11g
  591. * Rates: CCK, OFDM.
  592. * Channels: OFDM.
  593. */
  594. if (spec->num_modes > HWMODE_G) {
  595. hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
  596. hwmodes[HWMODE_G].num_channels = 14;
  597. hwmodes[HWMODE_G].num_rates = spec->num_rates;
  598. hwmodes[HWMODE_G].channels = channels;
  599. hwmodes[HWMODE_G].rates = rates;
  600. }
  601. /*
  602. * Intitialize 802.11a
  603. * Rates: OFDM.
  604. * Channels: OFDM, UNII, HiperLAN2.
  605. */
  606. if (spec->num_modes > HWMODE_A) {
  607. hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
  608. hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
  609. hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
  610. hwmodes[HWMODE_A].channels = &channels[14];
  611. hwmodes[HWMODE_A].rates = &rates[4];
  612. }
  613. if (spec->num_modes > HWMODE_G &&
  614. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
  615. goto exit_free_rates;
  616. if (spec->num_modes > HWMODE_B &&
  617. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
  618. goto exit_free_rates;
  619. if (spec->num_modes > HWMODE_A &&
  620. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
  621. goto exit_free_rates;
  622. rt2x00dev->hwmodes = hwmodes;
  623. return 0;
  624. exit_free_rates:
  625. kfree(rates);
  626. exit_free_channels:
  627. kfree(channels);
  628. exit_free_modes:
  629. kfree(hwmodes);
  630. exit:
  631. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  632. return -ENOMEM;
  633. }
  634. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  635. {
  636. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  637. ieee80211_unregister_hw(rt2x00dev->hw);
  638. if (likely(rt2x00dev->hwmodes)) {
  639. kfree(rt2x00dev->hwmodes->channels);
  640. kfree(rt2x00dev->hwmodes->rates);
  641. kfree(rt2x00dev->hwmodes);
  642. rt2x00dev->hwmodes = NULL;
  643. }
  644. }
  645. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  646. {
  647. struct hw_mode_spec *spec = &rt2x00dev->spec;
  648. int status;
  649. /*
  650. * Initialize HW modes.
  651. */
  652. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  653. if (status)
  654. return status;
  655. /*
  656. * Register HW.
  657. */
  658. status = ieee80211_register_hw(rt2x00dev->hw);
  659. if (status) {
  660. rt2x00lib_remove_hw(rt2x00dev);
  661. return status;
  662. }
  663. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  664. return 0;
  665. }
  666. /*
  667. * Initialization/uninitialization handlers.
  668. */
  669. static int rt2x00lib_alloc_entries(struct data_ring *ring,
  670. const u16 max_entries, const u16 data_size,
  671. const u16 desc_size)
  672. {
  673. struct data_entry *entry;
  674. unsigned int i;
  675. ring->stats.limit = max_entries;
  676. ring->data_size = data_size;
  677. ring->desc_size = desc_size;
  678. /*
  679. * Allocate all ring entries.
  680. */
  681. entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
  682. if (!entry)
  683. return -ENOMEM;
  684. for (i = 0; i < ring->stats.limit; i++) {
  685. entry[i].flags = 0;
  686. entry[i].ring = ring;
  687. entry[i].skb = NULL;
  688. }
  689. ring->entry = entry;
  690. return 0;
  691. }
  692. static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
  693. {
  694. struct data_ring *ring;
  695. /*
  696. * Allocate the RX ring.
  697. */
  698. if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
  699. rt2x00dev->ops->rxd_size))
  700. return -ENOMEM;
  701. /*
  702. * First allocate the TX rings.
  703. */
  704. txring_for_each(rt2x00dev, ring) {
  705. if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
  706. rt2x00dev->ops->txd_size))
  707. return -ENOMEM;
  708. }
  709. if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  710. return 0;
  711. /*
  712. * Allocate the BEACON ring.
  713. */
  714. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
  715. MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
  716. return -ENOMEM;
  717. /*
  718. * Allocate the Atim ring.
  719. */
  720. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
  721. DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
  722. return -ENOMEM;
  723. return 0;
  724. }
  725. static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
  726. {
  727. struct data_ring *ring;
  728. ring_for_each(rt2x00dev, ring) {
  729. kfree(ring->entry);
  730. ring->entry = NULL;
  731. }
  732. }
  733. void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  734. {
  735. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  736. return;
  737. /*
  738. * Unregister rfkill.
  739. */
  740. rt2x00rfkill_unregister(rt2x00dev);
  741. /*
  742. * Allow the HW to uninitialize.
  743. */
  744. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  745. /*
  746. * Free allocated ring entries.
  747. */
  748. rt2x00lib_free_ring_entries(rt2x00dev);
  749. }
  750. int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  751. {
  752. int status;
  753. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  754. return 0;
  755. /*
  756. * Allocate all ring entries.
  757. */
  758. status = rt2x00lib_alloc_ring_entries(rt2x00dev);
  759. if (status) {
  760. ERROR(rt2x00dev, "Ring entries allocation failed.\n");
  761. return status;
  762. }
  763. /*
  764. * Initialize the device.
  765. */
  766. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  767. if (status)
  768. goto exit;
  769. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  770. /*
  771. * Register the rfkill handler.
  772. */
  773. status = rt2x00rfkill_register(rt2x00dev);
  774. if (status)
  775. goto exit_unitialize;
  776. return 0;
  777. exit_unitialize:
  778. rt2x00lib_uninitialize(rt2x00dev);
  779. exit:
  780. rt2x00lib_free_ring_entries(rt2x00dev);
  781. return status;
  782. }
  783. /*
  784. * driver allocation handlers.
  785. */
  786. static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
  787. {
  788. struct data_ring *ring;
  789. /*
  790. * We need the following rings:
  791. * RX: 1
  792. * TX: hw->queues
  793. * Beacon: 1 (if required)
  794. * Atim: 1 (if required)
  795. */
  796. rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
  797. (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
  798. ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
  799. if (!ring) {
  800. ERROR(rt2x00dev, "Ring allocation failed.\n");
  801. return -ENOMEM;
  802. }
  803. /*
  804. * Initialize pointers
  805. */
  806. rt2x00dev->rx = ring;
  807. rt2x00dev->tx = &rt2x00dev->rx[1];
  808. if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  809. rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
  810. /*
  811. * Initialize ring parameters.
  812. * cw_min: 2^5 = 32.
  813. * cw_max: 2^10 = 1024.
  814. */
  815. ring_for_each(rt2x00dev, ring) {
  816. ring->rt2x00dev = rt2x00dev;
  817. ring->tx_params.aifs = 2;
  818. ring->tx_params.cw_min = 5;
  819. ring->tx_params.cw_max = 10;
  820. }
  821. return 0;
  822. }
  823. static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
  824. {
  825. kfree(rt2x00dev->rx);
  826. rt2x00dev->rx = NULL;
  827. rt2x00dev->tx = NULL;
  828. rt2x00dev->bcn = NULL;
  829. }
  830. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  831. {
  832. int retval = -ENOMEM;
  833. /*
  834. * Let the driver probe the device to detect the capabilities.
  835. */
  836. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  837. if (retval) {
  838. ERROR(rt2x00dev, "Failed to allocate device.\n");
  839. goto exit;
  840. }
  841. /*
  842. * Initialize configuration work.
  843. */
  844. INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
  845. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  846. INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
  847. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  848. /*
  849. * Reset current working type.
  850. */
  851. rt2x00dev->interface.type = INVALID_INTERFACE;
  852. /*
  853. * Allocate ring array.
  854. */
  855. retval = rt2x00lib_alloc_rings(rt2x00dev);
  856. if (retval)
  857. goto exit;
  858. /*
  859. * Initialize ieee80211 structure.
  860. */
  861. retval = rt2x00lib_probe_hw(rt2x00dev);
  862. if (retval) {
  863. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  864. goto exit;
  865. }
  866. /*
  867. * Allocatie rfkill.
  868. */
  869. retval = rt2x00rfkill_allocate(rt2x00dev);
  870. if (retval)
  871. goto exit;
  872. /*
  873. * Open the debugfs entry.
  874. */
  875. rt2x00debug_register(rt2x00dev);
  876. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  877. return 0;
  878. exit:
  879. rt2x00lib_remove_dev(rt2x00dev);
  880. return retval;
  881. }
  882. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  883. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  884. {
  885. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  886. /*
  887. * Disable radio.
  888. */
  889. rt2x00lib_disable_radio(rt2x00dev);
  890. /*
  891. * Uninitialize device.
  892. */
  893. rt2x00lib_uninitialize(rt2x00dev);
  894. /*
  895. * Close debugfs entry.
  896. */
  897. rt2x00debug_deregister(rt2x00dev);
  898. /*
  899. * Free rfkill
  900. */
  901. rt2x00rfkill_free(rt2x00dev);
  902. /*
  903. * Free ieee80211_hw memory.
  904. */
  905. rt2x00lib_remove_hw(rt2x00dev);
  906. /*
  907. * Free firmware image.
  908. */
  909. rt2x00lib_free_firmware(rt2x00dev);
  910. /*
  911. * Free ring structures.
  912. */
  913. rt2x00lib_free_rings(rt2x00dev);
  914. }
  915. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  916. /*
  917. * Device state handlers
  918. */
  919. #ifdef CONFIG_PM
  920. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  921. {
  922. int retval;
  923. NOTICE(rt2x00dev, "Going to sleep.\n");
  924. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  925. /*
  926. * Only continue if mac80211 has open interfaces.
  927. */
  928. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  929. goto exit;
  930. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  931. /*
  932. * Disable radio and unitialize all items
  933. * that must be recreated on resume.
  934. */
  935. rt2x00mac_stop(rt2x00dev->hw);
  936. rt2x00lib_uninitialize(rt2x00dev);
  937. rt2x00debug_deregister(rt2x00dev);
  938. exit:
  939. /*
  940. * Set device mode to sleep for power management.
  941. */
  942. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  943. if (retval)
  944. return retval;
  945. return 0;
  946. }
  947. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  948. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  949. {
  950. struct interface *intf = &rt2x00dev->interface;
  951. int retval;
  952. NOTICE(rt2x00dev, "Waking up.\n");
  953. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  954. /*
  955. * Open the debugfs entry.
  956. */
  957. rt2x00debug_register(rt2x00dev);
  958. /*
  959. * Only continue if mac80211 had open interfaces.
  960. */
  961. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  962. return 0;
  963. /*
  964. * Reinitialize device and all active interfaces.
  965. */
  966. retval = rt2x00mac_start(rt2x00dev->hw);
  967. if (retval)
  968. goto exit;
  969. /*
  970. * Reconfigure device.
  971. */
  972. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  973. if (!rt2x00dev->hw->conf.radio_enabled)
  974. rt2x00lib_disable_radio(rt2x00dev);
  975. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  976. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  977. rt2x00lib_config_type(rt2x00dev, intf->type);
  978. /*
  979. * It is possible that during that mac80211 has attempted
  980. * to send frames while we were suspending or resuming.
  981. * In that case we have disabled the TX queue and should
  982. * now enable it again
  983. */
  984. ieee80211_start_queues(rt2x00dev->hw);
  985. /*
  986. * When in Master or Ad-hoc mode,
  987. * restart Beacon transmitting by faking a beacondone event.
  988. */
  989. if (intf->type == IEEE80211_IF_TYPE_AP ||
  990. intf->type == IEEE80211_IF_TYPE_IBSS)
  991. rt2x00lib_beacondone(rt2x00dev);
  992. return 0;
  993. exit:
  994. rt2x00lib_disable_radio(rt2x00dev);
  995. rt2x00lib_uninitialize(rt2x00dev);
  996. rt2x00debug_deregister(rt2x00dev);
  997. return retval;
  998. }
  999. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1000. #endif /* CONFIG_PM */
  1001. /*
  1002. * rt2x00lib module information.
  1003. */
  1004. MODULE_AUTHOR(DRV_PROJECT);
  1005. MODULE_VERSION(DRV_VERSION);
  1006. MODULE_DESCRIPTION("rt2x00 library");
  1007. MODULE_LICENSE("GPL");