blob: f25475318c9b5ce3937bb497ea379a6cd4f8d2bb [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);
279
280 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
281 rt2x00dev->interface.filter,
282 &rt2x00dev->interface.filter,
283 0, NULL);
284}
285
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700286/*
287 * Interrupt context handlers.
288 */
289static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
290{
291 struct rt2x00_dev *rt2x00dev =
292 container_of(work, struct rt2x00_dev, beacon_work);
293 struct data_ring *ring =
294 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
295 struct data_entry *entry = rt2x00_get_data_entry(ring);
296 struct sk_buff *skb;
297
298 skb = ieee80211_beacon_get(rt2x00dev->hw,
299 rt2x00dev->interface.id,
300 &entry->tx_status.control);
301 if (!skb)
302 return;
303
304 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
305 &entry->tx_status.control);
306
307 dev_kfree_skb(skb);
308}
309
310void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
311{
312 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
313 return;
314
315 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
316}
317EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
318
319void rt2x00lib_txdone(struct data_entry *entry,
320 const int status, const int retry)
321{
322 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
323 struct ieee80211_tx_status *tx_status = &entry->tx_status;
324 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
325 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
326 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
327 status == TX_FAIL_OTHER);
328
329 /*
330 * Update TX statistics.
331 */
332 tx_status->flags = 0;
333 tx_status->ack_signal = 0;
334 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
335 tx_status->retry_count = retry;
336 rt2x00dev->link.tx_success += success;
337 rt2x00dev->link.tx_failed += retry + fail;
338
339 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
340 if (success)
341 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
342 else
343 stats->dot11ACKFailureCount++;
344 }
345
346 tx_status->queue_length = entry->ring->stats.limit;
347 tx_status->queue_number = tx_status->control.queue;
348
349 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
350 if (success)
351 stats->dot11RTSSuccessCount++;
352 else
353 stats->dot11RTSFailureCount++;
354 }
355
356 /*
357 * Send the tx_status to mac80211,
358 * that method also cleans up the skb structure.
359 */
360 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
361 entry->skb = NULL;
362}
363EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
364
365void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
Johannes Berg4150c572007-09-17 01:29:23 -0400366 struct rxdata_entry_desc *desc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700367{
368 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
369 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
370 struct ieee80211_hw_mode *mode;
371 struct ieee80211_rate *rate;
372 unsigned int i;
373 int val = 0;
374
375 /*
376 * Update RX statistics.
377 */
378 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
379 for (i = 0; i < mode->num_rates; i++) {
380 rate = &mode->rates[i];
381
382 /*
383 * When frame was received with an OFDM bitrate,
384 * the signal is the PLCP value. If it was received with
385 * a CCK bitrate the signal is the rate in 0.5kbit/s.
386 */
Johannes Berg4150c572007-09-17 01:29:23 -0400387 if (!desc->ofdm)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700388 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
389 else
390 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
391
Johannes Berg4150c572007-09-17 01:29:23 -0400392 if (val == desc->signal) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700393 val = rate->val;
394 break;
395 }
396 }
397
Johannes Berg4150c572007-09-17 01:29:23 -0400398 rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700399 rt2x00dev->link.rx_success++;
400 rx_status->rate = val;
Johannes Berg4150c572007-09-17 01:29:23 -0400401 rx_status->signal =
402 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
403 rx_status->ssi = desc->rssi;
404 rx_status->flag = desc->flags;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700405
406 /*
407 * Send frame to mac80211
408 */
409 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
410}
411EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
412
413/*
414 * TX descriptor initializer
415 */
416void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
417 struct data_desc *txd,
418 struct ieee80211_hdr *ieee80211hdr,
419 unsigned int length,
420 struct ieee80211_tx_control *control)
421{
Johannes Berg4150c572007-09-17 01:29:23 -0400422 struct txdata_entry_desc desc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700423 struct data_ring *ring;
424 int tx_rate;
425 int bitrate;
426 int duration;
427 int residual;
428 u16 frame_control;
429 u16 seq_ctrl;
430
431 /*
432 * Make sure the descriptor is properly cleared.
433 */
434 memset(&desc, 0x00, sizeof(desc));
435
436 /*
437 * Get ring pointer, if we fail to obtain the
438 * correct ring, then use the first TX ring.
439 */
440 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
441 if (!ring)
442 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
443
444 desc.cw_min = ring->tx_params.cw_min;
445 desc.cw_max = ring->tx_params.cw_max;
446 desc.aifs = ring->tx_params.aifs;
447
448 /*
449 * Identify queue
450 */
451 if (control->queue < rt2x00dev->hw->queues)
452 desc.queue = control->queue;
453 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
454 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
455 desc.queue = QUEUE_MGMT;
456 else
457 desc.queue = QUEUE_OTHER;
458
459 /*
460 * Read required fields from ieee80211 header.
461 */
462 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
463 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
464
465 tx_rate = control->tx_rate;
466
467 /*
468 * Check if this is a RTS/CTS frame
469 */
470 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
471 __set_bit(ENTRY_TXD_BURST, &desc.flags);
472 if (is_rts_frame(frame_control))
473 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
474 if (control->rts_cts_rate)
475 tx_rate = control->rts_cts_rate;
476 }
477
478 /*
479 * Check for OFDM
480 */
481 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
482 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
483
484 /*
485 * Check if more fragments are pending
486 */
487 if (ieee80211_get_morefrag(ieee80211hdr)) {
488 __set_bit(ENTRY_TXD_BURST, &desc.flags);
489 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
490 }
491
492 /*
493 * Beacons and probe responses require the tsf timestamp
494 * to be inserted into the frame.
495 */
496 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
497 is_probe_resp(frame_control))
498 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
499
500 /*
501 * Determine with what IFS priority this frame should be send.
502 * Set ifs to IFS_SIFS when the this is not the first fragment,
503 * or this fragment came after RTS/CTS.
504 */
505 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
506 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
507 desc.ifs = IFS_SIFS;
508 else
509 desc.ifs = IFS_BACKOFF;
510
511 /*
512 * PLCP setup
513 * Length calculation depends on OFDM/CCK rate.
514 */
515 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
516 desc.service = 0x04;
517
518 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
519 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
520 desc.length_low = ((length + FCS_LEN) & 0x3f);
521 } else {
522 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
523
524 /*
525 * Convert length to microseconds.
526 */
527 residual = get_duration_res(length + FCS_LEN, bitrate);
528 duration = get_duration(length + FCS_LEN, bitrate);
529
530 if (residual != 0) {
531 duration++;
532
533 /*
534 * Check if we need to set the Length Extension
535 */
536 if (bitrate == 110 && residual <= 3)
537 desc.service |= 0x80;
538 }
539
540 desc.length_high = (duration >> 8) & 0xff;
541 desc.length_low = duration & 0xff;
542
543 /*
544 * When preamble is enabled we should set the
545 * preamble bit for the signal.
546 */
547 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
548 desc.signal |= 0x08;
549 }
550
551 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
552 ieee80211hdr, length, control);
553}
554EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
555
556/*
557 * Driver initialization handlers.
558 */
559static void rt2x00lib_channel(struct ieee80211_channel *entry,
560 const int channel, const int tx_power,
561 const int value)
562{
563 entry->chan = channel;
564 if (channel <= 14)
565 entry->freq = 2407 + (5 * channel);
566 else
567 entry->freq = 5000 + (5 * channel);
568 entry->val = value;
569 entry->flag =
570 IEEE80211_CHAN_W_IBSS |
571 IEEE80211_CHAN_W_ACTIVE_SCAN |
572 IEEE80211_CHAN_W_SCAN;
573 entry->power_level = tx_power;
574 entry->antenna_max = 0xff;
575}
576
577static void rt2x00lib_rate(struct ieee80211_rate *entry,
578 const int rate, const int mask,
579 const int plcp, const int flags)
580{
581 entry->rate = rate;
582 entry->val =
583 DEVICE_SET_RATE_FIELD(rate, RATE) |
584 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
585 DEVICE_SET_RATE_FIELD(plcp, PLCP);
586 entry->flags = flags;
587 entry->val2 = entry->val;
588 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
589 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
590 entry->min_rssi_ack = 0;
591 entry->min_rssi_ack_delta = 0;
592}
593
594static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
595 struct hw_mode_spec *spec)
596{
597 struct ieee80211_hw *hw = rt2x00dev->hw;
598 struct ieee80211_hw_mode *hwmodes;
599 struct ieee80211_channel *channels;
600 struct ieee80211_rate *rates;
601 unsigned int i;
602 unsigned char tx_power;
603
604 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
605 if (!hwmodes)
606 goto exit;
607
608 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
609 if (!channels)
610 goto exit_free_modes;
611
612 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
613 if (!rates)
614 goto exit_free_channels;
615
616 /*
617 * Initialize Rate list.
618 */
619 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
620 0x00, IEEE80211_RATE_CCK);
621 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
622 0x01, IEEE80211_RATE_CCK_2);
623 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
624 0x02, IEEE80211_RATE_CCK_2);
625 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
626 0x03, IEEE80211_RATE_CCK_2);
627
628 if (spec->num_rates > 4) {
629 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
630 0x0b, IEEE80211_RATE_OFDM);
631 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
632 0x0f, IEEE80211_RATE_OFDM);
633 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
634 0x0a, IEEE80211_RATE_OFDM);
635 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
636 0x0e, IEEE80211_RATE_OFDM);
637 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
638 0x09, IEEE80211_RATE_OFDM);
639 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
640 0x0d, IEEE80211_RATE_OFDM);
641 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
642 0x08, IEEE80211_RATE_OFDM);
643 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
644 0x0c, IEEE80211_RATE_OFDM);
645 }
646
647 /*
648 * Initialize Channel list.
649 */
650 for (i = 0; i < spec->num_channels; i++) {
651 if (spec->channels[i].channel <= 14)
652 tx_power = spec->tx_power_bg[i];
653 else if (spec->tx_power_a)
654 tx_power = spec->tx_power_a[i];
655 else
656 tx_power = spec->tx_power_default;
657
658 rt2x00lib_channel(&channels[i],
659 spec->channels[i].channel, tx_power, i);
660 }
661
662 /*
663 * Intitialize 802.11b
664 * Rates: CCK.
665 * Channels: OFDM.
666 */
667 if (spec->num_modes > HWMODE_B) {
668 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
669 hwmodes[HWMODE_B].num_channels = 14;
670 hwmodes[HWMODE_B].num_rates = 4;
671 hwmodes[HWMODE_B].channels = channels;
672 hwmodes[HWMODE_B].rates = rates;
673 }
674
675 /*
676 * Intitialize 802.11g
677 * Rates: CCK, OFDM.
678 * Channels: OFDM.
679 */
680 if (spec->num_modes > HWMODE_G) {
681 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
682 hwmodes[HWMODE_G].num_channels = 14;
683 hwmodes[HWMODE_G].num_rates = spec->num_rates;
684 hwmodes[HWMODE_G].channels = channels;
685 hwmodes[HWMODE_G].rates = rates;
686 }
687
688 /*
689 * Intitialize 802.11a
690 * Rates: OFDM.
691 * Channels: OFDM, UNII, HiperLAN2.
692 */
693 if (spec->num_modes > HWMODE_A) {
694 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
695 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
696 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
697 hwmodes[HWMODE_A].channels = &channels[14];
698 hwmodes[HWMODE_A].rates = &rates[4];
699 }
700
701 if (spec->num_modes > HWMODE_G &&
702 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
703 goto exit_free_rates;
704
705 if (spec->num_modes > HWMODE_B &&
706 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
707 goto exit_free_rates;
708
709 if (spec->num_modes > HWMODE_A &&
710 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
711 goto exit_free_rates;
712
713 rt2x00dev->hwmodes = hwmodes;
714
715 return 0;
716
717exit_free_rates:
718 kfree(rates);
719
720exit_free_channels:
721 kfree(channels);
722
723exit_free_modes:
724 kfree(hwmodes);
725
726exit:
727 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
728 return -ENOMEM;
729}
730
731static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
732{
Ivo van Doorn066cb632007-09-25 20:55:39 +0200733 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700734 ieee80211_unregister_hw(rt2x00dev->hw);
735
736 if (likely(rt2x00dev->hwmodes)) {
737 kfree(rt2x00dev->hwmodes->channels);
738 kfree(rt2x00dev->hwmodes->rates);
739 kfree(rt2x00dev->hwmodes);
740 rt2x00dev->hwmodes = NULL;
741 }
742}
743
744static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
745{
746 struct hw_mode_spec *spec = &rt2x00dev->spec;
747 int status;
748
749 /*
750 * Initialize HW modes.
751 */
752 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
753 if (status)
754 return status;
755
756 /*
757 * Register HW.
758 */
759 status = ieee80211_register_hw(rt2x00dev->hw);
760 if (status) {
761 rt2x00lib_remove_hw(rt2x00dev);
762 return status;
763 }
764
Ivo van Doorn066cb632007-09-25 20:55:39 +0200765 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700766
767 return 0;
768}
769
770/*
771 * Initialization/uninitialization handlers.
772 */
773static int rt2x00lib_alloc_entries(struct data_ring *ring,
774 const u16 max_entries, const u16 data_size,
775 const u16 desc_size)
776{
777 struct data_entry *entry;
778 unsigned int i;
779
780 ring->stats.limit = max_entries;
781 ring->data_size = data_size;
782 ring->desc_size = desc_size;
783
784 /*
785 * Allocate all ring entries.
786 */
787 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
788 if (!entry)
789 return -ENOMEM;
790
791 for (i = 0; i < ring->stats.limit; i++) {
792 entry[i].flags = 0;
793 entry[i].ring = ring;
794 entry[i].skb = NULL;
795 }
796
797 ring->entry = entry;
798
799 return 0;
800}
801
802static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
803{
804 struct data_ring *ring;
805
806 /*
807 * Allocate the RX ring.
808 */
809 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
810 rt2x00dev->ops->rxd_size))
811 return -ENOMEM;
812
813 /*
814 * First allocate the TX rings.
815 */
816 txring_for_each(rt2x00dev, ring) {
817 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
818 rt2x00dev->ops->txd_size))
819 return -ENOMEM;
820 }
821
Ivo van Doorn066cb632007-09-25 20:55:39 +0200822 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700823 return 0;
824
825 /*
826 * Allocate the BEACON ring.
827 */
828 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
829 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
830 return -ENOMEM;
831
832 /*
833 * Allocate the Atim ring.
834 */
835 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
836 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
837 return -ENOMEM;
838
839 return 0;
840}
841
842static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
843{
844 struct data_ring *ring;
845
846 ring_for_each(rt2x00dev, ring) {
847 kfree(ring->entry);
848 ring->entry = NULL;
849 }
850}
851
852void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
853{
854 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
855 return;
856
857 /*
858 * Unregister rfkill.
859 */
860 rt2x00rfkill_unregister(rt2x00dev);
861
862 /*
863 * Allow the HW to uninitialize.
864 */
865 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
866
867 /*
868 * Free allocated ring entries.
869 */
870 rt2x00lib_free_ring_entries(rt2x00dev);
871}
872
873int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
874{
875 int status;
876
877 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
878 return 0;
879
880 /*
881 * Allocate all ring entries.
882 */
883 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
884 if (status) {
885 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
886 return status;
887 }
888
889 /*
890 * Initialize the device.
891 */
892 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
893 if (status)
894 goto exit;
895
896 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
897
898 /*
899 * Register the rfkill handler.
900 */
901 status = rt2x00rfkill_register(rt2x00dev);
902 if (status)
903 goto exit_unitialize;
904
905 return 0;
906
907exit_unitialize:
908 rt2x00lib_uninitialize(rt2x00dev);
909
910exit:
911 rt2x00lib_free_ring_entries(rt2x00dev);
912
913 return status;
914}
915
916/*
917 * driver allocation handlers.
918 */
919static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
920{
921 struct data_ring *ring;
922
923 /*
924 * We need the following rings:
925 * RX: 1
926 * TX: hw->queues
927 * Beacon: 1 (if required)
928 * Atim: 1 (if required)
929 */
930 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
Ivo van Doorn066cb632007-09-25 20:55:39 +0200931 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700932
933 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
934 if (!ring) {
935 ERROR(rt2x00dev, "Ring allocation failed.\n");
936 return -ENOMEM;
937 }
938
939 /*
940 * Initialize pointers
941 */
942 rt2x00dev->rx = ring;
943 rt2x00dev->tx = &rt2x00dev->rx[1];
Ivo van Doorn066cb632007-09-25 20:55:39 +0200944 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700945 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
946
947 /*
948 * Initialize ring parameters.
949 * cw_min: 2^5 = 32.
950 * cw_max: 2^10 = 1024.
951 */
952 ring_for_each(rt2x00dev, ring) {
953 ring->rt2x00dev = rt2x00dev;
954 ring->tx_params.aifs = 2;
955 ring->tx_params.cw_min = 5;
956 ring->tx_params.cw_max = 10;
957 }
958
959 return 0;
960}
961
962static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
963{
964 kfree(rt2x00dev->rx);
965 rt2x00dev->rx = NULL;
966 rt2x00dev->tx = NULL;
967 rt2x00dev->bcn = NULL;
968}
969
970int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
971{
972 int retval = -ENOMEM;
973
974 /*
975 * Let the driver probe the device to detect the capabilities.
976 */
977 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
978 if (retval) {
979 ERROR(rt2x00dev, "Failed to allocate device.\n");
980 goto exit;
981 }
982
983 /*
984 * Initialize configuration work.
985 */
986 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
Johannes Berg4150c572007-09-17 01:29:23 -0400987 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700988 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
989
990 /*
991 * Reset current working type.
992 */
993 rt2x00dev->interface.type = INVALID_INTERFACE;
994
995 /*
996 * Allocate ring array.
997 */
998 retval = rt2x00lib_alloc_rings(rt2x00dev);
999 if (retval)
1000 goto exit;
1001
1002 /*
1003 * Initialize ieee80211 structure.
1004 */
1005 retval = rt2x00lib_probe_hw(rt2x00dev);
1006 if (retval) {
1007 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1008 goto exit;
1009 }
1010
1011 /*
1012 * Allocatie rfkill.
1013 */
1014 retval = rt2x00rfkill_allocate(rt2x00dev);
1015 if (retval)
1016 goto exit;
1017
1018 /*
1019 * Open the debugfs entry.
1020 */
1021 rt2x00debug_register(rt2x00dev);
1022
Ivo van Doorn066cb632007-09-25 20:55:39 +02001023 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1024
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001025 return 0;
1026
1027exit:
1028 rt2x00lib_remove_dev(rt2x00dev);
1029
1030 return retval;
1031}
1032EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1033
1034void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1035{
Ivo van Doorn066cb632007-09-25 20:55:39 +02001036 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1037
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001038 /*
1039 * Disable radio.
1040 */
1041 rt2x00lib_disable_radio(rt2x00dev);
1042
1043 /*
1044 * Uninitialize device.
1045 */
1046 rt2x00lib_uninitialize(rt2x00dev);
1047
1048 /*
1049 * Close debugfs entry.
1050 */
1051 rt2x00debug_deregister(rt2x00dev);
1052
1053 /*
1054 * Free rfkill
1055 */
1056 rt2x00rfkill_free(rt2x00dev);
1057
1058 /*
1059 * Free ieee80211_hw memory.
1060 */
1061 rt2x00lib_remove_hw(rt2x00dev);
1062
1063 /*
1064 * Free firmware image.
1065 */
1066 rt2x00lib_free_firmware(rt2x00dev);
1067
1068 /*
1069 * Free ring structures.
1070 */
1071 rt2x00lib_free_rings(rt2x00dev);
1072}
1073EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1074
1075/*
1076 * Device state handlers
1077 */
1078#ifdef CONFIG_PM
1079int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1080{
1081 int retval;
1082
1083 NOTICE(rt2x00dev, "Going to sleep.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +02001084 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1085
1086 /*
1087 * Only continue if mac80211 has open interfaces.
1088 */
1089 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1090 goto exit;
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001091 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001092
1093 /*
1094 * Disable radio and unitialize all items
1095 * that must be recreated on resume.
1096 */
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001097 rt2x00mac_stop(rt2x00dev->hw);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001098 rt2x00lib_uninitialize(rt2x00dev);
1099 rt2x00debug_deregister(rt2x00dev);
1100
Ivo van Doorn066cb632007-09-25 20:55:39 +02001101exit:
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001102 /*
1103 * Set device mode to sleep for power management.
1104 */
1105 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1106 if (retval)
1107 return retval;
1108
1109 return 0;
1110}
1111EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1112
1113int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1114{
1115 struct interface *intf = &rt2x00dev->interface;
1116 int retval;
1117
1118 NOTICE(rt2x00dev, "Waking up.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +02001119 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001120
1121 /*
1122 * Open the debugfs entry.
1123 */
1124 rt2x00debug_register(rt2x00dev);
1125
1126 /*
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001127 * Only continue if mac80211 had open interfaces.
Ivo van Doorn066cb632007-09-25 20:55:39 +02001128 */
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001129 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
Ivo van Doorn066cb632007-09-25 20:55:39 +02001130 return 0;
1131
1132 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001133 * Reinitialize device and all active interfaces.
1134 */
1135 retval = rt2x00mac_start(rt2x00dev->hw);
1136 if (retval)
1137 goto exit;
1138
1139 /*
1140 * Reconfigure device.
1141 */
Ivo van Doorn066cb632007-09-25 20:55:39 +02001142 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1143 if (!rt2x00dev->hw->conf.radio_enabled)
1144 rt2x00lib_disable_radio(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001145
1146 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1147 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1148 rt2x00lib_config_type(rt2x00dev, intf->type);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001149
1150 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +02001151 * It is possible that during that mac80211 has attempted
1152 * to send frames while we were suspending or resuming.
1153 * In that case we have disabled the TX queue and should
1154 * now enable it again
1155 */
1156 ieee80211_start_queues(rt2x00dev->hw);
1157
1158 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001159 * When in Master or Ad-hoc mode,
1160 * restart Beacon transmitting by faking a beacondone event.
1161 */
1162 if (intf->type == IEEE80211_IF_TYPE_AP ||
1163 intf->type == IEEE80211_IF_TYPE_IBSS)
1164 rt2x00lib_beacondone(rt2x00dev);
1165
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001166 return 0;
1167
1168exit:
1169 rt2x00lib_disable_radio(rt2x00dev);
1170 rt2x00lib_uninitialize(rt2x00dev);
1171 rt2x00debug_deregister(rt2x00dev);
1172
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001173 return retval;
1174}
1175EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1176#endif /* CONFIG_PM */
1177
1178/*
1179 * rt2x00lib module information.
1180 */
1181MODULE_AUTHOR(DRV_PROJECT);
1182MODULE_VERSION(DRV_VERSION);
1183MODULE_DESCRIPTION("rt2x00 library");
1184MODULE_LICENSE("GPL");