signal: Document glibc's si_code of SI_ASYNCNL
[cris-mirror.git] / drivers / staging / vt6656 / card.c
blob4fd9cd64c6e8da2140eab7a555769e5fb8f433ee
1 /*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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.
16 * File: card.c
17 * Purpose: Provide functions to setup NIC operation mode
18 * Functions:
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
32 * Revision History:
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().
39 #include "device.h"
40 #include "card.h"
41 #include "baseband.h"
42 #include "mac.h"
43 #include "desc.h"
44 #include "rf.h"
45 #include "power.h"
46 #include "key.h"
47 #include "usbpipe.h"
49 /* const u16 cw_rxbcntsf_off[MAX_RATE] =
50 * {17, 34, 96, 192, 34, 23, 17, 11, 8, 5, 4, 3};
53 static const u16 cw_rxbcntsf_off[MAX_RATE] = {
54 192, 96, 34, 17, 34, 23, 17, 11, 8, 5, 4, 3
58 * Description: Set NIC media channel
60 * Parameters:
61 * In:
62 * pDevice - The adapter to be set
63 * connection_channel - Channel to be set
64 * Out:
65 * none
67 void vnt_set_channel(struct vnt_private *priv, u32 connection_channel)
69 if (connection_channel > CB_MAX_CHANNEL || !connection_channel)
70 return;
72 /* clear NAV */
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
88 * Parameters:
89 * In:
90 * priv - The adapter to be set
91 * rate_idx - Receiving data rate
92 * Out:
93 * none
95 * Return Value: response Control frame rate
98 static u16 vnt_get_cck_rate(struct vnt_private *priv, u16 rate_idx)
100 u16 ui = rate_idx;
102 while (ui > RATE_1M) {
103 if (priv->basic_rates & (1 << ui))
104 return ui;
105 ui--;
108 return RATE_1M;
112 * Description: Get OFDM mode basic rate
114 * Parameters:
115 * In:
116 * priv - The adapter to be set
117 * rate_idx - Receiving data rate
118 * Out:
119 * none
121 * Return Value: response Control frame rate
124 static u16 vnt_get_ofdm_rate(struct vnt_private *priv, u16 rate_idx)
126 u16 ui = 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",
133 __func__, rate_idx);
134 if (rate_idx > RATE_24M)
135 rate_idx = RATE_24M;
136 return rate_idx;
139 while (ui > RATE_11M) {
140 if (priv->basic_rates & (1 << ui)) {
141 dev_dbg(&priv->usb->dev, "%s rate: %d\n",
142 __func__, ui);
143 return ui;
145 ui--;
148 dev_dbg(&priv->usb->dev, "%s basic rate: 24M\n", __func__);
150 return RATE_24M;
154 * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
156 * Parameters:
157 * In:
158 * rate - Tx Rate
159 * bb_type - Tx Packet type
160 * Out:
161 * tx_rate - pointer to RSPINF TxRate field
162 * rsv_time- pointer to RSPINF RsvTime field
164 * Return Value: none
167 static void vnt_calculate_ofdm_rate(u16 rate, u8 bb_type,
168 u8 *tx_rate, u8 *rsv_time)
170 switch (rate) {
171 case RATE_6M:
172 if (bb_type == BB_TYPE_11A) {
173 *tx_rate = 0x9b;
174 *rsv_time = 24;
175 } else {
176 *tx_rate = 0x8b;
177 *rsv_time = 30;
179 break;
180 case RATE_9M:
181 if (bb_type == BB_TYPE_11A) {
182 *tx_rate = 0x9f;
183 *rsv_time = 16;
184 } else {
185 *tx_rate = 0x8f;
186 *rsv_time = 22;
188 break;
189 case RATE_12M:
190 if (bb_type == BB_TYPE_11A) {
191 *tx_rate = 0x9a;
192 *rsv_time = 12;
193 } else {
194 *tx_rate = 0x8a;
195 *rsv_time = 18;
197 break;
198 case RATE_18M:
199 if (bb_type == BB_TYPE_11A) {
200 *tx_rate = 0x9e;
201 *rsv_time = 8;
202 } else {
203 *tx_rate = 0x8e;
204 *rsv_time = 14;
206 break;
207 case RATE_36M:
208 if (bb_type == BB_TYPE_11A) {
209 *tx_rate = 0x9d;
210 *rsv_time = 4;
211 } else {
212 *tx_rate = 0x8d;
213 *rsv_time = 10;
215 break;
216 case RATE_48M:
217 if (bb_type == BB_TYPE_11A) {
218 *tx_rate = 0x98;
219 *rsv_time = 4;
220 } else {
221 *tx_rate = 0x88;
222 *rsv_time = 10;
224 break;
225 case RATE_54M:
226 if (bb_type == BB_TYPE_11A) {
227 *tx_rate = 0x9c;
228 *rsv_time = 4;
229 } else {
230 *tx_rate = 0x8c;
231 *rsv_time = 10;
233 break;
234 case RATE_24M:
235 default:
236 if (bb_type == BB_TYPE_11A) {
237 *tx_rate = 0x99;
238 *rsv_time = 8;
239 } else {
240 *tx_rate = 0x89;
241 *rsv_time = 14;
243 break;
248 * Description: Set RSPINF
250 * Parameters:
251 * In:
252 * pDevice - The adapter to be set
253 * Out:
254 * none
256 * Return Value: None.
260 void vnt_set_rspinf(struct vnt_private *priv, u8 bb_type)
262 struct vnt_phy_field phy[4];
263 u8 tx_rate[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; /* For OFDM */
264 u8 rsv_time[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
265 u8 data[34];
266 int i;
268 /*RSPINF_b_1*/
269 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_1M),
270 PK_TYPE_11B, &phy[0]);
272 /*RSPINF_b_2*/
273 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_2M),
274 PK_TYPE_11B, &phy[1]);
276 /*RSPINF_b_5*/
277 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_5M),
278 PK_TYPE_11B, &phy[2]);
280 /*RSPINF_b_11*/
281 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_11M),
282 PK_TYPE_11B, &phy[3]);
284 /*RSPINF_a_6*/
285 vnt_calculate_ofdm_rate(RATE_6M, bb_type, &tx_rate[0], &rsv_time[0]);
287 /*RSPINF_a_9*/
288 vnt_calculate_ofdm_rate(RATE_9M, bb_type, &tx_rate[1], &rsv_time[1]);
290 /*RSPINF_a_12*/
291 vnt_calculate_ofdm_rate(RATE_12M, bb_type, &tx_rate[2], &rsv_time[2]);
293 /*RSPINF_a_18*/
294 vnt_calculate_ofdm_rate(RATE_18M, bb_type, &tx_rate[3], &rsv_time[3]);
296 /*RSPINF_a_24*/
297 vnt_calculate_ofdm_rate(RATE_24M, bb_type, &tx_rate[4], &rsv_time[4]);
299 /*RSPINF_a_36*/
300 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_36M),
301 bb_type, &tx_rate[5], &rsv_time[5]);
303 /*RSPINF_a_48*/
304 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_48M),
305 bb_type, &tx_rate[6], &rsv_time[6]);
307 /*RSPINF_a_54*/
308 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_54M),
309 bb_type, &tx_rate[7], &rsv_time[7]);
311 /*RSPINF_a_72*/
312 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_54M),
313 bb_type, &tx_rate[8], &rsv_time[8]);
315 put_unaligned(phy[0].len, (u16 *)&data[0]);
316 data[2] = phy[0].signal;
317 data[3] = phy[0].service;
319 put_unaligned(phy[1].len, (u16 *)&data[4]);
320 data[6] = phy[1].signal;
321 data[7] = phy[1].service;
323 put_unaligned(phy[2].len, (u16 *)&data[8]);
324 data[10] = phy[2].signal;
325 data[11] = phy[2].service;
327 put_unaligned(phy[3].len, (u16 *)&data[12]);
328 data[14] = phy[3].signal;
329 data[15] = phy[3].service;
331 for (i = 0; i < 9; i++) {
332 data[16 + i * 2] = tx_rate[i];
333 data[16 + i * 2 + 1] = rsv_time[i];
336 vnt_control_out(priv, MESSAGE_TYPE_WRITE, MAC_REG_RSPINF_B_1,
337 MESSAGE_REQUEST_MACREG, 34, &data[0]);
341 * Description: Update IFS
343 * Parameters:
344 * In:
345 * priv - The adapter to be set
346 * Out:
347 * none
349 * Return Value: None.
352 void vnt_update_ifs(struct vnt_private *priv)
354 u8 max_min = 0;
355 u8 data[4];
357 if (priv->packet_type == PK_TYPE_11A) {
358 priv->slot = C_SLOT_SHORT;
359 priv->sifs = C_SIFS_A;
360 priv->difs = C_SIFS_A + 2 * C_SLOT_SHORT;
361 max_min = 4;
362 } else {
363 priv->sifs = C_SIFS_BG;
365 if (priv->short_slot_time) {
366 priv->slot = C_SLOT_SHORT;
367 max_min = 4;
368 } else {
369 priv->slot = C_SLOT_LONG;
370 max_min = 5;
373 priv->difs = C_SIFS_BG + 2 * priv->slot;
376 priv->eifs = C_EIFS;
378 switch (priv->rf_type) {
379 case RF_VT3226D0:
380 if (priv->bb_type != BB_TYPE_11B) {
381 priv->sifs -= 1;
382 priv->difs -= 1;
383 break;
385 /* fall through */
386 case RF_AIROHA7230:
387 case RF_AL2230:
388 case RF_AL2230S:
389 if (priv->bb_type != BB_TYPE_11B)
390 break;
391 /* fall through */
392 case RF_RFMD2959:
393 case RF_VT3226:
394 case RF_VT3342A0:
395 priv->sifs -= 3;
396 priv->difs -= 3;
397 break;
398 case RF_MAXIM2829:
399 if (priv->bb_type == BB_TYPE_11A) {
400 priv->sifs -= 5;
401 priv->difs -= 5;
402 } else {
403 priv->sifs -= 2;
404 priv->difs -= 2;
407 break;
410 data[0] = (u8)priv->sifs;
411 data[1] = (u8)priv->difs;
412 data[2] = (u8)priv->eifs;
413 data[3] = (u8)priv->slot;
415 vnt_control_out(priv, MESSAGE_TYPE_WRITE, MAC_REG_SIFS,
416 MESSAGE_REQUEST_MACREG, 4, &data[0]);
418 max_min |= 0xa0;
420 vnt_control_out(priv, MESSAGE_TYPE_WRITE, MAC_REG_CWMAXMIN0,
421 MESSAGE_REQUEST_MACREG, 1, &max_min);
424 void vnt_update_top_rates(struct vnt_private *priv)
426 u8 top_ofdm = RATE_24M, top_cck = RATE_1M;
427 u8 i;
429 /*Determines the highest basic rate.*/
430 for (i = RATE_54M; i >= RATE_6M; i--) {
431 if (priv->basic_rates & (u16)(1 << i)) {
432 top_ofdm = i;
433 break;
437 priv->top_ofdm_basic_rate = top_ofdm;
439 for (i = RATE_11M;; i--) {
440 if (priv->basic_rates & (u16)(1 << i)) {
441 top_cck = i;
442 break;
444 if (i == RATE_1M)
445 break;
448 priv->top_cck_basic_rate = top_cck;
451 int vnt_ofdm_min_rate(struct vnt_private *priv)
453 int ii;
455 for (ii = RATE_54M; ii >= RATE_6M; ii--) {
456 if ((priv->basic_rates) & ((u16)BIT(ii)))
457 return true;
460 return false;
463 u8 vnt_get_pkt_type(struct vnt_private *priv)
465 if (priv->bb_type == BB_TYPE_11A || priv->bb_type == BB_TYPE_11B)
466 return (u8)priv->bb_type;
467 else if (vnt_ofdm_min_rate(priv))
468 return PK_TYPE_11GA;
469 return PK_TYPE_11GB;
473 * Description: Calculate TSF offset of two TSF input
474 * Get TSF Offset from RxBCN's TSF and local TSF
476 * Parameters:
477 * In:
478 * rx_rate - rx rate.
479 * tsf1 - Rx BCN's TSF
480 * tsf2 - Local TSF
481 * Out:
482 * none
484 * Return Value: TSF Offset value
487 u64 vnt_get_tsf_offset(u8 rx_rate, u64 tsf1, u64 tsf2)
489 return tsf1 - tsf2 - (u64)cw_rxbcntsf_off[rx_rate % MAX_RATE];
493 * Description: Sync. TSF counter to BSS
494 * Get TSF offset and write to HW
496 * Parameters:
497 * In:
498 * priv - The adapter to be sync.
499 * time_stamp - Rx BCN's TSF
500 * local_tsf - Local TSF
501 * Out:
502 * none
504 * Return Value: none
507 void vnt_adjust_tsf(struct vnt_private *priv, u8 rx_rate,
508 u64 time_stamp, u64 local_tsf)
510 u64 tsf_offset = 0;
511 u8 data[8];
513 tsf_offset = vnt_get_tsf_offset(rx_rate, time_stamp, local_tsf);
515 data[0] = (u8)tsf_offset;
516 data[1] = (u8)(tsf_offset >> 8);
517 data[2] = (u8)(tsf_offset >> 16);
518 data[3] = (u8)(tsf_offset >> 24);
519 data[4] = (u8)(tsf_offset >> 32);
520 data[5] = (u8)(tsf_offset >> 40);
521 data[6] = (u8)(tsf_offset >> 48);
522 data[7] = (u8)(tsf_offset >> 56);
524 vnt_control_out(priv, MESSAGE_TYPE_SET_TSFTBTT,
525 MESSAGE_REQUEST_TSF, 0, 8, data);
529 * Description: Read NIC TSF counter
530 * Get local TSF counter
532 * Parameters:
533 * In:
534 * priv - The adapter to be read
535 * Out:
536 * current_tsf - Current TSF counter
538 * Return Value: true if success; otherwise false
541 bool vnt_get_current_tsf(struct vnt_private *priv, u64 *current_tsf)
543 *current_tsf = priv->current_tsf;
545 return true;
549 * Description: Clear NIC TSF counter
550 * Clear local TSF counter
552 * Parameters:
553 * In:
554 * priv - The adapter to be read
556 * Return Value: true if success; otherwise false
559 bool vnt_clear_current_tsf(struct vnt_private *priv)
561 vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
563 priv->current_tsf = 0;
565 return true;
569 * Description: Read NIC TSF counter
570 * Get NEXTTBTT from adjusted TSF and Beacon Interval
572 * Parameters:
573 * In:
574 * tsf - Current TSF counter
575 * beacon_interval - Beacon Interval
576 * Out:
577 * tsf - Current TSF counter
579 * Return Value: TSF value of next Beacon
582 u64 vnt_get_next_tbtt(u64 tsf, u16 beacon_interval)
584 u32 beacon_int;
586 beacon_int = beacon_interval * 1024;
588 /* Next TBTT =
589 * ((local_current_TSF / beacon_interval) + 1) * beacon_interval
591 if (beacon_int) {
592 do_div(tsf, beacon_int);
593 tsf += 1;
594 tsf *= beacon_int;
597 return tsf;
601 * Description: Set NIC TSF counter for first Beacon time
602 * Get NEXTTBTT from adjusted TSF and Beacon Interval
604 * Parameters:
605 * In:
606 * dwIoBase - IO Base
607 * beacon_interval - Beacon Interval
608 * Out:
609 * none
611 * Return Value: none
614 void vnt_reset_next_tbtt(struct vnt_private *priv, u16 beacon_interval)
616 u64 next_tbtt = 0;
617 u8 data[8];
619 vnt_clear_current_tsf(priv);
621 next_tbtt = vnt_get_next_tbtt(next_tbtt, beacon_interval);
623 data[0] = (u8)next_tbtt;
624 data[1] = (u8)(next_tbtt >> 8);
625 data[2] = (u8)(next_tbtt >> 16);
626 data[3] = (u8)(next_tbtt >> 24);
627 data[4] = (u8)(next_tbtt >> 32);
628 data[5] = (u8)(next_tbtt >> 40);
629 data[6] = (u8)(next_tbtt >> 48);
630 data[7] = (u8)(next_tbtt >> 56);
632 vnt_control_out(priv, MESSAGE_TYPE_SET_TSFTBTT,
633 MESSAGE_REQUEST_TBTT, 0, 8, data);
637 * Description: Sync NIC TSF counter for Beacon time
638 * Get NEXTTBTT and write to HW
640 * Parameters:
641 * In:
642 * priv - The adapter to be set
643 * tsf - Current TSF counter
644 * beacon_interval - Beacon Interval
645 * Out:
646 * none
648 * Return Value: none
651 void vnt_update_next_tbtt(struct vnt_private *priv, u64 tsf,
652 u16 beacon_interval)
654 u8 data[8];
656 tsf = vnt_get_next_tbtt(tsf, beacon_interval);
658 data[0] = (u8)tsf;
659 data[1] = (u8)(tsf >> 8);
660 data[2] = (u8)(tsf >> 16);
661 data[3] = (u8)(tsf >> 24);
662 data[4] = (u8)(tsf >> 32);
663 data[5] = (u8)(tsf >> 40);
664 data[6] = (u8)(tsf >> 48);
665 data[7] = (u8)(tsf >> 56);
667 vnt_control_out(priv, MESSAGE_TYPE_SET_TSFTBTT,
668 MESSAGE_REQUEST_TBTT, 0, 8, data);
670 dev_dbg(&priv->usb->dev, "%s TBTT: %8llx\n", __func__, tsf);
674 * Description: Turn off Radio power
676 * Parameters:
677 * In:
678 * priv - The adapter to be turned off
679 * Out:
680 * none
682 * Return Value: true if success; otherwise false
685 int vnt_radio_power_off(struct vnt_private *priv)
687 int ret = true;
689 switch (priv->rf_type) {
690 case RF_AL2230:
691 case RF_AL2230S:
692 case RF_AIROHA7230:
693 case RF_VT3226:
694 case RF_VT3226D0:
695 case RF_VT3342A0:
696 vnt_mac_reg_bits_off(priv, MAC_REG_SOFTPWRCTL,
697 (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
698 break;
701 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_RXON);
703 vnt_set_deep_sleep(priv);
705 vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL1, GPIO3_INTMD);
707 return ret;
711 * Description: Turn on Radio power
713 * Parameters:
714 * In:
715 * priv - The adapter to be turned on
716 * Out:
717 * none
719 * Return Value: true if success; otherwise false
722 int vnt_radio_power_on(struct vnt_private *priv)
724 int ret = true;
726 vnt_exit_deep_sleep(priv);
728 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_RXON);
730 switch (priv->rf_type) {
731 case RF_AL2230:
732 case RF_AL2230S:
733 case RF_AIROHA7230:
734 case RF_VT3226:
735 case RF_VT3226D0:
736 case RF_VT3342A0:
737 vnt_mac_reg_bits_on(priv, MAC_REG_SOFTPWRCTL,
738 (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
739 break;
742 vnt_mac_reg_bits_off(priv, MAC_REG_GPIOCTL1, GPIO3_INTMD);
744 return ret;
747 void vnt_set_bss_mode(struct vnt_private *priv)
749 if (priv->rf_type == RF_AIROHA7230 && priv->bb_type == BB_TYPE_11A)
750 vnt_mac_set_bb_type(priv, BB_TYPE_11G);
751 else
752 vnt_mac_set_bb_type(priv, priv->bb_type);
754 priv->packet_type = vnt_get_pkt_type(priv);
756 if (priv->bb_type == BB_TYPE_11A)
757 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG, 0x88, 0x03);
758 else if (priv->bb_type == BB_TYPE_11B)
759 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG, 0x88, 0x02);
760 else if (priv->bb_type == BB_TYPE_11G)
761 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG, 0x88, 0x08);
763 vnt_update_ifs(priv);
764 vnt_set_rspinf(priv, (u8)priv->bb_type);
766 if (priv->bb_type == BB_TYPE_11A) {
767 if (priv->rf_type == RF_AIROHA7230) {
768 priv->bb_vga[0] = 0x20;
770 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG,
771 0xe7, priv->bb_vga[0]);
774 priv->bb_vga[2] = 0x10;
775 priv->bb_vga[3] = 0x10;
776 } else {
777 if (priv->rf_type == RF_AIROHA7230) {
778 priv->bb_vga[0] = 0x1c;
780 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG,
781 0xe7, priv->bb_vga[0]);
784 priv->bb_vga[2] = 0x0;
785 priv->bb_vga[3] = 0x0;
788 vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);