rt2x00: Convert rt2x00 to use threaded interrupts
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
CommitLineData
95ea3627 1/*
9c9a0d14 2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
95ea3627
ID
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
95ea3627
ID
26#include <linux/kernel.h>
27#include <linux/module.h>
5a0e3ad6 28#include <linux/slab.h>
95ea3627
ID
29
30#include "rt2x00.h"
31#include "rt2x00lib.h"
32
95ea3627
ID
33/*
34 * Radio control handlers.
35 */
36int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
37{
38 int status;
39
40 /*
41 * Don't enable the radio twice.
42 * And check if the hardware button has been disabled.
43 */
4b9631a4 44 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
95ea3627
ID
45 return 0;
46
837e7f24 47 /*
181d6902 48 * Initialize all data queues.
837e7f24 49 */
798b7adb 50 rt2x00queue_init_queues(rt2x00dev);
837e7f24 51
95ea3627
ID
52 /*
53 * Enable radio.
54 */
a2e1d52a
ID
55 status =
56 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
95ea3627
ID
57 if (status)
58 return status;
59
2b08da3f
ID
60 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
61
a2e1d52a 62 rt2x00leds_led_radio(rt2x00dev, true);
61c2b682 63 rt2x00led_led_activity(rt2x00dev, true);
a2e1d52a 64
0262ab0d 65 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
95ea3627
ID
66
67 /*
68 * Enable RX.
69 */
5cbf830e 70 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
95ea3627 71
c965c74b
ID
72 /*
73 * Start watchdog monitoring.
74 */
75 rt2x00link_start_watchdog(rt2x00dev);
76
95ea3627
ID
77 /*
78 * Start the TX queues.
79 */
36d6825b 80 ieee80211_wake_queues(rt2x00dev->hw);
95ea3627
ID
81
82 return 0;
83}
84
85void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
86{
0262ab0d 87 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
95ea3627
ID
88 return;
89
95ea3627 90 /*
a2c9b652 91 * Stop the TX queues in mac80211.
95ea3627
ID
92 */
93 ieee80211_stop_queues(rt2x00dev->hw);
a2c9b652 94 rt2x00queue_stop_queues(rt2x00dev);
95ea3627 95
c965c74b
ID
96 /*
97 * Stop watchdog monitoring.
98 */
99 rt2x00link_stop_watchdog(rt2x00dev);
100
95ea3627
ID
101 /*
102 * Disable RX.
103 */
5cbf830e 104 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
95ea3627
ID
105
106 /*
107 * Disable radio.
108 */
109 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
2b08da3f 110 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
61c2b682 111 rt2x00led_led_activity(rt2x00dev, false);
a2e1d52a 112 rt2x00leds_led_radio(rt2x00dev, false);
95ea3627
ID
113}
114
5cbf830e 115void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
95ea3627 116{
95ea3627
ID
117 /*
118 * When we are disabling the RX, we should also stop the link tuner.
119 */
5cbf830e 120 if (state == STATE_RADIO_RX_OFF)
84e3196f 121 rt2x00link_stop_tuner(rt2x00dev);
95ea3627
ID
122
123 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
124
125 /*
126 * When we are enabling the RX, we should also start the link tuner.
127 */
84e3196f
ID
128 if (state == STATE_RADIO_RX_ON)
129 rt2x00link_start_tuner(rt2x00dev);
95ea3627
ID
130}
131
6bb40dd1
ID
132static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
133 struct ieee80211_vif *vif)
5c58ee51 134{
6bb40dd1
ID
135 struct rt2x00_dev *rt2x00dev = data;
136 struct rt2x00_intf *intf = vif_to_intf(vif);
6bb40dd1
ID
137 int delayed_flags;
138
139 /*
140 * Copy all data we need during this action under the protection
141 * of a spinlock. Otherwise race conditions might occur which results
142 * into an invalid configuration.
143 */
144 spin_lock(&intf->lock);
145
6bb40dd1
ID
146 delayed_flags = intf->delayed_flags;
147 intf->delayed_flags = 0;
148
149 spin_unlock(&intf->lock);
150
980dfcb9
ID
151 /*
152 * It is possible the radio was disabled while the work had been
153 * scheduled. If that happens we should return here immediately,
154 * note that in the spinlock protected area above the delayed_flags
155 * have been cleared correctly.
156 */
0262ab0d 157 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
980dfcb9
ID
158 return;
159
bd88a781 160 if (delayed_flags & DELAYED_UPDATE_BEACON)
a2c9b652 161 rt2x00queue_update_beacon(rt2x00dev, vif, true);
6bb40dd1 162}
5c58ee51 163
6bb40dd1
ID
164static void rt2x00lib_intf_scheduled(struct work_struct *work)
165{
166 struct rt2x00_dev *rt2x00dev =
167 container_of(work, struct rt2x00_dev, intf_work);
471b3efd
JB
168
169 /*
6bb40dd1
ID
170 * Iterate over each interface and perform the
171 * requested configurations.
471b3efd 172 */
6bb40dd1
ID
173 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
174 rt2x00lib_intf_scheduled_iter,
175 rt2x00dev);
5c58ee51
ID
176}
177
95ea3627
ID
178/*
179 * Interrupt context handlers.
180 */
6bb40dd1
ID
181static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
182 struct ieee80211_vif *vif)
95ea3627 183{
6bb40dd1 184 struct rt2x00_intf *intf = vif_to_intf(vif);
95ea3627 185
05c914fe 186 if (vif->type != NL80211_IFTYPE_AP &&
a07dbea2 187 vif->type != NL80211_IFTYPE_ADHOC &&
ce292a64
ID
188 vif->type != NL80211_IFTYPE_MESH_POINT &&
189 vif->type != NL80211_IFTYPE_WDS)
95ea3627
ID
190 return;
191
6bb40dd1
ID
192 spin_lock(&intf->lock);
193 intf->delayed_flags |= DELAYED_UPDATE_BEACON;
194 spin_unlock(&intf->lock);
95ea3627
ID
195}
196
197void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
198{
0262ab0d 199 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
95ea3627
ID
200 return;
201
633257d3
ID
202 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
203 rt2x00lib_beacondone_iter,
204 rt2x00dev);
6bb40dd1 205
42935eca 206 ieee80211_queue_work(rt2x00dev->hw, &rt2x00dev->intf_work);
95ea3627
ID
207}
208EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
209
181d6902
ID
210void rt2x00lib_txdone(struct queue_entry *entry,
211 struct txdone_entry_desc *txdesc)
95ea3627 212{
181d6902 213 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
e039fa4a 214 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
e6a9854b 215 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
d74f5ba4 216 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
9f166171 217 unsigned int header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
92ed48e5 218 u8 rate_idx, rate_flags, retry_rates;
7351c6bd 219 u8 skbdesc_flags = skbdesc->flags;
92ed48e5 220 unsigned int i;
2e27cff8 221 bool success;
d74f5ba4 222
e513a0b6
GW
223 /*
224 * Unmap the skb.
225 */
226 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
227
228 /*
229 * Remove the extra tx headroom from the skb.
230 */
231 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
232
233 /*
234 * Signal that the TX descriptor is no longer in the skb.
235 */
236 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
237
9f166171
ID
238 /*
239 * Remove L2 padding which was added during
240 */
241 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
daee6c09 242 rt2x00queue_remove_l2pad(entry->skb, header_length);
9f166171 243
2bb057d0
ID
244 /*
245 * If the IV/EIV data was stripped from the frame before it was
246 * passed to the hardware, we should now reinsert it again because
77c2061d 247 * mac80211 will expect the same data to be present it the
2bb057d0
ID
248 * frame as it was passed to us.
249 */
250 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
9f166171 251 rt2x00crypto_tx_insert_iv(entry->skb, header_length);
2bb057d0 252
e039fa4a
JB
253 /*
254 * Send frame to debugfs immediately, after this call is completed
255 * we are going to overwrite the skb->cb array.
256 */
257 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
95ea3627
ID
258
259 /*
2e27cff8 260 * Determine if the frame has been successfully transmitted.
95ea3627 261 */
2e27cff8 262 success =
ce4c45e0 263 test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
fd6dcb88 264 test_bit(TXDONE_UNKNOWN, &txdesc->flags);
2e27cff8
ID
265
266 /*
267 * Update TX statistics.
268 */
269 rt2x00dev->link.qual.tx_success += success;
270 rt2x00dev->link.qual.tx_failed += !success;
95ea3627 271
e6a9854b
JB
272 rate_idx = skbdesc->tx_rate_idx;
273 rate_flags = skbdesc->tx_rate_flags;
92ed48e5
BP
274 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
275 (txdesc->retry + 1) : 1;
e6a9854b 276
181d6902
ID
277 /*
278 * Initialize TX status
279 */
e039fa4a
JB
280 memset(&tx_info->status, 0, sizeof(tx_info->status));
281 tx_info->status.ack_signal = 0;
92ed48e5
BP
282
283 /*
284 * Frame was send with retries, hardware tried
285 * different rates to send out the frame, at each
3d2bc103
HS
286 * retry it lowered the rate 1 step except when the
287 * lowest rate was used.
92ed48e5
BP
288 */
289 for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
290 tx_info->status.rates[i].idx = rate_idx - i;
291 tx_info->status.rates[i].flags = rate_flags;
3d2bc103
HS
292
293 if (rate_idx - i == 0) {
294 /*
295 * The lowest rate (index 0) was used until the
296 * number of max retries was reached.
297 */
298 tx_info->status.rates[i].count = retry_rates - i;
299 i++;
300 break;
301 }
92ed48e5
BP
302 tx_info->status.rates[i].count = 1;
303 }
2e27cff8 304 if (i < (IEEE80211_TX_MAX_RATES - 1))
92ed48e5 305 tx_info->status.rates[i].idx = -1; /* terminate */
181d6902 306
e039fa4a 307 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
2e27cff8 308 if (success)
e039fa4a 309 tx_info->flags |= IEEE80211_TX_STAT_ACK;
2e27cff8 310 else
181d6902 311 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
95ea3627
ID
312 }
313
1df90809
HS
314 /*
315 * Every single frame has it's own tx status, hence report
316 * every frame as ampdu of size 1.
317 *
318 * TODO: if we can find out how many frames were aggregated
319 * by the hw we could provide the real ampdu_len to mac80211
320 * which would allow the rc algorithm to better decide on
321 * which rates are suitable.
322 */
323 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
324 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
325 tx_info->status.ampdu_len = 1;
326 tx_info->status.ampdu_ack_len = success ? 1 : 0;
327 }
328
e6a9854b 329 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
2e27cff8 330 if (success)
181d6902 331 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
2e27cff8 332 else
181d6902 333 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
95ea3627
ID
334 }
335
336 /*
7351c6bd
JB
337 * Only send the status report to mac80211 when it's a frame
338 * that originated in mac80211. If this was a extra frame coming
339 * through a mac80211 library call (RTS/CTS) then we should not
340 * send the status report back.
95ea3627 341 */
7351c6bd 342 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211))
78e256c9
HS
343 /*
344 * Only PCI and SOC devices process the tx status in process
345 * context. Hence use ieee80211_tx_status for PCI and SOC
346 * devices and stick to ieee80211_tx_status_irqsafe for USB.
347 */
348 if (rt2x00_is_usb(rt2x00dev))
349 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb);
350 else
351 ieee80211_tx_status(rt2x00dev->hw, entry->skb);
baf26a7e 352 else
78e256c9 353 dev_kfree_skb_any(entry->skb);
d74f5ba4
ID
354
355 /*
356 * Make this entry available for reuse.
357 */
95ea3627 358 entry->skb = NULL;
d74f5ba4
ID
359 entry->flags = 0;
360
798b7adb 361 rt2x00dev->ops->lib->clear_entry(entry);
d74f5ba4 362
0262ab0d 363 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
d74f5ba4
ID
364 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
365
366 /*
367 * If the data queue was below the threshold before the txdone
368 * handler we must make sure the packet queue in the mac80211 stack
369 * is reenabled when the txdone handler has finished.
370 */
371 if (!rt2x00queue_threshold(entry->queue))
372 ieee80211_wake_queue(rt2x00dev->hw, qid);
95ea3627
ID
373}
374EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
375
35f00cfc
ID
376static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
377 struct rxdone_entry_desc *rxdesc)
378{
379 struct ieee80211_supported_band *sband;
380 const struct rt2x00_rate *rate;
381 unsigned int i;
382 int signal;
383 int type;
384
385 /*
386 * For non-HT rates the MCS value needs to contain the
387 * actually used rate modulation (CCK or OFDM).
388 */
389 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
390 signal = RATE_MCS(rxdesc->rate_mode, rxdesc->signal);
391 else
392 signal = rxdesc->signal;
393
394 type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
395
396 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
397 for (i = 0; i < sband->n_bitrates; i++) {
398 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
399
400 if (((type == RXDONE_SIGNAL_PLCP) &&
401 (rate->plcp == signal)) ||
402 ((type == RXDONE_SIGNAL_BITRATE) &&
403 (rate->bitrate == signal)) ||
404 ((type == RXDONE_SIGNAL_MCS) &&
405 (rate->mcs == signal))) {
406 return i;
407 }
408 }
409
410 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
411 "signal=0x%.4x, type=%d.\n", signal, type);
412 return 0;
413}
414
c4da0048
GW
415void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev,
416 struct queue_entry *entry)
95ea3627 417{
c4da0048
GW
418 struct rxdone_entry_desc rxdesc;
419 struct sk_buff *skb;
95ea3627 420 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
2bb057d0 421 unsigned int header_length;
35f00cfc 422 int rate_idx;
c4da0048
GW
423 /*
424 * Allocate a new sk_buffer. If no new buffer available, drop the
425 * received frame and reuse the existing buffer.
426 */
427 skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry);
428 if (!skb)
429 return;
430
431 /*
432 * Unmap the skb.
433 */
434 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
435
436 /*
437 * Extract the RXD details.
438 */
439 memset(&rxdesc, 0, sizeof(rxdesc));
440 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
95ea3627 441
239c249d
GW
442 /*
443 * The data behind the ieee80211 header must be
a9f853dd 444 * aligned on a 4 byte boundary.
239c249d 445 */
2bb057d0 446 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
239c249d 447
2bb057d0
ID
448 /*
449 * Hardware might have stripped the IV/EIV/ICV data,
450 * in that case it is possible that the data was
3ad2f3fb 451 * provided separately (through hardware descriptor)
2bb057d0
ID
452 * in which case we should reinsert the data into the frame.
453 */
74415edb 454 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
9f166171 455 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
daee6c09 456 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
9f166171 457 &rxdesc);
b7340833
GW
458 else if (header_length &&
459 (rxdesc.size > header_length) &&
460 (rxdesc.dev_flags & RXDONE_L2PAD))
daee6c09 461 rt2x00queue_remove_l2pad(entry->skb, header_length);
9f166171 462 else
daee6c09 463 rt2x00queue_align_payload(entry->skb, header_length);
239c249d 464
1398d458
AB
465 /* Trim buffer to correct size */
466 skb_trim(entry->skb, rxdesc.size);
467
95ea3627 468 /*
35f00cfc
ID
469 * Check if the frame was received using HT. In that case,
470 * the rate is the MCS index and should be passed to mac80211
471 * directly. Otherwise we need to translate the signal to
472 * the correct bitrate index.
95ea3627 473 */
35f00cfc
ID
474 if (rxdesc.rate_mode == RATE_MODE_CCK ||
475 rxdesc.rate_mode == RATE_MODE_OFDM) {
476 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
477 } else {
478 rxdesc.flags |= RX_FLAG_HT;
479 rate_idx = rxdesc.signal;
866a0503
ID
480 }
481
61af43c5 482 /*
84e3196f 483 * Update extra components
61af43c5 484 */
84e3196f
ID
485 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
486 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
69f81a2c 487
ae73e58e 488 rx_status->mactime = rxdesc.timestamp;
35f00cfc 489 rx_status->rate_idx = rate_idx;
c4da0048
GW
490 rx_status->signal = rxdesc.rssi;
491 rx_status->flag = rxdesc.flags;
69f81a2c 492 rx_status->antenna = rt2x00dev->link.ant.active.rx;
95ea3627
ID
493
494 /*
181d6902
ID
495 * Send frame to mac80211 & debugfs.
496 * mac80211 will clean up the skb structure.
95ea3627 497 */
5a6e5999 498 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
f1d58c25 499 memcpy(IEEE80211_SKB_RXCB(entry->skb), rx_status, sizeof(*rx_status));
78e256c9
HS
500
501 /*
502 * Currently only PCI and SOC devices handle rx interrupts in process
503 * context. Hence, use ieee80211_rx_irqsafe for USB and ieee80211_rx_ni
504 * for PCI and SOC devices.
505 */
506 if (rt2x00_is_usb(rt2x00dev))
507 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb);
508 else
509 ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
c4da0048
GW
510
511 /*
512 * Replace the skb with the freshly allocated one.
513 */
514 entry->skb = skb;
d74f5ba4
ID
515 entry->flags = 0;
516
798b7adb 517 rt2x00dev->ops->lib->clear_entry(entry);
d74f5ba4
ID
518
519 rt2x00queue_index_inc(entry->queue, Q_INDEX);
95ea3627
ID
520}
521EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
522
95ea3627
ID
523/*
524 * Driver initialization handlers.
525 */
70e2fed4
ID
526const struct rt2x00_rate rt2x00_supported_rates[12] = {
527 {
3d8606a6 528 .flags = DEV_RATE_CCK,
70e2fed4 529 .bitrate = 10,
aa776721 530 .ratemask = BIT(0),
70e2fed4 531 .plcp = 0x00,
35f00cfc 532 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
70e2fed4
ID
533 },
534 {
3d8606a6 535 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 536 .bitrate = 20,
aa776721 537 .ratemask = BIT(1),
70e2fed4 538 .plcp = 0x01,
35f00cfc 539 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
70e2fed4
ID
540 },
541 {
3d8606a6 542 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 543 .bitrate = 55,
aa776721 544 .ratemask = BIT(2),
70e2fed4 545 .plcp = 0x02,
35f00cfc 546 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
70e2fed4
ID
547 },
548 {
3d8606a6 549 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 550 .bitrate = 110,
aa776721 551 .ratemask = BIT(3),
70e2fed4 552 .plcp = 0x03,
35f00cfc 553 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
70e2fed4
ID
554 },
555 {
3d8606a6 556 .flags = DEV_RATE_OFDM,
70e2fed4 557 .bitrate = 60,
aa776721 558 .ratemask = BIT(4),
70e2fed4 559 .plcp = 0x0b,
35f00cfc 560 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
70e2fed4
ID
561 },
562 {
563 .flags = DEV_RATE_OFDM,
564 .bitrate = 90,
aa776721 565 .ratemask = BIT(5),
70e2fed4 566 .plcp = 0x0f,
35f00cfc 567 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
70e2fed4
ID
568 },
569 {
3d8606a6 570 .flags = DEV_RATE_OFDM,
70e2fed4 571 .bitrate = 120,
aa776721 572 .ratemask = BIT(6),
70e2fed4 573 .plcp = 0x0a,
35f00cfc 574 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
70e2fed4
ID
575 },
576 {
577 .flags = DEV_RATE_OFDM,
578 .bitrate = 180,
aa776721 579 .ratemask = BIT(7),
70e2fed4 580 .plcp = 0x0e,
35f00cfc 581 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
70e2fed4
ID
582 },
583 {
3d8606a6 584 .flags = DEV_RATE_OFDM,
70e2fed4 585 .bitrate = 240,
aa776721 586 .ratemask = BIT(8),
70e2fed4 587 .plcp = 0x09,
35f00cfc 588 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
70e2fed4
ID
589 },
590 {
591 .flags = DEV_RATE_OFDM,
592 .bitrate = 360,
aa776721 593 .ratemask = BIT(9),
70e2fed4 594 .plcp = 0x0d,
35f00cfc 595 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
70e2fed4
ID
596 },
597 {
598 .flags = DEV_RATE_OFDM,
599 .bitrate = 480,
aa776721 600 .ratemask = BIT(10),
70e2fed4 601 .plcp = 0x08,
35f00cfc 602 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
70e2fed4
ID
603 },
604 {
605 .flags = DEV_RATE_OFDM,
606 .bitrate = 540,
aa776721 607 .ratemask = BIT(11),
70e2fed4 608 .plcp = 0x0c,
35f00cfc 609 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
70e2fed4
ID
610 },
611};
612
95ea3627
ID
613static void rt2x00lib_channel(struct ieee80211_channel *entry,
614 const int channel, const int tx_power,
615 const int value)
616{
f2a3c7f5 617 entry->center_freq = ieee80211_channel_to_frequency(channel);
8318d78a
JB
618 entry->hw_value = value;
619 entry->max_power = tx_power;
620 entry->max_antenna_gain = 0xff;
95ea3627
ID
621}
622
623static void rt2x00lib_rate(struct ieee80211_rate *entry,
70e2fed4 624 const u16 index, const struct rt2x00_rate *rate)
95ea3627 625{
70e2fed4
ID
626 entry->flags = 0;
627 entry->bitrate = rate->bitrate;
3ea96463
ID
628 entry->hw_value =index;
629 entry->hw_value_short = index;
70e2fed4 630
3ea96463 631 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
70e2fed4 632 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
95ea3627
ID
633}
634
635static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
636 struct hw_mode_spec *spec)
637{
638 struct ieee80211_hw *hw = rt2x00dev->hw;
95ea3627
ID
639 struct ieee80211_channel *channels;
640 struct ieee80211_rate *rates;
31562e80 641 unsigned int num_rates;
95ea3627 642 unsigned int i;
95ea3627 643
31562e80
ID
644 num_rates = 0;
645 if (spec->supported_rates & SUPPORT_RATE_CCK)
646 num_rates += 4;
647 if (spec->supported_rates & SUPPORT_RATE_OFDM)
648 num_rates += 8;
95ea3627
ID
649
650 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
651 if (!channels)
8318d78a 652 return -ENOMEM;
95ea3627 653
31562e80 654 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
95ea3627
ID
655 if (!rates)
656 goto exit_free_channels;
657
658 /*
659 * Initialize Rate list.
660 */
31562e80 661 for (i = 0; i < num_rates; i++)
8f5fa7f0 662 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
95ea3627
ID
663
664 /*
665 * Initialize Channel list.
666 */
667 for (i = 0; i < spec->num_channels; i++) {
95ea3627 668 rt2x00lib_channel(&channels[i],
8c5e7a5f
ID
669 spec->channels[i].channel,
670 spec->channels_info[i].tx_power1, i);
95ea3627
ID
671 }
672
673 /*
31562e80 674 * Intitialize 802.11b, 802.11g
95ea3627 675 * Rates: CCK, OFDM.
8318d78a 676 * Channels: 2.4 GHz
95ea3627 677 */
47ac2683 678 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
31562e80
ID
679 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
680 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
681 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
682 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
683 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
684 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
35f00cfc
ID
685 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
686 &spec->ht, sizeof(spec->ht));
95ea3627
ID
687 }
688
689 /*
690 * Intitialize 802.11a
691 * Rates: OFDM.
692 * Channels: OFDM, UNII, HiperLAN2.
693 */
47ac2683 694 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
31562e80
ID
695 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
696 spec->num_channels - 14;
697 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
698 num_rates - 4;
699 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
700 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
701 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
702 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
35f00cfc
ID
703 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
704 &spec->ht, sizeof(spec->ht));
95ea3627
ID
705 }
706
95ea3627
ID
707 return 0;
708
8318d78a 709 exit_free_channels:
95ea3627 710 kfree(channels);
95ea3627
ID
711 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
712 return -ENOMEM;
713}
714
715static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
716{
0262ab0d 717 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
95ea3627
ID
718 ieee80211_unregister_hw(rt2x00dev->hw);
719
8318d78a
JB
720 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
721 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
722 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
723 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
724 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
95ea3627 725 }
8c5e7a5f
ID
726
727 kfree(rt2x00dev->spec.channels_info);
95ea3627
ID
728}
729
730static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
731{
732 struct hw_mode_spec *spec = &rt2x00dev->spec;
733 int status;
734
0262ab0d
ID
735 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
736 return 0;
737
95ea3627
ID
738 /*
739 * Initialize HW modes.
740 */
741 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
742 if (status)
743 return status;
744
61448f88
GW
745 /*
746 * Initialize HW fields.
747 */
748 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
749
e6218cc4
GW
750 /*
751 * Initialize extra TX headroom required.
752 */
7a4a77b7
GW
753 rt2x00dev->hw->extra_tx_headroom =
754 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
755 rt2x00dev->ops->extra_tx_headroom);
756
757 /*
758 * Take TX headroom required for alignment into account.
759 */
760 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
761 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
762 else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
763 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
e6218cc4 764
95ea3627
ID
765 /*
766 * Register HW.
767 */
768 status = ieee80211_register_hw(rt2x00dev->hw);
f05faa31 769 if (status)
95ea3627 770 return status;
95ea3627 771
0262ab0d 772 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
95ea3627
ID
773
774 return 0;
775}
776
777/*
778 * Initialization/uninitialization handlers.
779 */
e37ea213 780static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
95ea3627 781{
0262ab0d 782 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
95ea3627
ID
783 return;
784
785 /*
1682fe6d 786 * Unregister extra components.
95ea3627
ID
787 */
788 rt2x00rfkill_unregister(rt2x00dev);
789
790 /*
791 * Allow the HW to uninitialize.
792 */
793 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
794
795 /*
181d6902 796 * Free allocated queue entries.
95ea3627 797 */
181d6902 798 rt2x00queue_uninitialize(rt2x00dev);
95ea3627
ID
799}
800
e37ea213 801static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
95ea3627
ID
802{
803 int status;
804
0262ab0d 805 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
95ea3627
ID
806 return 0;
807
808 /*
181d6902 809 * Allocate all queue entries.
95ea3627 810 */
181d6902
ID
811 status = rt2x00queue_initialize(rt2x00dev);
812 if (status)
95ea3627 813 return status;
95ea3627
ID
814
815 /*
816 * Initialize the device.
817 */
818 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
ed499983
ID
819 if (status) {
820 rt2x00queue_uninitialize(rt2x00dev);
821 return status;
822 }
95ea3627 823
0262ab0d 824 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
95ea3627
ID
825
826 /*
1682fe6d 827 * Register the extra components.
95ea3627 828 */
1682fe6d 829 rt2x00rfkill_register(rt2x00dev);
95ea3627
ID
830
831 return 0;
95ea3627
ID
832}
833
e37ea213
ID
834int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
835{
836 int retval;
837
0262ab0d 838 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
e37ea213
ID
839 return 0;
840
841 /*
842 * If this is the first interface which is added,
843 * we should load the firmware now.
844 */
9404ef34
ID
845 retval = rt2x00lib_load_firmware(rt2x00dev);
846 if (retval)
847 return retval;
e37ea213
ID
848
849 /*
850 * Initialize the device.
851 */
852 retval = rt2x00lib_initialize(rt2x00dev);
853 if (retval)
854 return retval;
855
6bb40dd1
ID
856 rt2x00dev->intf_ap_count = 0;
857 rt2x00dev->intf_sta_count = 0;
858 rt2x00dev->intf_associated = 0;
859
bdfa500b
ID
860 /* Enable the radio */
861 retval = rt2x00lib_enable_radio(rt2x00dev);
862 if (retval) {
863 rt2x00queue_uninitialize(rt2x00dev);
864 return retval;
865 }
866
0262ab0d 867 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
e37ea213
ID
868
869 return 0;
870}
871
872void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
873{
0262ab0d 874 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
e37ea213
ID
875 return;
876
877 /*
878 * Perhaps we can add something smarter here,
879 * but for now just disabling the radio should do.
880 */
881 rt2x00lib_disable_radio(rt2x00dev);
882
6bb40dd1
ID
883 rt2x00dev->intf_ap_count = 0;
884 rt2x00dev->intf_sta_count = 0;
885 rt2x00dev->intf_associated = 0;
e37ea213
ID
886}
887
95ea3627
ID
888/*
889 * driver allocation handlers.
890 */
95ea3627
ID
891int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
892{
893 int retval = -ENOMEM;
894
8ff48a8b
ID
895 mutex_init(&rt2x00dev->csr_mutex);
896
66f84d65
SC
897 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
898
6bb40dd1
ID
899 /*
900 * Make room for rt2x00_intf inside the per-interface
901 * structure ieee80211_vif.
902 */
903 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
904
3514a441
ID
905 /*
906 * Determine which operating modes are supported, all modes
907 * which require beaconing, depend on the availability of
908 * beacon entries.
909 */
910 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
911 if (rt2x00dev->ops->bcn->entry_num > 0)
912 rt2x00dev->hw->wiphy->interface_modes |=
913 BIT(NL80211_IFTYPE_ADHOC) |
a07dbea2 914 BIT(NL80211_IFTYPE_AP) |
ce292a64
ID
915 BIT(NL80211_IFTYPE_MESH_POINT) |
916 BIT(NL80211_IFTYPE_WDS);
f59ac048 917
95ea3627
ID
918 /*
919 * Let the driver probe the device to detect the capabilities.
920 */
921 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
922 if (retval) {
923 ERROR(rt2x00dev, "Failed to allocate device.\n");
924 goto exit;
925 }
926
927 /*
928 * Initialize configuration work.
929 */
6bb40dd1 930 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
95ea3627 931
95ea3627 932 /*
181d6902 933 * Allocate queue array.
95ea3627 934 */
181d6902 935 retval = rt2x00queue_allocate(rt2x00dev);
95ea3627
ID
936 if (retval)
937 goto exit;
938
939 /*
940 * Initialize ieee80211 structure.
941 */
942 retval = rt2x00lib_probe_hw(rt2x00dev);
943 if (retval) {
944 ERROR(rt2x00dev, "Failed to initialize hw.\n");
945 goto exit;
946 }
947
a9450b70 948 /*
1682fe6d 949 * Register extra components.
a9450b70 950 */
84e3196f 951 rt2x00link_register(rt2x00dev);
a9450b70 952 rt2x00leds_register(rt2x00dev);
95ea3627
ID
953 rt2x00debug_register(rt2x00dev);
954
955 return 0;
956
957exit:
958 rt2x00lib_remove_dev(rt2x00dev);
959
960 return retval;
961}
962EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
963
964void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
965{
0262ab0d 966 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
066cb637 967
95ea3627
ID
968 /*
969 * Disable radio.
970 */
971 rt2x00lib_disable_radio(rt2x00dev);
972
d8cc8926
PR
973 /*
974 * Stop all work.
975 */
d8cc8926
PR
976 cancel_work_sync(&rt2x00dev->intf_work);
977
95ea3627
ID
978 /*
979 * Uninitialize device.
980 */
981 rt2x00lib_uninitialize(rt2x00dev);
982
983 /*
1682fe6d 984 * Free extra components
95ea3627
ID
985 */
986 rt2x00debug_deregister(rt2x00dev);
a9450b70
ID
987 rt2x00leds_unregister(rt2x00dev);
988
95ea3627
ID
989 /*
990 * Free ieee80211_hw memory.
991 */
992 rt2x00lib_remove_hw(rt2x00dev);
993
994 /*
995 * Free firmware image.
996 */
997 rt2x00lib_free_firmware(rt2x00dev);
998
999 /*
181d6902 1000 * Free queue structures.
95ea3627 1001 */
181d6902 1002 rt2x00queue_free(rt2x00dev);
95ea3627
ID
1003}
1004EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1005
1006/*
1007 * Device state handlers
1008 */
1009#ifdef CONFIG_PM
1010int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1011{
95ea3627 1012 NOTICE(rt2x00dev, "Going to sleep.\n");
066cb637
ID
1013
1014 /*
07126127 1015 * Prevent mac80211 from accessing driver while suspended.
066cb637 1016 */
07126127
ID
1017 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1018 return 0;
95ea3627
ID
1019
1020 /*
07126127 1021 * Cleanup as much as possible.
95ea3627 1022 */
95ea3627 1023 rt2x00lib_uninitialize(rt2x00dev);
1682fe6d
ID
1024
1025 /*
1026 * Suspend/disable extra components.
1027 */
a9450b70 1028 rt2x00leds_suspend(rt2x00dev);
95ea3627
ID
1029 rt2x00debug_deregister(rt2x00dev);
1030
1031 /*
9896322a
ID
1032 * Set device mode to sleep for power management,
1033 * on some hardware this call seems to consistently fail.
1034 * From the specifications it is hard to tell why it fails,
1035 * and if this is a "bad thing".
1036 * Overall it is safe to just ignore the failure and
1037 * continue suspending. The only downside is that the
1038 * device will not be in optimal power save mode, but with
1039 * the radio and the other components already disabled the
1040 * device is as good as disabled.
95ea3627 1041 */
07126127 1042 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
9896322a
ID
1043 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1044 "continue suspending.\n");
95ea3627
ID
1045
1046 return 0;
1047}
1048EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1049
1050int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1051{
95ea3627 1052 NOTICE(rt2x00dev, "Waking up.\n");
95ea3627
ID
1053
1054 /*
1682fe6d 1055 * Restore/enable extra components.
95ea3627
ID
1056 */
1057 rt2x00debug_register(rt2x00dev);
a9450b70 1058 rt2x00leds_resume(rt2x00dev);
95ea3627 1059
e37ea213
ID
1060 /*
1061 * We are ready again to receive requests from mac80211.
1062 */
0262ab0d 1063 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
e37ea213 1064
95ea3627 1065 return 0;
95ea3627
ID
1066}
1067EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1068#endif /* CONFIG_PM */
1069
1070/*
1071 * rt2x00lib module information.
1072 */
1073MODULE_AUTHOR(DRV_PROJECT);
1074MODULE_VERSION(DRV_VERSION);
1075MODULE_DESCRIPTION("rt2x00 library");
1076MODULE_LICENSE("GPL");
This page took 0.685198 seconds and 5 git commands to generate.