1 /*
2  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3  * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /**
19  * DOC: wlan_hdd_sysfs_tdls_peers.c
20  *
21  * Implementation for creating sysfs file tdls_peers
22  */
23 
24 #include <wlan_hdd_includes.h>
25 #include "osif_vdev_sync.h"
26 #include "wlan_hdd_object_manager.h"
27 #include "wlan_hdd_sysfs_tdls_peers.h"
28 
29 /**
30  * __show_tdls_all_peers() - dump all TDLS peer info into output string
31  * @net_dev: net device
32  * @buf: output string buffer to hold the peer info
33  *
34  * Return: The size (in bytes) of the valid peer info in the output buffer
35  */
__show_tdls_all_peers(struct net_device * net_dev,char * buf)36 static int __show_tdls_all_peers(struct net_device *net_dev, char *buf)
37 {
38 	struct hdd_adapter *adapter = netdev_priv(net_dev);
39 	struct hdd_context *hdd_ctx;
40 	struct wlan_objmgr_vdev *vdev;
41 	int ret_val;
42 
43 	hdd_enter_dev(net_dev);
44 
45 	ret_val = scnprintf(buf, PAGE_SIZE, "%s     getTdlsPeers:",
46 			    net_dev->name);
47 
48 	if (hdd_validate_adapter(adapter)) {
49 		ret_val += scnprintf(buf + ret_val, PAGE_SIZE - ret_val,
50 				     "\nHDD adapter is not valid\n");
51 		goto exit;
52 	}
53 
54 	hdd_ctx = WLAN_HDD_GET_CTX(adapter);
55 	if (0 != (wlan_hdd_validate_context(hdd_ctx))) {
56 		ret_val += scnprintf(buf + ret_val, PAGE_SIZE - ret_val,
57 				     "\nHDD context is not valid\n");
58 		goto exit;
59 	}
60 
61 	if ((QDF_STA_MODE != adapter->device_mode) &&
62 	    (QDF_P2P_CLIENT_MODE != adapter->device_mode)) {
63 		ret_val += scnprintf(buf + ret_val, PAGE_SIZE - ret_val,
64 				     "\nNo TDLS support for this adapter\n");
65 		goto exit;
66 	}
67 
68 	vdev = hdd_objmgr_get_vdev_by_user(adapter->deflink, WLAN_OSIF_TDLS_ID);
69 	if (!vdev) {
70 		ret_val += scnprintf(buf + ret_val, PAGE_SIZE - ret_val,
71 				     "\nVDEV is NULL\n");
72 		goto exit;
73 	}
74 	ret_val += wlan_cfg80211_tdls_get_all_peers(vdev, buf + ret_val,
75 						    PAGE_SIZE - ret_val);
76 	hdd_objmgr_put_vdev_by_user(vdev, WLAN_OSIF_TDLS_ID);
77 
78 exit:
79 	if ((PAGE_SIZE - ret_val) > 0)
80 		ret_val += scnprintf(buf + ret_val, PAGE_SIZE - ret_val, "\n");
81 
82 	hdd_exit();
83 	return ret_val;
84 }
85 
show_tdls_all_peers(struct device * dev,struct device_attribute * attr,char * buf)86 static ssize_t show_tdls_all_peers(struct device *dev,
87 				   struct device_attribute *attr, char *buf)
88 {
89 	struct net_device *net_dev = container_of(dev, struct net_device, dev);
90 	struct osif_vdev_sync *vdev_sync;
91 	ssize_t err_size;
92 
93 	err_size = osif_vdev_sync_op_start(net_dev, &vdev_sync);
94 	if (err_size)
95 		return err_size;
96 
97 	err_size = __show_tdls_all_peers(net_dev, buf);
98 
99 	osif_vdev_sync_op_stop(vdev_sync);
100 
101 	return err_size;
102 }
103 
104 static DEVICE_ATTR(tdls_peers, 0444, show_tdls_all_peers, NULL);
105 
hdd_sysfs_tdls_peers_interface_create(struct hdd_adapter * adapter)106 void hdd_sysfs_tdls_peers_interface_create(struct hdd_adapter *adapter)
107 {
108 	int error;
109 
110 	error = device_create_file(&adapter->dev->dev,
111 				   &dev_attr_tdls_peers);
112 	if (error)
113 		hdd_err("could not create tdls_peers sysfs file");
114 }
115 
hdd_sysfs_tdls_peers_interface_destroy(struct hdd_adapter * adapter)116 void hdd_sysfs_tdls_peers_interface_destroy(struct hdd_adapter *adapter)
117 {
118 	device_remove_file(&adapter->dev->dev, &dev_attr_tdls_peers);
119 }
120