more rt2x00 fixes (thanks to sn9):
[openwrt.git] / package / rt2x00 / src / rt2x00dev.c
1 /*
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 Supported chipsets: RT2460, RT2560, RT2570,
25 rt2561, rt2561s, rt2661, rt2571W & rt2671.
26 */
27
28 /*
29 * Set enviroment defines for rt2x00.h
30 */
31 #define DRV_NAME "rt2x00lib"
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/version.h>
36 #include <linux/init.h>
37 #include <linux/delay.h>
38 #include <linux/etherdevice.h>
39
40 #include "rt2x00.h"
41 #include "rt2x00lib.h"
42 #include "rt2x00dev.h"
43
44 /*
45 * Radio control handlers.
46 */
47 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
48 {
49 int status;
50
51 /*
52 * Don't enable the radio twice.
53 * or if the hardware button has been disabled.
54 */
55 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
56 (test_bit(DEVICE_SUPPORT_HW_BUTTON, &rt2x00dev->flags) &&
57 !test_bit(DEVICE_ENABLED_RADIO_HW, &rt2x00dev->flags)))
58 return 0;
59
60 status = rt2x00dev->ops->lib->set_device_state(
61 rt2x00dev, STATE_RADIO_ON);
62 if (status)
63 return status;
64
65 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
66
67 rt2x00lib_toggle_rx(rt2x00dev, 1);
68
69 ieee80211_start_queues(rt2x00dev->hw);
70
71 if (is_interface_present(&rt2x00dev->interface))
72 rt2x00_start_link_tune(rt2x00dev);
73
74 return 0;
75 }
76
77 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
78 {
79 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
80 return;
81
82 rt2x00_stop_link_tune(rt2x00dev);
83
84 ieee80211_stop_queues(rt2x00dev->hw);
85
86 rt2x00lib_toggle_rx(rt2x00dev, 0);
87
88 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
89 }
90
91 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, int enable)
92 {
93 /*
94 * When we are disabling the rx, we should also stop the link tuner.
95 */
96 if (!enable)
97 rt2x00_stop_link_tune(rt2x00dev);
98
99 rt2x00dev->ops->lib->set_device_state(rt2x00dev,
100 enable ? STATE_RADIO_RX_ON : STATE_RADIO_RX_OFF);
101
102 /*
103 * When we are enabling the rx, we should also start the link tuner.
104 */
105 if (enable && is_interface_present(&rt2x00dev->interface))
106 rt2x00_start_link_tune(rt2x00dev);
107 }
108
109 static void rt2x00lib_link_tuner(struct work_struct *work)
110 {
111 struct rt2x00_dev *rt2x00dev =
112 container_of(work, struct rt2x00_dev, link.work.work);
113
114 /*
115 * Update promisc mode (this function will first check
116 * if updating is really required).
117 */
118 rt2x00lib_config_promisc(rt2x00dev, rt2x00dev->interface.promisc);
119
120 /*
121 * Cancel all link tuning if the eeprom has indicated
122 * it is not required.
123 */
124 if (test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
125 return;
126
127 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
128
129 /*
130 * Increase tuner counter, and reschedule the next link tuner run.
131 */
132 rt2x00dev->link.count++;
133 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
134 LINK_TUNE_INTERVAL);
135 }
136
137 /*
138 * Config handlers
139 */
140 void rt2x00lib_config_type(struct rt2x00_dev *rt2x00dev, const int type)
141 {
142 if (!(is_interface_present(&rt2x00dev->interface) ^
143 test_bit(INTERFACE_ENABLED, &rt2x00dev->flags)) &&
144 !(is_monitor_present(&rt2x00dev->interface) ^
145 test_bit(INTERFACE_ENABLED_MONITOR, &rt2x00dev->flags)))
146 return;
147
148 rt2x00dev->ops->lib->config_type(rt2x00dev, type);
149
150 if (type != IEEE80211_IF_TYPE_MNTR) {
151 if (is_interface_present(&rt2x00dev->interface))
152 __set_bit(INTERFACE_ENABLED, &rt2x00dev->flags);
153 else
154 __clear_bit(INTERFACE_ENABLED, &rt2x00dev->flags);
155 } else {
156 if (is_monitor_present(&rt2x00dev->interface))
157 __set_bit(INTERFACE_ENABLED_MONITOR,
158 &rt2x00dev->flags);
159 else
160 __clear_bit(INTERFACE_ENABLED_MONITOR,
161 &rt2x00dev->flags);
162 }
163 }
164
165 void rt2x00lib_config_phymode(struct rt2x00_dev *rt2x00dev, const int phymode)
166 {
167 if (rt2x00dev->rx_status.phymode == phymode)
168 return;
169
170 rt2x00dev->ops->lib->config_phymode(rt2x00dev, phymode);
171
172 rt2x00dev->rx_status.phymode = phymode;
173 }
174
175 void rt2x00lib_config_channel(struct rt2x00_dev *rt2x00dev, const int value,
176 const int channel, const int freq, const int txpower)
177 {
178 if (channel == rt2x00dev->rx_status.channel)
179 return;
180
181 rt2x00dev->ops->lib->config_channel(rt2x00dev, value, channel, txpower);
182
183 INFO(rt2x00dev, "Switching channel. "
184 "RF1: 0x%08x, RF2: 0x%08x, RF3: 0x%08x, RF3: 0x%08x.\n",
185 rt2x00dev->rf1, rt2x00dev->rf2,
186 rt2x00dev->rf3, rt2x00dev->rf4);
187
188 rt2x00dev->rx_status.freq = freq;
189 rt2x00dev->rx_status.channel = channel;
190 }
191
192 void rt2x00lib_config_promisc(struct rt2x00_dev *rt2x00dev, const int promisc)
193 {
194 /*
195 * Monitor mode implies promisc mode enabled.
196 * In all other instances, check if we need to toggle promisc mode.
197 */
198 if (is_monitor_present(&rt2x00dev->interface) &&
199 !test_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags)) {
200 rt2x00dev->ops->lib->config_promisc(rt2x00dev, 1);
201 __set_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags);
202 }
203
204 if (test_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags) != promisc) {
205 rt2x00dev->ops->lib->config_promisc(rt2x00dev, promisc);
206 __change_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags);
207 }
208 }
209
210 void rt2x00lib_config_txpower(struct rt2x00_dev *rt2x00dev, const int txpower)
211 {
212 if (txpower == rt2x00dev->tx_power)
213 return;
214
215 rt2x00dev->ops->lib->config_txpower(rt2x00dev, txpower);
216
217 rt2x00dev->tx_power = txpower;
218 }
219
220 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
221 const int antenna_tx, const int antenna_rx)
222 {
223 if (rt2x00dev->rx_status.antenna == antenna_rx)
224 return;
225
226 rt2x00dev->ops->lib->config_antenna(rt2x00dev, antenna_tx, antenna_rx);
227
228 rt2x00dev->rx_status.antenna = antenna_rx;
229 }
230
231 /*
232 * Driver initialization handlers.
233 */
234 static void rt2x00lib_channel(struct ieee80211_channel *entry,
235 const int channel, const int tx_power, const int value)
236 {
237 entry->chan = channel;
238 if (channel <= 14)
239 entry->freq = 2407 + (5 * channel);
240 else
241 entry->freq = 5000 + (5 * channel);
242 entry->val = value;
243 entry->flag =
244 IEEE80211_CHAN_W_IBSS |
245 IEEE80211_CHAN_W_ACTIVE_SCAN |
246 IEEE80211_CHAN_W_SCAN;
247 entry->power_level = tx_power;
248 entry->antenna_max = 0xff;
249 }
250
251 static void rt2x00lib_rate(struct ieee80211_rate *entry,
252 const int rate,const int mask, const int plcp, const int flags)
253 {
254 entry->rate = rate;
255 entry->val =
256 DEVICE_SET_RATE_FIELD(rate, RATE) |
257 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
258 DEVICE_SET_RATE_FIELD(plcp, PLCP);
259 entry->flags = flags;
260 entry->val2 = entry->val;
261 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
262 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
263 entry->min_rssi_ack = 0;
264 entry->min_rssi_ack_delta = 0;
265 }
266
267 static int rt2x00lib_init_hw_modes(struct rt2x00_dev *rt2x00dev,
268 struct hw_mode_spec *spec)
269 {
270 struct ieee80211_hw *hw = rt2x00dev->hw;
271 struct ieee80211_hw_mode *hwmodes;
272 struct ieee80211_channel *channels;
273 struct ieee80211_rate *rates;
274 unsigned int i;
275 unsigned char tx_power;
276
277 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
278 if (!hwmodes)
279 goto exit;
280
281 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
282 if (!channels)
283 goto exit_free_modes;
284
285 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
286 if (!rates)
287 goto exit_free_channels;
288
289 /*
290 * Initialize Rate list.
291 */
292 rt2x00lib_rate(&rates[0], 10, 0x001, 0x00, IEEE80211_RATE_CCK);
293 rt2x00lib_rate(&rates[1], 20, 0x003, 0x01, IEEE80211_RATE_CCK_2);
294 rt2x00lib_rate(&rates[2], 55, 0x007, 0x02, IEEE80211_RATE_CCK_2);
295 rt2x00lib_rate(&rates[3], 110, 0x00f, 0x03, IEEE80211_RATE_CCK_2);
296
297 if (spec->num_rates > 4) {
298 rt2x00lib_rate(&rates[4], 60, 0x01f, 0x0b, IEEE80211_RATE_OFDM);
299 rt2x00lib_rate(&rates[5], 90, 0x03f, 0x0f, IEEE80211_RATE_OFDM);
300 rt2x00lib_rate(&rates[6], 120, 0x07f, 0x0a, IEEE80211_RATE_OFDM);
301 rt2x00lib_rate(&rates[7], 180, 0x0ff, 0x0e, IEEE80211_RATE_OFDM);
302 rt2x00lib_rate(&rates[8], 240, 0x1ff, 0x09, IEEE80211_RATE_OFDM);
303 rt2x00lib_rate(&rates[9], 360, 0x3ff, 0x0d, IEEE80211_RATE_OFDM);
304 rt2x00lib_rate(&rates[10], 480, 0x7ff, 0x08, IEEE80211_RATE_OFDM);
305 rt2x00lib_rate(&rates[11], 540, 0xfff, 0x0c, IEEE80211_RATE_OFDM);
306 }
307
308 /*
309 * Initialize Channel list.
310 */
311 for (i = 0; i < 14; i++)
312 rt2x00lib_channel(&channels[i], i + 1,
313 spec->tx_power_bg[i], spec->chan_val_bg[i]);
314
315 if (spec->num_channels > 14) {
316 for (i = 14; i < spec->num_channels; i++) {
317 if (i < 22)
318 channels[i].chan = 36;
319 else if (i < 33)
320 channels[i].chan = 100;
321 else
322 channels[i].chan = 149;
323 channels[i].chan += ((i - 14) * 4);
324
325 if (spec->tx_power_a)
326 tx_power = spec->tx_power_a[i];
327 else
328 tx_power = spec->tx_power_default;
329
330 rt2x00lib_channel(&channels[i],
331 channels[i].chan, tx_power,
332 spec->chan_val_a[i]);
333 }
334 }
335
336 /*
337 * Intitialize 802.11b
338 * Rates: CCK.
339 * Channels: OFDM.
340 */
341 if (spec->num_modes > HWMODE_B) {
342 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
343 hwmodes[HWMODE_B].num_channels = 14;
344 hwmodes[HWMODE_B].num_rates = 4;
345 hwmodes[HWMODE_B].channels = channels;
346 hwmodes[HWMODE_B].rates = rates;
347 }
348
349 /*
350 * Intitialize 802.11g
351 * Rates: CCK, OFDM.
352 * Channels: OFDM.
353 */
354 if (spec->num_modes > HWMODE_G) {
355 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
356 hwmodes[HWMODE_G].num_channels = 14;
357 hwmodes[HWMODE_G].num_rates = spec->num_rates;
358 hwmodes[HWMODE_G].channels = channels;
359 hwmodes[HWMODE_G].rates = rates;
360 }
361
362 /*
363 * Intitialize 802.11a
364 * Rates: OFDM.
365 * Channels: OFDM, UNII, HiperLAN2.
366 */
367 if (spec->num_modes > HWMODE_A) {
368 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
369 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
370 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
371 hwmodes[HWMODE_A].channels = &channels[14];
372 hwmodes[HWMODE_A].rates = &rates[4];
373 }
374
375 if (spec->num_modes > HWMODE_G &&
376 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
377 goto exit_free_rates;
378
379 if (spec->num_modes > HWMODE_B &&
380 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
381 goto exit_free_rates;
382
383 if (spec->num_modes > HWMODE_A &&
384 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
385 goto exit_free_rates;
386
387 rt2x00dev->hwmodes = hwmodes;
388
389 return 0;
390
391 exit_free_rates:
392 kfree(rates);
393
394 exit_free_channels:
395 kfree(channels);
396
397 exit_free_modes:
398 kfree(hwmodes);
399
400 exit:
401 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
402 return -ENOMEM;
403 }
404
405 static void rt2x00lib_deinit_hw(struct rt2x00_dev *rt2x00dev)
406 {
407 if (test_bit(DEVICE_INITIALIZED_HW, &rt2x00dev->flags))
408 ieee80211_unregister_hw(rt2x00dev->hw);
409
410 if (likely(rt2x00dev->hwmodes)) {
411 kfree(rt2x00dev->hwmodes->channels);
412 kfree(rt2x00dev->hwmodes->rates);
413 kfree(rt2x00dev->hwmodes);
414 rt2x00dev->hwmodes = NULL;
415 }
416 }
417
418 static int rt2x00lib_init_hw(struct rt2x00_dev *rt2x00dev)
419 {
420 struct hw_mode_spec *spec = &rt2x00dev->spec;
421 int status;
422
423 /*
424 * Initialize HW modes.
425 */
426 status = rt2x00lib_init_hw_modes(rt2x00dev, spec);
427 if (status)
428 return status;
429
430 /*
431 * Register HW.
432 */
433 status = ieee80211_register_hw(rt2x00dev->hw);
434 if (status) {
435 rt2x00lib_deinit_hw(rt2x00dev);
436 return status;
437 }
438
439 __set_bit(DEVICE_INITIALIZED_HW, &rt2x00dev->flags);
440
441 return 0;
442 }
443
444 /*
445 * Initialization/uninitialization handlers.
446 */
447 static int rt2x00lib_alloc_ring_entries(struct data_ring *ring,
448 const u16 max_entries, const u16 data_size, const u16 desc_size)
449 {
450 struct data_entry *entry;
451 unsigned int i;
452
453 ring->stats.limit = max_entries;
454 ring->data_size = data_size;
455 ring->desc_size = desc_size;
456
457 /*
458 * Allocate all ring entries.
459 */
460 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
461 if (!entry)
462 return -ENOMEM;
463
464 for (i = 0; i < ring->stats.limit; i++) {
465 entry[i].flags = 0;
466 entry[i].ring = ring;
467 entry[i].skb = NULL;
468 }
469
470 ring->entry = entry;
471
472 return 0;
473 }
474
475 static int rt2x00lib_allocate_ring_entries(struct rt2x00_dev *rt2x00dev)
476 {
477 struct data_ring *ring;
478
479 /*
480 * Allocate the RX ring.
481 */
482 if (rt2x00lib_alloc_ring_entries(rt2x00dev->rx,
483 RX_ENTRIES, DATA_FRAME_SIZE, rt2x00dev->ops->rxd_size))
484 return -ENOMEM;
485
486 /*
487 * First allocate the TX rings.
488 */
489 txring_for_each(rt2x00dev, ring) {
490 if (rt2x00lib_alloc_ring_entries(ring,
491 TX_ENTRIES, DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
492 return -ENOMEM;
493 }
494
495 /*
496 * Allocate the BEACON ring.
497 */
498 if (rt2x00lib_alloc_ring_entries(&rt2x00dev->bcn[0],
499 BEACON_ENTRIES, MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
500 return -ENOMEM;
501
502 /*
503 * Allocate the Atim ring.
504 */
505 if (test_bit(DEVICE_SUPPORT_ATIM, &rt2x00dev->flags)) {
506 if (rt2x00lib_alloc_ring_entries(&rt2x00dev->bcn[1],
507 ATIM_ENTRIES, DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
508 return -ENOMEM;
509 }
510
511 return 0;
512 }
513
514 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
515 {
516 struct data_ring *ring;
517
518 ring_for_each(rt2x00dev, ring) {
519 kfree(ring->entry);
520 ring->entry = NULL;
521 }
522 }
523
524 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
525 {
526 int status;
527
528 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
529 return 0;
530
531 /*
532 * Allocate all data rings.
533 */
534 status = rt2x00lib_allocate_ring_entries(rt2x00dev);
535 if (status) {
536 ERROR(rt2x00dev, "DMA allocation failed.\n");
537 return status;
538 }
539
540 /*
541 * Initialize the device.
542 */
543 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
544 if (status)
545 goto exit;
546
547 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
548
549 /*
550 * Register the rfkill handler.
551 */
552 status = rt2x00lib_register_rfkill(rt2x00dev);
553 if (status)
554 goto exit_unitialize;
555
556 return 0;
557
558 exit_unitialize:
559 rt2x00lib_uninitialize(rt2x00dev);
560
561 exit:
562 rt2x00lib_free_ring_entries(rt2x00dev);
563
564 return status;
565 }
566
567 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
568 {
569 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
570 return;
571
572 /*
573 * Unregister rfkill.
574 */
575 rt2x00lib_unregister_rfkill(rt2x00dev);
576
577 /*
578 * Allow the HW to uninitialize.
579 */
580 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
581
582 /*
583 * Free allocated datarings.
584 */
585 rt2x00lib_free_ring_entries(rt2x00dev);
586 }
587
588 /*
589 * driver allocation handlers.
590 */
591 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
592 {
593 struct data_ring *ring;
594 unsigned int ring_num;
595
596 /*
597 * We need the following rings:
598 * RX: 1
599 * TX: hw->queues
600 * Beacon: 1
601 * Atim: 1 (if supported)
602 */
603 ring_num = 2 + rt2x00dev->hw->queues +
604 test_bit(DEVICE_SUPPORT_ATIM, &rt2x00dev->flags);
605
606 ring = kzalloc(sizeof(*ring) * ring_num, GFP_KERNEL);
607 if (!ring) {
608 ERROR(rt2x00dev, "Ring allocation failed.\n");
609 return -ENOMEM;
610 }
611
612 /*
613 * Initialize pointers
614 */
615 rt2x00dev->rx = &ring[0];
616 rt2x00dev->tx = &ring[1];
617 rt2x00dev->bcn = &ring[1 + rt2x00dev->hw->queues];
618
619 /*
620 * Initialize ring parameters.
621 * cw_min: 2^5 = 32.
622 * cw_max: 2^10 = 1024.
623 */
624 ring_for_each(rt2x00dev, ring) {
625 ring->rt2x00dev = rt2x00dev;
626 ring->tx_params.aifs = 2;
627 ring->tx_params.cw_min = 5;
628 ring->tx_params.cw_max = 10;
629 }
630
631 return 0;
632 }
633
634 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
635 {
636 int retval = -ENOMEM;
637
638 /*
639 * Let the driver probe the device to detect the capabilities.
640 */
641 retval = rt2x00dev->ops->lib->init_hw(rt2x00dev);
642 if (retval) {
643 ERROR(rt2x00dev, "Failed to allocate device.\n");
644 goto exit;
645 }
646
647 /*
648 * Initialize configuration work.
649 */
650 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
651
652 /*
653 * Reset current working type.
654 */
655 rt2x00dev->interface.type = -EINVAL;
656
657 /*
658 * Allocate ring array.
659 */
660 retval = rt2x00lib_alloc_rings(rt2x00dev);
661 if (retval)
662 goto exit;
663
664 /*
665 * Initialize ieee80211 structure.
666 */
667 retval = rt2x00lib_init_hw(rt2x00dev);
668 if (retval) {
669 ERROR(rt2x00dev, "Failed to initialize hw.\n");
670 goto exit;
671 }
672
673 /*
674 * Allocatie rfkill.
675 */
676 retval = rt2x00lib_allocate_rfkill(rt2x00dev);
677 if (retval)
678 goto exit;
679
680 /*
681 * Open the debugfs entry.
682 */
683 rt2x00debug_register(rt2x00dev);
684
685 /*
686 * Check if we need to load the firmware.
687 */
688 if (test_bit(FIRMWARE_REQUIRED, &rt2x00dev->flags)) {
689 /*
690 * Request firmware and wait with further
691 * initializing of the card until the firmware
692 * has been loaded.
693 */
694 retval = rt2x00lib_load_firmware(rt2x00dev);
695 if (retval)
696 goto exit;
697 }
698
699 return 0;
700
701 exit:
702 rt2x00lib_remove_dev(rt2x00dev);
703
704 return retval;
705 }
706 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
707
708 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
709 {
710 /*
711 * Disable radio.
712 */
713 rt2x00lib_disable_radio(rt2x00dev);
714
715 /*
716 * Uninitialize device.
717 */
718 rt2x00lib_uninitialize(rt2x00dev);
719
720 /*
721 * Close debugfs entry.
722 */
723 rt2x00debug_deregister(rt2x00dev);
724
725 /*
726 * Free rfkill
727 */
728 rt2x00lib_free_rfkill(rt2x00dev);
729
730 /*
731 * Free ieee80211_hw memory.
732 */
733 rt2x00lib_deinit_hw(rt2x00dev);
734
735 /*
736 * Free ring structures.
737 */
738 kfree(rt2x00dev->rx);
739 rt2x00dev->rx = NULL;
740 rt2x00dev->tx = NULL;
741 rt2x00dev->bcn = NULL;
742
743 /*
744 * Free EEPROM memory.
745 */
746 kfree(rt2x00dev->eeprom);
747 rt2x00dev->eeprom = NULL;
748 }
749 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
750
751 /*
752 * Device state handlers
753 */
754 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev,
755 pm_message_t state)
756 {
757 int retval;
758
759 NOTICE(rt2x00dev, "Going to sleep.\n");
760
761 rt2x00lib_disable_radio(rt2x00dev);
762
763 /*
764 * Set device mode to sleep for power management.
765 */
766 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
767 if (retval)
768 return retval;
769
770 rt2x00lib_remove_dev(rt2x00dev);
771
772 return 0;
773 }
774 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
775
776 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
777 {
778 int retval;
779
780 NOTICE(rt2x00dev, "Waking up.\n");
781
782 retval = rt2x00lib_probe_dev(rt2x00dev);
783 if (retval) {
784 ERROR(rt2x00dev, "Failed to allocate device.\n");
785 return retval;
786 }
787
788 return 0;
789 }
790 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
791
792 /*
793 * Interrupt context handlers.
794 */
795 void rt2x00lib_txdone(struct data_entry *entry,
796 const int status, const int retry)
797 {
798 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
799 struct ieee80211_tx_status *tx_status = &entry->tx_status;
800 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
801
802 /*
803 * Update TX statistics.
804 */
805 tx_status->flags = 0;
806 tx_status->ack_signal = 0;
807 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
808 tx_status->retry_count = retry;
809
810 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
811 if (status == TX_SUCCESS || status == TX_SUCCESS_RETRY)
812 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
813 else
814 stats->dot11ACKFailureCount++;
815 }
816
817 tx_status->queue_length = entry->ring->stats.limit;
818 tx_status->queue_number = tx_status->control.queue;
819
820 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
821 if (status == TX_SUCCESS || status == TX_SUCCESS_RETRY)
822 stats->dot11RTSSuccessCount++;
823 else
824 stats->dot11RTSFailureCount++;
825 }
826
827 /*
828 * Send the tx_status to mac80211,
829 * that method also cleans up the skb structure.
830 */
831 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
832
833 entry->skb = NULL;
834 }
835 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
836
837 void rt2x00lib_rxdone(struct data_entry *entry, char *data,
838 const int size, const int signal, const int rssi, const int ofdm)
839 {
840 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
841 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
842 struct ieee80211_hw_mode *mode;
843 struct ieee80211_rate *rate;
844 struct sk_buff *skb;
845 unsigned int i;
846 int val = 0;
847
848 /*
849 * Update RX statistics.
850 */
851 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
852 for (i = 0; i < mode->num_rates; i++) {
853 rate = &mode->rates[i];
854
855 /*
856 * When frame was received with an OFDM bitrate,
857 * the signal is the PLCP value. If it was received with
858 * a CCK bitrate the signal is the rate in 0.5kbit/s.
859 */
860 if (!ofdm)
861 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
862 else
863 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
864
865 if (val == signal) {
866 /*
867 * Check for preamble bit.
868 */
869 if (signal & 0x08)
870 val = rate->val2;
871 else
872 val = rate->val;
873 break;
874 }
875 }
876
877 rx_status->rate = val;
878 rx_status->ssi = rssi;
879 rt2x00_update_link_rssi(&rt2x00dev->link, rssi);
880
881 /*
882 * Let's allocate a sk_buff where we can store the received data in,
883 * note that if data is NULL, we still have to allocate a sk_buff
884 * but that we should use that to replace the sk_buff which is already
885 * inside the entry.
886 */
887 skb = dev_alloc_skb(size + NET_IP_ALIGN);
888 if (!skb)
889 return;
890
891 skb_reserve(skb, NET_IP_ALIGN);
892 skb_put(skb, size);
893
894 if (data) {
895 memcpy(skb->data, data, size);
896 entry->skb = skb;
897 skb = NULL;
898 }
899
900 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
901 entry->skb = skb;
902 }
903 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
904
905 /*
906 * TX descriptor initializer
907 */
908 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
909 struct data_entry *entry, struct data_desc *txd,
910 struct ieee80211_hdr *ieee80211hdr, unsigned int length,
911 struct ieee80211_tx_control *control)
912 {
913 struct data_entry_desc desc;
914 int tx_rate;
915 int bitrate;
916 int duration;
917 int residual;
918 u16 frame_control;
919 u16 seq_ctrl;
920
921 /*
922 * Identify queue
923 */
924 if (control->queue < rt2x00dev->hw->queues)
925 desc.queue = control->queue;
926 else
927 desc.queue = 15;
928
929 /*
930 * Read required fields from ieee80211 header.
931 */
932 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
933 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
934
935 tx_rate = control->tx_rate;
936
937 /*
938 * Check if this is a rts frame
939 */
940 if (is_rts_frame(frame_control)) {
941 __set_bit(ENTRY_TXD_RTS_FRAME, &entry->flags);
942 if (control->rts_cts_rate)
943 tx_rate = control->rts_cts_rate;
944 }
945
946 /*
947 * Check for OFDM
948 */
949 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATE)
950 __set_bit(ENTRY_TXD_OFDM_RATE, &entry->flags);
951
952 /*
953 * Check if more fragments are pending
954 */
955 if (ieee80211_get_morefrag(ieee80211hdr))
956 __set_bit(ENTRY_TXD_MORE_FRAG, &entry->flags);
957
958 /*
959 * Beacons and probe responses require the tsf timestamp
960 * to be inserted into the frame.
961 */
962 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
963 is_probe_resp(frame_control))
964 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &entry->flags);
965
966 /*
967 * Check if ACK is required
968 */
969 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
970 __set_bit(ENTRY_TXD_REQ_ACK, &entry->flags);
971
972 /*
973 * Determine with what IFS priority this frame should be send.
974 * Set ifs to IFS_SIFS when the this is not the first fragment,
975 * or this fragment came after RTS/CTS.
976 */
977 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
978 test_bit(ENTRY_TXD_RTS_FRAME, &entry->flags))
979 desc.ifs = IFS_SIFS;
980 else
981 desc.ifs = IFS_BACKOFF;
982
983 /*
984 * How the length should be processed depends
985 * on if we are working with OFDM rates or not.
986 */
987 if (test_bit(ENTRY_TXD_OFDM_RATE, &entry->flags)) {
988 residual = 0;
989 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
990 desc.length_low = ((length + FCS_LEN) & 0x3f);
991
992 } else {
993 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
994
995 /*
996 * Convert length to microseconds.
997 */
998 residual = get_duration_res(length + FCS_LEN, bitrate);
999 duration = get_duration(length + FCS_LEN, bitrate);
1000
1001 if (residual != 0)
1002 duration++;
1003
1004 desc.length_high = duration >> 8;
1005 desc.length_low = duration & 0xff;
1006 }
1007
1008 /*
1009 * Create the signal and service values.
1010 */
1011 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
1012 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
1013 desc.signal |= 0x08;
1014
1015 desc.service = 0x04;
1016 if (residual <= (8 % 11))
1017 desc.service |= 0x80;
1018
1019 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry, txd, &desc,
1020 ieee80211hdr, length, control);
1021 }
1022 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
1023
1024 /*
1025 * rt2x00lib module information.
1026 */
1027 MODULE_AUTHOR(DRV_PROJECT);
1028 MODULE_VERSION(DRV_VERSION);
1029 MODULE_DESCRIPTION("rt2x00 library");
1030 MODULE_LICENSE("GPL");
This page took 0.104896 seconds and 5 git commands to generate.