rt2x00: Merge PCI and USB versions of write_tx_data into single function.
[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
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) ||
fd6dcb88 239 test_bit(TXDONE_UNKNOWN, &txdesc->flags);
2e27cff8
ID
240
241 /*
242 * Update TX statistics.
243 */
244 rt2x00dev->link.qual.tx_success += success;
245 rt2x00dev->link.qual.tx_failed += !success;
95ea3627 246
e6a9854b
JB
247 rate_idx = skbdesc->tx_rate_idx;
248 rate_flags = skbdesc->tx_rate_flags;
92ed48e5
BP
249 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
250 (txdesc->retry + 1) : 1;
e6a9854b 251
181d6902
ID
252 /*
253 * Initialize TX status
254 */
e039fa4a
JB
255 memset(&tx_info->status, 0, sizeof(tx_info->status));
256 tx_info->status.ack_signal = 0;
92ed48e5
BP
257
258 /*
259 * Frame was send with retries, hardware tried
260 * different rates to send out the frame, at each
3d2bc103
HS
261 * retry it lowered the rate 1 step except when the
262 * lowest rate was used.
92ed48e5
BP
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;
3d2bc103
HS
267
268 if (rate_idx - i == 0) {
269 /*
270 * The lowest rate (index 0) was used until the
271 * number of max retries was reached.
272 */
273 tx_info->status.rates[i].count = retry_rates - i;
274 i++;
275 break;
276 }
92ed48e5
BP
277 tx_info->status.rates[i].count = 1;
278 }
2e27cff8 279 if (i < (IEEE80211_TX_MAX_RATES - 1))
92ed48e5 280 tx_info->status.rates[i].idx = -1; /* terminate */
181d6902 281
e039fa4a 282 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
2e27cff8 283 if (success)
e039fa4a 284 tx_info->flags |= IEEE80211_TX_STAT_ACK;
2e27cff8 285 else
181d6902 286 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
95ea3627
ID
287 }
288
1df90809
HS
289 /*
290 * Every single frame has it's own tx status, hence report
291 * every frame as ampdu of size 1.
292 *
293 * TODO: if we can find out how many frames were aggregated
294 * by the hw we could provide the real ampdu_len to mac80211
295 * which would allow the rc algorithm to better decide on
296 * which rates are suitable.
297 */
298 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
299 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
300 tx_info->status.ampdu_len = 1;
301 tx_info->status.ampdu_ack_len = success ? 1 : 0;
302 }
303
e6a9854b 304 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
2e27cff8 305 if (success)
181d6902 306 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
2e27cff8 307 else
181d6902 308 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
95ea3627
ID
309 }
310
311 /*
7351c6bd
JB
312 * Only send the status report to mac80211 when it's a frame
313 * that originated in mac80211. If this was a extra frame coming
314 * through a mac80211 library call (RTS/CTS) then we should not
315 * send the status report back.
95ea3627 316 */
7351c6bd 317 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211))
e039fa4a 318 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb);
baf26a7e 319 else
fb55f4d1 320 dev_kfree_skb_irq(entry->skb);
d74f5ba4
ID
321
322 /*
323 * Make this entry available for reuse.
324 */
95ea3627 325 entry->skb = NULL;
d74f5ba4
ID
326 entry->flags = 0;
327
798b7adb 328 rt2x00dev->ops->lib->clear_entry(entry);
d74f5ba4 329
0262ab0d 330 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
d74f5ba4
ID
331 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
332
333 /*
334 * If the data queue was below the threshold before the txdone
335 * handler we must make sure the packet queue in the mac80211 stack
336 * is reenabled when the txdone handler has finished.
337 */
338 if (!rt2x00queue_threshold(entry->queue))
339 ieee80211_wake_queue(rt2x00dev->hw, qid);
95ea3627
ID
340}
341EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
342
35f00cfc
ID
343static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
344 struct rxdone_entry_desc *rxdesc)
345{
346 struct ieee80211_supported_band *sband;
347 const struct rt2x00_rate *rate;
348 unsigned int i;
349 int signal;
350 int type;
351
352 /*
353 * For non-HT rates the MCS value needs to contain the
354 * actually used rate modulation (CCK or OFDM).
355 */
356 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
357 signal = RATE_MCS(rxdesc->rate_mode, rxdesc->signal);
358 else
359 signal = rxdesc->signal;
360
361 type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
362
363 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
364 for (i = 0; i < sband->n_bitrates; i++) {
365 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
366
367 if (((type == RXDONE_SIGNAL_PLCP) &&
368 (rate->plcp == signal)) ||
369 ((type == RXDONE_SIGNAL_BITRATE) &&
370 (rate->bitrate == signal)) ||
371 ((type == RXDONE_SIGNAL_MCS) &&
372 (rate->mcs == signal))) {
373 return i;
374 }
375 }
376
377 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
378 "signal=0x%.4x, type=%d.\n", signal, type);
379 return 0;
380}
381
c4da0048
GW
382void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev,
383 struct queue_entry *entry)
95ea3627 384{
c4da0048
GW
385 struct rxdone_entry_desc rxdesc;
386 struct sk_buff *skb;
95ea3627 387 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
2bb057d0 388 unsigned int header_length;
35f00cfc 389 int rate_idx;
c4da0048
GW
390 /*
391 * Allocate a new sk_buffer. If no new buffer available, drop the
392 * received frame and reuse the existing buffer.
393 */
394 skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry);
395 if (!skb)
396 return;
397
398 /*
399 * Unmap the skb.
400 */
401 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
402
403 /*
404 * Extract the RXD details.
405 */
406 memset(&rxdesc, 0, sizeof(rxdesc));
407 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
95ea3627 408
239c249d
GW
409 /*
410 * The data behind the ieee80211 header must be
a9f853dd 411 * aligned on a 4 byte boundary.
239c249d 412 */
2bb057d0 413 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
239c249d 414
2bb057d0
ID
415 /*
416 * Hardware might have stripped the IV/EIV/ICV data,
417 * in that case it is possible that the data was
3ad2f3fb 418 * provided separately (through hardware descriptor)
2bb057d0
ID
419 * in which case we should reinsert the data into the frame.
420 */
74415edb 421 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
9f166171 422 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
daee6c09 423 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
9f166171 424 &rxdesc);
b7340833
GW
425 else if (header_length &&
426 (rxdesc.size > header_length) &&
427 (rxdesc.dev_flags & RXDONE_L2PAD))
daee6c09 428 rt2x00queue_remove_l2pad(entry->skb, header_length);
9f166171 429 else
daee6c09 430 rt2x00queue_align_payload(entry->skb, header_length);
239c249d 431
1398d458
AB
432 /* Trim buffer to correct size */
433 skb_trim(entry->skb, rxdesc.size);
434
95ea3627 435 /*
35f00cfc
ID
436 * Check if the frame was received using HT. In that case,
437 * the rate is the MCS index and should be passed to mac80211
438 * directly. Otherwise we need to translate the signal to
439 * the correct bitrate index.
95ea3627 440 */
35f00cfc
ID
441 if (rxdesc.rate_mode == RATE_MODE_CCK ||
442 rxdesc.rate_mode == RATE_MODE_OFDM) {
443 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
444 } else {
445 rxdesc.flags |= RX_FLAG_HT;
446 rate_idx = rxdesc.signal;
866a0503
ID
447 }
448
61af43c5 449 /*
84e3196f 450 * Update extra components
61af43c5 451 */
84e3196f
ID
452 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
453 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
69f81a2c 454
ae73e58e 455 rx_status->mactime = rxdesc.timestamp;
35f00cfc 456 rx_status->rate_idx = rate_idx;
c4da0048
GW
457 rx_status->signal = rxdesc.rssi;
458 rx_status->flag = rxdesc.flags;
69f81a2c 459 rx_status->antenna = rt2x00dev->link.ant.active.rx;
95ea3627
ID
460
461 /*
181d6902
ID
462 * Send frame to mac80211 & debugfs.
463 * mac80211 will clean up the skb structure.
95ea3627 464 */
5a6e5999 465 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
f1d58c25
JB
466 memcpy(IEEE80211_SKB_RXCB(entry->skb), rx_status, sizeof(*rx_status));
467 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb);
c4da0048
GW
468
469 /*
470 * Replace the skb with the freshly allocated one.
471 */
472 entry->skb = skb;
d74f5ba4
ID
473 entry->flags = 0;
474
798b7adb 475 rt2x00dev->ops->lib->clear_entry(entry);
d74f5ba4
ID
476
477 rt2x00queue_index_inc(entry->queue, Q_INDEX);
95ea3627
ID
478}
479EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
480
95ea3627
ID
481/*
482 * Driver initialization handlers.
483 */
70e2fed4
ID
484const struct rt2x00_rate rt2x00_supported_rates[12] = {
485 {
3d8606a6 486 .flags = DEV_RATE_CCK,
70e2fed4 487 .bitrate = 10,
aa776721 488 .ratemask = BIT(0),
70e2fed4 489 .plcp = 0x00,
35f00cfc 490 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
70e2fed4
ID
491 },
492 {
3d8606a6 493 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 494 .bitrate = 20,
aa776721 495 .ratemask = BIT(1),
70e2fed4 496 .plcp = 0x01,
35f00cfc 497 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
70e2fed4
ID
498 },
499 {
3d8606a6 500 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 501 .bitrate = 55,
aa776721 502 .ratemask = BIT(2),
70e2fed4 503 .plcp = 0x02,
35f00cfc 504 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
70e2fed4
ID
505 },
506 {
3d8606a6 507 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 508 .bitrate = 110,
aa776721 509 .ratemask = BIT(3),
70e2fed4 510 .plcp = 0x03,
35f00cfc 511 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
70e2fed4
ID
512 },
513 {
3d8606a6 514 .flags = DEV_RATE_OFDM,
70e2fed4 515 .bitrate = 60,
aa776721 516 .ratemask = BIT(4),
70e2fed4 517 .plcp = 0x0b,
35f00cfc 518 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
70e2fed4
ID
519 },
520 {
521 .flags = DEV_RATE_OFDM,
522 .bitrate = 90,
aa776721 523 .ratemask = BIT(5),
70e2fed4 524 .plcp = 0x0f,
35f00cfc 525 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
70e2fed4
ID
526 },
527 {
3d8606a6 528 .flags = DEV_RATE_OFDM,
70e2fed4 529 .bitrate = 120,
aa776721 530 .ratemask = BIT(6),
70e2fed4 531 .plcp = 0x0a,
35f00cfc 532 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
70e2fed4
ID
533 },
534 {
535 .flags = DEV_RATE_OFDM,
536 .bitrate = 180,
aa776721 537 .ratemask = BIT(7),
70e2fed4 538 .plcp = 0x0e,
35f00cfc 539 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
70e2fed4
ID
540 },
541 {
3d8606a6 542 .flags = DEV_RATE_OFDM,
70e2fed4 543 .bitrate = 240,
aa776721 544 .ratemask = BIT(8),
70e2fed4 545 .plcp = 0x09,
35f00cfc 546 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
70e2fed4
ID
547 },
548 {
549 .flags = DEV_RATE_OFDM,
550 .bitrate = 360,
aa776721 551 .ratemask = BIT(9),
70e2fed4 552 .plcp = 0x0d,
35f00cfc 553 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
70e2fed4
ID
554 },
555 {
556 .flags = DEV_RATE_OFDM,
557 .bitrate = 480,
aa776721 558 .ratemask = BIT(10),
70e2fed4 559 .plcp = 0x08,
35f00cfc 560 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
70e2fed4
ID
561 },
562 {
563 .flags = DEV_RATE_OFDM,
564 .bitrate = 540,
aa776721 565 .ratemask = BIT(11),
70e2fed4 566 .plcp = 0x0c,
35f00cfc 567 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
70e2fed4
ID
568 },
569};
570
95ea3627
ID
571static void rt2x00lib_channel(struct ieee80211_channel *entry,
572 const int channel, const int tx_power,
573 const int value)
574{
f2a3c7f5 575 entry->center_freq = ieee80211_channel_to_frequency(channel);
8318d78a
JB
576 entry->hw_value = value;
577 entry->max_power = tx_power;
578 entry->max_antenna_gain = 0xff;
95ea3627
ID
579}
580
581static void rt2x00lib_rate(struct ieee80211_rate *entry,
70e2fed4 582 const u16 index, const struct rt2x00_rate *rate)
95ea3627 583{
70e2fed4
ID
584 entry->flags = 0;
585 entry->bitrate = rate->bitrate;
3ea96463
ID
586 entry->hw_value =index;
587 entry->hw_value_short = index;
70e2fed4 588
3ea96463 589 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
70e2fed4 590 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
95ea3627
ID
591}
592
593static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
594 struct hw_mode_spec *spec)
595{
596 struct ieee80211_hw *hw = rt2x00dev->hw;
95ea3627
ID
597 struct ieee80211_channel *channels;
598 struct ieee80211_rate *rates;
31562e80 599 unsigned int num_rates;
95ea3627 600 unsigned int i;
95ea3627 601
31562e80
ID
602 num_rates = 0;
603 if (spec->supported_rates & SUPPORT_RATE_CCK)
604 num_rates += 4;
605 if (spec->supported_rates & SUPPORT_RATE_OFDM)
606 num_rates += 8;
95ea3627
ID
607
608 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
609 if (!channels)
8318d78a 610 return -ENOMEM;
95ea3627 611
31562e80 612 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
95ea3627
ID
613 if (!rates)
614 goto exit_free_channels;
615
616 /*
617 * Initialize Rate list.
618 */
31562e80 619 for (i = 0; i < num_rates; i++)
8f5fa7f0 620 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
95ea3627
ID
621
622 /*
623 * Initialize Channel list.
624 */
625 for (i = 0; i < spec->num_channels; i++) {
95ea3627 626 rt2x00lib_channel(&channels[i],
8c5e7a5f
ID
627 spec->channels[i].channel,
628 spec->channels_info[i].tx_power1, i);
95ea3627
ID
629 }
630
631 /*
31562e80 632 * Intitialize 802.11b, 802.11g
95ea3627 633 * Rates: CCK, OFDM.
8318d78a 634 * Channels: 2.4 GHz
95ea3627 635 */
47ac2683 636 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
31562e80
ID
637 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
638 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
639 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
640 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
641 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
642 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
35f00cfc
ID
643 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
644 &spec->ht, sizeof(spec->ht));
95ea3627
ID
645 }
646
647 /*
648 * Intitialize 802.11a
649 * Rates: OFDM.
650 * Channels: OFDM, UNII, HiperLAN2.
651 */
47ac2683 652 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
31562e80
ID
653 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
654 spec->num_channels - 14;
655 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
656 num_rates - 4;
657 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
658 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
659 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
660 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
35f00cfc
ID
661 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
662 &spec->ht, sizeof(spec->ht));
95ea3627
ID
663 }
664
95ea3627
ID
665 return 0;
666
8318d78a 667 exit_free_channels:
95ea3627 668 kfree(channels);
95ea3627
ID
669 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
670 return -ENOMEM;
671}
672
673static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
674{
0262ab0d 675 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
95ea3627
ID
676 ieee80211_unregister_hw(rt2x00dev->hw);
677
8318d78a
JB
678 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
679 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
680 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
681 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
682 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
95ea3627 683 }
8c5e7a5f
ID
684
685 kfree(rt2x00dev->spec.channels_info);
95ea3627
ID
686}
687
688static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
689{
690 struct hw_mode_spec *spec = &rt2x00dev->spec;
691 int status;
692
0262ab0d
ID
693 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
694 return 0;
695
95ea3627
ID
696 /*
697 * Initialize HW modes.
698 */
699 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
700 if (status)
701 return status;
702
61448f88
GW
703 /*
704 * Initialize HW fields.
705 */
706 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
707
e6218cc4
GW
708 /*
709 * Initialize extra TX headroom required.
710 */
7a4a77b7
GW
711 rt2x00dev->hw->extra_tx_headroom =
712 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
713 rt2x00dev->ops->extra_tx_headroom);
714
715 /*
716 * Take TX headroom required for alignment into account.
717 */
718 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
719 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
720 else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
721 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
e6218cc4 722
95ea3627
ID
723 /*
724 * Register HW.
725 */
726 status = ieee80211_register_hw(rt2x00dev->hw);
f05faa31 727 if (status)
95ea3627 728 return status;
95ea3627 729
0262ab0d 730 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
95ea3627
ID
731
732 return 0;
733}
734
735/*
736 * Initialization/uninitialization handlers.
737 */
e37ea213 738static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
95ea3627 739{
0262ab0d 740 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
95ea3627
ID
741 return;
742
743 /*
1682fe6d 744 * Unregister extra components.
95ea3627
ID
745 */
746 rt2x00rfkill_unregister(rt2x00dev);
747
748 /*
749 * Allow the HW to uninitialize.
750 */
751 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
752
753 /*
181d6902 754 * Free allocated queue entries.
95ea3627 755 */
181d6902 756 rt2x00queue_uninitialize(rt2x00dev);
95ea3627
ID
757}
758
e37ea213 759static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
95ea3627
ID
760{
761 int status;
762
0262ab0d 763 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
95ea3627
ID
764 return 0;
765
766 /*
181d6902 767 * Allocate all queue entries.
95ea3627 768 */
181d6902
ID
769 status = rt2x00queue_initialize(rt2x00dev);
770 if (status)
95ea3627 771 return status;
95ea3627
ID
772
773 /*
774 * Initialize the device.
775 */
776 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
ed499983
ID
777 if (status) {
778 rt2x00queue_uninitialize(rt2x00dev);
779 return status;
780 }
95ea3627 781
0262ab0d 782 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
95ea3627
ID
783
784 /*
1682fe6d 785 * Register the extra components.
95ea3627 786 */
1682fe6d 787 rt2x00rfkill_register(rt2x00dev);
95ea3627
ID
788
789 return 0;
95ea3627
ID
790}
791
e37ea213
ID
792int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
793{
794 int retval;
795
0262ab0d 796 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
e37ea213
ID
797 return 0;
798
799 /*
800 * If this is the first interface which is added,
801 * we should load the firmware now.
802 */
9404ef34
ID
803 retval = rt2x00lib_load_firmware(rt2x00dev);
804 if (retval)
805 return retval;
e37ea213
ID
806
807 /*
808 * Initialize the device.
809 */
810 retval = rt2x00lib_initialize(rt2x00dev);
811 if (retval)
812 return retval;
813
6bb40dd1
ID
814 rt2x00dev->intf_ap_count = 0;
815 rt2x00dev->intf_sta_count = 0;
816 rt2x00dev->intf_associated = 0;
817
bdfa500b
ID
818 /* Enable the radio */
819 retval = rt2x00lib_enable_radio(rt2x00dev);
820 if (retval) {
821 rt2x00queue_uninitialize(rt2x00dev);
822 return retval;
823 }
824
0262ab0d 825 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
e37ea213
ID
826
827 return 0;
828}
829
830void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
831{
0262ab0d 832 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
e37ea213
ID
833 return;
834
835 /*
836 * Perhaps we can add something smarter here,
837 * but for now just disabling the radio should do.
838 */
839 rt2x00lib_disable_radio(rt2x00dev);
840
6bb40dd1
ID
841 rt2x00dev->intf_ap_count = 0;
842 rt2x00dev->intf_sta_count = 0;
843 rt2x00dev->intf_associated = 0;
e37ea213
ID
844}
845
95ea3627
ID
846/*
847 * driver allocation handlers.
848 */
95ea3627
ID
849int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
850{
851 int retval = -ENOMEM;
852
8ff48a8b
ID
853 mutex_init(&rt2x00dev->csr_mutex);
854
66f84d65
SC
855 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
856
6bb40dd1
ID
857 /*
858 * Make room for rt2x00_intf inside the per-interface
859 * structure ieee80211_vif.
860 */
861 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
862
3514a441
ID
863 /*
864 * Determine which operating modes are supported, all modes
865 * which require beaconing, depend on the availability of
866 * beacon entries.
867 */
868 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
869 if (rt2x00dev->ops->bcn->entry_num > 0)
870 rt2x00dev->hw->wiphy->interface_modes |=
871 BIT(NL80211_IFTYPE_ADHOC) |
a07dbea2 872 BIT(NL80211_IFTYPE_AP) |
ce292a64
ID
873 BIT(NL80211_IFTYPE_MESH_POINT) |
874 BIT(NL80211_IFTYPE_WDS);
f59ac048 875
95ea3627
ID
876 /*
877 * Let the driver probe the device to detect the capabilities.
878 */
879 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
880 if (retval) {
881 ERROR(rt2x00dev, "Failed to allocate device.\n");
882 goto exit;
883 }
884
885 /*
886 * Initialize configuration work.
887 */
6bb40dd1 888 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
95ea3627 889
95ea3627 890 /*
181d6902 891 * Allocate queue array.
95ea3627 892 */
181d6902 893 retval = rt2x00queue_allocate(rt2x00dev);
95ea3627
ID
894 if (retval)
895 goto exit;
896
897 /*
898 * Initialize ieee80211 structure.
899 */
900 retval = rt2x00lib_probe_hw(rt2x00dev);
901 if (retval) {
902 ERROR(rt2x00dev, "Failed to initialize hw.\n");
903 goto exit;
904 }
905
a9450b70 906 /*
1682fe6d 907 * Register extra components.
a9450b70 908 */
84e3196f 909 rt2x00link_register(rt2x00dev);
a9450b70 910 rt2x00leds_register(rt2x00dev);
95ea3627
ID
911 rt2x00debug_register(rt2x00dev);
912
913 return 0;
914
915exit:
916 rt2x00lib_remove_dev(rt2x00dev);
917
918 return retval;
919}
920EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
921
922void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
923{
0262ab0d 924 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
066cb637 925
95ea3627
ID
926 /*
927 * Disable radio.
928 */
929 rt2x00lib_disable_radio(rt2x00dev);
930
d8cc8926
PR
931 /*
932 * Stop all work.
933 */
d8cc8926
PR
934 cancel_work_sync(&rt2x00dev->intf_work);
935
95ea3627
ID
936 /*
937 * Uninitialize device.
938 */
939 rt2x00lib_uninitialize(rt2x00dev);
940
941 /*
1682fe6d 942 * Free extra components
95ea3627
ID
943 */
944 rt2x00debug_deregister(rt2x00dev);
a9450b70
ID
945 rt2x00leds_unregister(rt2x00dev);
946
95ea3627
ID
947 /*
948 * Free ieee80211_hw memory.
949 */
950 rt2x00lib_remove_hw(rt2x00dev);
951
952 /*
953 * Free firmware image.
954 */
955 rt2x00lib_free_firmware(rt2x00dev);
956
957 /*
181d6902 958 * Free queue structures.
95ea3627 959 */
181d6902 960 rt2x00queue_free(rt2x00dev);
95ea3627
ID
961}
962EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
963
964/*
965 * Device state handlers
966 */
967#ifdef CONFIG_PM
968int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
969{
95ea3627 970 NOTICE(rt2x00dev, "Going to sleep.\n");
066cb637
ID
971
972 /*
07126127 973 * Prevent mac80211 from accessing driver while suspended.
066cb637 974 */
07126127
ID
975 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
976 return 0;
95ea3627
ID
977
978 /*
07126127 979 * Cleanup as much as possible.
95ea3627 980 */
95ea3627 981 rt2x00lib_uninitialize(rt2x00dev);
1682fe6d
ID
982
983 /*
984 * Suspend/disable extra components.
985 */
a9450b70 986 rt2x00leds_suspend(rt2x00dev);
95ea3627
ID
987 rt2x00debug_deregister(rt2x00dev);
988
989 /*
9896322a
ID
990 * Set device mode to sleep for power management,
991 * on some hardware this call seems to consistently fail.
992 * From the specifications it is hard to tell why it fails,
993 * and if this is a "bad thing".
994 * Overall it is safe to just ignore the failure and
995 * continue suspending. The only downside is that the
996 * device will not be in optimal power save mode, but with
997 * the radio and the other components already disabled the
998 * device is as good as disabled.
95ea3627 999 */
07126127 1000 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
9896322a
ID
1001 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1002 "continue suspending.\n");
95ea3627
ID
1003
1004 return 0;
1005}
1006EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1007
1008int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1009{
95ea3627 1010 NOTICE(rt2x00dev, "Waking up.\n");
95ea3627
ID
1011
1012 /*
1682fe6d 1013 * Restore/enable extra components.
95ea3627
ID
1014 */
1015 rt2x00debug_register(rt2x00dev);
a9450b70 1016 rt2x00leds_resume(rt2x00dev);
95ea3627 1017
e37ea213
ID
1018 /*
1019 * We are ready again to receive requests from mac80211.
1020 */
0262ab0d 1021 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
e37ea213 1022
95ea3627 1023 return 0;
95ea3627
ID
1024}
1025EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1026#endif /* CONFIG_PM */
1027
1028/*
1029 * rt2x00lib module information.
1030 */
1031MODULE_AUTHOR(DRV_PROJECT);
1032MODULE_VERSION(DRV_VERSION);
1033MODULE_DESCRIPTION("rt2x00 library");
1034MODULE_LICENSE("GPL");
This page took 0.626581 seconds and 5 git commands to generate.