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