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.
17 * Purpose: Provide functions to setup NIC operation mode
19 * vnt_set_rspinf - Set RSPINF
20 * vnt_update_ifs - Update slotTime,SIFS,DIFS, and EIFS
21 * vnt_update_top_rates - Update BasicTopRate
22 * vnt_add_basic_rate - Add to BasicRateSet
23 * vnt_ofdm_min_rate - Check if any OFDM rate is in BasicRateSet
24 * vnt_get_tsf_offset - Calculate TSFOffset
25 * vnt_get_current_tsf - Read Current NIC TSF counter
26 * vnt_get_next_tbtt - Calculate Next Beacon TSF counter
27 * vnt_reset_next_tbtt - Set NIC Beacon time
28 * vnt_update_next_tbtt - Sync. NIC Beacon time
29 * vnt_radio_power_off - Turn Off NIC Radio Power
30 * vnt_radio_power_on - Turn On NIC Radio Power
33 * 06-10-2003 Bryan YC Fan: Re-write codes to support VT3253 spec.
34 * 08-26-2003 Kyle Hsu: Modify the definition type of dwIoBase.
35 * 09-01-2003 Bryan YC Fan: Add vnt_update_ifs().
49 /* const u16 cwRXBCNTSFOff[MAX_RATE] =
50 {17, 34, 96, 192, 34, 23, 17, 11, 8, 5, 4, 3}; */
52 static const u16 cwRXBCNTSFOff
[MAX_RATE
] = {
53 192, 96, 34, 17, 34, 23, 17, 11, 8, 5, 4, 3
57 * Description: Set NIC media channel
61 * pDevice - The adapter to be set
62 * connection_channel - Channel to be set
66 void vnt_set_channel(struct vnt_private
*priv
, u32 connection_channel
)
69 if (connection_channel
> CB_MAX_CHANNEL
|| !connection_channel
)
73 vnt_mac_reg_bits_on(priv
, MAC_REG_MACCR
, MACCR_CLRNAV
);
75 /* Set Channel[7] = 0 to tell H/W channel is changing now. */
76 vnt_mac_reg_bits_off(priv
, MAC_REG_CHANNEL
, 0xb0);
78 vnt_control_out(priv
, MESSAGE_TYPE_SELECT_CHANNEL
,
79 connection_channel
, 0, 0, NULL
);
81 vnt_control_out_u8(priv
, MESSAGE_REQUEST_MACREG
, MAC_REG_CHANNEL
,
82 (u8
)(connection_channel
| 0x80));
86 * Description: Get CCK mode basic rate
90 * priv - The adapter to be set
91 * rate_idx - Receiving data rate
95 * Return Value: response Control frame rate
98 static u16
vnt_get_cck_rate(struct vnt_private
*priv
, u16 rate_idx
)
102 while (ui
> RATE_1M
) {
103 if (priv
->basic_rates
& (1 << ui
))
112 * Description: Get OFDM mode basic rate
116 * priv - The adapter to be set
117 * rate_idx - Receiving data rate
121 * Return Value: response Control frame rate
124 static u16
vnt_get_ofdm_rate(struct vnt_private
*priv
, u16 rate_idx
)
128 dev_dbg(&priv
->usb
->dev
, "%s basic rate: %d\n",
129 __func__
, priv
->basic_rates
);
131 if (!vnt_ofdm_min_rate(priv
)) {
132 dev_dbg(&priv
->usb
->dev
, "%s (NO OFDM) %d\n",
134 if (rate_idx
> RATE_24M
)
139 while (ui
> RATE_11M
) {
140 if (priv
->basic_rates
& (1 << ui
)) {
141 dev_dbg(&priv
->usb
->dev
, "%s rate: %d\n",
148 dev_dbg(&priv
->usb
->dev
, "%s basic rate: 24M\n", __func__
);
154 * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
159 * bb_type - Tx Packet type
161 * tx_rate - pointer to RSPINF TxRate field
162 * rsv_time- pointer to RSPINF RsvTime field
167 static void vnt_calculate_ofdm_rate(u16 rate
, u8 bb_type
,
168 u8
*tx_rate
, u8
*rsv_time
)
173 if (bb_type
== BB_TYPE_11A
) {
182 if (bb_type
== BB_TYPE_11A
) {
191 if (bb_type
== BB_TYPE_11A
) {
200 if (bb_type
== BB_TYPE_11A
) {
209 if (bb_type
== BB_TYPE_11A
) {
218 if (bb_type
== BB_TYPE_11A
) {
227 if (bb_type
== BB_TYPE_11A
) {
237 if (bb_type
== BB_TYPE_11A
) {
249 * Description: Set RSPINF
253 * pDevice - The adapter to be set
257 * Return Value: None.
261 void vnt_set_rspinf(struct vnt_private
*priv
, u8 bb_type
)
263 struct vnt_phy_field phy
[4];
264 u8 tx_rate
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; /* For OFDM */
265 u8 rsv_time
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
270 vnt_get_phy_field(priv
, 14,
271 vnt_get_cck_rate(priv
, RATE_1M
), PK_TYPE_11B
, &phy
[0]);
274 vnt_get_phy_field(priv
, 14,
275 vnt_get_cck_rate(priv
, RATE_2M
), PK_TYPE_11B
, &phy
[1]);
278 vnt_get_phy_field(priv
, 14,
279 vnt_get_cck_rate(priv
, RATE_5M
), PK_TYPE_11B
, &phy
[2]);
282 vnt_get_phy_field(priv
, 14,
283 vnt_get_cck_rate(priv
, RATE_11M
), PK_TYPE_11B
, &phy
[3]);
286 vnt_calculate_ofdm_rate(RATE_6M
, bb_type
, &tx_rate
[0], &rsv_time
[0]);
289 vnt_calculate_ofdm_rate(RATE_9M
, bb_type
, &tx_rate
[1], &rsv_time
[1]);
292 vnt_calculate_ofdm_rate(RATE_12M
, bb_type
, &tx_rate
[2], &rsv_time
[2]);
295 vnt_calculate_ofdm_rate(RATE_18M
, bb_type
, &tx_rate
[3], &rsv_time
[3]);
298 vnt_calculate_ofdm_rate(RATE_24M
, bb_type
, &tx_rate
[4], &rsv_time
[4]);
301 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_36M
),
302 bb_type
, &tx_rate
[5], &rsv_time
[5]);
305 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_48M
),
306 bb_type
, &tx_rate
[6], &rsv_time
[6]);
309 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_54M
),
310 bb_type
, &tx_rate
[7], &rsv_time
[7]);
313 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_54M
),
314 bb_type
, &tx_rate
[8], &rsv_time
[8]);
316 put_unaligned(phy
[0].len
, (u16
*)&data
[0]);
317 data
[2] = phy
[0].signal
;
318 data
[3] = phy
[0].service
;
320 put_unaligned(phy
[1].len
, (u16
*)&data
[4]);
321 data
[6] = phy
[1].signal
;
322 data
[7] = phy
[1].service
;
324 put_unaligned(phy
[2].len
, (u16
*)&data
[8]);
325 data
[10] = phy
[2].signal
;
326 data
[11] = phy
[2].service
;
328 put_unaligned(phy
[3].len
, (u16
*)&data
[12]);
329 data
[14] = phy
[3].signal
;
330 data
[15] = phy
[3].service
;
332 for (i
= 0; i
< 9; i
++) {
333 data
[16 + i
* 2] = tx_rate
[i
];
334 data
[16 + i
* 2 + 1] = rsv_time
[i
];
337 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
,
338 MAC_REG_RSPINF_B_1
, MESSAGE_REQUEST_MACREG
, 34, &data
[0]);
342 * Description: Update IFS
346 * priv - The adapter to be set
350 * Return Value: None.
353 void vnt_update_ifs(struct vnt_private
*priv
)
358 if (priv
->packet_type
== PK_TYPE_11A
) {
359 priv
->slot
= C_SLOT_SHORT
;
360 priv
->sifs
= C_SIFS_A
;
361 priv
->difs
= C_SIFS_A
+ 2 * C_SLOT_SHORT
;
363 } else if (priv
->packet_type
== PK_TYPE_11B
) {
364 priv
->slot
= C_SLOT_LONG
;
365 priv
->sifs
= C_SIFS_BG
;
366 priv
->difs
= C_SIFS_BG
+ 2 * C_SLOT_LONG
;
368 } else {/* PK_TYPE_11GA & PK_TYPE_11GB */
369 bool ofdm_rate
= false;
372 priv
->sifs
= C_SIFS_BG
;
374 if (priv
->short_slot_time
)
375 priv
->slot
= C_SLOT_SHORT
;
377 priv
->slot
= C_SLOT_LONG
;
379 priv
->difs
= C_SIFS_BG
+ 2 * priv
->slot
;
381 for (ii
= RATE_54M
; ii
>= RATE_6M
; ii
--) {
382 if (priv
->basic_rates
& ((u32
)(0x1 << ii
))) {
396 switch (priv
->rf_type
) {
398 if (priv
->bb_type
!= BB_TYPE_11B
) {
406 if (priv
->bb_type
!= BB_TYPE_11B
)
415 if (priv
->bb_type
== BB_TYPE_11A
) {
426 data
[0] = (u8
)priv
->sifs
;
427 data
[1] = (u8
)priv
->difs
;
428 data
[2] = (u8
)priv
->eifs
;
429 data
[3] = (u8
)priv
->slot
;
431 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
, MAC_REG_SIFS
,
432 MESSAGE_REQUEST_MACREG
, 4, &data
[0]);
436 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
, MAC_REG_CWMAXMIN0
,
437 MESSAGE_REQUEST_MACREG
, 1, &max_min
);
440 void vnt_update_top_rates(struct vnt_private
*priv
)
442 u8 top_ofdm
= RATE_24M
, top_cck
= RATE_1M
;
445 /*Determines the highest basic rate.*/
446 for (i
= RATE_54M
; i
>= RATE_6M
; i
--) {
447 if (priv
->basic_rates
& (u16
)(1 << i
)) {
453 priv
->top_ofdm_basic_rate
= top_ofdm
;
455 for (i
= RATE_11M
;; i
--) {
456 if (priv
->basic_rates
& (u16
)(1 << i
)) {
464 priv
->top_cck_basic_rate
= top_cck
;
467 int vnt_ofdm_min_rate(struct vnt_private
*priv
)
471 for (ii
= RATE_54M
; ii
>= RATE_6M
; ii
--) {
472 if ((priv
->basic_rates
) & ((u16
)BIT(ii
)))
479 u8
vnt_get_pkt_type(struct vnt_private
*priv
)
482 if (priv
->bb_type
== BB_TYPE_11A
|| priv
->bb_type
== BB_TYPE_11B
)
483 return (u8
)priv
->bb_type
;
484 else if (vnt_ofdm_min_rate(priv
))
490 * Description: Calculate TSF offset of two TSF input
491 * Get TSF Offset from RxBCN's TSF and local TSF
496 * tsf1 - Rx BCN's TSF
501 * Return Value: TSF Offset value
504 u64
vnt_get_tsf_offset(u8 rx_rate
, u64 tsf1
, u64 tsf2
)
509 rx_bcn_offset
= cwRXBCNTSFOff
[rx_rate
% MAX_RATE
];
511 tsf2
+= (u64
)rx_bcn_offset
;
513 tsf_offset
= tsf1
- tsf2
;
519 * Description: Sync. TSF counter to BSS
520 * Get TSF offset and write to HW
524 * priv - The adapter to be sync.
525 * time_stamp - Rx BCN's TSF
526 * local_tsf - Local TSF
533 void vnt_adjust_tsf(struct vnt_private
*priv
, u8 rx_rate
,
534 u64 time_stamp
, u64 local_tsf
)
539 tsf_offset
= vnt_get_tsf_offset(rx_rate
, time_stamp
, local_tsf
);
541 data
[0] = (u8
)tsf_offset
;
542 data
[1] = (u8
)(tsf_offset
>> 8);
543 data
[2] = (u8
)(tsf_offset
>> 16);
544 data
[3] = (u8
)(tsf_offset
>> 24);
545 data
[4] = (u8
)(tsf_offset
>> 32);
546 data
[5] = (u8
)(tsf_offset
>> 40);
547 data
[6] = (u8
)(tsf_offset
>> 48);
548 data
[7] = (u8
)(tsf_offset
>> 56);
550 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
551 MESSAGE_REQUEST_TSF
, 0, 8, data
);
554 * Description: Read NIC TSF counter
555 * Get local TSF counter
559 * priv - The adapter to be read
561 * current_tsf - Current TSF counter
563 * Return Value: true if success; otherwise false
566 bool vnt_get_current_tsf(struct vnt_private
*priv
, u64
*current_tsf
)
569 *current_tsf
= priv
->current_tsf
;
575 * Description: Clear NIC TSF counter
576 * Clear local TSF counter
580 * priv - The adapter to be read
582 * Return Value: true if success; otherwise false
585 bool vnt_clear_current_tsf(struct vnt_private
*priv
)
588 vnt_mac_reg_bits_on(priv
, MAC_REG_TFTCTL
, TFTCTL_TSFCNTRST
);
590 priv
->current_tsf
= 0;
596 * Description: Read NIC TSF counter
597 * Get NEXTTBTT from adjusted TSF and Beacon Interval
601 * tsf - Current TSF counter
602 * beacon_interval - Beacon Interval
604 * tsf - Current TSF counter
606 * Return Value: TSF value of next Beacon
609 u64
vnt_get_next_tbtt(u64 tsf
, u16 beacon_interval
)
613 beacon_int
= beacon_interval
* 1024;
616 * ((local_current_TSF / beacon_interval) + 1) * beacon_interval
619 do_div(tsf
, beacon_int
);
628 * Description: Set NIC TSF counter for first Beacon time
629 * Get NEXTTBTT from adjusted TSF and Beacon Interval
634 * beacon_interval - Beacon Interval
641 void vnt_reset_next_tbtt(struct vnt_private
*priv
, u16 beacon_interval
)
646 vnt_clear_current_tsf(priv
);
648 next_tbtt
= vnt_get_next_tbtt(next_tbtt
, beacon_interval
);
650 data
[0] = (u8
)next_tbtt
;
651 data
[1] = (u8
)(next_tbtt
>> 8);
652 data
[2] = (u8
)(next_tbtt
>> 16);
653 data
[3] = (u8
)(next_tbtt
>> 24);
654 data
[4] = (u8
)(next_tbtt
>> 32);
655 data
[5] = (u8
)(next_tbtt
>> 40);
656 data
[6] = (u8
)(next_tbtt
>> 48);
657 data
[7] = (u8
)(next_tbtt
>> 56);
659 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
660 MESSAGE_REQUEST_TBTT
, 0, 8, data
);
664 * Description: Sync NIC TSF counter for Beacon time
665 * Get NEXTTBTT and write to HW
669 * priv - The adapter to be set
670 * tsf - Current TSF counter
671 * beacon_interval - Beacon Interval
678 void vnt_update_next_tbtt(struct vnt_private
*priv
, u64 tsf
,
683 tsf
= vnt_get_next_tbtt(tsf
, beacon_interval
);
686 data
[1] = (u8
)(tsf
>> 8);
687 data
[2] = (u8
)(tsf
>> 16);
688 data
[3] = (u8
)(tsf
>> 24);
689 data
[4] = (u8
)(tsf
>> 32);
690 data
[5] = (u8
)(tsf
>> 40);
691 data
[6] = (u8
)(tsf
>> 48);
692 data
[7] = (u8
)(tsf
>> 56);
694 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
695 MESSAGE_REQUEST_TBTT
, 0, 8, data
);
697 dev_dbg(&priv
->usb
->dev
, "%s TBTT: %8llx\n", __func__
, tsf
);
701 * Description: Turn off Radio power
705 * priv - The adapter to be turned off
709 * Return Value: true if success; otherwise false
712 int vnt_radio_power_off(struct vnt_private
*priv
)
716 switch (priv
->rf_type
) {
723 vnt_mac_reg_bits_off(priv
, MAC_REG_SOFTPWRCTL
,
724 (SOFTPWRCTL_SWPE2
| SOFTPWRCTL_SWPE3
));
728 vnt_mac_reg_bits_off(priv
, MAC_REG_HOSTCR
, HOSTCR_RXON
);
730 vnt_set_deep_sleep(priv
);
732 vnt_mac_reg_bits_on(priv
, MAC_REG_GPIOCTL1
, GPIO3_INTMD
);
738 * Description: Turn on Radio power
742 * priv - The adapter to be turned on
746 * Return Value: true if success; otherwise false
749 int vnt_radio_power_on(struct vnt_private
*priv
)
753 vnt_exit_deep_sleep(priv
);
755 vnt_mac_reg_bits_on(priv
, MAC_REG_HOSTCR
, HOSTCR_RXON
);
757 switch (priv
->rf_type
) {
764 vnt_mac_reg_bits_on(priv
, MAC_REG_SOFTPWRCTL
,
765 (SOFTPWRCTL_SWPE2
| SOFTPWRCTL_SWPE3
));
769 vnt_mac_reg_bits_off(priv
, MAC_REG_GPIOCTL1
, GPIO3_INTMD
);
774 void vnt_set_bss_mode(struct vnt_private
*priv
)
776 if (priv
->rf_type
== RF_AIROHA7230
&& priv
->bb_type
== BB_TYPE_11A
)
777 vnt_mac_set_bb_type(priv
, BB_TYPE_11G
);
779 vnt_mac_set_bb_type(priv
, priv
->bb_type
);
781 priv
->packet_type
= vnt_get_pkt_type(priv
);
783 if (priv
->bb_type
== BB_TYPE_11A
)
784 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x03);
785 else if (priv
->bb_type
== BB_TYPE_11B
)
786 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x02);
787 else if (priv
->bb_type
== BB_TYPE_11G
)
788 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x08);
790 vnt_update_ifs(priv
);
791 vnt_set_rspinf(priv
, (u8
)priv
->bb_type
);
793 if (priv
->bb_type
== BB_TYPE_11A
) {
794 if (priv
->rf_type
== RF_AIROHA7230
) {
795 priv
->bb_vga
[0] = 0x20;
797 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
,
798 0xe7, priv
->bb_vga
[0]);
801 priv
->bb_vga
[2] = 0x10;
802 priv
->bb_vga
[3] = 0x10;
804 if (priv
->rf_type
== RF_AIROHA7230
) {
805 priv
->bb_vga
[0] = 0x1c;
807 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
,
808 0xe7, priv
->bb_vga
[0]);
811 priv
->bb_vga
[2] = 0x0;
812 priv
->bb_vga
[3] = 0x0;
815 vnt_set_vga_gain_offset(priv
, priv
->bb_vga
[0]);