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