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