xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/marvell/mwifiex/debugfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * NXP Wireless LAN device driver: debugfs
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright 2011-2020 NXP
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This software file (the "File") is distributed by NXP
7*4882a593Smuzhiyun  * under the terms of the GNU General Public License Version 2, June 1991
8*4882a593Smuzhiyun  * (the "License").  You may use, redistribute and/or modify this File in
9*4882a593Smuzhiyun  * accordance with the terms and conditions of the License, a copy of which
10*4882a593Smuzhiyun  * is available by writing to the Free Software Foundation, Inc.,
11*4882a593Smuzhiyun  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12*4882a593Smuzhiyun  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16*4882a593Smuzhiyun  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17*4882a593Smuzhiyun  * this warranty disclaimer.
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/debugfs.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "main.h"
23*4882a593Smuzhiyun #include "11n.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static struct dentry *mwifiex_dfs_dir;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun static char *bss_modes[] = {
29*4882a593Smuzhiyun 	"UNSPECIFIED",
30*4882a593Smuzhiyun 	"ADHOC",
31*4882a593Smuzhiyun 	"STATION",
32*4882a593Smuzhiyun 	"AP",
33*4882a593Smuzhiyun 	"AP_VLAN",
34*4882a593Smuzhiyun 	"WDS",
35*4882a593Smuzhiyun 	"MONITOR",
36*4882a593Smuzhiyun 	"MESH_POINT",
37*4882a593Smuzhiyun 	"P2P_CLIENT",
38*4882a593Smuzhiyun 	"P2P_GO",
39*4882a593Smuzhiyun 	"P2P_DEVICE",
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * Proc info file read handler.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * This function is called when the 'info' file is opened for reading.
46*4882a593Smuzhiyun  * It prints the following driver related information -
47*4882a593Smuzhiyun  *      - Driver name
48*4882a593Smuzhiyun  *      - Driver version
49*4882a593Smuzhiyun  *      - Driver extended version
50*4882a593Smuzhiyun  *      - Interface name
51*4882a593Smuzhiyun  *      - BSS mode
52*4882a593Smuzhiyun  *      - Media state (connected or disconnected)
53*4882a593Smuzhiyun  *      - MAC address
54*4882a593Smuzhiyun  *      - Total number of Tx bytes
55*4882a593Smuzhiyun  *      - Total number of Rx bytes
56*4882a593Smuzhiyun  *      - Total number of Tx packets
57*4882a593Smuzhiyun  *      - Total number of Rx packets
58*4882a593Smuzhiyun  *      - Total number of dropped Tx packets
59*4882a593Smuzhiyun  *      - Total number of dropped Rx packets
60*4882a593Smuzhiyun  *      - Total number of corrupted Tx packets
61*4882a593Smuzhiyun  *      - Total number of corrupted Rx packets
62*4882a593Smuzhiyun  *      - Carrier status (on or off)
63*4882a593Smuzhiyun  *      - Tx queue status (started or stopped)
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * For STA mode drivers, it also prints the following extra -
66*4882a593Smuzhiyun  *      - ESSID
67*4882a593Smuzhiyun  *      - BSSID
68*4882a593Smuzhiyun  *      - Channel
69*4882a593Smuzhiyun  *      - Region code
70*4882a593Smuzhiyun  *      - Multicast count
71*4882a593Smuzhiyun  *      - Multicast addresses
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun static ssize_t
mwifiex_info_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)74*4882a593Smuzhiyun mwifiex_info_read(struct file *file, char __user *ubuf,
75*4882a593Smuzhiyun 		  size_t count, loff_t *ppos)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct mwifiex_private *priv =
78*4882a593Smuzhiyun 		(struct mwifiex_private *) file->private_data;
79*4882a593Smuzhiyun 	struct net_device *netdev = priv->netdev;
80*4882a593Smuzhiyun 	struct netdev_hw_addr *ha;
81*4882a593Smuzhiyun 	struct netdev_queue *txq;
82*4882a593Smuzhiyun 	unsigned long page = get_zeroed_page(GFP_KERNEL);
83*4882a593Smuzhiyun 	char *p = (char *) page, fmt[64];
84*4882a593Smuzhiyun 	struct mwifiex_bss_info info;
85*4882a593Smuzhiyun 	ssize_t ret;
86*4882a593Smuzhiyun 	int i = 0;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	if (!p)
89*4882a593Smuzhiyun 		return -ENOMEM;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	memset(&info, 0, sizeof(info));
92*4882a593Smuzhiyun 	ret = mwifiex_get_bss_info(priv, &info);
93*4882a593Smuzhiyun 	if (ret)
94*4882a593Smuzhiyun 		goto free_and_exit;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	mwifiex_drv_get_driver_version(priv->adapter, fmt, sizeof(fmt) - 1);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	mwifiex_get_ver_ext(priv, 0);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
101*4882a593Smuzhiyun 	p += sprintf(p, "driver_version = %s", fmt);
102*4882a593Smuzhiyun 	p += sprintf(p, "\nverext = %s", priv->version_str);
103*4882a593Smuzhiyun 	p += sprintf(p, "\ninterface_name=\"%s\"\n", netdev->name);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (info.bss_mode >= ARRAY_SIZE(bss_modes))
106*4882a593Smuzhiyun 		p += sprintf(p, "bss_mode=\"%d\"\n", info.bss_mode);
107*4882a593Smuzhiyun 	else
108*4882a593Smuzhiyun 		p += sprintf(p, "bss_mode=\"%s\"\n", bss_modes[info.bss_mode]);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	p += sprintf(p, "media_state=\"%s\"\n",
111*4882a593Smuzhiyun 		     (!priv->media_connected ? "Disconnected" : "Connected"));
112*4882a593Smuzhiyun 	p += sprintf(p, "mac_address=\"%pM\"\n", netdev->dev_addr);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) {
115*4882a593Smuzhiyun 		p += sprintf(p, "multicast_count=\"%d\"\n",
116*4882a593Smuzhiyun 			     netdev_mc_count(netdev));
117*4882a593Smuzhiyun 		p += sprintf(p, "essid=\"%.*s\"\n", info.ssid.ssid_len,
118*4882a593Smuzhiyun 			     info.ssid.ssid);
119*4882a593Smuzhiyun 		p += sprintf(p, "bssid=\"%pM\"\n", info.bssid);
120*4882a593Smuzhiyun 		p += sprintf(p, "channel=\"%d\"\n", (int) info.bss_chan);
121*4882a593Smuzhiyun 		p += sprintf(p, "country_code = \"%s\"\n", info.country_code);
122*4882a593Smuzhiyun 		p += sprintf(p, "region_code=\"0x%x\"\n",
123*4882a593Smuzhiyun 			     priv->adapter->region_code);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 		netdev_for_each_mc_addr(ha, netdev)
126*4882a593Smuzhiyun 			p += sprintf(p, "multicast_address[%d]=\"%pM\"\n",
127*4882a593Smuzhiyun 					i++, ha->addr);
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	p += sprintf(p, "num_tx_bytes = %lu\n", priv->stats.tx_bytes);
131*4882a593Smuzhiyun 	p += sprintf(p, "num_rx_bytes = %lu\n", priv->stats.rx_bytes);
132*4882a593Smuzhiyun 	p += sprintf(p, "num_tx_pkts = %lu\n", priv->stats.tx_packets);
133*4882a593Smuzhiyun 	p += sprintf(p, "num_rx_pkts = %lu\n", priv->stats.rx_packets);
134*4882a593Smuzhiyun 	p += sprintf(p, "num_tx_pkts_dropped = %lu\n", priv->stats.tx_dropped);
135*4882a593Smuzhiyun 	p += sprintf(p, "num_rx_pkts_dropped = %lu\n", priv->stats.rx_dropped);
136*4882a593Smuzhiyun 	p += sprintf(p, "num_tx_pkts_err = %lu\n", priv->stats.tx_errors);
137*4882a593Smuzhiyun 	p += sprintf(p, "num_rx_pkts_err = %lu\n", priv->stats.rx_errors);
138*4882a593Smuzhiyun 	p += sprintf(p, "carrier %s\n", ((netif_carrier_ok(priv->netdev))
139*4882a593Smuzhiyun 					 ? "on" : "off"));
140*4882a593Smuzhiyun 	p += sprintf(p, "tx queue");
141*4882a593Smuzhiyun 	for (i = 0; i < netdev->num_tx_queues; i++) {
142*4882a593Smuzhiyun 		txq = netdev_get_tx_queue(netdev, i);
143*4882a593Smuzhiyun 		p += sprintf(p, " %d:%s", i, netif_tx_queue_stopped(txq) ?
144*4882a593Smuzhiyun 			     "stopped" : "started");
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 	p += sprintf(p, "\n");
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
149*4882a593Smuzhiyun 				      (unsigned long) p - page);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun free_and_exit:
152*4882a593Smuzhiyun 	free_page(page);
153*4882a593Smuzhiyun 	return ret;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /*
157*4882a593Smuzhiyun  * Proc getlog file read handler.
158*4882a593Smuzhiyun  *
159*4882a593Smuzhiyun  * This function is called when the 'getlog' file is opened for reading
160*4882a593Smuzhiyun  * It prints the following log information -
161*4882a593Smuzhiyun  *      - Number of multicast Tx frames
162*4882a593Smuzhiyun  *      - Number of failed packets
163*4882a593Smuzhiyun  *      - Number of Tx retries
164*4882a593Smuzhiyun  *      - Number of multicast Tx retries
165*4882a593Smuzhiyun  *      - Number of duplicate frames
166*4882a593Smuzhiyun  *      - Number of RTS successes
167*4882a593Smuzhiyun  *      - Number of RTS failures
168*4882a593Smuzhiyun  *      - Number of ACK failures
169*4882a593Smuzhiyun  *      - Number of fragmented Rx frames
170*4882a593Smuzhiyun  *      - Number of multicast Rx frames
171*4882a593Smuzhiyun  *      - Number of FCS errors
172*4882a593Smuzhiyun  *      - Number of Tx frames
173*4882a593Smuzhiyun  *      - WEP ICV error counts
174*4882a593Smuzhiyun  *      - Number of received beacons
175*4882a593Smuzhiyun  *      - Number of missed beacons
176*4882a593Smuzhiyun  */
177*4882a593Smuzhiyun static ssize_t
mwifiex_getlog_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)178*4882a593Smuzhiyun mwifiex_getlog_read(struct file *file, char __user *ubuf,
179*4882a593Smuzhiyun 		    size_t count, loff_t *ppos)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	struct mwifiex_private *priv =
182*4882a593Smuzhiyun 		(struct mwifiex_private *) file->private_data;
183*4882a593Smuzhiyun 	unsigned long page = get_zeroed_page(GFP_KERNEL);
184*4882a593Smuzhiyun 	char *p = (char *) page;
185*4882a593Smuzhiyun 	ssize_t ret;
186*4882a593Smuzhiyun 	struct mwifiex_ds_get_stats stats;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (!p)
189*4882a593Smuzhiyun 		return -ENOMEM;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	memset(&stats, 0, sizeof(stats));
192*4882a593Smuzhiyun 	ret = mwifiex_get_stats_info(priv, &stats);
193*4882a593Smuzhiyun 	if (ret)
194*4882a593Smuzhiyun 		goto free_and_exit;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	p += sprintf(p, "\n"
197*4882a593Smuzhiyun 		     "mcasttxframe     %u\n"
198*4882a593Smuzhiyun 		     "failed           %u\n"
199*4882a593Smuzhiyun 		     "retry            %u\n"
200*4882a593Smuzhiyun 		     "multiretry       %u\n"
201*4882a593Smuzhiyun 		     "framedup         %u\n"
202*4882a593Smuzhiyun 		     "rtssuccess       %u\n"
203*4882a593Smuzhiyun 		     "rtsfailure       %u\n"
204*4882a593Smuzhiyun 		     "ackfailure       %u\n"
205*4882a593Smuzhiyun 		     "rxfrag           %u\n"
206*4882a593Smuzhiyun 		     "mcastrxframe     %u\n"
207*4882a593Smuzhiyun 		     "fcserror         %u\n"
208*4882a593Smuzhiyun 		     "txframe          %u\n"
209*4882a593Smuzhiyun 		     "wepicverrcnt-1   %u\n"
210*4882a593Smuzhiyun 		     "wepicverrcnt-2   %u\n"
211*4882a593Smuzhiyun 		     "wepicverrcnt-3   %u\n"
212*4882a593Smuzhiyun 		     "wepicverrcnt-4   %u\n"
213*4882a593Smuzhiyun 		     "bcn_rcv_cnt   %u\n"
214*4882a593Smuzhiyun 		     "bcn_miss_cnt   %u\n",
215*4882a593Smuzhiyun 		     stats.mcast_tx_frame,
216*4882a593Smuzhiyun 		     stats.failed,
217*4882a593Smuzhiyun 		     stats.retry,
218*4882a593Smuzhiyun 		     stats.multi_retry,
219*4882a593Smuzhiyun 		     stats.frame_dup,
220*4882a593Smuzhiyun 		     stats.rts_success,
221*4882a593Smuzhiyun 		     stats.rts_failure,
222*4882a593Smuzhiyun 		     stats.ack_failure,
223*4882a593Smuzhiyun 		     stats.rx_frag,
224*4882a593Smuzhiyun 		     stats.mcast_rx_frame,
225*4882a593Smuzhiyun 		     stats.fcs_error,
226*4882a593Smuzhiyun 		     stats.tx_frame,
227*4882a593Smuzhiyun 		     stats.wep_icv_error[0],
228*4882a593Smuzhiyun 		     stats.wep_icv_error[1],
229*4882a593Smuzhiyun 		     stats.wep_icv_error[2],
230*4882a593Smuzhiyun 		     stats.wep_icv_error[3],
231*4882a593Smuzhiyun 		     stats.bcn_rcv_cnt,
232*4882a593Smuzhiyun 		     stats.bcn_miss_cnt);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
236*4882a593Smuzhiyun 				      (unsigned long) p - page);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun free_and_exit:
239*4882a593Smuzhiyun 	free_page(page);
240*4882a593Smuzhiyun 	return ret;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun /* Sysfs histogram file read handler.
244*4882a593Smuzhiyun  *
245*4882a593Smuzhiyun  * This function is called when the 'histogram' file is opened for reading
246*4882a593Smuzhiyun  * It prints the following histogram information -
247*4882a593Smuzhiyun  *      - Number of histogram samples
248*4882a593Smuzhiyun  *      - Receive packet number of each rx_rate
249*4882a593Smuzhiyun  *      - Receive packet number of each snr
250*4882a593Smuzhiyun  *      - Receive packet number of each nosie_flr
251*4882a593Smuzhiyun  *      - Receive packet number of each signal streath
252*4882a593Smuzhiyun  */
253*4882a593Smuzhiyun static ssize_t
mwifiex_histogram_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)254*4882a593Smuzhiyun mwifiex_histogram_read(struct file *file, char __user *ubuf,
255*4882a593Smuzhiyun 		       size_t count, loff_t *ppos)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	struct mwifiex_private *priv =
258*4882a593Smuzhiyun 		(struct mwifiex_private *)file->private_data;
259*4882a593Smuzhiyun 	ssize_t ret;
260*4882a593Smuzhiyun 	struct mwifiex_histogram_data *phist_data;
261*4882a593Smuzhiyun 	int i, value;
262*4882a593Smuzhiyun 	unsigned long page = get_zeroed_page(GFP_KERNEL);
263*4882a593Smuzhiyun 	char *p = (char *)page;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	if (!p)
266*4882a593Smuzhiyun 		return -ENOMEM;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	if (!priv || !priv->hist_data)
269*4882a593Smuzhiyun 		return -EFAULT;
270*4882a593Smuzhiyun 	phist_data = priv->hist_data;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	p += sprintf(p, "\n"
273*4882a593Smuzhiyun 		     "total samples = %d\n",
274*4882a593Smuzhiyun 		     atomic_read(&phist_data->num_samples));
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	p += sprintf(p,
277*4882a593Smuzhiyun 		     "rx rates (in Mbps): 0=1M   1=2M 2=5.5M  3=11M   4=6M   5=9M  6=12M\n"
278*4882a593Smuzhiyun 		     "7=18M  8=24M  9=36M  10=48M  11=54M 12-27=MCS0-15(BW20) 28-43=MCS0-15(BW40)\n");
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info)) {
281*4882a593Smuzhiyun 		p += sprintf(p,
282*4882a593Smuzhiyun 			     "44-53=MCS0-9(VHT:BW20) 54-63=MCS0-9(VHT:BW40) 64-73=MCS0-9(VHT:BW80)\n\n");
283*4882a593Smuzhiyun 	} else {
284*4882a593Smuzhiyun 		p += sprintf(p, "\n");
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	for (i = 0; i < MWIFIEX_MAX_RX_RATES; i++) {
288*4882a593Smuzhiyun 		value = atomic_read(&phist_data->rx_rate[i]);
289*4882a593Smuzhiyun 		if (value)
290*4882a593Smuzhiyun 			p += sprintf(p, "rx_rate[%02d] = %d\n", i, value);
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info)) {
294*4882a593Smuzhiyun 		for (i = MWIFIEX_MAX_RX_RATES; i < MWIFIEX_MAX_AC_RX_RATES;
295*4882a593Smuzhiyun 		     i++) {
296*4882a593Smuzhiyun 			value = atomic_read(&phist_data->rx_rate[i]);
297*4882a593Smuzhiyun 			if (value)
298*4882a593Smuzhiyun 				p += sprintf(p, "rx_rate[%02d] = %d\n",
299*4882a593Smuzhiyun 					   i, value);
300*4882a593Smuzhiyun 		}
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	for (i = 0; i < MWIFIEX_MAX_SNR; i++) {
304*4882a593Smuzhiyun 		value =  atomic_read(&phist_data->snr[i]);
305*4882a593Smuzhiyun 		if (value)
306*4882a593Smuzhiyun 			p += sprintf(p, "snr[%02ddB] = %d\n", i, value);
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 	for (i = 0; i < MWIFIEX_MAX_NOISE_FLR; i++) {
309*4882a593Smuzhiyun 		value = atomic_read(&phist_data->noise_flr[i]);
310*4882a593Smuzhiyun 		if (value)
311*4882a593Smuzhiyun 			p += sprintf(p, "noise_flr[%02ddBm] = %d\n",
312*4882a593Smuzhiyun 				(int)(i-128), value);
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 	for (i = 0; i < MWIFIEX_MAX_SIG_STRENGTH; i++) {
315*4882a593Smuzhiyun 		value = atomic_read(&phist_data->sig_str[i]);
316*4882a593Smuzhiyun 		if (value)
317*4882a593Smuzhiyun 			p += sprintf(p, "sig_strength[-%02ddBm] = %d\n",
318*4882a593Smuzhiyun 				i, value);
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *)page,
322*4882a593Smuzhiyun 				      (unsigned long)p - page);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	return ret;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun static ssize_t
mwifiex_histogram_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)328*4882a593Smuzhiyun mwifiex_histogram_write(struct file *file, const char __user *ubuf,
329*4882a593Smuzhiyun 			size_t count, loff_t *ppos)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	struct mwifiex_private *priv = (void *)file->private_data;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	if (priv && priv->hist_data)
334*4882a593Smuzhiyun 		mwifiex_hist_data_reset(priv);
335*4882a593Smuzhiyun 	return 0;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun static struct mwifiex_debug_info info;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun /*
341*4882a593Smuzhiyun  * Proc debug file read handler.
342*4882a593Smuzhiyun  *
343*4882a593Smuzhiyun  * This function is called when the 'debug' file is opened for reading
344*4882a593Smuzhiyun  * It prints the following log information -
345*4882a593Smuzhiyun  *      - Interrupt count
346*4882a593Smuzhiyun  *      - WMM AC VO packets count
347*4882a593Smuzhiyun  *      - WMM AC VI packets count
348*4882a593Smuzhiyun  *      - WMM AC BE packets count
349*4882a593Smuzhiyun  *      - WMM AC BK packets count
350*4882a593Smuzhiyun  *      - Maximum Tx buffer size
351*4882a593Smuzhiyun  *      - Tx buffer size
352*4882a593Smuzhiyun  *      - Current Tx buffer size
353*4882a593Smuzhiyun  *      - Power Save mode
354*4882a593Smuzhiyun  *      - Power Save state
355*4882a593Smuzhiyun  *      - Deep Sleep status
356*4882a593Smuzhiyun  *      - Device wakeup required status
357*4882a593Smuzhiyun  *      - Number of wakeup tries
358*4882a593Smuzhiyun  *      - Host Sleep configured status
359*4882a593Smuzhiyun  *      - Host Sleep activated status
360*4882a593Smuzhiyun  *      - Number of Tx timeouts
361*4882a593Smuzhiyun  *      - Number of command timeouts
362*4882a593Smuzhiyun  *      - Last timed out command ID
363*4882a593Smuzhiyun  *      - Last timed out command action
364*4882a593Smuzhiyun  *      - Last command ID
365*4882a593Smuzhiyun  *      - Last command action
366*4882a593Smuzhiyun  *      - Last command index
367*4882a593Smuzhiyun  *      - Last command response ID
368*4882a593Smuzhiyun  *      - Last command response index
369*4882a593Smuzhiyun  *      - Last event
370*4882a593Smuzhiyun  *      - Last event index
371*4882a593Smuzhiyun  *      - Number of host to card command failures
372*4882a593Smuzhiyun  *      - Number of sleep confirm command failures
373*4882a593Smuzhiyun  *      - Number of host to card data failure
374*4882a593Smuzhiyun  *      - Number of deauthentication events
375*4882a593Smuzhiyun  *      - Number of disassociation events
376*4882a593Smuzhiyun  *      - Number of link lost events
377*4882a593Smuzhiyun  *      - Number of deauthentication commands
378*4882a593Smuzhiyun  *      - Number of association success commands
379*4882a593Smuzhiyun  *      - Number of association failure commands
380*4882a593Smuzhiyun  *      - Number of commands sent
381*4882a593Smuzhiyun  *      - Number of data packets sent
382*4882a593Smuzhiyun  *      - Number of command responses received
383*4882a593Smuzhiyun  *      - Number of events received
384*4882a593Smuzhiyun  *      - Tx BA stream table (TID, RA)
385*4882a593Smuzhiyun  *      - Rx reorder table (TID, TA, Start window, Window size, Buffer)
386*4882a593Smuzhiyun  */
387*4882a593Smuzhiyun static ssize_t
mwifiex_debug_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)388*4882a593Smuzhiyun mwifiex_debug_read(struct file *file, char __user *ubuf,
389*4882a593Smuzhiyun 		   size_t count, loff_t *ppos)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	struct mwifiex_private *priv =
392*4882a593Smuzhiyun 		(struct mwifiex_private *) file->private_data;
393*4882a593Smuzhiyun 	unsigned long page = get_zeroed_page(GFP_KERNEL);
394*4882a593Smuzhiyun 	char *p = (char *) page;
395*4882a593Smuzhiyun 	ssize_t ret;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (!p)
398*4882a593Smuzhiyun 		return -ENOMEM;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	ret = mwifiex_get_debug_info(priv, &info);
401*4882a593Smuzhiyun 	if (ret)
402*4882a593Smuzhiyun 		goto free_and_exit;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	p += mwifiex_debug_info_to_buffer(priv, p, &info);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
407*4882a593Smuzhiyun 				      (unsigned long) p - page);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun free_and_exit:
410*4882a593Smuzhiyun 	free_page(page);
411*4882a593Smuzhiyun 	return ret;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun static u32 saved_reg_type, saved_reg_offset, saved_reg_value;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun /*
417*4882a593Smuzhiyun  * Proc regrdwr file write handler.
418*4882a593Smuzhiyun  *
419*4882a593Smuzhiyun  * This function is called when the 'regrdwr' file is opened for writing
420*4882a593Smuzhiyun  *
421*4882a593Smuzhiyun  * This function can be used to write to a register.
422*4882a593Smuzhiyun  */
423*4882a593Smuzhiyun static ssize_t
mwifiex_regrdwr_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)424*4882a593Smuzhiyun mwifiex_regrdwr_write(struct file *file,
425*4882a593Smuzhiyun 		      const char __user *ubuf, size_t count, loff_t *ppos)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	char *buf;
428*4882a593Smuzhiyun 	int ret;
429*4882a593Smuzhiyun 	u32 reg_type = 0, reg_offset = 0, reg_value = UINT_MAX;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
432*4882a593Smuzhiyun 	if (IS_ERR(buf))
433*4882a593Smuzhiyun 		return PTR_ERR(buf);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	sscanf(buf, "%u %x %x", &reg_type, &reg_offset, &reg_value);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	if (reg_type == 0 || reg_offset == 0) {
438*4882a593Smuzhiyun 		ret = -EINVAL;
439*4882a593Smuzhiyun 		goto done;
440*4882a593Smuzhiyun 	} else {
441*4882a593Smuzhiyun 		saved_reg_type = reg_type;
442*4882a593Smuzhiyun 		saved_reg_offset = reg_offset;
443*4882a593Smuzhiyun 		saved_reg_value = reg_value;
444*4882a593Smuzhiyun 		ret = count;
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun done:
447*4882a593Smuzhiyun 	kfree(buf);
448*4882a593Smuzhiyun 	return ret;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun /*
452*4882a593Smuzhiyun  * Proc regrdwr file read handler.
453*4882a593Smuzhiyun  *
454*4882a593Smuzhiyun  * This function is called when the 'regrdwr' file is opened for reading
455*4882a593Smuzhiyun  *
456*4882a593Smuzhiyun  * This function can be used to read from a register.
457*4882a593Smuzhiyun  */
458*4882a593Smuzhiyun static ssize_t
mwifiex_regrdwr_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)459*4882a593Smuzhiyun mwifiex_regrdwr_read(struct file *file, char __user *ubuf,
460*4882a593Smuzhiyun 		     size_t count, loff_t *ppos)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 	struct mwifiex_private *priv =
463*4882a593Smuzhiyun 		(struct mwifiex_private *) file->private_data;
464*4882a593Smuzhiyun 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
465*4882a593Smuzhiyun 	char *buf = (char *) addr;
466*4882a593Smuzhiyun 	int pos = 0, ret = 0;
467*4882a593Smuzhiyun 	u32 reg_value;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	if (!buf)
470*4882a593Smuzhiyun 		return -ENOMEM;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	if (!saved_reg_type) {
473*4882a593Smuzhiyun 		/* No command has been given */
474*4882a593Smuzhiyun 		pos += snprintf(buf, PAGE_SIZE, "0");
475*4882a593Smuzhiyun 		goto done;
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun 	/* Set command has been given */
478*4882a593Smuzhiyun 	if (saved_reg_value != UINT_MAX) {
479*4882a593Smuzhiyun 		ret = mwifiex_reg_write(priv, saved_reg_type, saved_reg_offset,
480*4882a593Smuzhiyun 					saved_reg_value);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 		pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n",
483*4882a593Smuzhiyun 				saved_reg_type, saved_reg_offset,
484*4882a593Smuzhiyun 				saved_reg_value);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 		ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 		goto done;
489*4882a593Smuzhiyun 	}
490*4882a593Smuzhiyun 	/* Get command has been given */
491*4882a593Smuzhiyun 	ret = mwifiex_reg_read(priv, saved_reg_type,
492*4882a593Smuzhiyun 			       saved_reg_offset, &reg_value);
493*4882a593Smuzhiyun 	if (ret) {
494*4882a593Smuzhiyun 		ret = -EINVAL;
495*4882a593Smuzhiyun 		goto done;
496*4882a593Smuzhiyun 	}
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", saved_reg_type,
499*4882a593Smuzhiyun 			saved_reg_offset, reg_value);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun done:
504*4882a593Smuzhiyun 	free_page(addr);
505*4882a593Smuzhiyun 	return ret;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun /* Proc debug_mask file read handler.
509*4882a593Smuzhiyun  * This function is called when the 'debug_mask' file is opened for reading
510*4882a593Smuzhiyun  * This function can be used read driver debugging mask value.
511*4882a593Smuzhiyun  */
512*4882a593Smuzhiyun static ssize_t
mwifiex_debug_mask_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)513*4882a593Smuzhiyun mwifiex_debug_mask_read(struct file *file, char __user *ubuf,
514*4882a593Smuzhiyun 			size_t count, loff_t *ppos)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	struct mwifiex_private *priv =
517*4882a593Smuzhiyun 		(struct mwifiex_private *)file->private_data;
518*4882a593Smuzhiyun 	unsigned long page = get_zeroed_page(GFP_KERNEL);
519*4882a593Smuzhiyun 	char *buf = (char *)page;
520*4882a593Smuzhiyun 	size_t ret = 0;
521*4882a593Smuzhiyun 	int pos = 0;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	if (!buf)
524*4882a593Smuzhiyun 		return -ENOMEM;
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	pos += snprintf(buf, PAGE_SIZE, "debug mask=0x%08x\n",
527*4882a593Smuzhiyun 			priv->adapter->debug_mask);
528*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	free_page(page);
531*4882a593Smuzhiyun 	return ret;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun /* Proc debug_mask file read handler.
535*4882a593Smuzhiyun  * This function is called when the 'debug_mask' file is opened for reading
536*4882a593Smuzhiyun  * This function can be used read driver debugging mask value.
537*4882a593Smuzhiyun  */
538*4882a593Smuzhiyun static ssize_t
mwifiex_debug_mask_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)539*4882a593Smuzhiyun mwifiex_debug_mask_write(struct file *file, const char __user *ubuf,
540*4882a593Smuzhiyun 			 size_t count, loff_t *ppos)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun 	int ret;
543*4882a593Smuzhiyun 	unsigned long debug_mask;
544*4882a593Smuzhiyun 	struct mwifiex_private *priv = (void *)file->private_data;
545*4882a593Smuzhiyun 	char *buf;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
548*4882a593Smuzhiyun 	if (IS_ERR(buf))
549*4882a593Smuzhiyun 		return PTR_ERR(buf);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	if (kstrtoul(buf, 0, &debug_mask)) {
552*4882a593Smuzhiyun 		ret = -EINVAL;
553*4882a593Smuzhiyun 		goto done;
554*4882a593Smuzhiyun 	}
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	priv->adapter->debug_mask = debug_mask;
557*4882a593Smuzhiyun 	ret = count;
558*4882a593Smuzhiyun done:
559*4882a593Smuzhiyun 	kfree(buf);
560*4882a593Smuzhiyun 	return ret;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun /* debugfs verext file write handler.
564*4882a593Smuzhiyun  * This function is called when the 'verext' file is opened for write
565*4882a593Smuzhiyun  */
566*4882a593Smuzhiyun static ssize_t
mwifiex_verext_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)567*4882a593Smuzhiyun mwifiex_verext_write(struct file *file, const char __user *ubuf,
568*4882a593Smuzhiyun 		     size_t count, loff_t *ppos)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	int ret;
571*4882a593Smuzhiyun 	u32 versionstrsel;
572*4882a593Smuzhiyun 	struct mwifiex_private *priv = (void *)file->private_data;
573*4882a593Smuzhiyun 	char buf[16];
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	memset(buf, 0, sizeof(buf));
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
578*4882a593Smuzhiyun 		return -EFAULT;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	ret = kstrtou32(buf, 10, &versionstrsel);
581*4882a593Smuzhiyun 	if (ret)
582*4882a593Smuzhiyun 		return ret;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	priv->versionstrsel = versionstrsel;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	return count;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun /* Proc verext file read handler.
590*4882a593Smuzhiyun  * This function is called when the 'verext' file is opened for reading
591*4882a593Smuzhiyun  * This function can be used read driver exteneed verion string.
592*4882a593Smuzhiyun  */
593*4882a593Smuzhiyun static ssize_t
mwifiex_verext_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)594*4882a593Smuzhiyun mwifiex_verext_read(struct file *file, char __user *ubuf,
595*4882a593Smuzhiyun 		    size_t count, loff_t *ppos)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun 	struct mwifiex_private *priv =
598*4882a593Smuzhiyun 		(struct mwifiex_private *)file->private_data;
599*4882a593Smuzhiyun 	char buf[256];
600*4882a593Smuzhiyun 	int ret;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	mwifiex_get_ver_ext(priv, priv->versionstrsel);
603*4882a593Smuzhiyun 	ret = snprintf(buf, sizeof(buf), "version string: %s\n",
604*4882a593Smuzhiyun 		       priv->version_str);
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	return simple_read_from_buffer(ubuf, count, ppos, buf, ret);
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun /* Proc memrw file write handler.
610*4882a593Smuzhiyun  * This function is called when the 'memrw' file is opened for writing
611*4882a593Smuzhiyun  * This function can be used to write to a memory location.
612*4882a593Smuzhiyun  */
613*4882a593Smuzhiyun static ssize_t
mwifiex_memrw_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)614*4882a593Smuzhiyun mwifiex_memrw_write(struct file *file, const char __user *ubuf, size_t count,
615*4882a593Smuzhiyun 		    loff_t *ppos)
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun 	int ret;
618*4882a593Smuzhiyun 	char cmd;
619*4882a593Smuzhiyun 	struct mwifiex_ds_mem_rw mem_rw;
620*4882a593Smuzhiyun 	u16 cmd_action;
621*4882a593Smuzhiyun 	struct mwifiex_private *priv = (void *)file->private_data;
622*4882a593Smuzhiyun 	char *buf;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
625*4882a593Smuzhiyun 	if (IS_ERR(buf))
626*4882a593Smuzhiyun 		return PTR_ERR(buf);
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	ret = sscanf(buf, "%c %x %x", &cmd, &mem_rw.addr, &mem_rw.value);
629*4882a593Smuzhiyun 	if (ret != 3) {
630*4882a593Smuzhiyun 		ret = -EINVAL;
631*4882a593Smuzhiyun 		goto done;
632*4882a593Smuzhiyun 	}
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	if ((cmd == 'r') || (cmd == 'R')) {
635*4882a593Smuzhiyun 		cmd_action = HostCmd_ACT_GEN_GET;
636*4882a593Smuzhiyun 		mem_rw.value = 0;
637*4882a593Smuzhiyun 	} else if ((cmd == 'w') || (cmd == 'W')) {
638*4882a593Smuzhiyun 		cmd_action = HostCmd_ACT_GEN_SET;
639*4882a593Smuzhiyun 	} else {
640*4882a593Smuzhiyun 		ret = -EINVAL;
641*4882a593Smuzhiyun 		goto done;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	memcpy(&priv->mem_rw, &mem_rw, sizeof(mem_rw));
645*4882a593Smuzhiyun 	if (mwifiex_send_cmd(priv, HostCmd_CMD_MEM_ACCESS, cmd_action, 0,
646*4882a593Smuzhiyun 			     &mem_rw, true))
647*4882a593Smuzhiyun 		ret = -1;
648*4882a593Smuzhiyun 	else
649*4882a593Smuzhiyun 		ret = count;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun done:
652*4882a593Smuzhiyun 	kfree(buf);
653*4882a593Smuzhiyun 	return ret;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun /* Proc memrw file read handler.
657*4882a593Smuzhiyun  * This function is called when the 'memrw' file is opened for reading
658*4882a593Smuzhiyun  * This function can be used to read from a memory location.
659*4882a593Smuzhiyun  */
660*4882a593Smuzhiyun static ssize_t
mwifiex_memrw_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)661*4882a593Smuzhiyun mwifiex_memrw_read(struct file *file, char __user *ubuf,
662*4882a593Smuzhiyun 		   size_t count, loff_t *ppos)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun 	struct mwifiex_private *priv = (void *)file->private_data;
665*4882a593Smuzhiyun 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
666*4882a593Smuzhiyun 	char *buf = (char *)addr;
667*4882a593Smuzhiyun 	int ret, pos = 0;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	if (!buf)
670*4882a593Smuzhiyun 		return -ENOMEM;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	pos += snprintf(buf, PAGE_SIZE, "0x%x 0x%x\n", priv->mem_rw.addr,
673*4882a593Smuzhiyun 			priv->mem_rw.value);
674*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	free_page(addr);
677*4882a593Smuzhiyun 	return ret;
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun static u32 saved_offset = -1, saved_bytes = -1;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun  * Proc rdeeprom file write handler.
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  * This function is called when the 'rdeeprom' file is opened for writing
686*4882a593Smuzhiyun  *
687*4882a593Smuzhiyun  * This function can be used to write to a RDEEPROM location.
688*4882a593Smuzhiyun  */
689*4882a593Smuzhiyun static ssize_t
mwifiex_rdeeprom_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)690*4882a593Smuzhiyun mwifiex_rdeeprom_write(struct file *file,
691*4882a593Smuzhiyun 		       const char __user *ubuf, size_t count, loff_t *ppos)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	char *buf;
694*4882a593Smuzhiyun 	int ret = 0;
695*4882a593Smuzhiyun 	int offset = -1, bytes = -1;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
698*4882a593Smuzhiyun 	if (IS_ERR(buf))
699*4882a593Smuzhiyun 		return PTR_ERR(buf);
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	sscanf(buf, "%d %d", &offset, &bytes);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	if (offset == -1 || bytes == -1) {
704*4882a593Smuzhiyun 		ret = -EINVAL;
705*4882a593Smuzhiyun 		goto done;
706*4882a593Smuzhiyun 	} else {
707*4882a593Smuzhiyun 		saved_offset = offset;
708*4882a593Smuzhiyun 		saved_bytes = bytes;
709*4882a593Smuzhiyun 		ret = count;
710*4882a593Smuzhiyun 	}
711*4882a593Smuzhiyun done:
712*4882a593Smuzhiyun 	kfree(buf);
713*4882a593Smuzhiyun 	return ret;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun /*
717*4882a593Smuzhiyun  * Proc rdeeprom read write handler.
718*4882a593Smuzhiyun  *
719*4882a593Smuzhiyun  * This function is called when the 'rdeeprom' file is opened for reading
720*4882a593Smuzhiyun  *
721*4882a593Smuzhiyun  * This function can be used to read from a RDEEPROM location.
722*4882a593Smuzhiyun  */
723*4882a593Smuzhiyun static ssize_t
mwifiex_rdeeprom_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)724*4882a593Smuzhiyun mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
725*4882a593Smuzhiyun 		      size_t count, loff_t *ppos)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun 	struct mwifiex_private *priv =
728*4882a593Smuzhiyun 		(struct mwifiex_private *) file->private_data;
729*4882a593Smuzhiyun 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
730*4882a593Smuzhiyun 	char *buf = (char *) addr;
731*4882a593Smuzhiyun 	int pos, ret, i;
732*4882a593Smuzhiyun 	u8 value[MAX_EEPROM_DATA];
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	if (!buf)
735*4882a593Smuzhiyun 		return -ENOMEM;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	if (saved_offset == -1) {
738*4882a593Smuzhiyun 		/* No command has been given */
739*4882a593Smuzhiyun 		pos = snprintf(buf, PAGE_SIZE, "0");
740*4882a593Smuzhiyun 		goto done;
741*4882a593Smuzhiyun 	}
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* Get command has been given */
744*4882a593Smuzhiyun 	ret = mwifiex_eeprom_read(priv, (u16) saved_offset,
745*4882a593Smuzhiyun 				  (u16) saved_bytes, value);
746*4882a593Smuzhiyun 	if (ret) {
747*4882a593Smuzhiyun 		ret = -EINVAL;
748*4882a593Smuzhiyun 		goto out_free;
749*4882a593Smuzhiyun 	}
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	pos = snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	for (i = 0; i < saved_bytes; i++)
754*4882a593Smuzhiyun 		pos += scnprintf(buf + pos, PAGE_SIZE - pos, "%d ", value[i]);
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun done:
757*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
758*4882a593Smuzhiyun out_free:
759*4882a593Smuzhiyun 	free_page(addr);
760*4882a593Smuzhiyun 	return ret;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun /* Proc hscfg file write handler
764*4882a593Smuzhiyun  * This function can be used to configure the host sleep parameters.
765*4882a593Smuzhiyun  */
766*4882a593Smuzhiyun static ssize_t
mwifiex_hscfg_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)767*4882a593Smuzhiyun mwifiex_hscfg_write(struct file *file, const char __user *ubuf,
768*4882a593Smuzhiyun 		    size_t count, loff_t *ppos)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	struct mwifiex_private *priv = (void *)file->private_data;
771*4882a593Smuzhiyun 	char *buf;
772*4882a593Smuzhiyun 	int ret, arg_num;
773*4882a593Smuzhiyun 	struct mwifiex_ds_hs_cfg hscfg;
774*4882a593Smuzhiyun 	int conditions = HS_CFG_COND_DEF;
775*4882a593Smuzhiyun 	u32 gpio = HS_CFG_GPIO_DEF, gap = HS_CFG_GAP_DEF;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
778*4882a593Smuzhiyun 	if (IS_ERR(buf))
779*4882a593Smuzhiyun 		return PTR_ERR(buf);
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	arg_num = sscanf(buf, "%d %x %x", &conditions, &gpio, &gap);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	if (arg_num > 3) {
786*4882a593Smuzhiyun 		mwifiex_dbg(priv->adapter, ERROR,
787*4882a593Smuzhiyun 			    "Too many arguments\n");
788*4882a593Smuzhiyun 		ret = -EINVAL;
789*4882a593Smuzhiyun 		goto done;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	if (arg_num >= 1 && arg_num < 3)
793*4882a593Smuzhiyun 		mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
794*4882a593Smuzhiyun 				      MWIFIEX_SYNC_CMD, &hscfg);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	if (arg_num) {
797*4882a593Smuzhiyun 		if (conditions == HS_CFG_CANCEL) {
798*4882a593Smuzhiyun 			mwifiex_cancel_hs(priv, MWIFIEX_ASYNC_CMD);
799*4882a593Smuzhiyun 			ret = count;
800*4882a593Smuzhiyun 			goto done;
801*4882a593Smuzhiyun 		}
802*4882a593Smuzhiyun 		hscfg.conditions = conditions;
803*4882a593Smuzhiyun 	}
804*4882a593Smuzhiyun 	if (arg_num >= 2)
805*4882a593Smuzhiyun 		hscfg.gpio = gpio;
806*4882a593Smuzhiyun 	if (arg_num == 3)
807*4882a593Smuzhiyun 		hscfg.gap = gap;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	hscfg.is_invoke_hostcmd = false;
810*4882a593Smuzhiyun 	mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
811*4882a593Smuzhiyun 			      MWIFIEX_SYNC_CMD, &hscfg);
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	mwifiex_enable_hs(priv->adapter);
814*4882a593Smuzhiyun 	clear_bit(MWIFIEX_IS_HS_ENABLING, &priv->adapter->work_flags);
815*4882a593Smuzhiyun 	ret = count;
816*4882a593Smuzhiyun done:
817*4882a593Smuzhiyun 	kfree(buf);
818*4882a593Smuzhiyun 	return ret;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun /* Proc hscfg file read handler
822*4882a593Smuzhiyun  * This function can be used to read host sleep configuration
823*4882a593Smuzhiyun  * parameters from driver.
824*4882a593Smuzhiyun  */
825*4882a593Smuzhiyun static ssize_t
mwifiex_hscfg_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)826*4882a593Smuzhiyun mwifiex_hscfg_read(struct file *file, char __user *ubuf,
827*4882a593Smuzhiyun 		   size_t count, loff_t *ppos)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	struct mwifiex_private *priv = (void *)file->private_data;
830*4882a593Smuzhiyun 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
831*4882a593Smuzhiyun 	char *buf = (char *)addr;
832*4882a593Smuzhiyun 	int pos, ret;
833*4882a593Smuzhiyun 	struct mwifiex_ds_hs_cfg hscfg;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	if (!buf)
836*4882a593Smuzhiyun 		return -ENOMEM;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
839*4882a593Smuzhiyun 			      MWIFIEX_SYNC_CMD, &hscfg);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	pos = snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", hscfg.conditions,
842*4882a593Smuzhiyun 		       hscfg.gpio, hscfg.gap);
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	free_page(addr);
847*4882a593Smuzhiyun 	return ret;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun static ssize_t
mwifiex_timeshare_coex_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)851*4882a593Smuzhiyun mwifiex_timeshare_coex_read(struct file *file, char __user *ubuf,
852*4882a593Smuzhiyun 			    size_t count, loff_t *ppos)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	struct mwifiex_private *priv = file->private_data;
855*4882a593Smuzhiyun 	char buf[3];
856*4882a593Smuzhiyun 	bool timeshare_coex;
857*4882a593Smuzhiyun 	int ret;
858*4882a593Smuzhiyun 	unsigned int len;
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	if (priv->adapter->fw_api_ver != MWIFIEX_FW_V15)
861*4882a593Smuzhiyun 		return -EOPNOTSUPP;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_ROBUST_COEX,
864*4882a593Smuzhiyun 			       HostCmd_ACT_GEN_GET, 0, &timeshare_coex, true);
865*4882a593Smuzhiyun 	if (ret)
866*4882a593Smuzhiyun 		return ret;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	len = sprintf(buf, "%d\n", timeshare_coex);
869*4882a593Smuzhiyun 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun static ssize_t
mwifiex_timeshare_coex_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)873*4882a593Smuzhiyun mwifiex_timeshare_coex_write(struct file *file, const char __user *ubuf,
874*4882a593Smuzhiyun 			     size_t count, loff_t *ppos)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun 	bool timeshare_coex;
877*4882a593Smuzhiyun 	struct mwifiex_private *priv = file->private_data;
878*4882a593Smuzhiyun 	char kbuf[16];
879*4882a593Smuzhiyun 	int ret;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	if (priv->adapter->fw_api_ver != MWIFIEX_FW_V15)
882*4882a593Smuzhiyun 		return -EOPNOTSUPP;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	memset(kbuf, 0, sizeof(kbuf));
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	if (copy_from_user(&kbuf, ubuf, min_t(size_t, sizeof(kbuf) - 1, count)))
887*4882a593Smuzhiyun 		return -EFAULT;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	if (strtobool(kbuf, &timeshare_coex))
890*4882a593Smuzhiyun 		return -EINVAL;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_ROBUST_COEX,
893*4882a593Smuzhiyun 			       HostCmd_ACT_GEN_SET, 0, &timeshare_coex, true);
894*4882a593Smuzhiyun 	if (ret)
895*4882a593Smuzhiyun 		return ret;
896*4882a593Smuzhiyun 	else
897*4882a593Smuzhiyun 		return count;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun static ssize_t
mwifiex_reset_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)901*4882a593Smuzhiyun mwifiex_reset_write(struct file *file,
902*4882a593Smuzhiyun 		    const char __user *ubuf, size_t count, loff_t *ppos)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	struct mwifiex_private *priv = file->private_data;
905*4882a593Smuzhiyun 	struct mwifiex_adapter *adapter = priv->adapter;
906*4882a593Smuzhiyun 	bool result;
907*4882a593Smuzhiyun 	int rc;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	rc = kstrtobool_from_user(ubuf, count, &result);
910*4882a593Smuzhiyun 	if (rc)
911*4882a593Smuzhiyun 		return rc;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	if (!result)
914*4882a593Smuzhiyun 		return -EINVAL;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	if (adapter->if_ops.card_reset) {
917*4882a593Smuzhiyun 		dev_info(adapter->dev, "Resetting per request\n");
918*4882a593Smuzhiyun 		adapter->if_ops.card_reset(adapter);
919*4882a593Smuzhiyun 	}
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	return count;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun #define MWIFIEX_DFS_ADD_FILE(name) do {                                 \
925*4882a593Smuzhiyun 	debugfs_create_file(#name, 0644, priv->dfs_dev_dir, priv,       \
926*4882a593Smuzhiyun 			    &mwifiex_dfs_##name##_fops);                \
927*4882a593Smuzhiyun } while (0);
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun #define MWIFIEX_DFS_FILE_OPS(name)                                      \
930*4882a593Smuzhiyun static const struct file_operations mwifiex_dfs_##name##_fops = {       \
931*4882a593Smuzhiyun 	.read = mwifiex_##name##_read,                                  \
932*4882a593Smuzhiyun 	.write = mwifiex_##name##_write,                                \
933*4882a593Smuzhiyun 	.open = simple_open,                                            \
934*4882a593Smuzhiyun };
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun #define MWIFIEX_DFS_FILE_READ_OPS(name)                                 \
937*4882a593Smuzhiyun static const struct file_operations mwifiex_dfs_##name##_fops = {       \
938*4882a593Smuzhiyun 	.read = mwifiex_##name##_read,                                  \
939*4882a593Smuzhiyun 	.open = simple_open,                                            \
940*4882a593Smuzhiyun };
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun #define MWIFIEX_DFS_FILE_WRITE_OPS(name)                                \
943*4882a593Smuzhiyun static const struct file_operations mwifiex_dfs_##name##_fops = {       \
944*4882a593Smuzhiyun 	.write = mwifiex_##name##_write,                                \
945*4882a593Smuzhiyun 	.open = simple_open,                                            \
946*4882a593Smuzhiyun };
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun MWIFIEX_DFS_FILE_READ_OPS(info);
950*4882a593Smuzhiyun MWIFIEX_DFS_FILE_READ_OPS(debug);
951*4882a593Smuzhiyun MWIFIEX_DFS_FILE_READ_OPS(getlog);
952*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(regrdwr);
953*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(rdeeprom);
954*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(memrw);
955*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(hscfg);
956*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(histogram);
957*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(debug_mask);
958*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(timeshare_coex);
959*4882a593Smuzhiyun MWIFIEX_DFS_FILE_WRITE_OPS(reset);
960*4882a593Smuzhiyun MWIFIEX_DFS_FILE_OPS(verext);
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun /*
963*4882a593Smuzhiyun  * This function creates the debug FS directory structure and the files.
964*4882a593Smuzhiyun  */
965*4882a593Smuzhiyun void
mwifiex_dev_debugfs_init(struct mwifiex_private * priv)966*4882a593Smuzhiyun mwifiex_dev_debugfs_init(struct mwifiex_private *priv)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun 	if (!mwifiex_dfs_dir || !priv)
969*4882a593Smuzhiyun 		return;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	priv->dfs_dev_dir = debugfs_create_dir(priv->netdev->name,
972*4882a593Smuzhiyun 					       mwifiex_dfs_dir);
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	if (!priv->dfs_dev_dir)
975*4882a593Smuzhiyun 		return;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(info);
978*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(debug);
979*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(getlog);
980*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(regrdwr);
981*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(rdeeprom);
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(memrw);
984*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(hscfg);
985*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(histogram);
986*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(debug_mask);
987*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(timeshare_coex);
988*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(reset);
989*4882a593Smuzhiyun 	MWIFIEX_DFS_ADD_FILE(verext);
990*4882a593Smuzhiyun }
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun /*
993*4882a593Smuzhiyun  * This function removes the debug FS directory structure and the files.
994*4882a593Smuzhiyun  */
995*4882a593Smuzhiyun void
mwifiex_dev_debugfs_remove(struct mwifiex_private * priv)996*4882a593Smuzhiyun mwifiex_dev_debugfs_remove(struct mwifiex_private *priv)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun 	if (!priv)
999*4882a593Smuzhiyun 		return;
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	debugfs_remove_recursive(priv->dfs_dev_dir);
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun /*
1005*4882a593Smuzhiyun  * This function creates the top level proc directory.
1006*4882a593Smuzhiyun  */
1007*4882a593Smuzhiyun void
mwifiex_debugfs_init(void)1008*4882a593Smuzhiyun mwifiex_debugfs_init(void)
1009*4882a593Smuzhiyun {
1010*4882a593Smuzhiyun 	if (!mwifiex_dfs_dir)
1011*4882a593Smuzhiyun 		mwifiex_dfs_dir = debugfs_create_dir("mwifiex", NULL);
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun /*
1015*4882a593Smuzhiyun  * This function removes the top level proc directory.
1016*4882a593Smuzhiyun  */
1017*4882a593Smuzhiyun void
mwifiex_debugfs_remove(void)1018*4882a593Smuzhiyun mwifiex_debugfs_remove(void)
1019*4882a593Smuzhiyun {
1020*4882a593Smuzhiyun 	debugfs_remove(mwifiex_dfs_dir);
1021*4882a593Smuzhiyun }
1022