cfg80211: Extend channel to frequency mapping for 802.11j
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
1 /*
2 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3 Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
4 <http://rt2x00.serialmonkey.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the
18 Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 /*
23 Module: rt2x00lib
24 Abstract: rt2x00 generic device routines.
25 */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30
31 #include "rt2x00.h"
32 #include "rt2x00lib.h"
33
34 /*
35 * Radio control handlers.
36 */
37 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
38 {
39 int status;
40
41 /*
42 * Don't enable the radio twice.
43 * And check if the hardware button has been disabled.
44 */
45 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
46 return 0;
47
48 /*
49 * Initialize all data queues.
50 */
51 rt2x00queue_init_queues(rt2x00dev);
52
53 /*
54 * Enable radio.
55 */
56 status =
57 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
58 if (status)
59 return status;
60
61 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
62
63 rt2x00leds_led_radio(rt2x00dev, true);
64 rt2x00led_led_activity(rt2x00dev, true);
65
66 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
67
68 /*
69 * Enable queues.
70 */
71 rt2x00queue_start_queues(rt2x00dev);
72 rt2x00link_start_tuner(rt2x00dev);
73
74 /*
75 * Start watchdog monitoring.
76 */
77 rt2x00link_start_watchdog(rt2x00dev);
78
79 return 0;
80 }
81
82 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
83 {
84 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
85 return;
86
87 /*
88 * Stop watchdog monitoring.
89 */
90 rt2x00link_stop_watchdog(rt2x00dev);
91
92 /*
93 * Stop all queues
94 */
95 rt2x00link_stop_tuner(rt2x00dev);
96 rt2x00queue_stop_queues(rt2x00dev);
97 rt2x00queue_flush_queues(rt2x00dev, true);
98
99 /*
100 * Disable radio.
101 */
102 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
103 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
104 rt2x00led_led_activity(rt2x00dev, false);
105 rt2x00leds_led_radio(rt2x00dev, false);
106 }
107
108 static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
109 struct ieee80211_vif *vif)
110 {
111 struct rt2x00_dev *rt2x00dev = data;
112 struct rt2x00_intf *intf = vif_to_intf(vif);
113
114 /*
115 * It is possible the radio was disabled while the work had been
116 * scheduled. If that happens we should return here immediately,
117 * note that in the spinlock protected area above the delayed_flags
118 * have been cleared correctly.
119 */
120 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
121 return;
122
123 if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags))
124 rt2x00queue_update_beacon(rt2x00dev, vif, true);
125 }
126
127 static void rt2x00lib_intf_scheduled(struct work_struct *work)
128 {
129 struct rt2x00_dev *rt2x00dev =
130 container_of(work, struct rt2x00_dev, intf_work);
131
132 /*
133 * Iterate over each interface and perform the
134 * requested configurations.
135 */
136 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
137 rt2x00lib_intf_scheduled_iter,
138 rt2x00dev);
139 }
140
141 /*
142 * Interrupt context handlers.
143 */
144 static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
145 struct ieee80211_vif *vif)
146 {
147 struct rt2x00_dev *rt2x00dev = data;
148 struct sk_buff *skb;
149
150 /*
151 * Only AP mode interfaces do broad- and multicast buffering
152 */
153 if (vif->type != NL80211_IFTYPE_AP)
154 return;
155
156 /*
157 * Send out buffered broad- and multicast frames
158 */
159 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
160 while (skb) {
161 rt2x00mac_tx(rt2x00dev->hw, skb);
162 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
163 }
164 }
165
166 static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
167 struct ieee80211_vif *vif)
168 {
169 struct rt2x00_dev *rt2x00dev = data;
170
171 if (vif->type != NL80211_IFTYPE_AP &&
172 vif->type != NL80211_IFTYPE_ADHOC &&
173 vif->type != NL80211_IFTYPE_MESH_POINT &&
174 vif->type != NL80211_IFTYPE_WDS)
175 return;
176
177 rt2x00queue_update_beacon(rt2x00dev, vif, true);
178 }
179
180 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
181 {
182 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
183 return;
184
185 /* send buffered bc/mc frames out for every bssid */
186 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
187 rt2x00lib_bc_buffer_iter,
188 rt2x00dev);
189 /*
190 * Devices with pre tbtt interrupt don't need to update the beacon
191 * here as they will fetch the next beacon directly prior to
192 * transmission.
193 */
194 if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
195 return;
196
197 /* fetch next beacon */
198 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
199 rt2x00lib_beaconupdate_iter,
200 rt2x00dev);
201 }
202 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
203
204 void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
205 {
206 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
207 return;
208
209 /* fetch next beacon */
210 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
211 rt2x00lib_beaconupdate_iter,
212 rt2x00dev);
213 }
214 EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
215
216 void rt2x00lib_dmastart(struct queue_entry *entry)
217 {
218 set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
219 rt2x00queue_index_inc(entry->queue, Q_INDEX);
220 }
221 EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
222
223 void rt2x00lib_dmadone(struct queue_entry *entry)
224 {
225 set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
226 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
227 rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
228 }
229 EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
230
231 void rt2x00lib_txdone(struct queue_entry *entry,
232 struct txdone_entry_desc *txdesc)
233 {
234 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
235 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
236 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
237 unsigned int header_length, i;
238 u8 rate_idx, rate_flags, retry_rates;
239 u8 skbdesc_flags = skbdesc->flags;
240 bool success;
241
242 /*
243 * Unmap the skb.
244 */
245 rt2x00queue_unmap_skb(entry);
246
247 /*
248 * Remove the extra tx headroom from the skb.
249 */
250 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
251
252 /*
253 * Signal that the TX descriptor is no longer in the skb.
254 */
255 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
256
257 /*
258 * Determine the length of 802.11 header.
259 */
260 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
261
262 /*
263 * Remove L2 padding which was added during
264 */
265 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
266 rt2x00queue_remove_l2pad(entry->skb, header_length);
267
268 /*
269 * If the IV/EIV data was stripped from the frame before it was
270 * passed to the hardware, we should now reinsert it again because
271 * mac80211 will expect the same data to be present it the
272 * frame as it was passed to us.
273 */
274 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
275 rt2x00crypto_tx_insert_iv(entry->skb, header_length);
276
277 /*
278 * Send frame to debugfs immediately, after this call is completed
279 * we are going to overwrite the skb->cb array.
280 */
281 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
282
283 /*
284 * Determine if the frame has been successfully transmitted.
285 */
286 success =
287 test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
288 test_bit(TXDONE_UNKNOWN, &txdesc->flags);
289
290 /*
291 * Update TX statistics.
292 */
293 rt2x00dev->link.qual.tx_success += success;
294 rt2x00dev->link.qual.tx_failed += !success;
295
296 rate_idx = skbdesc->tx_rate_idx;
297 rate_flags = skbdesc->tx_rate_flags;
298 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
299 (txdesc->retry + 1) : 1;
300
301 /*
302 * Initialize TX status
303 */
304 memset(&tx_info->status, 0, sizeof(tx_info->status));
305 tx_info->status.ack_signal = 0;
306
307 /*
308 * Frame was send with retries, hardware tried
309 * different rates to send out the frame, at each
310 * retry it lowered the rate 1 step except when the
311 * lowest rate was used.
312 */
313 for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
314 tx_info->status.rates[i].idx = rate_idx - i;
315 tx_info->status.rates[i].flags = rate_flags;
316
317 if (rate_idx - i == 0) {
318 /*
319 * The lowest rate (index 0) was used until the
320 * number of max retries was reached.
321 */
322 tx_info->status.rates[i].count = retry_rates - i;
323 i++;
324 break;
325 }
326 tx_info->status.rates[i].count = 1;
327 }
328 if (i < (IEEE80211_TX_MAX_RATES - 1))
329 tx_info->status.rates[i].idx = -1; /* terminate */
330
331 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
332 if (success)
333 tx_info->flags |= IEEE80211_TX_STAT_ACK;
334 else
335 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
336 }
337
338 /*
339 * Every single frame has it's own tx status, hence report
340 * every frame as ampdu of size 1.
341 *
342 * TODO: if we can find out how many frames were aggregated
343 * by the hw we could provide the real ampdu_len to mac80211
344 * which would allow the rc algorithm to better decide on
345 * which rates are suitable.
346 */
347 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
348 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
349 tx_info->status.ampdu_len = 1;
350 tx_info->status.ampdu_ack_len = success ? 1 : 0;
351 }
352
353 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
354 if (success)
355 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
356 else
357 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
358 }
359
360 /*
361 * Only send the status report to mac80211 when it's a frame
362 * that originated in mac80211. If this was a extra frame coming
363 * through a mac80211 library call (RTS/CTS) then we should not
364 * send the status report back.
365 */
366 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
367 if (test_bit(DRIVER_REQUIRE_TASKLET_CONTEXT, &rt2x00dev->flags))
368 ieee80211_tx_status(rt2x00dev->hw, entry->skb);
369 else
370 ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
371 } else
372 dev_kfree_skb_any(entry->skb);
373
374 /*
375 * Make this entry available for reuse.
376 */
377 entry->skb = NULL;
378 entry->flags = 0;
379
380 rt2x00dev->ops->lib->clear_entry(entry);
381
382 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
383
384 /*
385 * If the data queue was below the threshold before the txdone
386 * handler we must make sure the packet queue in the mac80211 stack
387 * is reenabled when the txdone handler has finished.
388 */
389 if (!rt2x00queue_threshold(entry->queue))
390 rt2x00queue_unpause_queue(entry->queue);
391 }
392 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
393
394 void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
395 {
396 struct txdone_entry_desc txdesc;
397
398 txdesc.flags = 0;
399 __set_bit(status, &txdesc.flags);
400 txdesc.retry = 0;
401
402 rt2x00lib_txdone(entry, &txdesc);
403 }
404 EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
405
406 static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
407 struct rxdone_entry_desc *rxdesc)
408 {
409 struct ieee80211_supported_band *sband;
410 const struct rt2x00_rate *rate;
411 unsigned int i;
412 int signal = rxdesc->signal;
413 int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
414
415 switch (rxdesc->rate_mode) {
416 case RATE_MODE_CCK:
417 case RATE_MODE_OFDM:
418 /*
419 * For non-HT rates the MCS value needs to contain the
420 * actually used rate modulation (CCK or OFDM).
421 */
422 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
423 signal = RATE_MCS(rxdesc->rate_mode, signal);
424
425 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
426 for (i = 0; i < sband->n_bitrates; i++) {
427 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
428 if (((type == RXDONE_SIGNAL_PLCP) &&
429 (rate->plcp == signal)) ||
430 ((type == RXDONE_SIGNAL_BITRATE) &&
431 (rate->bitrate == signal)) ||
432 ((type == RXDONE_SIGNAL_MCS) &&
433 (rate->mcs == signal))) {
434 return i;
435 }
436 }
437 break;
438 case RATE_MODE_HT_MIX:
439 case RATE_MODE_HT_GREENFIELD:
440 if (signal >= 0 && signal <= 76)
441 return signal;
442 break;
443 default:
444 break;
445 }
446
447 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
448 "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
449 rxdesc->rate_mode, signal, type);
450 return 0;
451 }
452
453 void rt2x00lib_rxdone(struct queue_entry *entry)
454 {
455 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
456 struct rxdone_entry_desc rxdesc;
457 struct sk_buff *skb;
458 struct ieee80211_rx_status *rx_status;
459 unsigned int header_length;
460 int rate_idx;
461
462 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
463 !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
464 goto submit_entry;
465
466 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
467 goto submit_entry;
468
469 /*
470 * Allocate a new sk_buffer. If no new buffer available, drop the
471 * received frame and reuse the existing buffer.
472 */
473 skb = rt2x00queue_alloc_rxskb(entry);
474 if (!skb)
475 goto submit_entry;
476
477 /*
478 * Unmap the skb.
479 */
480 rt2x00queue_unmap_skb(entry);
481
482 /*
483 * Extract the RXD details.
484 */
485 memset(&rxdesc, 0, sizeof(rxdesc));
486 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
487
488 /*
489 * The data behind the ieee80211 header must be
490 * aligned on a 4 byte boundary.
491 */
492 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
493
494 /*
495 * Hardware might have stripped the IV/EIV/ICV data,
496 * in that case it is possible that the data was
497 * provided separately (through hardware descriptor)
498 * in which case we should reinsert the data into the frame.
499 */
500 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
501 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
502 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
503 &rxdesc);
504 else if (header_length &&
505 (rxdesc.size > header_length) &&
506 (rxdesc.dev_flags & RXDONE_L2PAD))
507 rt2x00queue_remove_l2pad(entry->skb, header_length);
508 else
509 rt2x00queue_align_payload(entry->skb, header_length);
510
511 /* Trim buffer to correct size */
512 skb_trim(entry->skb, rxdesc.size);
513
514 /*
515 * Translate the signal to the correct bitrate index.
516 */
517 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
518 if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
519 rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
520 rxdesc.flags |= RX_FLAG_HT;
521
522 /*
523 * Update extra components
524 */
525 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
526 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
527 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
528
529 /*
530 * Initialize RX status information, and send frame
531 * to mac80211.
532 */
533 rx_status = IEEE80211_SKB_RXCB(entry->skb);
534 rx_status->mactime = rxdesc.timestamp;
535 rx_status->band = rt2x00dev->curr_band;
536 rx_status->freq = rt2x00dev->curr_freq;
537 rx_status->rate_idx = rate_idx;
538 rx_status->signal = rxdesc.rssi;
539 rx_status->flag = rxdesc.flags;
540 rx_status->antenna = rt2x00dev->link.ant.active.rx;
541
542 ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
543
544 /*
545 * Replace the skb with the freshly allocated one.
546 */
547 entry->skb = skb;
548
549 submit_entry:
550 entry->flags = 0;
551 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
552 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
553 test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
554 rt2x00dev->ops->lib->clear_entry(entry);
555 }
556 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
557
558 /*
559 * Driver initialization handlers.
560 */
561 const struct rt2x00_rate rt2x00_supported_rates[12] = {
562 {
563 .flags = DEV_RATE_CCK,
564 .bitrate = 10,
565 .ratemask = BIT(0),
566 .plcp = 0x00,
567 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
568 },
569 {
570 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
571 .bitrate = 20,
572 .ratemask = BIT(1),
573 .plcp = 0x01,
574 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
575 },
576 {
577 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
578 .bitrate = 55,
579 .ratemask = BIT(2),
580 .plcp = 0x02,
581 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
582 },
583 {
584 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
585 .bitrate = 110,
586 .ratemask = BIT(3),
587 .plcp = 0x03,
588 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
589 },
590 {
591 .flags = DEV_RATE_OFDM,
592 .bitrate = 60,
593 .ratemask = BIT(4),
594 .plcp = 0x0b,
595 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
596 },
597 {
598 .flags = DEV_RATE_OFDM,
599 .bitrate = 90,
600 .ratemask = BIT(5),
601 .plcp = 0x0f,
602 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
603 },
604 {
605 .flags = DEV_RATE_OFDM,
606 .bitrate = 120,
607 .ratemask = BIT(6),
608 .plcp = 0x0a,
609 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
610 },
611 {
612 .flags = DEV_RATE_OFDM,
613 .bitrate = 180,
614 .ratemask = BIT(7),
615 .plcp = 0x0e,
616 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
617 },
618 {
619 .flags = DEV_RATE_OFDM,
620 .bitrate = 240,
621 .ratemask = BIT(8),
622 .plcp = 0x09,
623 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
624 },
625 {
626 .flags = DEV_RATE_OFDM,
627 .bitrate = 360,
628 .ratemask = BIT(9),
629 .plcp = 0x0d,
630 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
631 },
632 {
633 .flags = DEV_RATE_OFDM,
634 .bitrate = 480,
635 .ratemask = BIT(10),
636 .plcp = 0x08,
637 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
638 },
639 {
640 .flags = DEV_RATE_OFDM,
641 .bitrate = 540,
642 .ratemask = BIT(11),
643 .plcp = 0x0c,
644 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
645 },
646 };
647
648 static void rt2x00lib_channel(struct ieee80211_channel *entry,
649 const int channel, const int tx_power,
650 const int value)
651 {
652 /* XXX: this assumption about the band is wrong for 802.11j */
653 entry->band = channel <= 14 ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
654 entry->center_freq = ieee80211_channel_to_frequency(channel,
655 entry->band);
656 entry->hw_value = value;
657 entry->max_power = tx_power;
658 entry->max_antenna_gain = 0xff;
659 }
660
661 static void rt2x00lib_rate(struct ieee80211_rate *entry,
662 const u16 index, const struct rt2x00_rate *rate)
663 {
664 entry->flags = 0;
665 entry->bitrate = rate->bitrate;
666 entry->hw_value = index;
667 entry->hw_value_short = index;
668
669 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
670 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
671 }
672
673 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
674 struct hw_mode_spec *spec)
675 {
676 struct ieee80211_hw *hw = rt2x00dev->hw;
677 struct ieee80211_channel *channels;
678 struct ieee80211_rate *rates;
679 unsigned int num_rates;
680 unsigned int i;
681
682 num_rates = 0;
683 if (spec->supported_rates & SUPPORT_RATE_CCK)
684 num_rates += 4;
685 if (spec->supported_rates & SUPPORT_RATE_OFDM)
686 num_rates += 8;
687
688 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
689 if (!channels)
690 return -ENOMEM;
691
692 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
693 if (!rates)
694 goto exit_free_channels;
695
696 /*
697 * Initialize Rate list.
698 */
699 for (i = 0; i < num_rates; i++)
700 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
701
702 /*
703 * Initialize Channel list.
704 */
705 for (i = 0; i < spec->num_channels; i++) {
706 rt2x00lib_channel(&channels[i],
707 spec->channels[i].channel,
708 spec->channels_info[i].max_power, i);
709 }
710
711 /*
712 * Intitialize 802.11b, 802.11g
713 * Rates: CCK, OFDM.
714 * Channels: 2.4 GHz
715 */
716 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
717 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
718 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
719 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
720 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
721 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
722 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
723 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
724 &spec->ht, sizeof(spec->ht));
725 }
726
727 /*
728 * Intitialize 802.11a
729 * Rates: OFDM.
730 * Channels: OFDM, UNII, HiperLAN2.
731 */
732 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
733 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
734 spec->num_channels - 14;
735 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
736 num_rates - 4;
737 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
738 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
739 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
740 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
741 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
742 &spec->ht, sizeof(spec->ht));
743 }
744
745 return 0;
746
747 exit_free_channels:
748 kfree(channels);
749 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
750 return -ENOMEM;
751 }
752
753 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
754 {
755 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
756 ieee80211_unregister_hw(rt2x00dev->hw);
757
758 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
759 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
760 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
761 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
762 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
763 }
764
765 kfree(rt2x00dev->spec.channels_info);
766 }
767
768 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
769 {
770 struct hw_mode_spec *spec = &rt2x00dev->spec;
771 int status;
772
773 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
774 return 0;
775
776 /*
777 * Initialize HW modes.
778 */
779 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
780 if (status)
781 return status;
782
783 /*
784 * Initialize HW fields.
785 */
786 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
787
788 /*
789 * Initialize extra TX headroom required.
790 */
791 rt2x00dev->hw->extra_tx_headroom =
792 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
793 rt2x00dev->ops->extra_tx_headroom);
794
795 /*
796 * Take TX headroom required for alignment into account.
797 */
798 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
799 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
800 else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
801 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
802
803 /*
804 * Allocate tx status FIFO for driver use.
805 */
806 if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags)) {
807 /*
808 * Allocate txstatus fifo and tasklet, we use a size of 512
809 * for the kfifo which is big enough to store 512/4=128 tx
810 * status reports. In the worst case (tx status for all tx
811 * queues gets reported before we've got a chance to handle
812 * them) 24*4=384 tx status reports need to be cached.
813 */
814 status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
815 GFP_KERNEL);
816 if (status)
817 return status;
818
819 /* tasklet for processing the tx status reports. */
820 if (rt2x00dev->ops->lib->txstatus_tasklet)
821 tasklet_init(&rt2x00dev->txstatus_tasklet,
822 rt2x00dev->ops->lib->txstatus_tasklet,
823 (unsigned long)rt2x00dev);
824
825 }
826
827 /*
828 * Register HW.
829 */
830 status = ieee80211_register_hw(rt2x00dev->hw);
831 if (status)
832 return status;
833
834 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
835
836 return 0;
837 }
838
839 /*
840 * Initialization/uninitialization handlers.
841 */
842 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
843 {
844 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
845 return;
846
847 /*
848 * Unregister extra components.
849 */
850 rt2x00rfkill_unregister(rt2x00dev);
851
852 /*
853 * Allow the HW to uninitialize.
854 */
855 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
856
857 /*
858 * Free allocated queue entries.
859 */
860 rt2x00queue_uninitialize(rt2x00dev);
861 }
862
863 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
864 {
865 int status;
866
867 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
868 return 0;
869
870 /*
871 * Allocate all queue entries.
872 */
873 status = rt2x00queue_initialize(rt2x00dev);
874 if (status)
875 return status;
876
877 /*
878 * Initialize the device.
879 */
880 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
881 if (status) {
882 rt2x00queue_uninitialize(rt2x00dev);
883 return status;
884 }
885
886 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
887
888 /*
889 * Register the extra components.
890 */
891 rt2x00rfkill_register(rt2x00dev);
892
893 return 0;
894 }
895
896 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
897 {
898 int retval;
899
900 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
901 return 0;
902
903 /*
904 * If this is the first interface which is added,
905 * we should load the firmware now.
906 */
907 retval = rt2x00lib_load_firmware(rt2x00dev);
908 if (retval)
909 return retval;
910
911 /*
912 * Initialize the device.
913 */
914 retval = rt2x00lib_initialize(rt2x00dev);
915 if (retval)
916 return retval;
917
918 rt2x00dev->intf_ap_count = 0;
919 rt2x00dev->intf_sta_count = 0;
920 rt2x00dev->intf_associated = 0;
921
922 /* Enable the radio */
923 retval = rt2x00lib_enable_radio(rt2x00dev);
924 if (retval)
925 return retval;
926
927 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
928
929 return 0;
930 }
931
932 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
933 {
934 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
935 return;
936
937 /*
938 * Perhaps we can add something smarter here,
939 * but for now just disabling the radio should do.
940 */
941 rt2x00lib_disable_radio(rt2x00dev);
942
943 rt2x00dev->intf_ap_count = 0;
944 rt2x00dev->intf_sta_count = 0;
945 rt2x00dev->intf_associated = 0;
946 }
947
948 /*
949 * driver allocation handlers.
950 */
951 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
952 {
953 int retval = -ENOMEM;
954
955 mutex_init(&rt2x00dev->csr_mutex);
956
957 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
958
959 /*
960 * Make room for rt2x00_intf inside the per-interface
961 * structure ieee80211_vif.
962 */
963 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
964
965 /*
966 * Determine which operating modes are supported, all modes
967 * which require beaconing, depend on the availability of
968 * beacon entries.
969 */
970 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
971 if (rt2x00dev->ops->bcn->entry_num > 0)
972 rt2x00dev->hw->wiphy->interface_modes |=
973 BIT(NL80211_IFTYPE_ADHOC) |
974 BIT(NL80211_IFTYPE_AP) |
975 BIT(NL80211_IFTYPE_MESH_POINT) |
976 BIT(NL80211_IFTYPE_WDS);
977
978 /*
979 * Initialize configuration work.
980 */
981 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
982
983 /*
984 * Let the driver probe the device to detect the capabilities.
985 */
986 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
987 if (retval) {
988 ERROR(rt2x00dev, "Failed to allocate device.\n");
989 goto exit;
990 }
991
992 /*
993 * Allocate queue array.
994 */
995 retval = rt2x00queue_allocate(rt2x00dev);
996 if (retval)
997 goto exit;
998
999 /*
1000 * Initialize ieee80211 structure.
1001 */
1002 retval = rt2x00lib_probe_hw(rt2x00dev);
1003 if (retval) {
1004 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1005 goto exit;
1006 }
1007
1008 /*
1009 * Register extra components.
1010 */
1011 rt2x00link_register(rt2x00dev);
1012 rt2x00leds_register(rt2x00dev);
1013 rt2x00debug_register(rt2x00dev);
1014
1015 return 0;
1016
1017 exit:
1018 rt2x00lib_remove_dev(rt2x00dev);
1019
1020 return retval;
1021 }
1022 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1023
1024 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1025 {
1026 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1027
1028 /*
1029 * Disable radio.
1030 */
1031 rt2x00lib_disable_radio(rt2x00dev);
1032
1033 /*
1034 * Stop all work.
1035 */
1036 cancel_work_sync(&rt2x00dev->intf_work);
1037 cancel_work_sync(&rt2x00dev->rxdone_work);
1038 cancel_work_sync(&rt2x00dev->txdone_work);
1039
1040 /*
1041 * Free the tx status fifo.
1042 */
1043 kfifo_free(&rt2x00dev->txstatus_fifo);
1044
1045 /*
1046 * Kill the tx status tasklet.
1047 */
1048 tasklet_kill(&rt2x00dev->txstatus_tasklet);
1049
1050 /*
1051 * Uninitialize device.
1052 */
1053 rt2x00lib_uninitialize(rt2x00dev);
1054
1055 /*
1056 * Free extra components
1057 */
1058 rt2x00debug_deregister(rt2x00dev);
1059 rt2x00leds_unregister(rt2x00dev);
1060
1061 /*
1062 * Free ieee80211_hw memory.
1063 */
1064 rt2x00lib_remove_hw(rt2x00dev);
1065
1066 /*
1067 * Free firmware image.
1068 */
1069 rt2x00lib_free_firmware(rt2x00dev);
1070
1071 /*
1072 * Free queue structures.
1073 */
1074 rt2x00queue_free(rt2x00dev);
1075 }
1076 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1077
1078 /*
1079 * Device state handlers
1080 */
1081 #ifdef CONFIG_PM
1082 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1083 {
1084 NOTICE(rt2x00dev, "Going to sleep.\n");
1085
1086 /*
1087 * Prevent mac80211 from accessing driver while suspended.
1088 */
1089 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1090 return 0;
1091
1092 /*
1093 * Cleanup as much as possible.
1094 */
1095 rt2x00lib_uninitialize(rt2x00dev);
1096
1097 /*
1098 * Suspend/disable extra components.
1099 */
1100 rt2x00leds_suspend(rt2x00dev);
1101 rt2x00debug_deregister(rt2x00dev);
1102
1103 /*
1104 * Set device mode to sleep for power management,
1105 * on some hardware this call seems to consistently fail.
1106 * From the specifications it is hard to tell why it fails,
1107 * and if this is a "bad thing".
1108 * Overall it is safe to just ignore the failure and
1109 * continue suspending. The only downside is that the
1110 * device will not be in optimal power save mode, but with
1111 * the radio and the other components already disabled the
1112 * device is as good as disabled.
1113 */
1114 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
1115 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1116 "continue suspending.\n");
1117
1118 return 0;
1119 }
1120 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1121
1122 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1123 {
1124 NOTICE(rt2x00dev, "Waking up.\n");
1125
1126 /*
1127 * Restore/enable extra components.
1128 */
1129 rt2x00debug_register(rt2x00dev);
1130 rt2x00leds_resume(rt2x00dev);
1131
1132 /*
1133 * We are ready again to receive requests from mac80211.
1134 */
1135 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1136
1137 return 0;
1138 }
1139 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1140 #endif /* CONFIG_PM */
1141
1142 /*
1143 * rt2x00lib module information.
1144 */
1145 MODULE_AUTHOR(DRV_PROJECT);
1146 MODULE_VERSION(DRV_VERSION);
1147 MODULE_DESCRIPTION("rt2x00 library");
1148 MODULE_LICENSE("GPL");
This page took 0.053711 seconds and 5 git commands to generate.