2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
23 Abstract: rt2x00 generic device routines.
27 * Set enviroment defines for rt2x00.h
29 #define DRV_NAME "rt2x00lib"
31 #include <linux/kernel.h>
32 #include <linux/module.h>
35 #include "rt2x00lib.h"
40 struct data_ring
*rt2x00lib_get_ring(struct rt2x00_dev
*rt2x00dev
,
41 const unsigned int queue
)
43 int beacon
= test_bit(REQUIRE_BEACON_RING
, &rt2x00dev
->flags
);
46 * Check if we are requesting a reqular TX ring,
47 * or if we are requesting a Beacon or Atim ring.
48 * For Atim rings, we should check if it is supported.
50 if (queue
< rt2x00dev
->hw
->queues
&& rt2x00dev
->tx
)
51 return &rt2x00dev
->tx
[queue
];
53 if (!rt2x00dev
->bcn
|| !beacon
)
56 if (queue
== IEEE80211_TX_QUEUE_BEACON
)
57 return &rt2x00dev
->bcn
[0];
58 else if (queue
== IEEE80211_TX_QUEUE_AFTER_BEACON
)
59 return &rt2x00dev
->bcn
[1];
63 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring
);
66 * Link tuning handlers
68 static void rt2x00lib_start_link_tuner(struct rt2x00_dev
*rt2x00dev
)
70 rt2x00_clear_link(&rt2x00dev
->link
);
73 * Reset the link tuner.
75 rt2x00dev
->ops
->lib
->reset_tuner(rt2x00dev
);
77 queue_delayed_work(rt2x00dev
->hw
->workqueue
,
78 &rt2x00dev
->link
.work
, LINK_TUNE_INTERVAL
);
81 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev
*rt2x00dev
)
83 if (delayed_work_pending(&rt2x00dev
->link
.work
))
84 cancel_rearming_delayed_work(&rt2x00dev
->link
.work
);
87 void rt2x00lib_reset_link_tuner(struct rt2x00_dev
*rt2x00dev
)
89 rt2x00lib_stop_link_tuner(rt2x00dev
);
90 rt2x00lib_start_link_tuner(rt2x00dev
);
94 * Radio control handlers.
96 int rt2x00lib_enable_radio(struct rt2x00_dev
*rt2x00dev
)
101 * Don't enable the radio twice.
102 * And check if the hardware button has been disabled.
104 if (test_bit(DEVICE_ENABLED_RADIO
, &rt2x00dev
->flags
) ||
105 (test_bit(DEVICE_SUPPORT_HW_BUTTON
, &rt2x00dev
->flags
) &&
106 !test_bit(DEVICE_ENABLED_RADIO_HW
, &rt2x00dev
->flags
)))
112 status
= rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
,
117 __set_bit(DEVICE_ENABLED_RADIO
, &rt2x00dev
->flags
);
122 rt2x00lib_toggle_rx(rt2x00dev
, 1);
125 * Start the TX queues.
127 ieee80211_start_queues(rt2x00dev
->hw
);
132 void rt2x00lib_disable_radio(struct rt2x00_dev
*rt2x00dev
)
134 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO
, &rt2x00dev
->flags
))
138 * Stop beacon generation.
140 if (work_pending(&rt2x00dev
->beacon_work
))
141 cancel_work_sync(&rt2x00dev
->beacon_work
);
144 * Stop the TX queues.
146 ieee80211_stop_queues(rt2x00dev
->hw
);
151 rt2x00lib_toggle_rx(rt2x00dev
, 0);
156 rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_RADIO_OFF
);
159 void rt2x00lib_toggle_rx(struct rt2x00_dev
*rt2x00dev
, int enable
)
161 enum dev_state state
= enable
? STATE_RADIO_RX_ON
: STATE_RADIO_RX_OFF
;
164 * When we are disabling the RX, we should also stop the link tuner.
167 rt2x00lib_stop_link_tuner(rt2x00dev
);
169 rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, state
);
172 * When we are enabling the RX, we should also start the link tuner.
174 if (enable
&& is_interface_present(&rt2x00dev
->interface
))
175 rt2x00lib_start_link_tuner(rt2x00dev
);
178 static void rt2x00lib_precalculate_link_signal(struct link
*link
)
180 if (link
->rx_failed
|| link
->rx_success
)
181 link
->rx_percentage
=
182 (link
->rx_success
* 100) /
183 (link
->rx_failed
+ link
->rx_success
);
185 link
->rx_percentage
= 50;
187 if (link
->tx_failed
|| link
->tx_success
)
188 link
->tx_percentage
=
189 (link
->tx_success
* 100) /
190 (link
->tx_failed
+ link
->tx_success
);
192 link
->tx_percentage
= 50;
194 link
->rx_success
= 0;
196 link
->tx_success
= 0;
200 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev
*rt2x00dev
,
203 int rssi_percentage
= 0;
207 * We need a positive value for the RSSI.
210 rssi
+= rt2x00dev
->rssi_offset
;
213 * Calculate the different percentages,
214 * which will be used for the signal.
216 if (rt2x00dev
->rssi_offset
)
217 rssi_percentage
= (rssi
* 100) / rt2x00dev
->rssi_offset
;
220 * Add the individual percentages and use the WEIGHT
221 * defines to calculate the current link signal.
223 signal
= ((WEIGHT_RSSI
* rssi_percentage
) +
224 (WEIGHT_TX
* rt2x00dev
->link
.tx_percentage
) +
225 (WEIGHT_RX
* rt2x00dev
->link
.rx_percentage
)) / 100;
227 return (signal
> 100) ? 100 : signal
;
230 static void rt2x00lib_link_tuner(struct work_struct
*work
)
232 struct rt2x00_dev
*rt2x00dev
=
233 container_of(work
, struct rt2x00_dev
, link
.work
.work
);
238 rt2x00dev
->ops
->lib
->link_stats(rt2x00dev
);
240 rt2x00dev
->low_level_stats
.dot11FCSErrorCount
+=
241 rt2x00dev
->link
.rx_failed
;
243 rt2x00lib_precalculate_link_signal(&rt2x00dev
->link
);
246 * Only perform the link tuning when Link tuning
247 * has been enabled (This could have been disabled from the EEPROM).
249 if (!test_bit(CONFIG_DISABLE_LINK_TUNING
, &rt2x00dev
->flags
))
250 rt2x00dev
->ops
->lib
->link_tuner(rt2x00dev
);
253 * Increase tuner counter, and reschedule the next link tuner run.
255 rt2x00dev
->link
.count
++;
256 queue_delayed_work(rt2x00dev
->hw
->workqueue
, &rt2x00dev
->link
.work
,
261 * Interrupt context handlers.
263 static void rt2x00lib_beacondone_scheduled(struct work_struct
*work
)
265 struct rt2x00_dev
*rt2x00dev
=
266 container_of(work
, struct rt2x00_dev
, beacon_work
);
267 struct data_ring
*ring
=
268 rt2x00lib_get_ring(rt2x00dev
, IEEE80211_TX_QUEUE_BEACON
);
269 struct data_entry
*entry
= rt2x00_get_data_entry(ring
);
272 skb
= ieee80211_beacon_get(rt2x00dev
->hw
,
273 rt2x00dev
->interface
.id
,
274 &entry
->tx_status
.control
);
278 rt2x00dev
->ops
->hw
->beacon_update(rt2x00dev
->hw
, skb
,
279 &entry
->tx_status
.control
);
284 void rt2x00lib_beacondone(struct rt2x00_dev
*rt2x00dev
)
286 if (!test_bit(DEVICE_ENABLED_RADIO
, &rt2x00dev
->flags
))
289 queue_work(rt2x00dev
->hw
->workqueue
, &rt2x00dev
->beacon_work
);
291 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone
);
293 void rt2x00lib_txdone(struct data_entry
*entry
,
294 const int status
, const int retry
)
296 struct rt2x00_dev
*rt2x00dev
= entry
->ring
->rt2x00dev
;
297 struct ieee80211_tx_status
*tx_status
= &entry
->tx_status
;
298 struct ieee80211_low_level_stats
*stats
= &rt2x00dev
->low_level_stats
;
299 int success
= !!(status
== TX_SUCCESS
|| status
== TX_SUCCESS_RETRY
);
300 int fail
= !!(status
== TX_FAIL_RETRY
|| status
== TX_FAIL_INVALID
||
301 status
== TX_FAIL_OTHER
);
304 * Update TX statistics.
306 tx_status
->flags
= 0;
307 tx_status
->ack_signal
= 0;
308 tx_status
->excessive_retries
= (status
== TX_FAIL_RETRY
);
309 tx_status
->retry_count
= retry
;
310 rt2x00dev
->link
.tx_success
+= success
;
311 rt2x00dev
->link
.tx_failed
+= retry
+ fail
;
313 if (!(tx_status
->control
.flags
& IEEE80211_TXCTL_NO_ACK
)) {
315 tx_status
->flags
|= IEEE80211_TX_STATUS_ACK
;
317 stats
->dot11ACKFailureCount
++;
320 tx_status
->queue_length
= entry
->ring
->stats
.limit
;
321 tx_status
->queue_number
= tx_status
->control
.queue
;
323 if (tx_status
->control
.flags
& IEEE80211_TXCTL_USE_RTS_CTS
) {
325 stats
->dot11RTSSuccessCount
++;
327 stats
->dot11RTSFailureCount
++;
331 * Send the tx_status to mac80211,
332 * that method also cleans up the skb structure.
334 ieee80211_tx_status_irqsafe(rt2x00dev
->hw
, entry
->skb
, tx_status
);
337 EXPORT_SYMBOL_GPL(rt2x00lib_txdone
);
339 void rt2x00lib_rxdone(struct data_entry
*entry
, struct sk_buff
*skb
,
340 const int signal
, const int rssi
, const int ofdm
)
342 struct rt2x00_dev
*rt2x00dev
= entry
->ring
->rt2x00dev
;
343 struct ieee80211_rx_status
*rx_status
= &rt2x00dev
->rx_status
;
344 struct ieee80211_hw_mode
*mode
;
345 struct ieee80211_rate
*rate
;
350 * Update RX statistics.
352 mode
= &rt2x00dev
->hwmodes
[rt2x00dev
->curr_hwmode
];
353 for (i
= 0; i
< mode
->num_rates
; i
++) {
354 rate
= &mode
->rates
[i
];
357 * When frame was received with an OFDM bitrate,
358 * the signal is the PLCP value. If it was received with
359 * a CCK bitrate the signal is the rate in 0.5kbit/s.
362 val
= DEVICE_GET_RATE_FIELD(rate
->val
, RATE
);
364 val
= DEVICE_GET_RATE_FIELD(rate
->val
, PLCP
);
372 rt2x00_update_link_rssi(&rt2x00dev
->link
, rssi
);
373 rt2x00dev
->link
.rx_success
++;
374 rx_status
->rate
= val
;
375 rx_status
->signal
= rt2x00lib_calculate_link_signal(rt2x00dev
, rssi
);
376 rx_status
->ssi
= rssi
;
379 * Send frame to mac80211
381 ieee80211_rx_irqsafe(rt2x00dev
->hw
, skb
, rx_status
);
383 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone
);
386 * TX descriptor initializer
388 void rt2x00lib_write_tx_desc(struct rt2x00_dev
*rt2x00dev
,
389 struct data_desc
*txd
,
390 struct ieee80211_hdr
*ieee80211hdr
,
392 struct ieee80211_tx_control
*control
)
394 struct data_entry_desc desc
;
395 struct data_ring
*ring
;
404 * Make sure the descriptor is properly cleared.
406 memset(&desc
, 0x00, sizeof(desc
));
409 * Get ring pointer, if we fail to obtain the
410 * correct ring, then use the first TX ring.
412 ring
= rt2x00lib_get_ring(rt2x00dev
, control
->queue
);
414 ring
= rt2x00lib_get_ring(rt2x00dev
, IEEE80211_TX_QUEUE_DATA0
);
416 desc
.cw_min
= ring
->tx_params
.cw_min
;
417 desc
.cw_max
= ring
->tx_params
.cw_max
;
418 desc
.aifs
= ring
->tx_params
.aifs
;
423 if (control
->queue
< rt2x00dev
->hw
->queues
)
424 desc
.queue
= control
->queue
;
425 else if (control
->queue
== IEEE80211_TX_QUEUE_BEACON
||
426 control
->queue
== IEEE80211_TX_QUEUE_AFTER_BEACON
)
427 desc
.queue
= QUEUE_MGMT
;
429 desc
.queue
= QUEUE_OTHER
;
432 * Read required fields from ieee80211 header.
434 frame_control
= le16_to_cpu(ieee80211hdr
->frame_control
);
435 seq_ctrl
= le16_to_cpu(ieee80211hdr
->seq_ctrl
);
437 tx_rate
= control
->tx_rate
;
440 * Check if this is a RTS/CTS frame
442 if (is_rts_frame(frame_control
) || is_cts_frame(frame_control
)) {
443 __set_bit(ENTRY_TXD_BURST
, &desc
.flags
);
444 if (is_rts_frame(frame_control
))
445 __set_bit(ENTRY_TXD_RTS_FRAME
, &desc
.flags
);
446 if (control
->rts_cts_rate
)
447 tx_rate
= control
->rts_cts_rate
;
453 if (DEVICE_GET_RATE_FIELD(tx_rate
, RATEMASK
) & DEV_OFDM_RATEMASK
)
454 __set_bit(ENTRY_TXD_OFDM_RATE
, &desc
.flags
);
457 * Check if more fragments are pending
459 if (ieee80211_get_morefrag(ieee80211hdr
)) {
460 __set_bit(ENTRY_TXD_BURST
, &desc
.flags
);
461 __set_bit(ENTRY_TXD_MORE_FRAG
, &desc
.flags
);
465 * Beacons and probe responses require the tsf timestamp
466 * to be inserted into the frame.
468 if (control
->queue
== IEEE80211_TX_QUEUE_BEACON
||
469 is_probe_resp(frame_control
))
470 __set_bit(ENTRY_TXD_REQ_TIMESTAMP
, &desc
.flags
);
473 * Determine with what IFS priority this frame should be send.
474 * Set ifs to IFS_SIFS when the this is not the first fragment,
475 * or this fragment came after RTS/CTS.
477 if ((seq_ctrl
& IEEE80211_SCTL_FRAG
) > 0 ||
478 test_bit(ENTRY_TXD_RTS_FRAME
, &desc
.flags
))
481 desc
.ifs
= IFS_BACKOFF
;
485 * Length calculation depends on OFDM/CCK rate.
487 desc
.signal
= DEVICE_GET_RATE_FIELD(tx_rate
, PLCP
);
490 if (test_bit(ENTRY_TXD_OFDM_RATE
, &desc
.flags
)) {
491 desc
.length_high
= ((length
+ FCS_LEN
) >> 6) & 0x3f;
492 desc
.length_low
= ((length
+ FCS_LEN
) & 0x3f);
494 bitrate
= DEVICE_GET_RATE_FIELD(tx_rate
, RATE
);
497 * Convert length to microseconds.
499 residual
= get_duration_res(length
+ FCS_LEN
, bitrate
);
500 duration
= get_duration(length
+ FCS_LEN
, bitrate
);
506 * Check if we need to set the Length Extension
508 if (bitrate
== 110 && residual
<= 3)
509 desc
.service
|= 0x80;
512 desc
.length_high
= (duration
>> 8) & 0xff;
513 desc
.length_low
= duration
& 0xff;
516 * When preamble is enabled we should set the
517 * preamble bit for the signal.
519 if (DEVICE_GET_RATE_FIELD(tx_rate
, PREAMBLE
))
523 rt2x00dev
->ops
->lib
->write_tx_desc(rt2x00dev
, txd
, &desc
,
524 ieee80211hdr
, length
, control
);
526 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc
);
529 * Driver initialization handlers.
531 static void rt2x00lib_channel(struct ieee80211_channel
*entry
,
532 const int channel
, const int tx_power
,
535 entry
->chan
= channel
;
537 entry
->freq
= 2407 + (5 * channel
);
539 entry
->freq
= 5000 + (5 * channel
);
542 IEEE80211_CHAN_W_IBSS
|
543 IEEE80211_CHAN_W_ACTIVE_SCAN
|
544 IEEE80211_CHAN_W_SCAN
;
545 entry
->power_level
= tx_power
;
546 entry
->antenna_max
= 0xff;
549 static void rt2x00lib_rate(struct ieee80211_rate
*entry
,
550 const int rate
, const int mask
,
551 const int plcp
, const int flags
)
555 DEVICE_SET_RATE_FIELD(rate
, RATE
) |
556 DEVICE_SET_RATE_FIELD(mask
, RATEMASK
) |
557 DEVICE_SET_RATE_FIELD(plcp
, PLCP
);
558 entry
->flags
= flags
;
559 entry
->val2
= entry
->val
;
560 if (entry
->flags
& IEEE80211_RATE_PREAMBLE2
)
561 entry
->val2
|= DEVICE_SET_RATE_FIELD(1, PREAMBLE
);
562 entry
->min_rssi_ack
= 0;
563 entry
->min_rssi_ack_delta
= 0;
566 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev
*rt2x00dev
,
567 struct hw_mode_spec
*spec
)
569 struct ieee80211_hw
*hw
= rt2x00dev
->hw
;
570 struct ieee80211_hw_mode
*hwmodes
;
571 struct ieee80211_channel
*channels
;
572 struct ieee80211_rate
*rates
;
574 unsigned char tx_power
;
576 hwmodes
= kzalloc(sizeof(*hwmodes
) * spec
->num_modes
, GFP_KERNEL
);
580 channels
= kzalloc(sizeof(*channels
) * spec
->num_channels
, GFP_KERNEL
);
582 goto exit_free_modes
;
584 rates
= kzalloc(sizeof(*rates
) * spec
->num_rates
, GFP_KERNEL
);
586 goto exit_free_channels
;
589 * Initialize Rate list.
591 rt2x00lib_rate(&rates
[0], 10, DEV_RATEMASK_1MB
,
592 0x00, IEEE80211_RATE_CCK
);
593 rt2x00lib_rate(&rates
[1], 20, DEV_RATEMASK_2MB
,
594 0x01, IEEE80211_RATE_CCK_2
);
595 rt2x00lib_rate(&rates
[2], 55, DEV_RATEMASK_5_5MB
,
596 0x02, IEEE80211_RATE_CCK_2
);
597 rt2x00lib_rate(&rates
[3], 110, DEV_RATEMASK_11MB
,
598 0x03, IEEE80211_RATE_CCK_2
);
600 if (spec
->num_rates
> 4) {
601 rt2x00lib_rate(&rates
[4], 60, DEV_RATEMASK_6MB
,
602 0x0b, IEEE80211_RATE_OFDM
);
603 rt2x00lib_rate(&rates
[5], 90, DEV_RATEMASK_9MB
,
604 0x0f, IEEE80211_RATE_OFDM
);
605 rt2x00lib_rate(&rates
[6], 120, DEV_RATEMASK_12MB
,
606 0x0a, IEEE80211_RATE_OFDM
);
607 rt2x00lib_rate(&rates
[7], 180, DEV_RATEMASK_18MB
,
608 0x0e, IEEE80211_RATE_OFDM
);
609 rt2x00lib_rate(&rates
[8], 240, DEV_RATEMASK_24MB
,
610 0x09, IEEE80211_RATE_OFDM
);
611 rt2x00lib_rate(&rates
[9], 360, DEV_RATEMASK_36MB
,
612 0x0d, IEEE80211_RATE_OFDM
);
613 rt2x00lib_rate(&rates
[10], 480, DEV_RATEMASK_48MB
,
614 0x08, IEEE80211_RATE_OFDM
);
615 rt2x00lib_rate(&rates
[11], 540, DEV_RATEMASK_54MB
,
616 0x0c, IEEE80211_RATE_OFDM
);
620 * Initialize Channel list.
622 for (i
= 0; i
< spec
->num_channels
; i
++) {
623 if (spec
->channels
[i
].channel
<= 14)
624 tx_power
= spec
->tx_power_bg
[i
];
625 else if (spec
->tx_power_a
)
626 tx_power
= spec
->tx_power_a
[i
];
628 tx_power
= spec
->tx_power_default
;
630 rt2x00lib_channel(&channels
[i
],
631 spec
->channels
[i
].channel
, tx_power
, i
);
635 * Intitialize 802.11b
639 if (spec
->num_modes
> HWMODE_B
) {
640 hwmodes
[HWMODE_B
].mode
= MODE_IEEE80211B
;
641 hwmodes
[HWMODE_B
].num_channels
= 14;
642 hwmodes
[HWMODE_B
].num_rates
= 4;
643 hwmodes
[HWMODE_B
].channels
= channels
;
644 hwmodes
[HWMODE_B
].rates
= rates
;
648 * Intitialize 802.11g
652 if (spec
->num_modes
> HWMODE_G
) {
653 hwmodes
[HWMODE_G
].mode
= MODE_IEEE80211G
;
654 hwmodes
[HWMODE_G
].num_channels
= 14;
655 hwmodes
[HWMODE_G
].num_rates
= spec
->num_rates
;
656 hwmodes
[HWMODE_G
].channels
= channels
;
657 hwmodes
[HWMODE_G
].rates
= rates
;
661 * Intitialize 802.11a
663 * Channels: OFDM, UNII, HiperLAN2.
665 if (spec
->num_modes
> HWMODE_A
) {
666 hwmodes
[HWMODE_A
].mode
= MODE_IEEE80211A
;
667 hwmodes
[HWMODE_A
].num_channels
= spec
->num_channels
- 14;
668 hwmodes
[HWMODE_A
].num_rates
= spec
->num_rates
- 4;
669 hwmodes
[HWMODE_A
].channels
= &channels
[14];
670 hwmodes
[HWMODE_A
].rates
= &rates
[4];
673 if (spec
->num_modes
> HWMODE_G
&&
674 ieee80211_register_hwmode(hw
, &hwmodes
[HWMODE_G
]))
675 goto exit_free_rates
;
677 if (spec
->num_modes
> HWMODE_B
&&
678 ieee80211_register_hwmode(hw
, &hwmodes
[HWMODE_B
]))
679 goto exit_free_rates
;
681 if (spec
->num_modes
> HWMODE_A
&&
682 ieee80211_register_hwmode(hw
, &hwmodes
[HWMODE_A
]))
683 goto exit_free_rates
;
685 rt2x00dev
->hwmodes
= hwmodes
;
699 ERROR(rt2x00dev
, "Allocation ieee80211 modes failed.\n");
703 static void rt2x00lib_remove_hw(struct rt2x00_dev
*rt2x00dev
)
705 if (test_bit(DEVICE_INITIALIZED_HW
, &rt2x00dev
->flags
))
706 ieee80211_unregister_hw(rt2x00dev
->hw
);
708 if (likely(rt2x00dev
->hwmodes
)) {
709 kfree(rt2x00dev
->hwmodes
->channels
);
710 kfree(rt2x00dev
->hwmodes
->rates
);
711 kfree(rt2x00dev
->hwmodes
);
712 rt2x00dev
->hwmodes
= NULL
;
716 static int rt2x00lib_probe_hw(struct rt2x00_dev
*rt2x00dev
)
718 struct hw_mode_spec
*spec
= &rt2x00dev
->spec
;
722 * Initialize HW modes.
724 status
= rt2x00lib_probe_hw_modes(rt2x00dev
, spec
);
731 status
= ieee80211_register_hw(rt2x00dev
->hw
);
733 rt2x00lib_remove_hw(rt2x00dev
);
737 __set_bit(DEVICE_INITIALIZED_HW
, &rt2x00dev
->flags
);
743 * Initialization/uninitialization handlers.
745 static int rt2x00lib_alloc_entries(struct data_ring
*ring
,
746 const u16 max_entries
, const u16 data_size
,
749 struct data_entry
*entry
;
752 ring
->stats
.limit
= max_entries
;
753 ring
->data_size
= data_size
;
754 ring
->desc_size
= desc_size
;
757 * Allocate all ring entries.
759 entry
= kzalloc(ring
->stats
.limit
* sizeof(*entry
), GFP_KERNEL
);
763 for (i
= 0; i
< ring
->stats
.limit
; i
++) {
765 entry
[i
].ring
= ring
;
774 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev
*rt2x00dev
)
776 struct data_ring
*ring
;
779 * Allocate the RX ring.
781 if (rt2x00lib_alloc_entries(rt2x00dev
->rx
, RX_ENTRIES
, DATA_FRAME_SIZE
,
782 rt2x00dev
->ops
->rxd_size
))
786 * First allocate the TX rings.
788 txring_for_each(rt2x00dev
, ring
) {
789 if (rt2x00lib_alloc_entries(ring
, TX_ENTRIES
, DATA_FRAME_SIZE
,
790 rt2x00dev
->ops
->txd_size
))
794 if (!test_bit(REQUIRE_BEACON_RING
, &rt2x00dev
->flags
))
798 * Allocate the BEACON ring.
800 if (rt2x00lib_alloc_entries(&rt2x00dev
->bcn
[0], BEACON_ENTRIES
,
801 MGMT_FRAME_SIZE
, rt2x00dev
->ops
->txd_size
))
805 * Allocate the Atim ring.
807 if (rt2x00lib_alloc_entries(&rt2x00dev
->bcn
[1], ATIM_ENTRIES
,
808 DATA_FRAME_SIZE
, rt2x00dev
->ops
->txd_size
))
814 static void rt2x00lib_free_ring_entries(struct rt2x00_dev
*rt2x00dev
)
816 struct data_ring
*ring
;
818 ring_for_each(rt2x00dev
, ring
) {
824 void rt2x00lib_uninitialize(struct rt2x00_dev
*rt2x00dev
)
826 if (!__test_and_clear_bit(DEVICE_INITIALIZED
, &rt2x00dev
->flags
))
832 rt2x00rfkill_unregister(rt2x00dev
);
835 * Allow the HW to uninitialize.
837 rt2x00dev
->ops
->lib
->uninitialize(rt2x00dev
);
840 * Free allocated ring entries.
842 rt2x00lib_free_ring_entries(rt2x00dev
);
845 int rt2x00lib_initialize(struct rt2x00_dev
*rt2x00dev
)
849 if (test_bit(DEVICE_INITIALIZED
, &rt2x00dev
->flags
))
853 * Allocate all ring entries.
855 status
= rt2x00lib_alloc_ring_entries(rt2x00dev
);
857 ERROR(rt2x00dev
, "Ring entries allocation failed.\n");
862 * Initialize the device.
864 status
= rt2x00dev
->ops
->lib
->initialize(rt2x00dev
);
868 __set_bit(DEVICE_INITIALIZED
, &rt2x00dev
->flags
);
871 * Register the rfkill handler.
873 status
= rt2x00rfkill_register(rt2x00dev
);
875 goto exit_unitialize
;
880 rt2x00lib_uninitialize(rt2x00dev
);
883 rt2x00lib_free_ring_entries(rt2x00dev
);
889 * driver allocation handlers.
891 static int rt2x00lib_alloc_rings(struct rt2x00_dev
*rt2x00dev
)
893 struct data_ring
*ring
;
896 * We need the following rings:
899 * Beacon: 1 (if required)
900 * Atim: 1 (if required)
902 rt2x00dev
->data_rings
= 1 + rt2x00dev
->hw
->queues
+
903 (2 * test_bit(REQUIRE_BEACON_RING
, &rt2x00dev
->flags
));
905 ring
= kzalloc(rt2x00dev
->data_rings
* sizeof(*ring
), GFP_KERNEL
);
907 ERROR(rt2x00dev
, "Ring allocation failed.\n");
912 * Initialize pointers
914 rt2x00dev
->rx
= ring
;
915 rt2x00dev
->tx
= &rt2x00dev
->rx
[1];
916 if (test_bit(REQUIRE_BEACON_RING
, &rt2x00dev
->flags
))
917 rt2x00dev
->bcn
= &rt2x00dev
->tx
[rt2x00dev
->hw
->queues
];
920 * Initialize ring parameters.
922 * cw_max: 2^10 = 1024.
924 ring_for_each(rt2x00dev
, ring
) {
925 ring
->rt2x00dev
= rt2x00dev
;
926 ring
->tx_params
.aifs
= 2;
927 ring
->tx_params
.cw_min
= 5;
928 ring
->tx_params
.cw_max
= 10;
934 static void rt2x00lib_free_rings(struct rt2x00_dev
*rt2x00dev
)
936 kfree(rt2x00dev
->rx
);
937 rt2x00dev
->rx
= NULL
;
938 rt2x00dev
->tx
= NULL
;
939 rt2x00dev
->bcn
= NULL
;
942 int rt2x00lib_probe_dev(struct rt2x00_dev
*rt2x00dev
)
944 int retval
= -ENOMEM
;
947 * Let the driver probe the device to detect the capabilities.
949 retval
= rt2x00dev
->ops
->lib
->probe_hw(rt2x00dev
);
951 ERROR(rt2x00dev
, "Failed to allocate device.\n");
956 * Initialize configuration work.
958 INIT_WORK(&rt2x00dev
->beacon_work
, rt2x00lib_beacondone_scheduled
);
959 INIT_DELAYED_WORK(&rt2x00dev
->link
.work
, rt2x00lib_link_tuner
);
962 * Reset current working type.
964 rt2x00dev
->interface
.type
= INVALID_INTERFACE
;
967 * Allocate ring array.
969 retval
= rt2x00lib_alloc_rings(rt2x00dev
);
974 * Initialize ieee80211 structure.
976 retval
= rt2x00lib_probe_hw(rt2x00dev
);
978 ERROR(rt2x00dev
, "Failed to initialize hw.\n");
985 retval
= rt2x00rfkill_allocate(rt2x00dev
);
990 * Open the debugfs entry.
992 rt2x00debug_register(rt2x00dev
);
997 rt2x00lib_remove_dev(rt2x00dev
);
1001 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev
);
1003 void rt2x00lib_remove_dev(struct rt2x00_dev
*rt2x00dev
)
1008 rt2x00lib_disable_radio(rt2x00dev
);
1011 * Uninitialize device.
1013 rt2x00lib_uninitialize(rt2x00dev
);
1016 * Close debugfs entry.
1018 rt2x00debug_deregister(rt2x00dev
);
1023 rt2x00rfkill_free(rt2x00dev
);
1026 * Free ieee80211_hw memory.
1028 rt2x00lib_remove_hw(rt2x00dev
);
1031 * Free firmware image.
1033 rt2x00lib_free_firmware(rt2x00dev
);
1036 * Free ring structures.
1038 rt2x00lib_free_rings(rt2x00dev
);
1040 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev
);
1043 * Device state handlers
1046 int rt2x00lib_suspend(struct rt2x00_dev
*rt2x00dev
, pm_message_t state
)
1050 NOTICE(rt2x00dev
, "Going to sleep.\n");
1053 * Disable radio and unitialize all items
1054 * that must be recreated on resume.
1056 rt2x00lib_disable_radio(rt2x00dev
);
1057 rt2x00lib_uninitialize(rt2x00dev
);
1058 rt2x00debug_deregister(rt2x00dev
);
1061 * Set device mode to sleep for power management.
1063 retval
= rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_SLEEP
);
1069 EXPORT_SYMBOL_GPL(rt2x00lib_suspend
);
1071 int rt2x00lib_resume(struct rt2x00_dev
*rt2x00dev
)
1073 struct interface
*intf
= &rt2x00dev
->interface
;
1076 NOTICE(rt2x00dev
, "Waking up.\n");
1077 __set_bit(INTERFACE_RESUME
, &rt2x00dev
->flags
);
1080 * Open the debugfs entry.
1082 rt2x00debug_register(rt2x00dev
);
1085 * Reinitialize device and all active interfaces.
1087 retval
= rt2x00mac_start(rt2x00dev
->hw
);
1092 * Reconfigure device.
1094 retval
= rt2x00mac_config(rt2x00dev
->hw
, &rt2x00dev
->hw
->conf
);
1098 rt2x00lib_config_mac_addr(rt2x00dev
, intf
->mac
);
1099 rt2x00lib_config_bssid(rt2x00dev
, intf
->bssid
);
1100 rt2x00lib_config_type(rt2x00dev
, intf
->type
);
1101 rt2x00lib_config_packet_filter(rt2x00dev
, intf
->filter
);
1104 * When in Master or Ad-hoc mode,
1105 * restart Beacon transmitting by faking a beacondone event.
1107 if (intf
->type
== IEEE80211_IF_TYPE_AP
||
1108 intf
->type
== IEEE80211_IF_TYPE_IBSS
)
1109 rt2x00lib_beacondone(rt2x00dev
);
1111 __clear_bit(INTERFACE_RESUME
, &rt2x00dev
->flags
);
1116 rt2x00lib_disable_radio(rt2x00dev
);
1117 rt2x00lib_uninitialize(rt2x00dev
);
1118 rt2x00debug_deregister(rt2x00dev
);
1120 __clear_bit(INTERFACE_RESUME
, &rt2x00dev
->flags
);
1124 EXPORT_SYMBOL_GPL(rt2x00lib_resume
);
1125 #endif /* CONFIG_PM */
1128 * rt2x00lib module information.
1130 MODULE_AUTHOR(DRV_PROJECT
);
1131 MODULE_VERSION(DRV_VERSION
);
1132 MODULE_DESCRIPTION("rt2x00 library");
1133 MODULE_LICENSE("GPL");