sysfs.c 4.21 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7
/*
 * This file provides /sys/class/ieee80211/<wiphy name>/
 * and some default attributes.
 *
 * Copyright 2005-2006	Jiri Benc <jbenc@suse.cz>
 * Copyright 2006	Johannes Berg <johannes@sipsolutions.net>
8
 * Copyright (C) 2020-2021, 2023 Intel Corporation
9 10 11 12 13 14 15 16 17 18
 */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/nl80211.h>
#include <linux/rtnetlink.h>
#include <net/cfg80211.h>
#include "sysfs.h"
#include "core.h"
19
#include "rdev-ops.h"
20 21 22 23 24 25 26

static inline struct cfg80211_registered_device *dev_to_rdev(
	struct device *dev)
{
	return container_of(dev, struct cfg80211_registered_device, wiphy.dev);
}

27 28 29 30 31 32
#define SHOW_FMT(name, fmt, member)					\
static ssize_t name ## _show(struct device *dev,			\
			      struct device_attribute *attr,		\
			      char *buf)				\
{									\
	return sprintf(buf, fmt "\n", dev_to_rdev(dev)->member);	\
33 34
}									\
static DEVICE_ATTR_RO(name)
35

36
SHOW_FMT(index, "%d", wiphy_idx);
37
SHOW_FMT(macaddress, "%pM", wiphy.perm_addr);
38 39
SHOW_FMT(address_mask, "%pM", wiphy.addr_mask);

40 41
static ssize_t name_show(struct device *dev,
			 struct device_attribute *attr,
42 43
			 char *buf)
{
44
	struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
45 46

	return sprintf(buf, "%s\n", wiphy_name(wiphy));
47
}
48
static DEVICE_ATTR_RO(name);
49

50 51 52 53 54 55 56 57 58 59 60 61
static ssize_t addresses_show(struct device *dev,
			      struct device_attribute *attr,
			      char *buf)
{
	struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
	char *start = buf;
	int i;

	if (!wiphy->addresses)
		return sprintf(buf, "%pM\n", wiphy->perm_addr);

	for (i = 0; i < wiphy->n_addresses; i++)
62
		buf += sprintf(buf, "%pM\n", wiphy->addresses[i].addr);
63 64 65

	return buf - start;
}
66 67 68 69 70 71 72 73 74
static DEVICE_ATTR_RO(addresses);

static struct attribute *ieee80211_attrs[] = {
	&dev_attr_index.attr,
	&dev_attr_macaddress.attr,
	&dev_attr_address_mask.attr,
	&dev_attr_addresses.attr,
	&dev_attr_name.attr,
	NULL,
75
};
76
ATTRIBUTE_GROUPS(ieee80211);
77 78 79 80 81 82 83 84

static void wiphy_dev_release(struct device *dev)
{
	struct cfg80211_registered_device *rdev = dev_to_rdev(dev);

	cfg80211_dev_free(rdev);
}

85
#ifdef CONFIG_PM_SLEEP
86 87 88 89
static void cfg80211_leave_all(struct cfg80211_registered_device *rdev)
{
	struct wireless_dev *wdev;

90
	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
91 92 93
		cfg80211_leave(rdev, wdev);
}

94
static int wiphy_suspend(struct device *dev)
Johannes Berg's avatar
Johannes Berg committed
95 96 97 98
{
	struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
	int ret = 0;

99
	rdev->suspend_at = ktime_get_boottime_seconds();
100

101
	rtnl_lock();
102
	wiphy_lock(&rdev->wiphy);
103
	if (rdev->wiphy.registered) {
104
		if (!rdev->wiphy.wowlan_config) {
105
			cfg80211_leave_all(rdev);
106 107
			cfg80211_process_rdev_events(rdev);
		}
108
		cfg80211_process_wiphy_works(rdev);
109
		if (rdev->ops->suspend)
110
			ret = rdev_suspend(rdev, rdev->wiphy.wowlan_config);
111 112 113
		if (ret == 1) {
			/* Driver refuse to configure wowlan */
			cfg80211_leave_all(rdev);
114
			cfg80211_process_rdev_events(rdev);
115
			cfg80211_process_wiphy_works(rdev);
116 117
			ret = rdev_suspend(rdev, NULL);
		}
118 119
		if (ret == 0)
			rdev->suspended = true;
Johannes Berg's avatar
Johannes Berg committed
120
	}
121
	wiphy_unlock(&rdev->wiphy);
122
	rtnl_unlock();
Johannes Berg's avatar
Johannes Berg committed
123 124 125 126 127 128 129 130 131

	return ret;
}

static int wiphy_resume(struct device *dev)
{
	struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
	int ret = 0;

132
	/* Age scan results with time spent in suspend */
133
	cfg80211_bss_age(rdev, ktime_get_boottime_seconds() - rdev->suspend_at);
134

135
	rtnl_lock();
136
	wiphy_lock(&rdev->wiphy);
137 138
	if (rdev->wiphy.registered && rdev->ops->resume)
		ret = rdev_resume(rdev);
139 140
	rdev->suspended = false;
	schedule_work(&rdev->wiphy_work);
141
	wiphy_unlock(&rdev->wiphy);
142 143 144 145

	if (ret)
		cfg80211_shutdown_all_interfaces(&rdev->wiphy);

146
	rtnl_unlock();
Johannes Berg's avatar
Johannes Berg committed
147 148 149

	return ret;
}
150 151 152 153 154

static SIMPLE_DEV_PM_OPS(wiphy_pm_ops, wiphy_suspend, wiphy_resume);
#define WIPHY_PM_OPS (&wiphy_pm_ops)
#else
#define WIPHY_PM_OPS NULL
155
#endif
Johannes Berg's avatar
Johannes Berg committed
156

157
static const void *wiphy_namespace(const struct device *d)
158 159 160 161 162 163
{
	struct wiphy *wiphy = container_of(d, struct wiphy, dev);

	return wiphy_net(wiphy);
}

164 165 166
struct class ieee80211_class = {
	.name = "ieee80211",
	.dev_release = wiphy_dev_release,
167
	.dev_groups = ieee80211_groups,
168
	.pm = WIPHY_PM_OPS,
169 170
	.ns_type = &net_ns_type_operations,
	.namespace = wiphy_namespace,
171 172 173 174 175 176 177 178 179 180 181
};

int wiphy_sysfs_init(void)
{
	return class_register(&ieee80211_class);
}

void wiphy_sysfs_exit(void)
{
	class_unregister(&ieee80211_class);
}