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