1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4  * All rights reserved.
5  *
6  * File: int.c
7  *
8  * Purpose: Handle USB interrupt endpoint
9  *
10  * Author: Jerry Chen
11  *
12  * Date: Apr. 2, 2004
13  *
14  * Functions:
15  *
16  * Revision History:
17  *      04-02-2004 Jerry Chen:  Initial release
18  *
19  */
20 
21 #include "int.h"
22 #include "mac.h"
23 #include "power.h"
24 #include "usbpipe.h"
25 
26 static const u8 fallback_rate0[5][5] = {
27 	{RATE_18M, RATE_18M, RATE_12M, RATE_12M, RATE_12M},
28 	{RATE_24M, RATE_24M, RATE_18M, RATE_12M, RATE_12M},
29 	{RATE_36M, RATE_36M, RATE_24M, RATE_18M, RATE_18M},
30 	{RATE_48M, RATE_48M, RATE_36M, RATE_24M, RATE_24M},
31 	{RATE_54M, RATE_54M, RATE_48M, RATE_36M, RATE_36M}
32 };
33 
34 static const u8 fallback_rate1[5][5] = {
35 	{RATE_18M, RATE_18M, RATE_12M, RATE_6M, RATE_6M},
36 	{RATE_24M, RATE_24M, RATE_18M, RATE_6M, RATE_6M},
37 	{RATE_36M, RATE_36M, RATE_24M, RATE_12M, RATE_12M},
38 	{RATE_48M, RATE_48M, RATE_24M, RATE_12M, RATE_12M},
39 	{RATE_54M, RATE_54M, RATE_36M, RATE_18M, RATE_18M}
40 };
41 
vnt_int_start_interrupt(struct vnt_private * priv)42 int vnt_int_start_interrupt(struct vnt_private *priv)
43 {
44 	int ret = 0;
45 	unsigned long flags;
46 
47 	dev_dbg(&priv->usb->dev, "---->Interrupt Polling Thread\n");
48 
49 	spin_lock_irqsave(&priv->lock, flags);
50 
51 	ret = vnt_start_interrupt_urb(priv);
52 
53 	spin_unlock_irqrestore(&priv->lock, flags);
54 
55 	return ret;
56 }
57 
vnt_int_report_rate(struct vnt_private * priv,u8 pkt_no,u8 tsr)58 static int vnt_int_report_rate(struct vnt_private *priv, u8 pkt_no, u8 tsr)
59 {
60 	struct vnt_usb_send_context *context;
61 	struct ieee80211_tx_info *info;
62 	struct ieee80211_rate *rate;
63 	u8 tx_retry = (tsr & 0xf0) >> 4;
64 	s8 idx;
65 
66 	if (pkt_no >= priv->num_tx_context)
67 		return -EINVAL;
68 
69 	context = priv->tx_context[pkt_no];
70 
71 	if (!context->skb)
72 		return -EINVAL;
73 
74 	info = IEEE80211_SKB_CB(context->skb);
75 	idx = info->control.rates[0].idx;
76 
77 	if (context->fb_option && !(tsr & (TSR_TMO | TSR_RETRYTMO))) {
78 		u8 tx_rate;
79 		u8 retry = tx_retry;
80 
81 		rate = ieee80211_get_tx_rate(priv->hw, info);
82 		tx_rate = rate->hw_value - RATE_18M;
83 
84 		if (retry > 4)
85 			retry = 4;
86 
87 		if (context->fb_option == AUTO_FB_0)
88 			tx_rate = fallback_rate0[tx_rate][retry];
89 		else if (context->fb_option == AUTO_FB_1)
90 			tx_rate = fallback_rate1[tx_rate][retry];
91 
92 		if (info->band == NL80211_BAND_5GHZ)
93 			idx = tx_rate - RATE_6M;
94 		else
95 			idx = tx_rate;
96 	}
97 
98 	ieee80211_tx_info_clear_status(info);
99 
100 	info->status.rates[0].count = tx_retry;
101 
102 	if (!(tsr & (TSR_TMO | TSR_RETRYTMO))) {
103 		info->status.rates[0].idx = idx;
104 		info->flags |= IEEE80211_TX_STAT_ACK;
105 	}
106 
107 	ieee80211_tx_status_irqsafe(priv->hw, context->skb);
108 
109 	context->in_use = false;
110 
111 	return 0;
112 }
113 
vnt_int_process_data(struct vnt_private * priv)114 void vnt_int_process_data(struct vnt_private *priv)
115 {
116 	struct vnt_interrupt_data *int_data;
117 	struct ieee80211_low_level_stats *low_stats = &priv->low_stats;
118 
119 	dev_dbg(&priv->usb->dev, "---->s_nsInterruptProcessData\n");
120 
121 	int_data = (struct vnt_interrupt_data *)priv->int_buf.data_buf;
122 
123 	if (int_data->tsr0 & TSR_VALID)
124 		vnt_int_report_rate(priv, int_data->pkt0, int_data->tsr0);
125 
126 	if (int_data->tsr1 & TSR_VALID)
127 		vnt_int_report_rate(priv, int_data->pkt1, int_data->tsr1);
128 
129 	if (int_data->tsr2 & TSR_VALID)
130 		vnt_int_report_rate(priv, int_data->pkt2, int_data->tsr2);
131 
132 	if (int_data->tsr3 & TSR_VALID)
133 		vnt_int_report_rate(priv, int_data->pkt3, int_data->tsr3);
134 
135 	if (int_data->isr0 != 0) {
136 		if (int_data->isr0 & ISR_BNTX &&
137 		    priv->op_mode == NL80211_IFTYPE_AP)
138 			vnt_schedule_command(priv, WLAN_CMD_BECON_SEND);
139 
140 		if (int_data->isr0 & ISR_TBTT &&
141 		    priv->hw->conf.flags & IEEE80211_CONF_PS) {
142 			if (!priv->wake_up_count)
143 				priv->wake_up_count =
144 					priv->hw->conf.listen_interval;
145 
146 			--priv->wake_up_count;
147 
148 			/* Turn on wake up to listen next beacon */
149 			if (priv->wake_up_count == 1)
150 				vnt_schedule_command(priv,
151 						     WLAN_CMD_TBTT_WAKEUP);
152 		}
153 		priv->current_tsf = le64_to_cpu(int_data->tsf);
154 
155 		low_stats->dot11RTSSuccessCount += int_data->rts_success;
156 		low_stats->dot11RTSFailureCount += int_data->rts_fail;
157 		low_stats->dot11ACKFailureCount += int_data->ack_fail;
158 		low_stats->dot11FCSErrorCount += int_data->fcs_err;
159 	}
160 
161 	priv->int_buf.in_use = false;
162 }
163