[PATCH] rt2x00: Correctly identify rt2561turbo
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
CommitLineData
95ea3627
ID
1/*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21/*
22 Module: rt2x00lib
23 Abstract: rt2x00 generic device routines.
24 */
25
26/*
27 * Set enviroment defines for rt2x00.h
28 */
29#define DRV_NAME "rt2x00lib"
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33
34#include "rt2x00.h"
35#include "rt2x00lib.h"
36
37/*
38 * Ring handler.
39 */
40struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
41 const unsigned int queue)
42{
43 int beacon = test_bit(REQUIRE_BEACON_RING, &rt2x00dev->flags);
44
45 /*
46 * Check if we are requesting a reqular TX ring,
47 * or if we are requesting a Beacon or Atim ring.
48 * For Atim rings, we should check if it is supported.
49 */
50 if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
51 return &rt2x00dev->tx[queue];
52
53 if (!rt2x00dev->bcn || !beacon)
54 return NULL;
55
56 if (queue == IEEE80211_TX_QUEUE_BEACON)
57 return &rt2x00dev->bcn[0];
58 else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
59 return &rt2x00dev->bcn[1];
60
61 return NULL;
62}
63EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
64
65/*
66 * Link tuning handlers
67 */
68static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
69{
70 rt2x00_clear_link(&rt2x00dev->link);
71
72 /*
73 * Reset the link tuner.
74 */
75 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
76
77 queue_delayed_work(rt2x00dev->hw->workqueue,
78 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
79}
80
81static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
82{
83 if (delayed_work_pending(&rt2x00dev->link.work))
84 cancel_rearming_delayed_work(&rt2x00dev->link.work);
85}
86
87void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
88{
89 rt2x00lib_stop_link_tuner(rt2x00dev);
90 rt2x00lib_start_link_tuner(rt2x00dev);
91}
92
93/*
94 * Radio control handlers.
95 */
96int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
97{
98 int status;
99
100 /*
101 * Don't enable the radio twice.
102 * And check if the hardware button has been disabled.
103 */
104 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
105 (test_bit(DEVICE_SUPPORT_HW_BUTTON, &rt2x00dev->flags) &&
106 !test_bit(DEVICE_ENABLED_RADIO_HW, &rt2x00dev->flags)))
107 return 0;
108
109 /*
110 * Enable radio.
111 */
112 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
113 STATE_RADIO_ON);
114 if (status)
115 return status;
116
117 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
118
119 /*
120 * Enable RX.
121 */
122 rt2x00lib_toggle_rx(rt2x00dev, 1);
123
124 /*
125 * Start the TX queues.
126 */
127 ieee80211_start_queues(rt2x00dev->hw);
128
129 return 0;
130}
131
132void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
133{
134 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
135 return;
136
137 /*
4150c572 138 * Stop all scheduled work.
95ea3627
ID
139 */
140 if (work_pending(&rt2x00dev->beacon_work))
141 cancel_work_sync(&rt2x00dev->beacon_work);
4150c572
JB
142 if (work_pending(&rt2x00dev->filter_work))
143 cancel_work_sync(&rt2x00dev->filter_work);
95ea3627
ID
144
145 /*
146 * Stop the TX queues.
147 */
148 ieee80211_stop_queues(rt2x00dev->hw);
149
150 /*
151 * Disable RX.
152 */
153 rt2x00lib_toggle_rx(rt2x00dev, 0);
154
155 /*
156 * Disable radio.
157 */
158 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
159}
160
161void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, int enable)
162{
163 enum dev_state state = enable ? STATE_RADIO_RX_ON : STATE_RADIO_RX_OFF;
164
165 /*
166 * When we are disabling the RX, we should also stop the link tuner.
167 */
168 if (!enable)
169 rt2x00lib_stop_link_tuner(rt2x00dev);
170
171 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
172
173 /*
174 * When we are enabling the RX, we should also start the link tuner.
175 */
176 if (enable && is_interface_present(&rt2x00dev->interface))
177 rt2x00lib_start_link_tuner(rt2x00dev);
178}
179
180static void rt2x00lib_precalculate_link_signal(struct link *link)
181{
182 if (link->rx_failed || link->rx_success)
183 link->rx_percentage =
184 (link->rx_success * 100) /
185 (link->rx_failed + link->rx_success);
186 else
187 link->rx_percentage = 50;
188
189 if (link->tx_failed || link->tx_success)
190 link->tx_percentage =
191 (link->tx_success * 100) /
192 (link->tx_failed + link->tx_success);
193 else
194 link->tx_percentage = 50;
195
196 link->rx_success = 0;
197 link->rx_failed = 0;
198 link->tx_success = 0;
199 link->tx_failed = 0;
200}
201
202static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
203 int rssi)
204{
205 int rssi_percentage = 0;
206 int signal;
207
208 /*
209 * We need a positive value for the RSSI.
210 */
211 if (rssi < 0)
212 rssi += rt2x00dev->rssi_offset;
213
214 /*
215 * Calculate the different percentages,
216 * which will be used for the signal.
217 */
218 if (rt2x00dev->rssi_offset)
219 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
220
221 /*
222 * Add the individual percentages and use the WEIGHT
223 * defines to calculate the current link signal.
224 */
225 signal = ((WEIGHT_RSSI * rssi_percentage) +
226 (WEIGHT_TX * rt2x00dev->link.tx_percentage) +
227 (WEIGHT_RX * rt2x00dev->link.rx_percentage)) / 100;
228
229 return (signal > 100) ? 100 : signal;
230}
231
232static void rt2x00lib_link_tuner(struct work_struct *work)
233{
234 struct rt2x00_dev *rt2x00dev =
235 container_of(work, struct rt2x00_dev, link.work.work);
236
237 /*
238 * Update statistics.
239 */
240 rt2x00dev->ops->lib->link_stats(rt2x00dev);
241
242 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
243 rt2x00dev->link.rx_failed;
244
95ea3627
ID
245 /*
246 * Only perform the link tuning when Link tuning
247 * has been enabled (This could have been disabled from the EEPROM).
248 */
249 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
250 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
251
725d99d4
ID
252 /*
253 * Precalculate a portion of the link signal which is
254 * in based on the tx/rx success/failure counters.
255 */
256 rt2x00lib_precalculate_link_signal(&rt2x00dev->link);
257
95ea3627
ID
258 /*
259 * Increase tuner counter, and reschedule the next link tuner run.
260 */
261 rt2x00dev->link.count++;
262 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
263 LINK_TUNE_INTERVAL);
264}
265
4150c572
JB
266static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
267{
268 struct rt2x00_dev *rt2x00dev =
269 container_of(work, struct rt2x00_dev, filter_work);
270
271 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
272 rt2x00dev->interface.filter,
273 &rt2x00dev->interface.filter,
274 0, NULL);
275}
276
95ea3627
ID
277/*
278 * Interrupt context handlers.
279 */
280static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
281{
282 struct rt2x00_dev *rt2x00dev =
283 container_of(work, struct rt2x00_dev, beacon_work);
284 struct data_ring *ring =
285 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
286 struct data_entry *entry = rt2x00_get_data_entry(ring);
287 struct sk_buff *skb;
288
289 skb = ieee80211_beacon_get(rt2x00dev->hw,
290 rt2x00dev->interface.id,
291 &entry->tx_status.control);
292 if (!skb)
293 return;
294
295 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
296 &entry->tx_status.control);
297
298 dev_kfree_skb(skb);
299}
300
301void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
302{
303 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
304 return;
305
306 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
307}
308EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
309
310void rt2x00lib_txdone(struct data_entry *entry,
311 const int status, const int retry)
312{
313 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
314 struct ieee80211_tx_status *tx_status = &entry->tx_status;
315 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
316 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
317 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
318 status == TX_FAIL_OTHER);
319
320 /*
321 * Update TX statistics.
322 */
323 tx_status->flags = 0;
324 tx_status->ack_signal = 0;
325 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
326 tx_status->retry_count = retry;
327 rt2x00dev->link.tx_success += success;
328 rt2x00dev->link.tx_failed += retry + fail;
329
330 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
331 if (success)
332 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
333 else
334 stats->dot11ACKFailureCount++;
335 }
336
337 tx_status->queue_length = entry->ring->stats.limit;
338 tx_status->queue_number = tx_status->control.queue;
339
340 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
341 if (success)
342 stats->dot11RTSSuccessCount++;
343 else
344 stats->dot11RTSFailureCount++;
345 }
346
347 /*
348 * Send the tx_status to mac80211,
349 * that method also cleans up the skb structure.
350 */
351 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
352 entry->skb = NULL;
353}
354EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
355
356void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
4150c572 357 struct rxdata_entry_desc *desc)
95ea3627
ID
358{
359 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
360 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
361 struct ieee80211_hw_mode *mode;
362 struct ieee80211_rate *rate;
363 unsigned int i;
364 int val = 0;
365
366 /*
367 * Update RX statistics.
368 */
369 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
370 for (i = 0; i < mode->num_rates; i++) {
371 rate = &mode->rates[i];
372
373 /*
374 * When frame was received with an OFDM bitrate,
375 * the signal is the PLCP value. If it was received with
376 * a CCK bitrate the signal is the rate in 0.5kbit/s.
377 */
4150c572 378 if (!desc->ofdm)
95ea3627
ID
379 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
380 else
381 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
382
4150c572 383 if (val == desc->signal) {
95ea3627
ID
384 val = rate->val;
385 break;
386 }
387 }
388
4150c572 389 rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
95ea3627
ID
390 rt2x00dev->link.rx_success++;
391 rx_status->rate = val;
4150c572
JB
392 rx_status->signal =
393 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
394 rx_status->ssi = desc->rssi;
395 rx_status->flag = desc->flags;
95ea3627
ID
396
397 /*
398 * Send frame to mac80211
399 */
400 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
401}
402EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
403
404/*
405 * TX descriptor initializer
406 */
407void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
408 struct data_desc *txd,
409 struct ieee80211_hdr *ieee80211hdr,
410 unsigned int length,
411 struct ieee80211_tx_control *control)
412{
4150c572 413 struct txdata_entry_desc desc;
95ea3627
ID
414 struct data_ring *ring;
415 int tx_rate;
416 int bitrate;
417 int duration;
418 int residual;
419 u16 frame_control;
420 u16 seq_ctrl;
421
422 /*
423 * Make sure the descriptor is properly cleared.
424 */
425 memset(&desc, 0x00, sizeof(desc));
426
427 /*
428 * Get ring pointer, if we fail to obtain the
429 * correct ring, then use the first TX ring.
430 */
431 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
432 if (!ring)
433 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
434
435 desc.cw_min = ring->tx_params.cw_min;
436 desc.cw_max = ring->tx_params.cw_max;
437 desc.aifs = ring->tx_params.aifs;
438
439 /*
440 * Identify queue
441 */
442 if (control->queue < rt2x00dev->hw->queues)
443 desc.queue = control->queue;
444 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
445 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
446 desc.queue = QUEUE_MGMT;
447 else
448 desc.queue = QUEUE_OTHER;
449
450 /*
451 * Read required fields from ieee80211 header.
452 */
453 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
454 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
455
456 tx_rate = control->tx_rate;
457
458 /*
459 * Check if this is a RTS/CTS frame
460 */
461 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
462 __set_bit(ENTRY_TXD_BURST, &desc.flags);
463 if (is_rts_frame(frame_control))
464 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
465 if (control->rts_cts_rate)
466 tx_rate = control->rts_cts_rate;
467 }
468
469 /*
470 * Check for OFDM
471 */
472 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
473 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
474
475 /*
476 * Check if more fragments are pending
477 */
478 if (ieee80211_get_morefrag(ieee80211hdr)) {
479 __set_bit(ENTRY_TXD_BURST, &desc.flags);
480 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
481 }
482
483 /*
484 * Beacons and probe responses require the tsf timestamp
485 * to be inserted into the frame.
486 */
487 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
488 is_probe_resp(frame_control))
489 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
490
491 /*
492 * Determine with what IFS priority this frame should be send.
493 * Set ifs to IFS_SIFS when the this is not the first fragment,
494 * or this fragment came after RTS/CTS.
495 */
496 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
497 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
498 desc.ifs = IFS_SIFS;
499 else
500 desc.ifs = IFS_BACKOFF;
501
502 /*
503 * PLCP setup
504 * Length calculation depends on OFDM/CCK rate.
505 */
506 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
507 desc.service = 0x04;
508
509 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
510 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
511 desc.length_low = ((length + FCS_LEN) & 0x3f);
512 } else {
513 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
514
515 /*
516 * Convert length to microseconds.
517 */
518 residual = get_duration_res(length + FCS_LEN, bitrate);
519 duration = get_duration(length + FCS_LEN, bitrate);
520
521 if (residual != 0) {
522 duration++;
523
524 /*
525 * Check if we need to set the Length Extension
526 */
527 if (bitrate == 110 && residual <= 3)
528 desc.service |= 0x80;
529 }
530
531 desc.length_high = (duration >> 8) & 0xff;
532 desc.length_low = duration & 0xff;
533
534 /*
535 * When preamble is enabled we should set the
536 * preamble bit for the signal.
537 */
538 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
539 desc.signal |= 0x08;
540 }
541
542 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
543 ieee80211hdr, length, control);
544}
545EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
546
547/*
548 * Driver initialization handlers.
549 */
550static void rt2x00lib_channel(struct ieee80211_channel *entry,
551 const int channel, const int tx_power,
552 const int value)
553{
554 entry->chan = channel;
555 if (channel <= 14)
556 entry->freq = 2407 + (5 * channel);
557 else
558 entry->freq = 5000 + (5 * channel);
559 entry->val = value;
560 entry->flag =
561 IEEE80211_CHAN_W_IBSS |
562 IEEE80211_CHAN_W_ACTIVE_SCAN |
563 IEEE80211_CHAN_W_SCAN;
564 entry->power_level = tx_power;
565 entry->antenna_max = 0xff;
566}
567
568static void rt2x00lib_rate(struct ieee80211_rate *entry,
569 const int rate, const int mask,
570 const int plcp, const int flags)
571{
572 entry->rate = rate;
573 entry->val =
574 DEVICE_SET_RATE_FIELD(rate, RATE) |
575 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
576 DEVICE_SET_RATE_FIELD(plcp, PLCP);
577 entry->flags = flags;
578 entry->val2 = entry->val;
579 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
580 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
581 entry->min_rssi_ack = 0;
582 entry->min_rssi_ack_delta = 0;
583}
584
585static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
586 struct hw_mode_spec *spec)
587{
588 struct ieee80211_hw *hw = rt2x00dev->hw;
589 struct ieee80211_hw_mode *hwmodes;
590 struct ieee80211_channel *channels;
591 struct ieee80211_rate *rates;
592 unsigned int i;
593 unsigned char tx_power;
594
595 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
596 if (!hwmodes)
597 goto exit;
598
599 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
600 if (!channels)
601 goto exit_free_modes;
602
603 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
604 if (!rates)
605 goto exit_free_channels;
606
607 /*
608 * Initialize Rate list.
609 */
610 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
611 0x00, IEEE80211_RATE_CCK);
612 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
613 0x01, IEEE80211_RATE_CCK_2);
614 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
615 0x02, IEEE80211_RATE_CCK_2);
616 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
617 0x03, IEEE80211_RATE_CCK_2);
618
619 if (spec->num_rates > 4) {
620 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
621 0x0b, IEEE80211_RATE_OFDM);
622 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
623 0x0f, IEEE80211_RATE_OFDM);
624 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
625 0x0a, IEEE80211_RATE_OFDM);
626 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
627 0x0e, IEEE80211_RATE_OFDM);
628 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
629 0x09, IEEE80211_RATE_OFDM);
630 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
631 0x0d, IEEE80211_RATE_OFDM);
632 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
633 0x08, IEEE80211_RATE_OFDM);
634 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
635 0x0c, IEEE80211_RATE_OFDM);
636 }
637
638 /*
639 * Initialize Channel list.
640 */
641 for (i = 0; i < spec->num_channels; i++) {
642 if (spec->channels[i].channel <= 14)
643 tx_power = spec->tx_power_bg[i];
644 else if (spec->tx_power_a)
645 tx_power = spec->tx_power_a[i];
646 else
647 tx_power = spec->tx_power_default;
648
649 rt2x00lib_channel(&channels[i],
650 spec->channels[i].channel, tx_power, i);
651 }
652
653 /*
654 * Intitialize 802.11b
655 * Rates: CCK.
656 * Channels: OFDM.
657 */
658 if (spec->num_modes > HWMODE_B) {
659 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
660 hwmodes[HWMODE_B].num_channels = 14;
661 hwmodes[HWMODE_B].num_rates = 4;
662 hwmodes[HWMODE_B].channels = channels;
663 hwmodes[HWMODE_B].rates = rates;
664 }
665
666 /*
667 * Intitialize 802.11g
668 * Rates: CCK, OFDM.
669 * Channels: OFDM.
670 */
671 if (spec->num_modes > HWMODE_G) {
672 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
673 hwmodes[HWMODE_G].num_channels = 14;
674 hwmodes[HWMODE_G].num_rates = spec->num_rates;
675 hwmodes[HWMODE_G].channels = channels;
676 hwmodes[HWMODE_G].rates = rates;
677 }
678
679 /*
680 * Intitialize 802.11a
681 * Rates: OFDM.
682 * Channels: OFDM, UNII, HiperLAN2.
683 */
684 if (spec->num_modes > HWMODE_A) {
685 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
686 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
687 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
688 hwmodes[HWMODE_A].channels = &channels[14];
689 hwmodes[HWMODE_A].rates = &rates[4];
690 }
691
692 if (spec->num_modes > HWMODE_G &&
693 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
694 goto exit_free_rates;
695
696 if (spec->num_modes > HWMODE_B &&
697 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
698 goto exit_free_rates;
699
700 if (spec->num_modes > HWMODE_A &&
701 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
702 goto exit_free_rates;
703
704 rt2x00dev->hwmodes = hwmodes;
705
706 return 0;
707
708exit_free_rates:
709 kfree(rates);
710
711exit_free_channels:
712 kfree(channels);
713
714exit_free_modes:
715 kfree(hwmodes);
716
717exit:
718 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
719 return -ENOMEM;
720}
721
722static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
723{
724 if (test_bit(DEVICE_INITIALIZED_HW, &rt2x00dev->flags))
725 ieee80211_unregister_hw(rt2x00dev->hw);
726
727 if (likely(rt2x00dev->hwmodes)) {
728 kfree(rt2x00dev->hwmodes->channels);
729 kfree(rt2x00dev->hwmodes->rates);
730 kfree(rt2x00dev->hwmodes);
731 rt2x00dev->hwmodes = NULL;
732 }
733}
734
735static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
736{
737 struct hw_mode_spec *spec = &rt2x00dev->spec;
738 int status;
739
740 /*
741 * Initialize HW modes.
742 */
743 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
744 if (status)
745 return status;
746
747 /*
748 * Register HW.
749 */
750 status = ieee80211_register_hw(rt2x00dev->hw);
751 if (status) {
752 rt2x00lib_remove_hw(rt2x00dev);
753 return status;
754 }
755
756 __set_bit(DEVICE_INITIALIZED_HW, &rt2x00dev->flags);
757
758 return 0;
759}
760
761/*
762 * Initialization/uninitialization handlers.
763 */
764static int rt2x00lib_alloc_entries(struct data_ring *ring,
765 const u16 max_entries, const u16 data_size,
766 const u16 desc_size)
767{
768 struct data_entry *entry;
769 unsigned int i;
770
771 ring->stats.limit = max_entries;
772 ring->data_size = data_size;
773 ring->desc_size = desc_size;
774
775 /*
776 * Allocate all ring entries.
777 */
778 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
779 if (!entry)
780 return -ENOMEM;
781
782 for (i = 0; i < ring->stats.limit; i++) {
783 entry[i].flags = 0;
784 entry[i].ring = ring;
785 entry[i].skb = NULL;
786 }
787
788 ring->entry = entry;
789
790 return 0;
791}
792
793static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
794{
795 struct data_ring *ring;
796
797 /*
798 * Allocate the RX ring.
799 */
800 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
801 rt2x00dev->ops->rxd_size))
802 return -ENOMEM;
803
804 /*
805 * First allocate the TX rings.
806 */
807 txring_for_each(rt2x00dev, ring) {
808 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
809 rt2x00dev->ops->txd_size))
810 return -ENOMEM;
811 }
812
813 if (!test_bit(REQUIRE_BEACON_RING, &rt2x00dev->flags))
814 return 0;
815
816 /*
817 * Allocate the BEACON ring.
818 */
819 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
820 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
821 return -ENOMEM;
822
823 /*
824 * Allocate the Atim ring.
825 */
826 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
827 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
828 return -ENOMEM;
829
830 return 0;
831}
832
833static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
834{
835 struct data_ring *ring;
836
837 ring_for_each(rt2x00dev, ring) {
838 kfree(ring->entry);
839 ring->entry = NULL;
840 }
841}
842
843void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
844{
845 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
846 return;
847
848 /*
849 * Unregister rfkill.
850 */
851 rt2x00rfkill_unregister(rt2x00dev);
852
853 /*
854 * Allow the HW to uninitialize.
855 */
856 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
857
858 /*
859 * Free allocated ring entries.
860 */
861 rt2x00lib_free_ring_entries(rt2x00dev);
862}
863
864int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
865{
866 int status;
867
868 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
869 return 0;
870
871 /*
872 * Allocate all ring entries.
873 */
874 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
875 if (status) {
876 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
877 return status;
878 }
879
880 /*
881 * Initialize the device.
882 */
883 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
884 if (status)
885 goto exit;
886
887 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
888
889 /*
890 * Register the rfkill handler.
891 */
892 status = rt2x00rfkill_register(rt2x00dev);
893 if (status)
894 goto exit_unitialize;
895
896 return 0;
897
898exit_unitialize:
899 rt2x00lib_uninitialize(rt2x00dev);
900
901exit:
902 rt2x00lib_free_ring_entries(rt2x00dev);
903
904 return status;
905}
906
907/*
908 * driver allocation handlers.
909 */
910static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
911{
912 struct data_ring *ring;
913
914 /*
915 * We need the following rings:
916 * RX: 1
917 * TX: hw->queues
918 * Beacon: 1 (if required)
919 * Atim: 1 (if required)
920 */
921 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
922 (2 * test_bit(REQUIRE_BEACON_RING, &rt2x00dev->flags));
923
924 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
925 if (!ring) {
926 ERROR(rt2x00dev, "Ring allocation failed.\n");
927 return -ENOMEM;
928 }
929
930 /*
931 * Initialize pointers
932 */
933 rt2x00dev->rx = ring;
934 rt2x00dev->tx = &rt2x00dev->rx[1];
935 if (test_bit(REQUIRE_BEACON_RING, &rt2x00dev->flags))
936 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
937
938 /*
939 * Initialize ring parameters.
940 * cw_min: 2^5 = 32.
941 * cw_max: 2^10 = 1024.
942 */
943 ring_for_each(rt2x00dev, ring) {
944 ring->rt2x00dev = rt2x00dev;
945 ring->tx_params.aifs = 2;
946 ring->tx_params.cw_min = 5;
947 ring->tx_params.cw_max = 10;
948 }
949
950 return 0;
951}
952
953static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
954{
955 kfree(rt2x00dev->rx);
956 rt2x00dev->rx = NULL;
957 rt2x00dev->tx = NULL;
958 rt2x00dev->bcn = NULL;
959}
960
961int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
962{
963 int retval = -ENOMEM;
964
965 /*
966 * Let the driver probe the device to detect the capabilities.
967 */
968 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
969 if (retval) {
970 ERROR(rt2x00dev, "Failed to allocate device.\n");
971 goto exit;
972 }
973
974 /*
975 * Initialize configuration work.
976 */
977 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
4150c572 978 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
95ea3627
ID
979 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
980
981 /*
982 * Reset current working type.
983 */
984 rt2x00dev->interface.type = INVALID_INTERFACE;
985
986 /*
987 * Allocate ring array.
988 */
989 retval = rt2x00lib_alloc_rings(rt2x00dev);
990 if (retval)
991 goto exit;
992
993 /*
994 * Initialize ieee80211 structure.
995 */
996 retval = rt2x00lib_probe_hw(rt2x00dev);
997 if (retval) {
998 ERROR(rt2x00dev, "Failed to initialize hw.\n");
999 goto exit;
1000 }
1001
1002 /*
1003 * Allocatie rfkill.
1004 */
1005 retval = rt2x00rfkill_allocate(rt2x00dev);
1006 if (retval)
1007 goto exit;
1008
1009 /*
1010 * Open the debugfs entry.
1011 */
1012 rt2x00debug_register(rt2x00dev);
1013
1014 return 0;
1015
1016exit:
1017 rt2x00lib_remove_dev(rt2x00dev);
1018
1019 return retval;
1020}
1021EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1022
1023void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1024{
1025 /*
1026 * Disable radio.
1027 */
1028 rt2x00lib_disable_radio(rt2x00dev);
1029
1030 /*
1031 * Uninitialize device.
1032 */
1033 rt2x00lib_uninitialize(rt2x00dev);
1034
1035 /*
1036 * Close debugfs entry.
1037 */
1038 rt2x00debug_deregister(rt2x00dev);
1039
1040 /*
1041 * Free rfkill
1042 */
1043 rt2x00rfkill_free(rt2x00dev);
1044
1045 /*
1046 * Free ieee80211_hw memory.
1047 */
1048 rt2x00lib_remove_hw(rt2x00dev);
1049
1050 /*
1051 * Free firmware image.
1052 */
1053 rt2x00lib_free_firmware(rt2x00dev);
1054
1055 /*
1056 * Free ring structures.
1057 */
1058 rt2x00lib_free_rings(rt2x00dev);
1059}
1060EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1061
1062/*
1063 * Device state handlers
1064 */
1065#ifdef CONFIG_PM
1066int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1067{
1068 int retval;
1069
1070 NOTICE(rt2x00dev, "Going to sleep.\n");
1071
1072 /*
1073 * Disable radio and unitialize all items
1074 * that must be recreated on resume.
1075 */
1076 rt2x00lib_disable_radio(rt2x00dev);
1077 rt2x00lib_uninitialize(rt2x00dev);
1078 rt2x00debug_deregister(rt2x00dev);
1079
1080 /*
1081 * Set device mode to sleep for power management.
1082 */
1083 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1084 if (retval)
1085 return retval;
1086
1087 return 0;
1088}
1089EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1090
1091int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1092{
1093 struct interface *intf = &rt2x00dev->interface;
1094 int retval;
1095
1096 NOTICE(rt2x00dev, "Waking up.\n");
1097 __set_bit(INTERFACE_RESUME, &rt2x00dev->flags);
1098
1099 /*
1100 * Open the debugfs entry.
1101 */
1102 rt2x00debug_register(rt2x00dev);
1103
1104 /*
1105 * Reinitialize device and all active interfaces.
1106 */
1107 retval = rt2x00mac_start(rt2x00dev->hw);
1108 if (retval)
1109 goto exit;
1110
1111 /*
1112 * Reconfigure device.
1113 */
1114 retval = rt2x00mac_config(rt2x00dev->hw, &rt2x00dev->hw->conf);
1115 if (retval)
1116 goto exit;
1117
1118 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1119 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1120 rt2x00lib_config_type(rt2x00dev, intf->type);
95ea3627
ID
1121
1122 /*
1123 * When in Master or Ad-hoc mode,
1124 * restart Beacon transmitting by faking a beacondone event.
1125 */
1126 if (intf->type == IEEE80211_IF_TYPE_AP ||
1127 intf->type == IEEE80211_IF_TYPE_IBSS)
1128 rt2x00lib_beacondone(rt2x00dev);
1129
1130 __clear_bit(INTERFACE_RESUME, &rt2x00dev->flags);
1131
1132 return 0;
1133
1134exit:
1135 rt2x00lib_disable_radio(rt2x00dev);
1136 rt2x00lib_uninitialize(rt2x00dev);
1137 rt2x00debug_deregister(rt2x00dev);
1138
1139 __clear_bit(INTERFACE_RESUME, &rt2x00dev->flags);
1140
1141 return retval;
1142}
1143EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1144#endif /* CONFIG_PM */
1145
1146/*
1147 * rt2x00lib module information.
1148 */
1149MODULE_AUTHOR(DRV_PROJECT);
1150MODULE_VERSION(DRV_VERSION);
1151MODULE_DESCRIPTION("rt2x00 library");
1152MODULE_LICENSE("GPL");