rt2x00: Driver requiring firmware should select crc algo
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
1 /*
2         Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the
17         Free Software Foundation, Inc.,
18         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /*
22         Module: rt2x00lib
23         Abstract: rt2x00 generic device routines.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
31 #include "rt2x00dump.h"
32
33 /*
34  * Link tuning handlers
35  */
36 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
37 {
38         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
39                 return;
40
41         /*
42          * Reset link information.
43          * Both the currently active vgc level as well as
44          * the link tuner counter should be reset. Resetting
45          * the counter is important for devices where the
46          * device should only perform link tuning during the
47          * first minute after being enabled.
48          */
49         rt2x00dev->link.count = 0;
50         rt2x00dev->link.vgc_level = 0;
51
52         /*
53          * Reset the link tuner.
54          */
55         rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
56 }
57
58 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
59 {
60         /*
61          * Clear all (possibly) pre-existing quality statistics.
62          */
63         memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
64
65         /*
66          * The RX and TX percentage should start at 50%
67          * this will assure we will get at least get some
68          * decent value when the link tuner starts.
69          * The value will be dropped and overwritten with
70          * the correct (measured )value anyway during the
71          * first run of the link tuner.
72          */
73         rt2x00dev->link.qual.rx_percentage = 50;
74         rt2x00dev->link.qual.tx_percentage = 50;
75
76         rt2x00lib_reset_link_tuner(rt2x00dev);
77
78         queue_delayed_work(rt2x00dev->hw->workqueue,
79                            &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
80 }
81
82 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
83 {
84         cancel_delayed_work_sync(&rt2x00dev->link.work);
85 }
86
87 /*
88  * Radio control handlers.
89  */
90 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
91 {
92         int status;
93
94         /*
95          * Don't enable the radio twice.
96          * And check if the hardware button has been disabled.
97          */
98         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
99             test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
100                 return 0;
101
102         /*
103          * Initialize all data queues.
104          */
105         rt2x00queue_init_rx(rt2x00dev);
106         rt2x00queue_init_tx(rt2x00dev);
107
108         /*
109          * Enable radio.
110          */
111         status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
112                                                        STATE_RADIO_ON);
113         if (status)
114                 return status;
115
116         __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
117
118         /*
119          * Enable RX.
120          */
121         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
122
123         /*
124          * Start the TX queues.
125          */
126         ieee80211_start_queues(rt2x00dev->hw);
127
128         return 0;
129 }
130
131 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
132 {
133         if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
134                 return;
135
136         /*
137          * Stop all scheduled work.
138          */
139         if (work_pending(&rt2x00dev->beacon_work))
140                 cancel_work_sync(&rt2x00dev->beacon_work);
141         if (work_pending(&rt2x00dev->filter_work))
142                 cancel_work_sync(&rt2x00dev->filter_work);
143         if (work_pending(&rt2x00dev->config_work))
144                 cancel_work_sync(&rt2x00dev->config_work);
145
146         /*
147          * Stop the TX queues.
148          */
149         ieee80211_stop_queues(rt2x00dev->hw);
150
151         /*
152          * Disable RX.
153          */
154         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
155
156         /*
157          * Disable radio.
158          */
159         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
160 }
161
162 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
163 {
164         /*
165          * When we are disabling the RX, we should also stop the link tuner.
166          */
167         if (state == STATE_RADIO_RX_OFF)
168                 rt2x00lib_stop_link_tuner(rt2x00dev);
169
170         rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
171
172         /*
173          * When we are enabling the RX, we should also start the link tuner.
174          */
175         if (state == STATE_RADIO_RX_ON &&
176             is_interface_present(&rt2x00dev->interface))
177                 rt2x00lib_start_link_tuner(rt2x00dev);
178 }
179
180 static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
181 {
182         enum antenna rx = rt2x00dev->link.ant.active.rx;
183         enum antenna tx = rt2x00dev->link.ant.active.tx;
184         int sample_a =
185             rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
186         int sample_b =
187             rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
188
189         /*
190          * We are done sampling. Now we should evaluate the results.
191          */
192         rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
193
194         /*
195          * During the last period we have sampled the RSSI
196          * from both antenna's. It now is time to determine
197          * which antenna demonstrated the best performance.
198          * When we are already on the antenna with the best
199          * performance, then there really is nothing for us
200          * left to do.
201          */
202         if (sample_a == sample_b)
203                 return;
204
205         if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
206                 rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
207
208         if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
209                 tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
210
211         rt2x00lib_config_antenna(rt2x00dev, rx, tx);
212 }
213
214 static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
215 {
216         enum antenna rx = rt2x00dev->link.ant.active.rx;
217         enum antenna tx = rt2x00dev->link.ant.active.tx;
218         int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
219         int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
220
221         /*
222          * Legacy driver indicates that we should swap antenna's
223          * when the difference in RSSI is greater that 5. This
224          * also should be done when the RSSI was actually better
225          * then the previous sample.
226          * When the difference exceeds the threshold we should
227          * sample the rssi from the other antenna to make a valid
228          * comparison between the 2 antennas.
229          */
230         if (abs(rssi_curr - rssi_old) < 5)
231                 return;
232
233         rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
234
235         if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
236                 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
237
238         if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
239                 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
240
241         rt2x00lib_config_antenna(rt2x00dev, rx, tx);
242 }
243
244 static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
245 {
246         /*
247          * Determine if software diversity is enabled for
248          * either the TX or RX antenna (or both).
249          * Always perform this check since within the link
250          * tuner interval the configuration might have changed.
251          */
252         rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
253         rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
254
255         if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
256             rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
257                 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
258         if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
259             rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
260                 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
261
262         if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
263             !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
264                 rt2x00dev->link.ant.flags = 0;
265                 return;
266         }
267
268         /*
269          * If we have only sampled the data over the last period
270          * we should now harvest the data. Otherwise just evaluate
271          * the data. The latter should only be performed once
272          * every 2 seconds.
273          */
274         if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
275                 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
276         else if (rt2x00dev->link.count & 1)
277                 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
278 }
279
280 static void rt2x00lib_update_link_stats(struct link *link, int rssi)
281 {
282         int avg_rssi = rssi;
283
284         /*
285          * Update global RSSI
286          */
287         if (link->qual.avg_rssi)
288                 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
289         link->qual.avg_rssi = avg_rssi;
290
291         /*
292          * Update antenna RSSI
293          */
294         if (link->ant.rssi_ant)
295                 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
296         link->ant.rssi_ant = rssi;
297 }
298
299 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
300 {
301         if (qual->rx_failed || qual->rx_success)
302                 qual->rx_percentage =
303                     (qual->rx_success * 100) /
304                     (qual->rx_failed + qual->rx_success);
305         else
306                 qual->rx_percentage = 50;
307
308         if (qual->tx_failed || qual->tx_success)
309                 qual->tx_percentage =
310                     (qual->tx_success * 100) /
311                     (qual->tx_failed + qual->tx_success);
312         else
313                 qual->tx_percentage = 50;
314
315         qual->rx_success = 0;
316         qual->rx_failed = 0;
317         qual->tx_success = 0;
318         qual->tx_failed = 0;
319 }
320
321 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
322                                            int rssi)
323 {
324         int rssi_percentage = 0;
325         int signal;
326
327         /*
328          * We need a positive value for the RSSI.
329          */
330         if (rssi < 0)
331                 rssi += rt2x00dev->rssi_offset;
332
333         /*
334          * Calculate the different percentages,
335          * which will be used for the signal.
336          */
337         if (rt2x00dev->rssi_offset)
338                 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
339
340         /*
341          * Add the individual percentages and use the WEIGHT
342          * defines to calculate the current link signal.
343          */
344         signal = ((WEIGHT_RSSI * rssi_percentage) +
345                   (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
346                   (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
347
348         return (signal > 100) ? 100 : signal;
349 }
350
351 static void rt2x00lib_link_tuner(struct work_struct *work)
352 {
353         struct rt2x00_dev *rt2x00dev =
354             container_of(work, struct rt2x00_dev, link.work.work);
355
356         /*
357          * When the radio is shutting down we should
358          * immediately cease all link tuning.
359          */
360         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
361                 return;
362
363         /*
364          * Update statistics.
365          */
366         rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
367         rt2x00dev->low_level_stats.dot11FCSErrorCount +=
368             rt2x00dev->link.qual.rx_failed;
369
370         /*
371          * Only perform the link tuning when Link tuning
372          * has been enabled (This could have been disabled from the EEPROM).
373          */
374         if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
375                 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
376
377         /*
378          * Precalculate a portion of the link signal which is
379          * in based on the tx/rx success/failure counters.
380          */
381         rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
382
383         /*
384          * Evaluate antenna setup, make this the last step since this could
385          * possibly reset some statistics.
386          */
387         rt2x00lib_evaluate_antenna(rt2x00dev);
388
389         /*
390          * Increase tuner counter, and reschedule the next link tuner run.
391          */
392         rt2x00dev->link.count++;
393         queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
394                            LINK_TUNE_INTERVAL);
395 }
396
397 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
398 {
399         struct rt2x00_dev *rt2x00dev =
400             container_of(work, struct rt2x00_dev, filter_work);
401         unsigned int filter = rt2x00dev->packet_filter;
402
403         /*
404          * Since we had stored the filter inside interface.filter,
405          * we should now clear that field. Otherwise the driver will
406          * assume nothing has changed (*total_flags will be compared
407          * to interface.filter to determine if any action is required).
408          */
409         rt2x00dev->packet_filter = 0;
410
411         rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
412                                              filter, &filter, 0, NULL);
413 }
414
415 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
416 {
417         struct rt2x00_dev *rt2x00dev =
418             container_of(work, struct rt2x00_dev, config_work);
419         struct ieee80211_bss_conf bss_conf;
420
421         bss_conf.use_short_preamble =
422                 test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
423
424         /*
425          * FIXME: shouldn't invoke it this way because all other contents
426          *        of bss_conf is invalid.
427          */
428         rt2x00mac_bss_info_changed(rt2x00dev->hw, rt2x00dev->interface.id,
429                                    &bss_conf, BSS_CHANGED_ERP_PREAMBLE);
430 }
431
432 /*
433  * Interrupt context handlers.
434  */
435 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
436 {
437         struct rt2x00_dev *rt2x00dev =
438             container_of(work, struct rt2x00_dev, beacon_work);
439         struct ieee80211_tx_control control;
440         struct sk_buff *skb;
441
442         skb = ieee80211_beacon_get(rt2x00dev->hw,
443                                    rt2x00dev->interface.id, &control);
444         if (!skb)
445                 return;
446
447         rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb, &control);
448
449         dev_kfree_skb(skb);
450 }
451
452 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
453 {
454         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
455                 return;
456
457         queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
458 }
459 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
460
461 void rt2x00lib_txdone(struct queue_entry *entry,
462                       struct txdone_entry_desc *txdesc)
463 {
464         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
465         struct ieee80211_tx_status tx_status;
466         int success = !!(txdesc->status == TX_SUCCESS ||
467                          txdesc->status == TX_SUCCESS_RETRY);
468         int fail = !!(txdesc->status == TX_FAIL_RETRY ||
469                       txdesc->status == TX_FAIL_INVALID ||
470                       txdesc->status == TX_FAIL_OTHER);
471
472         /*
473          * Update TX statistics.
474          */
475         rt2x00dev->link.qual.tx_success += success;
476         rt2x00dev->link.qual.tx_failed += txdesc->retry + fail;
477
478         /*
479          * Initialize TX status
480          */
481         tx_status.flags = 0;
482         tx_status.ack_signal = 0;
483         tx_status.excessive_retries = (txdesc->status == TX_FAIL_RETRY);
484         tx_status.retry_count = txdesc->retry;
485         memcpy(&tx_status.control, txdesc->control, sizeof(txdesc->control));
486
487         if (!(tx_status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
488                 if (success)
489                         tx_status.flags |= IEEE80211_TX_STATUS_ACK;
490                 else
491                         rt2x00dev->low_level_stats.dot11ACKFailureCount++;
492         }
493
494         tx_status.queue_length = entry->queue->limit;
495         tx_status.queue_number = tx_status.control.queue;
496
497         if (tx_status.control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
498                 if (success)
499                         rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
500                 else
501                         rt2x00dev->low_level_stats.dot11RTSFailureCount++;
502         }
503
504         /*
505          * Send the tx_status to mac80211 & debugfs.
506          * mac80211 will clean up the skb structure.
507          */
508         get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
509         rt2x00debug_dump_frame(rt2x00dev, entry->skb);
510         ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, &tx_status);
511         entry->skb = NULL;
512 }
513 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
514
515 void rt2x00lib_rxdone(struct queue_entry *entry,
516                       struct rxdone_entry_desc *rxdesc)
517 {
518         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
519         struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
520         struct ieee80211_hw_mode *mode;
521         struct ieee80211_rate *rate;
522         struct ieee80211_hdr *hdr;
523         unsigned int i;
524         int val = 0;
525         u16 fc;
526
527         /*
528          * Update RX statistics.
529          */
530         mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
531         for (i = 0; i < mode->num_rates; i++) {
532                 rate = &mode->rates[i];
533
534                 /*
535                  * When frame was received with an OFDM bitrate,
536                  * the signal is the PLCP value. If it was received with
537                  * a CCK bitrate the signal is the rate in 0.5kbit/s.
538                  */
539                 if (!rxdesc->ofdm)
540                         val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
541                 else
542                         val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
543
544                 if (val == rxdesc->signal) {
545                         val = rate->val;
546                         break;
547                 }
548         }
549
550         /*
551          * Only update link status if this is a beacon frame carrying our bssid.
552          */
553         hdr = (struct ieee80211_hdr*)entry->skb->data;
554         fc = le16_to_cpu(hdr->frame_control);
555         if (is_beacon(fc) && rxdesc->my_bss)
556                 rt2x00lib_update_link_stats(&rt2x00dev->link, rxdesc->rssi);
557
558         rt2x00dev->link.qual.rx_success++;
559
560         rx_status->rate = val;
561         rx_status->signal =
562             rt2x00lib_calculate_link_signal(rt2x00dev, rxdesc->rssi);
563         rx_status->ssi = rxdesc->rssi;
564         rx_status->flag = rxdesc->flags;
565         rx_status->antenna = rt2x00dev->link.ant.active.rx;
566
567         /*
568          * Send frame to mac80211 & debugfs.
569          * mac80211 will clean up the skb structure.
570          */
571         get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_RXDONE;
572         rt2x00debug_dump_frame(rt2x00dev, entry->skb);
573         ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
574         entry->skb = NULL;
575 }
576 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
577
578 /*
579  * TX descriptor initializer
580  */
581 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
582                              struct sk_buff *skb,
583                              struct ieee80211_tx_control *control)
584 {
585         struct txentry_desc txdesc;
586         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
587         struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
588         int tx_rate;
589         int bitrate;
590         int length;
591         int duration;
592         int residual;
593         u16 frame_control;
594         u16 seq_ctrl;
595
596         memset(&txdesc, 0, sizeof(txdesc));
597
598         txdesc.cw_min = skbdesc->entry->queue->cw_min;
599         txdesc.cw_max = skbdesc->entry->queue->cw_max;
600         txdesc.aifs = skbdesc->entry->queue->aifs;
601
602         /*
603          * Identify queue
604          */
605         if (control->queue < rt2x00dev->hw->queues)
606                 txdesc.queue = control->queue;
607         else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
608                  control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
609                 txdesc.queue = QID_MGMT;
610         else
611                 txdesc.queue = QID_OTHER;
612
613         /*
614          * Read required fields from ieee80211 header.
615          */
616         frame_control = le16_to_cpu(ieee80211hdr->frame_control);
617         seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
618
619         tx_rate = control->tx_rate;
620
621         /*
622          * Check whether this frame is to be acked
623          */
624         if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
625                 __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
626
627         /*
628          * Check if this is a RTS/CTS frame
629          */
630         if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
631                 __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
632                 if (is_rts_frame(frame_control)) {
633                         __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags);
634                         __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
635                 } else
636                         __clear_bit(ENTRY_TXD_ACK, &txdesc.flags);
637                 if (control->rts_cts_rate)
638                         tx_rate = control->rts_cts_rate;
639         }
640
641         /*
642          * Check for OFDM
643          */
644         if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
645                 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags);
646
647         /*
648          * Check if more fragments are pending
649          */
650         if (ieee80211_get_morefrag(ieee80211hdr)) {
651                 __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
652                 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc.flags);
653         }
654
655         /*
656          * Beacons and probe responses require the tsf timestamp
657          * to be inserted into the frame.
658          */
659         if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
660             is_probe_resp(frame_control))
661                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc.flags);
662
663         /*
664          * Determine with what IFS priority this frame should be send.
665          * Set ifs to IFS_SIFS when the this is not the first fragment,
666          * or this fragment came after RTS/CTS.
667          */
668         if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
669             test_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags))
670                 txdesc.ifs = IFS_SIFS;
671         else
672                 txdesc.ifs = IFS_BACKOFF;
673
674         /*
675          * PLCP setup
676          * Length calculation depends on OFDM/CCK rate.
677          */
678         txdesc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
679         txdesc.service = 0x04;
680
681         length = skb->len + FCS_LEN;
682         if (test_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags)) {
683                 txdesc.length_high = (length >> 6) & 0x3f;
684                 txdesc.length_low = length & 0x3f;
685         } else {
686                 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
687
688                 /*
689                  * Convert length to microseconds.
690                  */
691                 residual = get_duration_res(length, bitrate);
692                 duration = get_duration(length, bitrate);
693
694                 if (residual != 0) {
695                         duration++;
696
697                         /*
698                          * Check if we need to set the Length Extension
699                          */
700                         if (bitrate == 110 && residual <= 30)
701                                 txdesc.service |= 0x80;
702                 }
703
704                 txdesc.length_high = (duration >> 8) & 0xff;
705                 txdesc.length_low = duration & 0xff;
706
707                 /*
708                  * When preamble is enabled we should set the
709                  * preamble bit for the signal.
710                  */
711                 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
712                         txdesc.signal |= 0x08;
713         }
714
715         rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &txdesc, control);
716
717         /*
718          * Update queue entry.
719          */
720         skbdesc->entry->skb = skb;
721
722         /*
723          * The frame has been completely initialized and ready
724          * for sending to the device. The caller will push the
725          * frame to the device, but we are going to push the
726          * frame to debugfs here.
727          */
728         skbdesc->frame_type = DUMP_FRAME_TX;
729         rt2x00debug_dump_frame(rt2x00dev, skb);
730 }
731 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
732
733 /*
734  * Driver initialization handlers.
735  */
736 static void rt2x00lib_channel(struct ieee80211_channel *entry,
737                               const int channel, const int tx_power,
738                               const int value)
739 {
740         entry->chan = channel;
741         if (channel <= 14)
742                 entry->freq = 2407 + (5 * channel);
743         else
744                 entry->freq = 5000 + (5 * channel);
745         entry->val = value;
746         entry->flag =
747             IEEE80211_CHAN_W_IBSS |
748             IEEE80211_CHAN_W_ACTIVE_SCAN |
749             IEEE80211_CHAN_W_SCAN;
750         entry->power_level = tx_power;
751         entry->antenna_max = 0xff;
752 }
753
754 static void rt2x00lib_rate(struct ieee80211_rate *entry,
755                            const int rate, const int mask,
756                            const int plcp, const int flags)
757 {
758         entry->rate = rate;
759         entry->val =
760             DEVICE_SET_RATE_FIELD(rate, RATE) |
761             DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
762             DEVICE_SET_RATE_FIELD(plcp, PLCP);
763         entry->flags = flags;
764         entry->val2 = entry->val;
765         if (entry->flags & IEEE80211_RATE_PREAMBLE2)
766                 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
767         entry->min_rssi_ack = 0;
768         entry->min_rssi_ack_delta = 0;
769 }
770
771 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
772                                     struct hw_mode_spec *spec)
773 {
774         struct ieee80211_hw *hw = rt2x00dev->hw;
775         struct ieee80211_hw_mode *hwmodes;
776         struct ieee80211_channel *channels;
777         struct ieee80211_rate *rates;
778         unsigned int i;
779         unsigned char tx_power;
780
781         hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
782         if (!hwmodes)
783                 goto exit;
784
785         channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
786         if (!channels)
787                 goto exit_free_modes;
788
789         rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
790         if (!rates)
791                 goto exit_free_channels;
792
793         /*
794          * Initialize Rate list.
795          */
796         rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
797                        0x00, IEEE80211_RATE_CCK);
798         rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
799                        0x01, IEEE80211_RATE_CCK_2);
800         rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
801                        0x02, IEEE80211_RATE_CCK_2);
802         rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
803                        0x03, IEEE80211_RATE_CCK_2);
804
805         if (spec->num_rates > 4) {
806                 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
807                                0x0b, IEEE80211_RATE_OFDM);
808                 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
809                                0x0f, IEEE80211_RATE_OFDM);
810                 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
811                                0x0a, IEEE80211_RATE_OFDM);
812                 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
813                                0x0e, IEEE80211_RATE_OFDM);
814                 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
815                                0x09, IEEE80211_RATE_OFDM);
816                 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
817                                0x0d, IEEE80211_RATE_OFDM);
818                 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
819                                0x08, IEEE80211_RATE_OFDM);
820                 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
821                                0x0c, IEEE80211_RATE_OFDM);
822         }
823
824         /*
825          * Initialize Channel list.
826          */
827         for (i = 0; i < spec->num_channels; i++) {
828                 if (spec->channels[i].channel <= 14)
829                         tx_power = spec->tx_power_bg[i];
830                 else if (spec->tx_power_a)
831                         tx_power = spec->tx_power_a[i];
832                 else
833                         tx_power = spec->tx_power_default;
834
835                 rt2x00lib_channel(&channels[i],
836                                   spec->channels[i].channel, tx_power, i);
837         }
838
839         /*
840          * Intitialize 802.11b
841          * Rates: CCK.
842          * Channels: OFDM.
843          */
844         if (spec->num_modes > HWMODE_B) {
845                 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
846                 hwmodes[HWMODE_B].num_channels = 14;
847                 hwmodes[HWMODE_B].num_rates = 4;
848                 hwmodes[HWMODE_B].channels = channels;
849                 hwmodes[HWMODE_B].rates = rates;
850         }
851
852         /*
853          * Intitialize 802.11g
854          * Rates: CCK, OFDM.
855          * Channels: OFDM.
856          */
857         if (spec->num_modes > HWMODE_G) {
858                 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
859                 hwmodes[HWMODE_G].num_channels = 14;
860                 hwmodes[HWMODE_G].num_rates = spec->num_rates;
861                 hwmodes[HWMODE_G].channels = channels;
862                 hwmodes[HWMODE_G].rates = rates;
863         }
864
865         /*
866          * Intitialize 802.11a
867          * Rates: OFDM.
868          * Channels: OFDM, UNII, HiperLAN2.
869          */
870         if (spec->num_modes > HWMODE_A) {
871                 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
872                 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
873                 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
874                 hwmodes[HWMODE_A].channels = &channels[14];
875                 hwmodes[HWMODE_A].rates = &rates[4];
876         }
877
878         if (spec->num_modes > HWMODE_G &&
879             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
880                 goto exit_free_rates;
881
882         if (spec->num_modes > HWMODE_B &&
883             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
884                 goto exit_free_rates;
885
886         if (spec->num_modes > HWMODE_A &&
887             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
888                 goto exit_free_rates;
889
890         rt2x00dev->hwmodes = hwmodes;
891
892         return 0;
893
894 exit_free_rates:
895         kfree(rates);
896
897 exit_free_channels:
898         kfree(channels);
899
900 exit_free_modes:
901         kfree(hwmodes);
902
903 exit:
904         ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
905         return -ENOMEM;
906 }
907
908 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
909 {
910         if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
911                 ieee80211_unregister_hw(rt2x00dev->hw);
912
913         if (likely(rt2x00dev->hwmodes)) {
914                 kfree(rt2x00dev->hwmodes->channels);
915                 kfree(rt2x00dev->hwmodes->rates);
916                 kfree(rt2x00dev->hwmodes);
917                 rt2x00dev->hwmodes = NULL;
918         }
919 }
920
921 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
922 {
923         struct hw_mode_spec *spec = &rt2x00dev->spec;
924         int status;
925
926         /*
927          * Initialize HW modes.
928          */
929         status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
930         if (status)
931                 return status;
932
933         /*
934          * Register HW.
935          */
936         status = ieee80211_register_hw(rt2x00dev->hw);
937         if (status) {
938                 rt2x00lib_remove_hw(rt2x00dev);
939                 return status;
940         }
941
942         __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
943
944         return 0;
945 }
946
947 /*
948  * Initialization/uninitialization handlers.
949  */
950 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
951 {
952         if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
953                 return;
954
955         /*
956          * Unregister rfkill.
957          */
958         rt2x00rfkill_unregister(rt2x00dev);
959
960         /*
961          * Allow the HW to uninitialize.
962          */
963         rt2x00dev->ops->lib->uninitialize(rt2x00dev);
964
965         /*
966          * Free allocated queue entries.
967          */
968         rt2x00queue_uninitialize(rt2x00dev);
969 }
970
971 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
972 {
973         int status;
974
975         if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
976                 return 0;
977
978         /*
979          * Allocate all queue entries.
980          */
981         status = rt2x00queue_initialize(rt2x00dev);
982         if (status)
983                 return status;
984
985         /*
986          * Initialize the device.
987          */
988         status = rt2x00dev->ops->lib->initialize(rt2x00dev);
989         if (status)
990                 goto exit;
991
992         __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
993
994         /*
995          * Register the rfkill handler.
996          */
997         status = rt2x00rfkill_register(rt2x00dev);
998         if (status)
999                 goto exit;
1000
1001         return 0;
1002
1003 exit:
1004         rt2x00lib_uninitialize(rt2x00dev);
1005
1006         return status;
1007 }
1008
1009 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
1010 {
1011         int retval;
1012
1013         if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1014                 return 0;
1015
1016         /*
1017          * If this is the first interface which is added,
1018          * we should load the firmware now.
1019          */
1020         retval = rt2x00lib_load_firmware(rt2x00dev);
1021         if (retval)
1022                 return retval;
1023
1024         /*
1025          * Initialize the device.
1026          */
1027         retval = rt2x00lib_initialize(rt2x00dev);
1028         if (retval)
1029                 return retval;
1030
1031         /*
1032          * Enable radio.
1033          */
1034         retval = rt2x00lib_enable_radio(rt2x00dev);
1035         if (retval) {
1036                 rt2x00lib_uninitialize(rt2x00dev);
1037                 return retval;
1038         }
1039
1040         __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
1041
1042         return 0;
1043 }
1044
1045 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
1046 {
1047         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1048                 return;
1049
1050         /*
1051          * Perhaps we can add something smarter here,
1052          * but for now just disabling the radio should do.
1053          */
1054         rt2x00lib_disable_radio(rt2x00dev);
1055
1056         __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
1057 }
1058
1059 /*
1060  * driver allocation handlers.
1061  */
1062 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1063 {
1064         int retval = -ENOMEM;
1065
1066         /*
1067          * Let the driver probe the device to detect the capabilities.
1068          */
1069         retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1070         if (retval) {
1071                 ERROR(rt2x00dev, "Failed to allocate device.\n");
1072                 goto exit;
1073         }
1074
1075         /*
1076          * Initialize configuration work.
1077          */
1078         INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1079         INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1080         INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1081         INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1082
1083         /*
1084          * Reset current working type.
1085          */
1086         rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
1087
1088         /*
1089          * Allocate queue array.
1090          */
1091         retval = rt2x00queue_allocate(rt2x00dev);
1092         if (retval)
1093                 goto exit;
1094
1095         /*
1096          * Initialize ieee80211 structure.
1097          */
1098         retval = rt2x00lib_probe_hw(rt2x00dev);
1099         if (retval) {
1100                 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1101                 goto exit;
1102         }
1103
1104         /*
1105          * Allocatie rfkill.
1106          */
1107         retval = rt2x00rfkill_allocate(rt2x00dev);
1108         if (retval)
1109                 goto exit;
1110
1111         /*
1112          * Open the debugfs entry.
1113          */
1114         rt2x00debug_register(rt2x00dev);
1115
1116         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1117
1118         return 0;
1119
1120 exit:
1121         rt2x00lib_remove_dev(rt2x00dev);
1122
1123         return retval;
1124 }
1125 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1126
1127 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1128 {
1129         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1130
1131         /*
1132          * Disable radio.
1133          */
1134         rt2x00lib_disable_radio(rt2x00dev);
1135
1136         /*
1137          * Uninitialize device.
1138          */
1139         rt2x00lib_uninitialize(rt2x00dev);
1140
1141         /*
1142          * Close debugfs entry.
1143          */
1144         rt2x00debug_deregister(rt2x00dev);
1145
1146         /*
1147          * Free rfkill
1148          */
1149         rt2x00rfkill_free(rt2x00dev);
1150
1151         /*
1152          * Free ieee80211_hw memory.
1153          */
1154         rt2x00lib_remove_hw(rt2x00dev);
1155
1156         /*
1157          * Free firmware image.
1158          */
1159         rt2x00lib_free_firmware(rt2x00dev);
1160
1161         /*
1162          * Free queue structures.
1163          */
1164         rt2x00queue_free(rt2x00dev);
1165 }
1166 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1167
1168 /*
1169  * Device state handlers
1170  */
1171 #ifdef CONFIG_PM
1172 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1173 {
1174         int retval;
1175
1176         NOTICE(rt2x00dev, "Going to sleep.\n");
1177         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1178
1179         /*
1180          * Only continue if mac80211 has open interfaces.
1181          */
1182         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1183                 goto exit;
1184         __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1185
1186         /*
1187          * Disable radio and unitialize all items
1188          * that must be recreated on resume.
1189          */
1190         rt2x00lib_stop(rt2x00dev);
1191         rt2x00lib_uninitialize(rt2x00dev);
1192         rt2x00debug_deregister(rt2x00dev);
1193
1194 exit:
1195         /*
1196          * Set device mode to sleep for power management.
1197          */
1198         retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1199         if (retval)
1200                 return retval;
1201
1202         return 0;
1203 }
1204 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1205
1206 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1207 {
1208         struct interface *intf = &rt2x00dev->interface;
1209         int retval;
1210
1211         NOTICE(rt2x00dev, "Waking up.\n");
1212
1213         /*
1214          * Open the debugfs entry.
1215          */
1216         rt2x00debug_register(rt2x00dev);
1217
1218         /*
1219          * Only continue if mac80211 had open interfaces.
1220          */
1221         if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1222                 return 0;
1223
1224         /*
1225          * Reinitialize device and all active interfaces.
1226          */
1227         retval = rt2x00lib_start(rt2x00dev);
1228         if (retval)
1229                 goto exit;
1230
1231         /*
1232          * Reconfigure device.
1233          */
1234         rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1235         if (!rt2x00dev->hw->conf.radio_enabled)
1236                 rt2x00lib_disable_radio(rt2x00dev);
1237
1238         rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1239         rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1240         rt2x00lib_config_type(rt2x00dev, intf->type);
1241
1242         /*
1243          * We are ready again to receive requests from mac80211.
1244          */
1245         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1246
1247         /*
1248          * It is possible that during that mac80211 has attempted
1249          * to send frames while we were suspending or resuming.
1250          * In that case we have disabled the TX queue and should
1251          * now enable it again
1252          */
1253         ieee80211_start_queues(rt2x00dev->hw);
1254
1255         /*
1256          * When in Master or Ad-hoc mode,
1257          * restart Beacon transmitting by faking a beacondone event.
1258          */
1259         if (intf->type == IEEE80211_IF_TYPE_AP ||
1260             intf->type == IEEE80211_IF_TYPE_IBSS)
1261                 rt2x00lib_beacondone(rt2x00dev);
1262
1263         return 0;
1264
1265 exit:
1266         rt2x00lib_disable_radio(rt2x00dev);
1267         rt2x00lib_uninitialize(rt2x00dev);
1268         rt2x00debug_deregister(rt2x00dev);
1269
1270         return retval;
1271 }
1272 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1273 #endif /* CONFIG_PM */
1274
1275 /*
1276  * rt2x00lib module information.
1277  */
1278 MODULE_AUTHOR(DRV_PROJECT);
1279 MODULE_VERSION(DRV_VERSION);
1280 MODULE_DESCRIPTION("rt2x00 library");
1281 MODULE_LICENSE("GPL");