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