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