2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * Purpose: Provide functions to setup NIC operation mode
22 * vnt_set_rspinf - Set RSPINF
23 * vnt_update_ifs - Update slotTime,SIFS,DIFS, and EIFS
24 * vnt_update_top_rates - Update BasicTopRate
25 * vnt_add_basic_rate - Add to BasicRateSet
26 * vnt_ofdm_min_rate - Check if any OFDM rate is in BasicRateSet
27 * vnt_get_tsf_offset - Calculate TSFOffset
28 * vnt_get_current_tsf - Read Current NIC TSF counter
29 * vnt_get_next_tbtt - Calculate Next Beacon TSF counter
30 * vnt_reset_next_tbtt - Set NIC Beacon time
31 * vnt_update_next_tbtt - Sync. NIC Beacon time
32 * vnt_radio_power_off - Turn Off NIC Radio Power
33 * vnt_radio_power_on - Turn On NIC Radio Power
36 * 06-10-2003 Bryan YC Fan: Re-write codes to support VT3253 spec.
37 * 08-26-2003 Kyle Hsu: Modify the definition type of dwIoBase.
38 * 09-01-2003 Bryan YC Fan: Add vnt_update_ifs().
52 /* const u16 cwRXBCNTSFOff[MAX_RATE] =
53 {17, 34, 96, 192, 34, 23, 17, 11, 8, 5, 4, 3}; */
55 static const u16 cwRXBCNTSFOff
[MAX_RATE
] = {
56 192, 96, 34, 17, 34, 23, 17, 11, 8, 5, 4, 3
60 * Description: Set NIC media channel
64 * pDevice - The adapter to be set
65 * connection_channel - Channel to be set
69 void vnt_set_channel(struct vnt_private
*priv
, u32 connection_channel
)
72 if (connection_channel
> CB_MAX_CHANNEL
|| !connection_channel
)
76 vnt_mac_reg_bits_on(priv
, MAC_REG_MACCR
, MACCR_CLRNAV
);
78 /* Set Channel[7] = 0 to tell H/W channel is changing now. */
79 vnt_mac_reg_bits_off(priv
, MAC_REG_CHANNEL
, 0xb0);
81 vnt_control_out(priv
, MESSAGE_TYPE_SELECT_CHANNEL
,
82 connection_channel
, 0, 0, NULL
);
84 vnt_control_out_u8(priv
, MESSAGE_REQUEST_MACREG
, MAC_REG_CHANNEL
,
85 (u8
)(connection_channel
| 0x80));
89 * Description: Get CCK mode basic rate
93 * priv - The adapter to be set
94 * rate_idx - Receiving data rate
98 * Return Value: response Control frame rate
101 static u16
vnt_get_cck_rate(struct vnt_private
*priv
, u16 rate_idx
)
105 while (ui
> RATE_1M
) {
106 if (priv
->basic_rates
& (1 << ui
))
115 * Description: Get OFDM mode basic rate
119 * priv - The adapter to be set
120 * rate_idx - Receiving data rate
124 * Return Value: response Control frame rate
127 static u16
vnt_get_ofdm_rate(struct vnt_private
*priv
, u16 rate_idx
)
131 dev_dbg(&priv
->usb
->dev
, "%s basic rate: %d\n",
132 __func__
, priv
->basic_rates
);
134 if (!vnt_ofdm_min_rate(priv
)) {
135 dev_dbg(&priv
->usb
->dev
, "%s (NO OFDM) %d\n",
137 if (rate_idx
> RATE_24M
)
142 while (ui
> RATE_11M
) {
143 if (priv
->basic_rates
& (1 << ui
)) {
144 dev_dbg(&priv
->usb
->dev
, "%s rate: %d\n",
151 dev_dbg(&priv
->usb
->dev
, "%s basic rate: 24M\n", __func__
);
157 * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
162 * bb_type - Tx Packet type
164 * tx_rate - pointer to RSPINF TxRate field
165 * rsv_time- pointer to RSPINF RsvTime field
170 static void vnt_calculate_ofdm_rate(u16 rate
, u8 bb_type
,
171 u8
*tx_rate
, u8
*rsv_time
)
176 if (bb_type
== BB_TYPE_11A
) {
185 if (bb_type
== BB_TYPE_11A
) {
194 if (bb_type
== BB_TYPE_11A
) {
203 if (bb_type
== BB_TYPE_11A
) {
212 if (bb_type
== BB_TYPE_11A
) {
221 if (bb_type
== BB_TYPE_11A
) {
230 if (bb_type
== BB_TYPE_11A
) {
240 if (bb_type
== BB_TYPE_11A
) {
252 * Description: Set RSPINF
256 * pDevice - The adapter to be set
260 * Return Value: None.
264 void vnt_set_rspinf(struct vnt_private
*priv
, u8 bb_type
)
266 struct vnt_phy_field phy
[4];
267 u8 tx_rate
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; /* For OFDM */
268 u8 rsv_time
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
273 vnt_get_phy_field(priv
, 14,
274 vnt_get_cck_rate(priv
, RATE_1M
), PK_TYPE_11B
, &phy
[0]);
277 vnt_get_phy_field(priv
, 14,
278 vnt_get_cck_rate(priv
, RATE_2M
), PK_TYPE_11B
, &phy
[1]);
281 vnt_get_phy_field(priv
, 14,
282 vnt_get_cck_rate(priv
, RATE_5M
), PK_TYPE_11B
, &phy
[2]);
285 vnt_get_phy_field(priv
, 14,
286 vnt_get_cck_rate(priv
, RATE_11M
), PK_TYPE_11B
, &phy
[3]);
289 vnt_calculate_ofdm_rate(RATE_6M
, bb_type
, &tx_rate
[0], &rsv_time
[0]);
292 vnt_calculate_ofdm_rate(RATE_9M
, bb_type
, &tx_rate
[1], &rsv_time
[1]);
295 vnt_calculate_ofdm_rate(RATE_12M
, bb_type
, &tx_rate
[2], &rsv_time
[2]);
298 vnt_calculate_ofdm_rate(RATE_18M
, bb_type
, &tx_rate
[3], &rsv_time
[3]);
301 vnt_calculate_ofdm_rate(RATE_24M
, bb_type
, &tx_rate
[4], &rsv_time
[4]);
304 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_36M
),
305 bb_type
, &tx_rate
[5], &rsv_time
[5]);
308 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_48M
),
309 bb_type
, &tx_rate
[6], &rsv_time
[6]);
312 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_54M
),
313 bb_type
, &tx_rate
[7], &rsv_time
[7]);
316 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_54M
),
317 bb_type
, &tx_rate
[8], &rsv_time
[8]);
319 put_unaligned(phy
[0].len
, (u16
*)&data
[0]);
320 data
[2] = phy
[0].signal
;
321 data
[3] = phy
[0].service
;
323 put_unaligned(phy
[1].len
, (u16
*)&data
[4]);
324 data
[6] = phy
[1].signal
;
325 data
[7] = phy
[1].service
;
327 put_unaligned(phy
[2].len
, (u16
*)&data
[8]);
328 data
[10] = phy
[2].signal
;
329 data
[11] = phy
[2].service
;
331 put_unaligned(phy
[3].len
, (u16
*)&data
[12]);
332 data
[14] = phy
[3].signal
;
333 data
[15] = phy
[3].service
;
335 for (i
= 0; i
< 9; i
++) {
336 data
[16 + i
* 2] = tx_rate
[i
];
337 data
[16 + i
* 2 + 1] = rsv_time
[i
];
340 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
,
341 MAC_REG_RSPINF_B_1
, MESSAGE_REQUEST_MACREG
, 34, &data
[0]);
345 * Description: Update IFS
349 * priv - The adapter to be set
353 * Return Value: None.
356 void vnt_update_ifs(struct vnt_private
*priv
)
361 if (priv
->packet_type
== PK_TYPE_11A
) {
362 priv
->slot
= C_SLOT_SHORT
;
363 priv
->sifs
= C_SIFS_A
;
364 priv
->difs
= C_SIFS_A
+ 2 * C_SLOT_SHORT
;
366 } else if (priv
->packet_type
== PK_TYPE_11B
) {
367 priv
->slot
= C_SLOT_LONG
;
368 priv
->sifs
= C_SIFS_BG
;
369 priv
->difs
= C_SIFS_BG
+ 2 * C_SLOT_LONG
;
371 } else {/* PK_TYPE_11GA & PK_TYPE_11GB */
372 bool ofdm_rate
= false;
375 priv
->sifs
= C_SIFS_BG
;
377 if (priv
->short_slot_time
)
378 priv
->slot
= C_SLOT_SHORT
;
380 priv
->slot
= C_SLOT_LONG
;
382 priv
->difs
= C_SIFS_BG
+ 2 * priv
->slot
;
384 for (ii
= RATE_54M
; ii
>= RATE_6M
; ii
--) {
385 if (priv
->basic_rates
& ((u32
)(0x1 << ii
))) {
399 switch (priv
->rf_type
) {
401 if (priv
->bb_type
!= BB_TYPE_11B
) {
409 if (priv
->bb_type
!= BB_TYPE_11B
)
418 if (priv
->bb_type
== BB_TYPE_11A
) {
429 data
[0] = (u8
)priv
->sifs
;
430 data
[1] = (u8
)priv
->difs
;
431 data
[2] = (u8
)priv
->eifs
;
432 data
[3] = (u8
)priv
->slot
;
434 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
, MAC_REG_SIFS
,
435 MESSAGE_REQUEST_MACREG
, 4, &data
[0]);
439 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
, MAC_REG_CWMAXMIN0
,
440 MESSAGE_REQUEST_MACREG
, 1, &max_min
);
443 void vnt_update_top_rates(struct vnt_private
*priv
)
445 u8 top_ofdm
= RATE_24M
, top_cck
= RATE_1M
;
448 /*Determines the highest basic rate.*/
449 for (i
= RATE_54M
; i
>= RATE_6M
; i
--) {
450 if (priv
->basic_rates
& (u16
)(1 << i
)) {
456 priv
->top_ofdm_basic_rate
= top_ofdm
;
458 for (i
= RATE_11M
;; i
--) {
459 if (priv
->basic_rates
& (u16
)(1 << i
)) {
467 priv
->top_cck_basic_rate
= top_cck
;
470 int vnt_ofdm_min_rate(struct vnt_private
*priv
)
474 for (ii
= RATE_54M
; ii
>= RATE_6M
; ii
--) {
475 if ((priv
->basic_rates
) & ((u16
)BIT(ii
)))
482 u8
vnt_get_pkt_type(struct vnt_private
*priv
)
485 if (priv
->bb_type
== BB_TYPE_11A
|| priv
->bb_type
== BB_TYPE_11B
)
486 return (u8
)priv
->bb_type
;
487 else if (vnt_ofdm_min_rate(priv
))
493 * Description: Calculate TSF offset of two TSF input
494 * Get TSF Offset from RxBCN's TSF and local TSF
499 * tsf1 - Rx BCN's TSF
504 * Return Value: TSF Offset value
507 u64
vnt_get_tsf_offset(u8 rx_rate
, u64 tsf1
, u64 tsf2
)
512 rx_bcn_offset
= cwRXBCNTSFOff
[rx_rate
% MAX_RATE
];
514 tsf2
+= (u64
)rx_bcn_offset
;
516 tsf_offset
= tsf1
- tsf2
;
522 * Description: Sync. TSF counter to BSS
523 * Get TSF offset and write to HW
527 * priv - The adapter to be sync.
528 * time_stamp - Rx BCN's TSF
529 * local_tsf - Local TSF
536 void vnt_adjust_tsf(struct vnt_private
*priv
, u8 rx_rate
,
537 u64 time_stamp
, u64 local_tsf
)
542 tsf_offset
= vnt_get_tsf_offset(rx_rate
, time_stamp
, local_tsf
);
544 data
[0] = (u8
)tsf_offset
;
545 data
[1] = (u8
)(tsf_offset
>> 8);
546 data
[2] = (u8
)(tsf_offset
>> 16);
547 data
[3] = (u8
)(tsf_offset
>> 24);
548 data
[4] = (u8
)(tsf_offset
>> 32);
549 data
[5] = (u8
)(tsf_offset
>> 40);
550 data
[6] = (u8
)(tsf_offset
>> 48);
551 data
[7] = (u8
)(tsf_offset
>> 56);
553 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
554 MESSAGE_REQUEST_TSF
, 0, 8, data
);
557 * Description: Read NIC TSF counter
558 * Get local TSF counter
562 * priv - The adapter to be read
564 * current_tsf - Current TSF counter
566 * Return Value: true if success; otherwise false
569 bool vnt_get_current_tsf(struct vnt_private
*priv
, u64
*current_tsf
)
572 *current_tsf
= priv
->current_tsf
;
578 * Description: Clear NIC TSF counter
579 * Clear local TSF counter
583 * priv - The adapter to be read
585 * Return Value: true if success; otherwise false
588 bool vnt_clear_current_tsf(struct vnt_private
*priv
)
591 vnt_mac_reg_bits_on(priv
, MAC_REG_TFTCTL
, TFTCTL_TSFCNTRST
);
593 priv
->current_tsf
= 0;
599 * Description: Read NIC TSF counter
600 * Get NEXTTBTT from adjusted TSF and Beacon Interval
604 * tsf - Current TSF counter
605 * beacon_interval - Beacon Interval
607 * tsf - Current TSF counter
609 * Return Value: TSF value of next Beacon
612 u64
vnt_get_next_tbtt(u64 tsf
, u16 beacon_interval
)
616 beacon_int
= beacon_interval
* 1024;
619 * ((local_current_TSF / beacon_interval) + 1) * beacon_interval
622 do_div(tsf
, beacon_int
);
631 * Description: Set NIC TSF counter for first Beacon time
632 * Get NEXTTBTT from adjusted TSF and Beacon Interval
637 * beacon_interval - Beacon Interval
644 void vnt_reset_next_tbtt(struct vnt_private
*priv
, u16 beacon_interval
)
649 vnt_clear_current_tsf(priv
);
651 next_tbtt
= vnt_get_next_tbtt(next_tbtt
, beacon_interval
);
653 data
[0] = (u8
)next_tbtt
;
654 data
[1] = (u8
)(next_tbtt
>> 8);
655 data
[2] = (u8
)(next_tbtt
>> 16);
656 data
[3] = (u8
)(next_tbtt
>> 24);
657 data
[4] = (u8
)(next_tbtt
>> 32);
658 data
[5] = (u8
)(next_tbtt
>> 40);
659 data
[6] = (u8
)(next_tbtt
>> 48);
660 data
[7] = (u8
)(next_tbtt
>> 56);
662 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
663 MESSAGE_REQUEST_TBTT
, 0, 8, data
);
667 * Description: Sync NIC TSF counter for Beacon time
668 * Get NEXTTBTT and write to HW
672 * priv - The adapter to be set
673 * tsf - Current TSF counter
674 * beacon_interval - Beacon Interval
681 void vnt_update_next_tbtt(struct vnt_private
*priv
, u64 tsf
,
686 tsf
= vnt_get_next_tbtt(tsf
, beacon_interval
);
689 data
[1] = (u8
)(tsf
>> 8);
690 data
[2] = (u8
)(tsf
>> 16);
691 data
[3] = (u8
)(tsf
>> 24);
692 data
[4] = (u8
)(tsf
>> 32);
693 data
[5] = (u8
)(tsf
>> 40);
694 data
[6] = (u8
)(tsf
>> 48);
695 data
[7] = (u8
)(tsf
>> 56);
697 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
698 MESSAGE_REQUEST_TBTT
, 0, 8, data
);
700 dev_dbg(&priv
->usb
->dev
, "%s TBTT: %8llx\n", __func__
, tsf
);
704 * Description: Turn off Radio power
708 * priv - The adapter to be turned off
712 * Return Value: true if success; otherwise false
715 int vnt_radio_power_off(struct vnt_private
*priv
)
719 switch (priv
->rf_type
) {
726 vnt_mac_reg_bits_off(priv
, MAC_REG_SOFTPWRCTL
,
727 (SOFTPWRCTL_SWPE2
| SOFTPWRCTL_SWPE3
));
731 vnt_mac_reg_bits_off(priv
, MAC_REG_HOSTCR
, HOSTCR_RXON
);
733 vnt_set_deep_sleep(priv
);
735 vnt_mac_reg_bits_on(priv
, MAC_REG_GPIOCTL1
, GPIO3_INTMD
);
741 * Description: Turn on Radio power
745 * priv - The adapter to be turned on
749 * Return Value: true if success; otherwise false
752 int vnt_radio_power_on(struct vnt_private
*priv
)
756 vnt_exit_deep_sleep(priv
);
758 vnt_mac_reg_bits_on(priv
, MAC_REG_HOSTCR
, HOSTCR_RXON
);
760 switch (priv
->rf_type
) {
767 vnt_mac_reg_bits_on(priv
, MAC_REG_SOFTPWRCTL
,
768 (SOFTPWRCTL_SWPE2
| SOFTPWRCTL_SWPE3
));
772 vnt_mac_reg_bits_off(priv
, MAC_REG_GPIOCTL1
, GPIO3_INTMD
);
777 void vnt_set_bss_mode(struct vnt_private
*priv
)
779 if (priv
->rf_type
== RF_AIROHA7230
&& priv
->bb_type
== BB_TYPE_11A
)
780 vnt_mac_set_bb_type(priv
, BB_TYPE_11G
);
782 vnt_mac_set_bb_type(priv
, priv
->bb_type
);
784 priv
->packet_type
= vnt_get_pkt_type(priv
);
786 if (priv
->bb_type
== BB_TYPE_11A
)
787 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x03);
788 else if (priv
->bb_type
== BB_TYPE_11B
)
789 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x02);
790 else if (priv
->bb_type
== BB_TYPE_11G
)
791 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x08);
793 vnt_update_ifs(priv
);
794 vnt_set_rspinf(priv
, (u8
)priv
->bb_type
);
796 if (priv
->bb_type
== BB_TYPE_11A
) {
797 if (priv
->rf_type
== RF_AIROHA7230
) {
798 priv
->bb_vga
[0] = 0x20;
800 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
,
801 0xe7, priv
->bb_vga
[0]);
804 priv
->bb_vga
[2] = 0x10;
805 priv
->bb_vga
[3] = 0x10;
807 if (priv
->rf_type
== RF_AIROHA7230
) {
808 priv
->bb_vga
[0] = 0x1c;
810 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
,
811 0xe7, priv
->bb_vga
[0]);
814 priv
->bb_vga
[2] = 0x0;
815 priv
->bb_vga
[3] = 0x0;
818 vnt_set_vga_gain_offset(priv
, priv
->bb_vga
[0]);