xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/mvl88w8977/mlinux/moal_proc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /**  @file moal_proc.c
2   *
3   * @brief This file contains functions for proc file.
4   *
5   * Copyright (C) 2008-2017, Marvell International Ltd.
6   *
7   * This software file (the "File") is distributed by Marvell International
8   * Ltd. under the terms of the GNU General Public License Version 2, June 1991
9   * (the "License").  You may use, redistribute and/or modify this File in
10   * accordance with the terms and conditions of the License, a copy of which
11   * is available by writing to the Free Software Foundation, Inc.,
12   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
13   * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
14   *
15   * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16   * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17   * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
18   * this warranty disclaimer.
19   *
20   */
21 
22 /********************************************************
23 Change log:
24     10/21/2008: initial version
25 ********************************************************/
26 
27 #include	"moal_main.h"
28 #ifdef UAP_SUPPORT
29 #include    "moal_uap.h"
30 #endif
31 #include    "moal_sdio.h"
32 
33 /********************************************************
34 		Local Variables
35 ********************************************************/
36 #ifdef CONFIG_PROC_FS
37 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
38 #define PROC_DIR	NULL
39 #define MWLAN_PROC_DIR  "mwlan/"
40 #define MWLAN_PROC  "mwlan"
41 /** Proc top level directory entry */
42 struct proc_dir_entry *proc_mwlan;
43 int proc_dir_entry_use_count;
44 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
45 #define PROC_DIR	(&proc_root)
46 #else
47 #define PROC_DIR	proc_net
48 #endif
49 
50 #ifdef STA_SUPPORT
51 static char *szModes[] = {
52 	"Unknown",
53 	"Managed",
54 	"Ad-hoc",
55 	"Auto",
56 };
57 #endif
58 
59 extern int drv_mode;
60 
61 /********************************************************
62 		Global Variables
63 ********************************************************/
64 
65 /********************************************************
66 		Local Functions
67 ********************************************************/
68 /**
69  *  @brief Proc read function for info
70  *
71  *  @param sfp      pointer to seq_file structure
72  *  @param data     void pointer to data
73  *
74  *  @return         Number of output data
75  */
76 static int
woal_info_proc_read(struct seq_file * sfp,void * data)77 woal_info_proc_read(struct seq_file *sfp, void *data)
78 {
79 	struct net_device *netdev = (struct net_device *)sfp->private;
80 	char fmt[MLAN_MAX_VER_STR_LEN];
81 	moal_private *priv = (moal_private *)netdev_priv(netdev);
82 #ifdef STA_SUPPORT
83 	int i = 0;
84 	moal_handle *handle = NULL;
85 	mlan_bss_info info;
86 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
87 	struct dev_mc_list *mcptr = netdev->mc_list;
88 	int mc_count = netdev->mc_count;
89 #else
90 	struct netdev_hw_addr *mcptr = NULL;
91 	int mc_count = netdev_mc_count(netdev);
92 #endif /* < 2.6.35 */
93 #else
94 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
95 	int i = 0;
96 #endif /* >= 2.6.29 */
97 #endif
98 #ifdef UAP_SUPPORT
99 	mlan_ds_uap_stats ustats;
100 #endif
101 
102 	ENTER();
103 
104 	if (priv == NULL)
105 		goto exit;
106 #ifdef STA_SUPPORT
107 	handle = priv->phandle;
108 	if (handle == NULL)
109 		goto exit;
110 #endif
111 
112 	if (!MODULE_GET) {
113 		LEAVE();
114 		return 0;
115 	}
116 
117 	memset(fmt, 0, sizeof(fmt));
118 #ifdef UAP_SUPPORT
119 	if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_UAP) {
120 		seq_printf(sfp, "driver_name = " "\"uap\"\n");
121 		woal_uap_get_version(priv, fmt, sizeof(fmt) - 1);
122 		if (MLAN_STATUS_SUCCESS !=
123 		    woal_uap_get_stats(priv, MOAL_IOCTL_WAIT, &ustats)) {
124 			MODULE_PUT;
125 			LEAVE();
126 			return -EFAULT;
127 		}
128 	}
129 #endif /* UAP_SUPPORT */
130 #ifdef STA_SUPPORT
131 	memset(&info, 0, sizeof(info));
132 	if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_STA) {
133 		woal_get_version(handle, fmt, sizeof(fmt) - 1);
134 		if (MLAN_STATUS_SUCCESS !=
135 		    woal_get_bss_info(priv, MOAL_IOCTL_WAIT, &info)) {
136 			MODULE_PUT;
137 			LEAVE();
138 			return -EFAULT;
139 		}
140 		seq_printf(sfp, "driver_name = " "\"wlan\"\n");
141 	}
142 #endif
143 	seq_printf(sfp, "driver_version = %s", fmt);
144 	seq_printf(sfp, "\ninterface_name=\"%s\"\n", netdev->name);
145 #if defined(WIFI_DIRECT_SUPPORT)
146 	if (priv->bss_type == MLAN_BSS_TYPE_WIFIDIRECT) {
147 		if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_STA)
148 			seq_printf(sfp, "bss_mode = \"WIFIDIRECT-Client\"\n");
149 		else
150 			seq_printf(sfp, "bss_mode = \"WIFIDIRECT-GO\"\n");
151 	}
152 #endif
153 #ifdef STA_SUPPORT
154 	if (priv->bss_type == MLAN_BSS_TYPE_STA)
155 		seq_printf(sfp, "bss_mode =\"%s\"\n", szModes[info.bss_mode]);
156 #endif
157 	seq_printf(sfp, "media_state=\"%s\"\n",
158 		   ((priv->media_connected ==
159 		     MFALSE) ? "Disconnected" : "Connected"));
160 	seq_printf(sfp, "mac_address=\"%02x:%02x:%02x:%02x:%02x:%02x\"\n",
161 		   netdev->dev_addr[0], netdev->dev_addr[1],
162 		   netdev->dev_addr[2], netdev->dev_addr[3],
163 		   netdev->dev_addr[4], netdev->dev_addr[5]);
164 #ifdef STA_SUPPORT
165 	if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_STA) {
166 		seq_printf(sfp, "multicast_count=\"%d\"\n", mc_count);
167 		seq_printf(sfp, "essid=\"%s\"\n", info.ssid.ssid);
168 		seq_printf(sfp, "bssid=\"%02x:%02x:%02x:%02x:%02x:%02x\"\n",
169 			   info.bssid[0], info.bssid[1],
170 			   info.bssid[2], info.bssid[3],
171 			   info.bssid[4], info.bssid[5]);
172 		seq_printf(sfp, "channel=\"%d\"\n", (int)info.bss_chan);
173 		seq_printf(sfp, "region_code = \"%02x\"\n",
174 			   (t_u8)info.region_code);
175 
176 		/*
177 		 * Put out the multicast list
178 		 */
179 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
180 		for (i = 0; i < netdev->mc_count; i++) {
181 			seq_printf(sfp,
182 				   "multicast_address[%d]=\"%02x:%02x:%02x:%02x:%02x:%02x\"\n",
183 				   i,
184 				   mcptr->dmi_addr[0], mcptr->dmi_addr[1],
185 				   mcptr->dmi_addr[2], mcptr->dmi_addr[3],
186 				   mcptr->dmi_addr[4], mcptr->dmi_addr[5]);
187 
188 			mcptr = mcptr->next;
189 		}
190 #else
191 		netdev_for_each_mc_addr(mcptr, netdev)
192 			seq_printf(sfp,
193 				   "multicast_address[%d]=\"%02x:%02x:%02x:%02x:%02x:%02x\"\n",
194 				   i++,
195 				   mcptr->addr[0], mcptr->addr[1],
196 				   mcptr->addr[2], mcptr->addr[3],
197 				   mcptr->addr[4], mcptr->addr[5]);
198 #endif /* < 2.6.35 */
199 	}
200 #endif
201 	seq_printf(sfp, "num_tx_bytes = %lu\n", priv->stats.tx_bytes);
202 	seq_printf(sfp, "num_rx_bytes = %lu\n", priv->stats.rx_bytes);
203 	seq_printf(sfp, "num_tx_pkts = %lu\n", priv->stats.tx_packets);
204 	seq_printf(sfp, "num_rx_pkts = %lu\n", priv->stats.rx_packets);
205 	seq_printf(sfp, "num_tx_pkts_dropped = %lu\n", priv->stats.tx_dropped);
206 	seq_printf(sfp, "num_rx_pkts_dropped = %lu\n", priv->stats.rx_dropped);
207 	seq_printf(sfp, "num_tx_pkts_err = %lu\n", priv->stats.tx_errors);
208 	seq_printf(sfp, "num_rx_pkts_err = %lu\n", priv->stats.rx_errors);
209 	seq_printf(sfp, "carrier %s\n",
210 		   ((netif_carrier_ok(priv->netdev)) ? "on" : "off"));
211 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
212 	for (i = 0; i < netdev->num_tx_queues; i++) {
213 		seq_printf(sfp, "tx queue %d:  %s\n", i,
214 			   ((netif_tx_queue_stopped
215 			     (netdev_get_tx_queue(netdev, 0))) ? "stopped" :
216 			    "started"));
217 	}
218 #else
219 	seq_printf(sfp, "tx queue %s\n",
220 		   ((netif_queue_stopped(priv->netdev)) ? "stopped" :
221 		    "started"));
222 #endif
223 #ifdef UAP_SUPPORT
224 	if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_UAP) {
225 		seq_printf(sfp, "tkip_mic_failures = %u\n",
226 			   ustats.tkip_mic_failures);
227 		seq_printf(sfp, "ccmp_decrypt_errors = %u\n",
228 			   ustats.ccmp_decrypt_errors);
229 		seq_printf(sfp, "wep_undecryptable_count = %u\n",
230 			   ustats.wep_undecryptable_count);
231 		seq_printf(sfp, "wep_icv_error_count = %u\n",
232 			   ustats.wep_icv_error_count);
233 		seq_printf(sfp, "decrypt_failure_count = %u\n",
234 			   ustats.decrypt_failure_count);
235 		seq_printf(sfp, "mcast_tx_count = %u\n", ustats.mcast_tx_count);
236 		seq_printf(sfp, "failed_count = %u\n", ustats.failed_count);
237 		seq_printf(sfp, "retry_count = %u\n", ustats.retry_count);
238 		seq_printf(sfp, "multiple_retry_count = %u\n",
239 			   ustats.multi_retry_count);
240 		seq_printf(sfp, "frame_duplicate_count = %u\n",
241 			   ustats.frame_dup_count);
242 		seq_printf(sfp, "rts_success_count = %u\n",
243 			   ustats.rts_success_count);
244 		seq_printf(sfp, "rts_failure_count = %u\n",
245 			   ustats.rts_failure_count);
246 		seq_printf(sfp, "ack_failure_count = %u\n",
247 			   ustats.ack_failure_count);
248 		seq_printf(sfp, "rx_fragment_count = %u\n",
249 			   ustats.rx_fragment_count);
250 		seq_printf(sfp, "mcast_rx_frame_count = %u\n",
251 			   ustats.mcast_rx_frame_count);
252 		seq_printf(sfp, "fcs_error_count = %u\n",
253 			   ustats.fcs_error_count);
254 		seq_printf(sfp, "tx_frame_count = %u\n", ustats.tx_frame_count);
255 		seq_printf(sfp, "rsna_tkip_cm_invoked = %u\n",
256 			   ustats.rsna_tkip_cm_invoked);
257 		seq_printf(sfp, "rsna_4way_hshk_failures = %u\n",
258 			   ustats.rsna_4way_hshk_failures);
259 	}
260 #endif /* UAP_SUPPORT */
261 exit:
262 	LEAVE();
263 	MODULE_PUT;
264 	return 0;
265 }
266 
267 static int
woal_info_proc_open(struct inode * inode,struct file * file)268 woal_info_proc_open(struct inode *inode, struct file *file)
269 {
270 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
271 	return single_open(file, woal_info_proc_read, PDE_DATA(inode));
272 #else
273 	return single_open(file, woal_info_proc_read, PDE(inode)->data);
274 #endif
275 }
276 
277 static const struct file_operations info_proc_fops = {
278 	.owner = THIS_MODULE,
279 	.open = woal_info_proc_open,
280 	.read = seq_read,
281 	.llseek = seq_lseek,
282 	.release = single_release,
283 };
284 
285 #define     CMD52_STR_LEN   50
286 /*
287  *  @brief Parse cmd52 string
288  *
289  *  @param buffer   A pointer user buffer
290  *  @param len      Length user buffer
291  *  @param func     Parsed func number
292  *  @param reg      Parsed reg value
293  *  @param val      Parsed value to set
294  *  @return         BT_STATUS_SUCCESS
295  */
296 static int
parse_cmd52_string(const char * buffer,size_t len,int * func,int * reg,int * val)297 parse_cmd52_string(const char *buffer, size_t len, int *func, int *reg,
298 		   int *val)
299 {
300 	int ret = MLAN_STATUS_SUCCESS;
301 	char *string = NULL;
302 	char *pos = NULL;
303 	gfp_t flag;
304 
305 	ENTER();
306 	flag = (in_atomic() || irqs_disabled())? GFP_ATOMIC : GFP_KERNEL;
307 	string = kzalloc(CMD52_STR_LEN, flag);
308 	if (string == NULL)
309 		return -ENOMEM;
310 
311 	memcpy(string, buffer + strlen("sdcmd52rw="),
312 	       MIN((CMD52_STR_LEN - 1), (len - strlen("sdcmd52rw="))));
313 	string = strstrip(string);
314 
315 	*func = -1;
316 	*reg = -1;
317 	*val = -1;
318 
319 	/* Get func */
320 	pos = strsep(&string, " \t");
321 	if (pos)
322 		*func = woal_string_to_number(pos);
323 
324 	/* Get reg */
325 	pos = strsep(&string, " \t");
326 	if (pos)
327 		*reg = woal_string_to_number(pos);
328 
329 	/* Get val (optional) */
330 	pos = strsep(&string, " \t");
331 	if (pos)
332 		*val = woal_string_to_number(pos);
333 	kfree(string);
334 	LEAVE();
335 	return ret;
336 }
337 
338 /**
339  *  @brief config proc write function
340  *
341  *  @param f        file pointer
342  *  @param buf      pointer to data buffer
343  *  @param count    data number to write
344  *  @param off      Offset
345  *
346  *  @return         number of data
347  */
348 static ssize_t
woal_config_write(struct file * f,const char __user * buf,size_t count,loff_t * off)349 woal_config_write(struct file *f, const char __user * buf, size_t count,
350 		  loff_t * off)
351 {
352 	char databuf[101];
353 	char *line = NULL;
354 	t_u32 config_data = 0;
355 	struct seq_file *sfp = f->private_data;
356 	moal_handle *handle = (moal_handle *)sfp->private;
357 
358 	int func = 0, reg = 0, val = 0;
359 	int copy_len;
360 	moal_private *priv = NULL;
361 
362 	ENTER();
363 	if (!MODULE_GET) {
364 		LEAVE();
365 		return 0;
366 	}
367 
368 	if (count >= sizeof(databuf)) {
369 		MODULE_PUT;
370 		LEAVE();
371 		return (int)count;
372 	}
373 	memset(databuf, 0, sizeof(databuf));
374 	copy_len = MIN((sizeof(databuf) - 1), count);
375 	if (copy_from_user(databuf, buf, copy_len)) {
376 		MODULE_PUT;
377 		LEAVE();
378 		return 0;
379 	}
380 	line = databuf;
381 	if (!strncmp(databuf, "soft_reset", strlen("soft_reset"))) {
382 		line += strlen("soft_reset") + 1;
383 		config_data = (t_u32)woal_string_to_number(line);
384 		PRINTM(MINFO, "soft_reset: %d\n", (int)config_data);
385 		if (woal_request_soft_reset(handle) == MLAN_STATUS_SUCCESS)
386 			handle->hardware_status = HardwareStatusReset;
387 		else
388 			PRINTM(MERROR, "Could not perform soft reset\n");
389 	}
390 	if (!strncmp(databuf, "drv_mode", strlen("drv_mode"))) {
391 		line += strlen("drv_mode") + 1;
392 		config_data = (t_u32)woal_string_to_number(line);
393 		PRINTM(MINFO, "drv_mode: %d\n", (int)config_data);
394 		if (config_data != (t_u32)drv_mode)
395 			if (woal_switch_drv_mode(handle, config_data) !=
396 			    MLAN_STATUS_SUCCESS) {
397 				PRINTM(MERROR, "Could not switch drv mode\n");
398 			}
399 	}
400 	if (!strncmp(databuf, "sdcmd52rw=", strlen("sdcmd52rw=")) &&
401 	    count > strlen("sdcmd52rw=")) {
402 		parse_cmd52_string((const char *)databuf, (size_t) count, &func,
403 				   &reg, &val);
404 		woal_sdio_read_write_cmd52(handle, func, reg, val);
405 	}
406 	if (!strncmp(databuf, "debug_dump", strlen("debug_dump"))) {
407 		priv = woal_get_priv(handle, MLAN_BSS_ROLE_ANY);
408 		if (priv) {
409 			PRINTM(MERROR, "Recevie debug_dump command\n");
410 #ifdef DEBUG_LEVEL1
411 			drvdbg &= ~MFW_D;
412 #endif
413 			woal_mlan_debug_info(priv);
414 			woal_moal_debug_info(priv, NULL, MFALSE);
415 
416 			woal_dump_firmware_info_v3(handle);
417 		}
418 	}
419 
420 	if (!strncmp(databuf, "fwdump_file=", strlen("fwdump_file="))) {
421 		int len = copy_len - strlen("fwdump_file=");
422 		gfp_t flag;
423 		if (len) {
424 			kfree(handle->fwdump_fname);
425 			flag = (in_atomic() ||
426 				irqs_disabled())? GFP_ATOMIC : GFP_KERNEL;
427 			handle->fwdump_fname = kzalloc(len, flag);
428 			if (handle->fwdump_fname)
429 				memcpy(handle->fwdump_fname,
430 				       databuf + strlen("fwdump_file="),
431 				       len - 1);
432 		}
433 	}
434 	if (!strncmp(databuf, "fw_reload", strlen("fw_reload"))) {
435 		if (!strncmp(databuf, "fw_reload=", strlen("fw_reload="))) {
436 			line += strlen("fw_reload") + 1;
437 			config_data = (t_u32)woal_string_to_number(line);
438 		} else
439 			config_data = FW_RELOAD_SDIO_INBAND_RESET;
440 		PRINTM(MMSG, "Request fw_reload=%d\n", config_data);
441 		woal_request_fw_reload(handle, config_data);
442 	}
443 	MODULE_PUT;
444 	LEAVE();
445 	return (int)count;
446 }
447 
448 /**
449  *  @brief config proc read function
450  *
451  *  @param sfp      pointer to seq_file structure
452  *  @param data     Void pointer to data
453  *
454  *  @return         number of output data
455  */
456 static int
woal_config_read(struct seq_file * sfp,void * data)457 woal_config_read(struct seq_file *sfp, void *data)
458 {
459 	moal_handle *handle = (moal_handle *)sfp->private;
460 
461 	ENTER();
462 
463 	if (!MODULE_GET) {
464 		LEAVE();
465 		return 0;
466 	}
467 
468 	seq_printf(sfp, "hardware_status=%d\n", (int)handle->hardware_status);
469 	seq_printf(sfp, "netlink_num=%d\n", (int)handle->netlink_num);
470 	seq_printf(sfp, "drv_mode=%d\n", (int)drv_mode);
471 	seq_printf(sfp, "sdcmd52rw=%d 0x%0x 0x%02X\n", handle->cmd52_func,
472 		   handle->cmd52_reg, handle->cmd52_val);
473 
474 	MODULE_PUT;
475 	LEAVE();
476 	return 0;
477 }
478 
479 static int
woal_config_proc_open(struct inode * inode,struct file * file)480 woal_config_proc_open(struct inode *inode, struct file *file)
481 {
482 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
483 	return single_open(file, woal_config_read, PDE_DATA(inode));
484 #else
485 	return single_open(file, woal_config_read, PDE(inode)->data);
486 #endif
487 }
488 
489 static const struct file_operations config_proc_fops = {
490 	.owner = THIS_MODULE,
491 	.open = woal_config_proc_open,
492 	.read = seq_read,
493 	.llseek = seq_lseek,
494 	.release = single_release,
495 	.write = woal_config_write,
496 };
497 
498 /********************************************************
499 		Global Functions
500 ********************************************************/
501 /**
502  *  @brief Convert string to number
503  *
504  *  @param s        Pointer to numbered string
505  *
506  *  @return         Converted number from string s
507  */
508 int
woal_string_to_number(char * s)509 woal_string_to_number(char *s)
510 {
511 	int r = 0;
512 	int base = 0;
513 	int pn = 1;
514 
515 	if (!strncmp(s, "-", 1)) {
516 		pn = -1;
517 		s++;
518 	}
519 	if (!strncmp(s, "0x", 2) || !strncmp(s, "0X", 2)) {
520 		base = 16;
521 		s += 2;
522 	} else
523 		base = 10;
524 
525 	for (s = s; *s; s++) {
526 		if ((*s >= '0') && (*s <= '9'))
527 			r = (r * base) + (*s - '0');
528 		else if ((*s >= 'A') && (*s <= 'F'))
529 			r = (r * base) + (*s - 'A' + 10);
530 		else if ((*s >= 'a') && (*s <= 'f'))
531 			r = (r * base) + (*s - 'a' + 10);
532 		else
533 			break;
534 	}
535 
536 	return r * pn;
537 }
538 
539 /**
540  *  @brief Create the top level proc directory
541  *
542  *  @param handle   Pointer to woal_handle
543  *
544  *  @return         N/A
545  */
546 void
woal_proc_init(moal_handle * handle)547 woal_proc_init(moal_handle *handle)
548 {
549 	struct proc_dir_entry *r;
550 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
551 	struct proc_dir_entry *pde = PROC_DIR;
552 #endif
553 	char config_proc_dir[20];
554 
555 	ENTER();
556 
557 	PRINTM(MINFO, "Create Proc Interface\n");
558 	if (!handle->proc_mwlan) {
559 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
560 		/* Check if directory already exists */
561 		for (pde = pde->subdir; pde; pde = pde->next) {
562 			if (pde->namelen && !strcmp(MWLAN_PROC, pde->name)) {
563 				/* Directory exists */
564 				PRINTM(MWARN,
565 				       "proc interface already exists!\n");
566 				handle->proc_mwlan = pde;
567 				break;
568 			}
569 		}
570 		if (pde == NULL) {
571 			handle->proc_mwlan = proc_mkdir(MWLAN_PROC, PROC_DIR);
572 			if (!handle->proc_mwlan)
573 				PRINTM(MERROR,
574 				       "Cannot create proc interface!\n");
575 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
576 			else
577 				atomic_set(&handle->proc_mwlan->count, 1);
578 #endif
579 		}
580 #else
581 		if (!proc_mwlan) {
582 			handle->proc_mwlan = proc_mkdir(MWLAN_PROC, PROC_DIR);
583 			if (!handle->proc_mwlan) {
584 				PRINTM(MERROR,
585 				       "Cannot create proc interface!\n");
586 			}
587 		} else {
588 			handle->proc_mwlan = proc_mwlan;
589 		}
590 #endif
591 		if (handle->proc_mwlan) {
592 			if (handle->handle_idx)
593 				sprintf(config_proc_dir, "config%d",
594 					handle->handle_idx);
595 			else
596 				strcpy(config_proc_dir, "config");
597 
598 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
599 			r = proc_create_data(config_proc_dir, 0644,
600 					     handle->proc_mwlan,
601 					     &config_proc_fops, handle);
602 			if (r == NULL)
603 #else
604 			r = create_proc_entry(config_proc_dir, 0644,
605 					      handle->proc_mwlan);
606 			if (r) {
607 				r->data = handle;
608 				r->proc_fops = &config_proc_fops;
609 			} else
610 #endif
611 				PRINTM(MMSG, "Fail to create proc config\n");
612 			proc_dir_entry_use_count++;
613 		}
614 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 26)
615 		proc_mwlan = handle->proc_mwlan;
616 #endif
617 	}
618 
619 	LEAVE();
620 }
621 
622 /**
623  *  @brief Remove the top level proc directory
624  *
625  *  @param handle   pointer moal_handle
626  *
627  *  @return         N/A
628  */
629 void
woal_proc_exit(moal_handle * handle)630 woal_proc_exit(moal_handle *handle)
631 {
632 	ENTER();
633 
634 	PRINTM(MINFO, "Remove Proc Interface\n");
635 	if (handle->proc_mwlan) {
636 		char config_proc_dir[20];
637 		if (handle->handle_idx)
638 			sprintf(config_proc_dir, "config%d",
639 				handle->handle_idx);
640 		else
641 			strcpy(config_proc_dir, "config");
642 		remove_proc_entry(config_proc_dir, handle->proc_mwlan);
643 
644 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
645 		/* Remove only if we are the only instance using this */
646 		if (atomic_read(&(handle->proc_mwlan->count)) > 1) {
647 			PRINTM(MWARN, "More than one interface using proc!\n");
648 		} else {
649 #endif
650 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
651 			atomic_dec(&(handle->proc_mwlan->count));
652 #endif
653 			if (!--proc_dir_entry_use_count) {
654 				remove_proc_entry(MWLAN_PROC, PROC_DIR);
655 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 26)
656 				proc_mwlan = NULL;
657 #endif
658 			}
659 
660 			handle->proc_mwlan = NULL;
661 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
662 		}
663 #endif
664 	}
665 
666 	LEAVE();
667 }
668 
669 /**
670  *  @brief Create proc file for interface
671  *
672  *  @param priv     pointer moal_private
673  *
674  *  @return         N/A
675  */
676 void
woal_create_proc_entry(moal_private * priv)677 woal_create_proc_entry(moal_private *priv)
678 {
679 	struct proc_dir_entry *r;
680 	struct net_device *dev = priv->netdev;
681 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
682 	char proc_dir_name[22];
683 #endif
684 
685 	ENTER();
686 
687 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
688 	if (!priv->proc_entry) {
689 		memset(proc_dir_name, 0, sizeof(proc_dir_name));
690 		strcpy(proc_dir_name, MWLAN_PROC_DIR);
691 
692 		if (strlen(dev->name) >
693 		    ((sizeof(proc_dir_name) - 1) - strlen(MWLAN_PROC_DIR))) {
694 			PRINTM(MERROR,
695 			       "Failed to create proc entry, device name is too long\n");
696 			LEAVE();
697 			return;
698 		}
699 		strcat(proc_dir_name, dev->name);
700 		/* Try to create mwlan/mlanX first */
701 		priv->proc_entry = proc_mkdir(proc_dir_name, PROC_DIR);
702 		if (priv->proc_entry) {
703 			/* Success. Continue normally */
704 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
705 			if (!priv->phandle->proc_mwlan) {
706 				priv->phandle->proc_mwlan =
707 					priv->proc_entry->parent;
708 			}
709 			atomic_inc(&(priv->phandle->proc_mwlan->count));
710 #endif
711 		} else {
712 			/* Failure. mwlan may not exist. Try to create that first */
713 			priv->phandle->proc_mwlan =
714 				proc_mkdir(MWLAN_PROC, PROC_DIR);
715 			if (!priv->phandle->proc_mwlan) {
716 				/* Failure. Something broken */
717 				LEAVE();
718 				return;
719 			} else {
720 				/* Success. Now retry creating mlanX */
721 				priv->proc_entry =
722 					proc_mkdir(proc_dir_name, PROC_DIR);
723 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
724 				atomic_inc(&(priv->phandle->proc_mwlan->count));
725 #endif
726 			}
727 		}
728 #else
729 	if (priv->phandle->proc_mwlan && !priv->proc_entry) {
730 		priv->proc_entry =
731 			proc_mkdir(dev->name, priv->phandle->proc_mwlan);
732 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
733 		atomic_inc(&(priv->phandle->proc_mwlan->count));
734 #endif /* < 3.10.0 */
735 #endif /* < 2.6.26 */
736 		strcpy(priv->proc_entry_name, dev->name);
737 		if (priv->proc_entry) {
738 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
739 			r = proc_create_data("info", 0, priv->proc_entry,
740 					     &info_proc_fops, dev);
741 			if (r == NULL)
742 #else
743 			r = create_proc_entry("info", 0, priv->proc_entry);
744 			if (r) {
745 				r->data = dev;
746 				r->proc_fops = &info_proc_fops;
747 			} else
748 #endif
749 				PRINTM(MMSG, "Fail to create proc info\n");
750 		}
751 	}
752 
753 	LEAVE();
754 }
755 
756 /**
757  *  @brief Remove proc file
758  *
759  *  @param priv     Pointer moal_private
760  *
761  *  @return         N/A
762  */
763 void
764 woal_proc_remove(moal_private *priv)
765 {
766 	ENTER();
767 	if (priv->phandle->proc_mwlan && priv->proc_entry) {
768 		remove_proc_entry("info", priv->proc_entry);
769 		remove_proc_entry(priv->proc_entry_name,
770 				  priv->phandle->proc_mwlan);
771 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
772 		atomic_dec(&(priv->phandle->proc_mwlan->count));
773 #endif
774 		priv->proc_entry = NULL;
775 	}
776 	LEAVE();
777 }
778 #endif
779