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