1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2013 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include <drv_types.h>
9 #include <rtw_debug.h>
10 #include "rtw_proc.h"
11 
12 #ifdef PROC_DEBUG
13 
14 static struct proc_dir_entry *rtw_proc;
15 
16 #define RTW_PROC_NAME "rtl8723bs"
17 
18 #define get_proc_net init_net.proc_net
19 
rtw_proc_create_dir(const char * name,struct proc_dir_entry * parent,void * data)20 inline struct proc_dir_entry *rtw_proc_create_dir(const char *name, struct proc_dir_entry *parent, void *data)
21 {
22 	struct proc_dir_entry *entry;
23 
24 	entry = proc_mkdir_data(name, S_IRUGO|S_IXUGO, parent, data);
25 
26 	return entry;
27 }
28 
rtw_proc_create_entry(const char * name,struct proc_dir_entry * parent,const struct file_operations * fops,void * data)29 inline struct proc_dir_entry *rtw_proc_create_entry(const char *name, struct proc_dir_entry *parent,
30 	const struct file_operations *fops, void *data)
31 {
32 	struct proc_dir_entry *entry;
33 
34 	entry = proc_create_data(name,  S_IFREG|S_IRUGO, parent, fops, data);
35 
36 	return entry;
37 }
38 
proc_get_dummy(struct seq_file * m,void * v)39 static int proc_get_dummy(struct seq_file *m, void *v)
40 {
41 	return 0;
42 }
43 
proc_get_drv_version(struct seq_file * m,void * v)44 static int proc_get_drv_version(struct seq_file *m, void *v)
45 {
46 	dump_drv_version(m);
47 	return 0;
48 }
49 
proc_get_log_level(struct seq_file * m,void * v)50 static int proc_get_log_level(struct seq_file *m, void *v)
51 {
52 	dump_log_level(m);
53 	return 0;
54 }
55 
proc_set_log_level(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)56 static ssize_t proc_set_log_level(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
57 {
58 	char tmp[32];
59 	int log_level;
60 
61 	if (count < 1)
62 		return -EINVAL;
63 
64 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
65 		sscanf(tmp, "%d ", &log_level);
66 		if (log_level >= _drv_always_ && log_level <= _drv_debug_)
67 		{
68 			GlobalDebugLevel = log_level;
69 			printk("%d\n", GlobalDebugLevel);
70 		}
71 	} else {
72 		return -EFAULT;
73 	}
74 
75 	return count;
76 }
77 
78 /*
79 * rtw_drv_proc:
80 * init/deinit when register/unregister driver
81 */
82 static const struct rtw_proc_hdl drv_proc_hdls[] = {
83 	{"ver_info", proc_get_drv_version, NULL},
84 	{"log_level", proc_get_log_level, proc_set_log_level},
85 };
86 
87 static const int drv_proc_hdls_num = sizeof(drv_proc_hdls) / sizeof(struct rtw_proc_hdl);
88 
rtw_drv_proc_open(struct inode * inode,struct file * file)89 static int rtw_drv_proc_open(struct inode *inode, struct file *file)
90 {
91 	/* struct net_device *dev = proc_get_parent_data(inode); */
92 	ssize_t index = (ssize_t)PDE_DATA(inode);
93 	const struct rtw_proc_hdl *hdl = drv_proc_hdls+index;
94 
95 	return single_open(file, hdl->show, NULL);
96 }
97 
rtw_drv_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * pos)98 static ssize_t rtw_drv_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
99 {
100 	ssize_t index = (ssize_t)PDE_DATA(file_inode(file));
101 	const struct rtw_proc_hdl *hdl = drv_proc_hdls+index;
102 	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *, void *) = hdl->write;
103 
104 	if (write)
105 		return write(file, buffer, count, pos, NULL);
106 
107 	return -EROFS;
108 }
109 
110 static const struct file_operations rtw_drv_proc_fops = {
111 	.owner = THIS_MODULE,
112 	.open = rtw_drv_proc_open,
113 	.read = seq_read,
114 	.llseek = seq_lseek,
115 	.release = single_release,
116 	.write = rtw_drv_proc_write,
117 };
118 
rtw_drv_proc_init(void)119 int rtw_drv_proc_init(void)
120 {
121 	int ret = _FAIL;
122 	ssize_t i;
123 	struct proc_dir_entry *entry = NULL;
124 
125 	if (rtw_proc != NULL) {
126 		rtw_warn_on(1);
127 		goto exit;
128 	}
129 
130 	rtw_proc = rtw_proc_create_dir(RTW_PROC_NAME, get_proc_net, NULL);
131 
132 	if (rtw_proc == NULL) {
133 		rtw_warn_on(1);
134 		goto exit;
135 	}
136 
137 	for (i = 0; i < drv_proc_hdls_num; i++) {
138 		entry = rtw_proc_create_entry(drv_proc_hdls[i].name, rtw_proc, &rtw_drv_proc_fops, (void *)i);
139 		if (!entry) {
140 			rtw_warn_on(1);
141 			goto exit;
142 		}
143 	}
144 
145 	ret = _SUCCESS;
146 
147 exit:
148 	return ret;
149 }
150 
rtw_drv_proc_deinit(void)151 void rtw_drv_proc_deinit(void)
152 {
153 	int i;
154 
155 	if (rtw_proc == NULL)
156 		return;
157 
158 	for (i = 0; i < drv_proc_hdls_num; i++)
159 		remove_proc_entry(drv_proc_hdls[i].name, rtw_proc);
160 
161 	remove_proc_entry(RTW_PROC_NAME, get_proc_net);
162 	rtw_proc = NULL;
163 }
164 
proc_get_sd_f0_reg_dump(struct seq_file * m,void * v)165 static int proc_get_sd_f0_reg_dump(struct seq_file *m, void *v)
166 {
167 	struct net_device *dev = m->private;
168 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
169 
170 	sd_f0_reg_dump(m, adapter);
171 
172 	return 0;
173 }
174 
proc_get_mac_reg_dump(struct seq_file * m,void * v)175 static int proc_get_mac_reg_dump(struct seq_file *m, void *v)
176 {
177 	struct net_device *dev = m->private;
178 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
179 
180 	mac_reg_dump(m, adapter);
181 
182 	return 0;
183 }
184 
proc_get_bb_reg_dump(struct seq_file * m,void * v)185 static int proc_get_bb_reg_dump(struct seq_file *m, void *v)
186 {
187 	struct net_device *dev = m->private;
188 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
189 
190 	bb_reg_dump(m, adapter);
191 
192 	return 0;
193 }
194 
proc_get_rf_reg_dump(struct seq_file * m,void * v)195 static int proc_get_rf_reg_dump(struct seq_file *m, void *v)
196 {
197 	struct net_device *dev = m->private;
198 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
199 
200 	rf_reg_dump(m, adapter);
201 
202 	return 0;
203 }
proc_get_linked_info_dump(struct seq_file * m,void * v)204 static int proc_get_linked_info_dump(struct seq_file *m, void *v)
205 {
206 	struct net_device *dev = m->private;
207 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
208 
209 	if (padapter)
210 		DBG_871X_SEL_NL(m, "linked_info_dump :%s\n", (padapter->bLinkInfoDump)?"enable":"disable");
211 
212 	return 0;
213 }
214 
proc_set_linked_info_dump(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)215 static ssize_t proc_set_linked_info_dump(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
216 {
217 	struct net_device *dev = data;
218 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
219 
220 	char tmp[2];
221 	int mode = 0;
222 
223 	if (count < 1)
224 		return -EFAULT;
225 
226 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
227 		if (padapter)
228 		{
229 			/* padapter->bLinkInfoDump = mode; */
230 			/* DBG_871X("linked_info_dump =%s\n", (padapter->bLinkInfoDump)?"enable":"disable"); */
231 			 linked_info_dump(padapter, mode);
232 		}
233 
234 	}
235 
236 	return count;
237 
238 }
239 
proc_get_rx_info(struct seq_file * m,void * v)240 static int proc_get_rx_info(struct seq_file *m, void *v)
241 {
242 	struct net_device *dev = m->private;
243 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
244 	struct dvobj_priv *psdpriv = padapter->dvobj;
245 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
246 
247 	/* Counts of packets whose seq_num is less than preorder_ctrl->indicate_seq, Ex delay, retransmission, redundant packets and so on */
248 	DBG_871X_SEL_NL(m,"Counts of Packets Whose Seq_Num Less Than Reorder Control Seq_Num: %llu\n", (unsigned long long)pdbgpriv->dbg_rx_ampdu_drop_count);
249 	/* How many times the Rx Reorder Timer is triggered. */
250 	DBG_871X_SEL_NL(m,"Rx Reorder Time-out Trigger Counts: %llu\n", (unsigned long long)pdbgpriv->dbg_rx_ampdu_forced_indicate_count);
251 	/* Total counts of packets loss */
252 	DBG_871X_SEL_NL(m,"Rx Packet Loss Counts: %llu\n", (unsigned long long)pdbgpriv->dbg_rx_ampdu_loss_count);
253 	DBG_871X_SEL_NL(m,"Duplicate Management Frame Drop Count: %llu\n", (unsigned long long)pdbgpriv->dbg_rx_dup_mgt_frame_drop_count);
254 	DBG_871X_SEL_NL(m,"AMPDU BA window shift Count: %llu\n", (unsigned long long)pdbgpriv->dbg_rx_ampdu_window_shift_cnt);
255 	return 0;
256 }
257 
258 
proc_reset_rx_info(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)259 static ssize_t proc_reset_rx_info(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
260 {
261 	struct net_device *dev = data;
262 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
263 	struct dvobj_priv *psdpriv = padapter->dvobj;
264 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
265 	char cmd[32];
266 	if (buffer && !copy_from_user(cmd, buffer, sizeof(cmd))) {
267 		if ('0' == cmd[0]) {
268 			pdbgpriv->dbg_rx_ampdu_drop_count = 0;
269 			pdbgpriv->dbg_rx_ampdu_forced_indicate_count = 0;
270 			pdbgpriv->dbg_rx_ampdu_loss_count = 0;
271 			pdbgpriv->dbg_rx_dup_mgt_frame_drop_count = 0;
272 			pdbgpriv->dbg_rx_ampdu_window_shift_cnt = 0;
273 		}
274 	}
275 
276 	return count;
277 }
278 
proc_get_cam(struct seq_file * m,void * v)279 static int proc_get_cam(struct seq_file *m, void *v)
280 {
281 	return 0;
282 }
283 
proc_set_cam(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)284 static ssize_t proc_set_cam(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
285 {
286 	struct net_device *dev = data;
287 	struct adapter *adapter;
288 
289 	char tmp[32];
290 	char cmd[5];
291 	u8 id;
292 
293 	adapter = (struct adapter *)rtw_netdev_priv(dev);
294 	if (!adapter)
295 		return -EFAULT;
296 
297 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
298 
299 		/* c <id>: clear specific cam entry */
300 		/* wfc <id>: write specific cam entry from cam cache */
301 
302 		int num = sscanf(tmp, "%4s %hhu", cmd, &id);
303 
304 		if (num < 2)
305 			return count;
306 		if (id >= TOTAL_CAM_ENTRY)
307 			return -EINVAL;
308 
309 		if (strcmp("c", cmd) == 0) {
310 			_clear_cam_entry(adapter, id);
311 			adapter->securitypriv.hw_decrypted = false; /* temporarily set this for TX path to use SW enc */
312 		} else if (strcmp("wfc", cmd) == 0) {
313 			write_cam_from_cache(adapter, id);
314 		}
315 	}
316 
317 	return count;
318 }
319 
proc_get_cam_cache(struct seq_file * m,void * v)320 static int proc_get_cam_cache(struct seq_file *m, void *v)
321 {
322 	struct net_device *dev = m->private;
323 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
324 	struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
325 	u8 i;
326 
327 	DBG_871X_SEL_NL(m, "cam bitmap:0x%016llx\n", dvobj->cam_ctl.bitmap);
328 
329 	DBG_871X_SEL_NL(m, "%-2s %-6s %-17s %-32s %-3s %-7s"
330 		/*  %-2s %-2s %-4s %-5s" */
331 		"\n"
332 		, "id", "ctrl", "addr", "key", "kid", "type"
333 		/*  "MK", "GK", "MFB", "valid" */
334 	);
335 
336 	for (i = 0; i < 32; i++) {
337 		if (dvobj->cam_cache[i].ctrl != 0)
338 			DBG_871X_SEL_NL(m, "%2u 0x%04x "MAC_FMT" "KEY_FMT" %3u %-7s"
339 				/*  %2u %2u 0x%02x %5u" */
340 				"\n", i
341 				, dvobj->cam_cache[i].ctrl
342 				, MAC_ARG(dvobj->cam_cache[i].mac)
343 				, KEY_ARG(dvobj->cam_cache[i].key)
344 				, (dvobj->cam_cache[i].ctrl)&0x03
345 				, security_type_str(((dvobj->cam_cache[i].ctrl)>>2)&0x07)
346 				/*  ((dvobj->cam_cache[i].ctrl)>>5)&0x01 */
347 				/*  ((dvobj->cam_cache[i].ctrl)>>6)&0x01 */
348 				/*  ((dvobj->cam_cache[i].ctrl)>>8)&0x7f */
349 				/*  ((dvobj->cam_cache[i].ctrl)>>15)&0x01 */
350 			);
351 	}
352 
353 	return 0;
354 }
355 
356 /*
357 * rtw_adapter_proc:
358 * init/deinit when register/unregister net_device
359 */
360 static const struct rtw_proc_hdl adapter_proc_hdls[] = {
361 	{"write_reg", proc_get_dummy, proc_set_write_reg},
362 	{"read_reg", proc_get_read_reg, proc_set_read_reg},
363 	{"fwstate", proc_get_fwstate, NULL},
364 	{"sec_info", proc_get_sec_info, NULL},
365 	{"mlmext_state", proc_get_mlmext_state, NULL},
366 	{"qos_option", proc_get_qos_option, NULL},
367 	{"ht_option", proc_get_ht_option, NULL},
368 	{"rf_info", proc_get_rf_info, NULL},
369 	{"survey_info", proc_get_survey_info, NULL},
370 	{"ap_info", proc_get_ap_info, NULL},
371 	{"adapter_state", proc_get_adapter_state, NULL},
372 	{"trx_info", proc_get_trx_info, NULL},
373 	{"rate_ctl", proc_get_rate_ctl, proc_set_rate_ctl},
374 	{"cam", proc_get_cam, proc_set_cam},
375 	{"cam_cache", proc_get_cam_cache, NULL},
376 	{"suspend_info", proc_get_suspend_resume_info, NULL},
377 	{"rx_info", proc_get_rx_info, proc_reset_rx_info},
378 
379 	{"roam_flags", proc_get_roam_flags, proc_set_roam_flags},
380 	{"roam_param", proc_get_roam_param, proc_set_roam_param},
381 	{"roam_tgt_addr", proc_get_dummy, proc_set_roam_tgt_addr},
382 
383 	{"sd_f0_reg_dump", proc_get_sd_f0_reg_dump, NULL},
384 
385 	{"fwdl_test_case", proc_get_dummy, proc_set_fwdl_test_case},
386 	{"wait_hiq_empty", proc_get_dummy, proc_set_wait_hiq_empty},
387 
388 	{"mac_reg_dump", proc_get_mac_reg_dump, NULL},
389 	{"bb_reg_dump", proc_get_bb_reg_dump, NULL},
390 	{"rf_reg_dump", proc_get_rf_reg_dump, NULL},
391 
392 	{"all_sta_info", proc_get_all_sta_info, NULL},
393 
394 	{"rx_signal", proc_get_rx_signal, proc_set_rx_signal},
395 	{"hw_info", proc_get_hw_status, NULL},
396 
397 	{"ht_enable", proc_get_ht_enable, proc_set_ht_enable},
398 	{"bw_mode", proc_get_bw_mode, proc_set_bw_mode},
399 	{"ampdu_enable", proc_get_ampdu_enable, proc_set_ampdu_enable},
400 	{"rx_stbc", proc_get_rx_stbc, proc_set_rx_stbc},
401 	{"rx_ampdu", proc_get_rx_ampdu, proc_set_rx_ampdu},
402 
403 	{"en_fwps", proc_get_en_fwps, proc_set_en_fwps},
404 
405 	/* path_rssi", proc_get_two_path_rssi, NULL}, */
406 	{"rssi_disp", proc_get_rssi_disp, proc_set_rssi_disp},
407 
408 	{"btcoex_dbg", proc_get_btcoex_dbg, proc_set_btcoex_dbg},
409 	{"btcoex", proc_get_btcoex_info, NULL},
410 
411 	{"linked_info_dump", proc_get_linked_info_dump, proc_set_linked_info_dump},
412 #ifdef CONFIG_DBG_COUNTER
413 	{"rx_logs", proc_get_rx_logs, NULL},
414 	{"tx_logs", proc_get_tx_logs, NULL},
415 	{"int_logs", proc_get_int_logs, NULL},
416 #endif
417 };
418 
419 static const int adapter_proc_hdls_num = sizeof(adapter_proc_hdls) / sizeof(struct rtw_proc_hdl);
420 
rtw_adapter_proc_open(struct inode * inode,struct file * file)421 static int rtw_adapter_proc_open(struct inode *inode, struct file *file)
422 {
423 	ssize_t index = (ssize_t)PDE_DATA(inode);
424 	const struct rtw_proc_hdl *hdl = adapter_proc_hdls+index;
425 
426 	return single_open(file, hdl->show, proc_get_parent_data(inode));
427 }
428 
rtw_adapter_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * pos)429 static ssize_t rtw_adapter_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
430 {
431 	ssize_t index = (ssize_t)PDE_DATA(file_inode(file));
432 	const struct rtw_proc_hdl *hdl = adapter_proc_hdls+index;
433 	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *, void *) = hdl->write;
434 
435 	if (write)
436 		return write(file, buffer, count, pos, ((struct seq_file *)file->private_data)->private);
437 
438 	return -EROFS;
439 }
440 
441 static const struct file_operations rtw_adapter_proc_fops = {
442 	.owner = THIS_MODULE,
443 	.open = rtw_adapter_proc_open,
444 	.read = seq_read,
445 	.llseek = seq_lseek,
446 	.release = single_release,
447 	.write = rtw_adapter_proc_write,
448 };
449 
proc_get_odm_dbg_comp(struct seq_file * m,void * v)450 int proc_get_odm_dbg_comp(struct seq_file *m, void *v)
451 {
452 	struct net_device *dev = m->private;
453 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
454 
455 	rtw_odm_dbg_comp_msg(m, adapter);
456 
457 	return 0;
458 }
459 
proc_set_odm_dbg_comp(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)460 ssize_t proc_set_odm_dbg_comp(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
461 {
462 	struct net_device *dev = data;
463 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
464 	char tmp[32];
465 
466 	u64 dbg_comp;
467 
468 	if (count < 1)
469 		return -EFAULT;
470 
471 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
472 
473 		int num = sscanf(tmp, "%llx", &dbg_comp);
474 
475 		if (num != 1)
476 			return count;
477 
478 		rtw_odm_dbg_comp_set(adapter, dbg_comp);
479 	}
480 
481 	return count;
482 }
483 
proc_get_odm_dbg_level(struct seq_file * m,void * v)484 int proc_get_odm_dbg_level(struct seq_file *m, void *v)
485 {
486 	struct net_device *dev = m->private;
487 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
488 
489 	rtw_odm_dbg_level_msg(m, adapter);
490 
491 	return 0;
492 }
493 
proc_set_odm_dbg_level(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)494 ssize_t proc_set_odm_dbg_level(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
495 {
496 	struct net_device *dev = data;
497 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
498 	char tmp[32];
499 
500 	u32 dbg_level;
501 
502 	if (count < 1)
503 		return -EFAULT;
504 
505 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
506 
507 		int num = sscanf(tmp, "%u", &dbg_level);
508 
509 		if (num != 1)
510 			return count;
511 
512 		rtw_odm_dbg_level_set(adapter, dbg_level);
513 	}
514 
515 	return count;
516 }
517 
proc_get_odm_ability(struct seq_file * m,void * v)518 static int proc_get_odm_ability(struct seq_file *m, void *v)
519 {
520 	struct net_device *dev = m->private;
521 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
522 
523 	rtw_odm_ability_msg(m, adapter);
524 
525 	return 0;
526 }
527 
proc_set_odm_ability(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)528 static ssize_t proc_set_odm_ability(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
529 {
530 	struct net_device *dev = data;
531 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(dev);
532 	char tmp[32];
533 
534 	u32 ability;
535 
536 	if (count < 1)
537 		return -EFAULT;
538 
539 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
540 
541 		int num = sscanf(tmp, "%x", &ability);
542 
543 		if (num != 1)
544 			return count;
545 
546 		rtw_odm_ability_set(adapter, ability);
547 	}
548 
549 	return count;
550 }
551 
proc_get_odm_adaptivity(struct seq_file * m,void * v)552 int proc_get_odm_adaptivity(struct seq_file *m, void *v)
553 {
554 	struct net_device *dev = m->private;
555 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
556 
557 	rtw_odm_adaptivity_parm_msg(m, padapter);
558 
559 	return 0;
560 }
561 
proc_set_odm_adaptivity(struct file * file,const char __user * buffer,size_t count,loff_t * pos,void * data)562 ssize_t proc_set_odm_adaptivity(struct file *file, const char __user *buffer, size_t count, loff_t *pos, void *data)
563 {
564 	struct net_device *dev = data;
565 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
566 	char tmp[32];
567 	u32 TH_L2H_ini;
568 	s8 TH_EDCCA_HL_diff;
569 	u32 IGI_Base;
570 	int ForceEDCCA;
571 	u8 AdapEn_RSSI;
572 	u8 IGI_LowerBound;
573 
574 	if (count < 1)
575 		return -EFAULT;
576 
577 	if (buffer && !copy_from_user(tmp, buffer, sizeof(tmp))) {
578 
579 		int num = sscanf(tmp, "%x %hhd %x %d %hhu %hhu",
580 			&TH_L2H_ini, &TH_EDCCA_HL_diff, &IGI_Base, &ForceEDCCA, &AdapEn_RSSI, &IGI_LowerBound);
581 
582 		if (num != 6)
583 			return count;
584 
585 		rtw_odm_adaptivity_parm_set(padapter, (s8)TH_L2H_ini, TH_EDCCA_HL_diff, (s8)IGI_Base, (bool)ForceEDCCA, AdapEn_RSSI, IGI_LowerBound);
586 	}
587 
588 	return count;
589 }
590 
591 /*
592 * rtw_odm_proc:
593 * init/deinit when register/unregister net_device, along with rtw_adapter_proc
594 */
595 static const struct rtw_proc_hdl odm_proc_hdls[] = {
596 	{"dbg_comp", proc_get_odm_dbg_comp, proc_set_odm_dbg_comp},
597 	{"dbg_level", proc_get_odm_dbg_level, proc_set_odm_dbg_level},
598 	{"ability", proc_get_odm_ability, proc_set_odm_ability},
599 	{"adaptivity", proc_get_odm_adaptivity, proc_set_odm_adaptivity},
600 };
601 
602 static const int odm_proc_hdls_num = sizeof(odm_proc_hdls) / sizeof(struct rtw_proc_hdl);
603 
rtw_odm_proc_open(struct inode * inode,struct file * file)604 static int rtw_odm_proc_open(struct inode *inode, struct file *file)
605 {
606 	ssize_t index = (ssize_t)PDE_DATA(inode);
607 	const struct rtw_proc_hdl *hdl = odm_proc_hdls+index;
608 
609 	return single_open(file, hdl->show, proc_get_parent_data(inode));
610 }
611 
rtw_odm_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * pos)612 static ssize_t rtw_odm_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
613 {
614 	ssize_t index = (ssize_t)PDE_DATA(file_inode(file));
615 	const struct rtw_proc_hdl *hdl = odm_proc_hdls+index;
616 	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *, void *) = hdl->write;
617 
618 	if (write)
619 		return write(file, buffer, count, pos, ((struct seq_file *)file->private_data)->private);
620 
621 	return -EROFS;
622 }
623 
624 static const struct file_operations rtw_odm_proc_fops = {
625 	.owner = THIS_MODULE,
626 	.open = rtw_odm_proc_open,
627 	.read = seq_read,
628 	.llseek = seq_lseek,
629 	.release = single_release,
630 	.write = rtw_odm_proc_write,
631 };
632 
rtw_odm_proc_init(struct net_device * dev)633 static struct proc_dir_entry *rtw_odm_proc_init(struct net_device *dev)
634 {
635 	struct proc_dir_entry *dir_odm = NULL;
636 	struct proc_dir_entry *entry = NULL;
637 	struct adapter	*adapter = rtw_netdev_priv(dev);
638 	ssize_t i;
639 
640 	if (adapter->dir_dev == NULL) {
641 		rtw_warn_on(1);
642 		goto exit;
643 	}
644 
645 	if (adapter->dir_odm != NULL) {
646 		rtw_warn_on(1);
647 		goto exit;
648 	}
649 
650 	dir_odm = rtw_proc_create_dir("odm", adapter->dir_dev, dev);
651 	if (dir_odm == NULL) {
652 		rtw_warn_on(1);
653 		goto exit;
654 	}
655 
656 	adapter->dir_odm = dir_odm;
657 
658 	for (i = 0; i < odm_proc_hdls_num; i++) {
659 		entry = rtw_proc_create_entry(odm_proc_hdls[i].name, dir_odm, &rtw_odm_proc_fops, (void *)i);
660 		if (!entry) {
661 			rtw_warn_on(1);
662 			goto exit;
663 		}
664 	}
665 
666 exit:
667 	return dir_odm;
668 }
669 
rtw_odm_proc_deinit(struct adapter * adapter)670 static void rtw_odm_proc_deinit(struct adapter	*adapter)
671 {
672 	struct proc_dir_entry *dir_odm = NULL;
673 	int i;
674 
675 	dir_odm = adapter->dir_odm;
676 
677 	if (dir_odm == NULL) {
678 		rtw_warn_on(1);
679 		return;
680 	}
681 
682 	for (i = 0; i < odm_proc_hdls_num; i++)
683 		remove_proc_entry(odm_proc_hdls[i].name, dir_odm);
684 
685 	remove_proc_entry("odm", adapter->dir_dev);
686 
687 	adapter->dir_odm = NULL;
688 }
689 
rtw_adapter_proc_init(struct net_device * dev)690 struct proc_dir_entry *rtw_adapter_proc_init(struct net_device *dev)
691 {
692 	struct proc_dir_entry *drv_proc = rtw_proc;
693 	struct proc_dir_entry *dir_dev = NULL;
694 	struct proc_dir_entry *entry = NULL;
695 	struct adapter *adapter = rtw_netdev_priv(dev);
696 	ssize_t i;
697 
698 	if (drv_proc == NULL) {
699 		rtw_warn_on(1);
700 		goto exit;
701 	}
702 
703 	if (adapter->dir_dev != NULL) {
704 		rtw_warn_on(1);
705 		goto exit;
706 	}
707 
708 	dir_dev = rtw_proc_create_dir(dev->name, drv_proc, dev);
709 	if (dir_dev == NULL) {
710 		rtw_warn_on(1);
711 		goto exit;
712 	}
713 
714 	adapter->dir_dev = dir_dev;
715 
716 	for (i = 0; i < adapter_proc_hdls_num; i++) {
717 		entry = rtw_proc_create_entry(adapter_proc_hdls[i].name, dir_dev, &rtw_adapter_proc_fops, (void *)i);
718 		if (!entry) {
719 			rtw_warn_on(1);
720 			goto exit;
721 		}
722 	}
723 
724 	rtw_odm_proc_init(dev);
725 
726 exit:
727 	return dir_dev;
728 }
729 
rtw_adapter_proc_deinit(struct net_device * dev)730 void rtw_adapter_proc_deinit(struct net_device *dev)
731 {
732 	struct proc_dir_entry *drv_proc = rtw_proc;
733 	struct proc_dir_entry *dir_dev = NULL;
734 	struct adapter *adapter = rtw_netdev_priv(dev);
735 	int i;
736 
737 	dir_dev = adapter->dir_dev;
738 
739 	if (dir_dev == NULL) {
740 		rtw_warn_on(1);
741 		return;
742 	}
743 
744 	for (i = 0; i < adapter_proc_hdls_num; i++)
745 		remove_proc_entry(adapter_proc_hdls[i].name, dir_dev);
746 
747 	rtw_odm_proc_deinit(adapter);
748 
749 	remove_proc_entry(dev->name, drv_proc);
750 
751 	adapter->dir_dev = NULL;
752 }
753 
rtw_adapter_proc_replace(struct net_device * dev)754 void rtw_adapter_proc_replace(struct net_device *dev)
755 {
756 	struct proc_dir_entry *drv_proc = rtw_proc;
757 	struct proc_dir_entry *dir_dev = NULL;
758 	struct adapter *adapter = rtw_netdev_priv(dev);
759 	int i;
760 
761 	dir_dev = adapter->dir_dev;
762 
763 	if (dir_dev == NULL) {
764 		rtw_warn_on(1);
765 		return;
766 	}
767 
768 	for (i = 0; i < adapter_proc_hdls_num; i++)
769 		remove_proc_entry(adapter_proc_hdls[i].name, dir_dev);
770 
771 	rtw_odm_proc_deinit(adapter);
772 
773 	remove_proc_entry(adapter->old_ifname, drv_proc);
774 
775 	adapter->dir_dev = NULL;
776 
777 	rtw_adapter_proc_init(dev);
778 
779 }
780 
781 #endif /* PROC_DEBUG */
782