rt2x00dev.c 26 KB

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