xref: /openbmc/linux/net/wireless/sysfs.c (revision 6c71a0574249f5e5a45fe055ab5f837023d5eeca)
128c61a66SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2704232c2SJohannes Berg /*
3704232c2SJohannes Berg  * This file provides /sys/class/ieee80211/<wiphy name>/
4704232c2SJohannes Berg  * and some default attributes.
5704232c2SJohannes Berg  *
6704232c2SJohannes Berg  * Copyright 2005-2006	Jiri Benc <jbenc@suse.cz>
7704232c2SJohannes Berg  * Copyright 2006	Johannes Berg <johannes@sipsolutions.net>
8*46b7eff5SJohannes Berg  * Copyright (C) 2020-2021, 2023-2024 Intel Corporation
9704232c2SJohannes Berg  */
10704232c2SJohannes Berg 
11704232c2SJohannes Berg #include <linux/device.h>
12704232c2SJohannes Berg #include <linux/module.h>
13704232c2SJohannes Berg #include <linux/netdevice.h>
14704232c2SJohannes Berg #include <linux/nl80211.h>
15704232c2SJohannes Berg #include <linux/rtnetlink.h>
16704232c2SJohannes Berg #include <net/cfg80211.h>
17704232c2SJohannes Berg #include "sysfs.h"
18704232c2SJohannes Berg #include "core.h"
19e35e4d28SHila Gonen #include "rdev-ops.h"
20704232c2SJohannes Berg 
dev_to_rdev(struct device * dev)21704232c2SJohannes Berg static inline struct cfg80211_registered_device *dev_to_rdev(
22704232c2SJohannes Berg 	struct device *dev)
23704232c2SJohannes Berg {
24704232c2SJohannes Berg 	return container_of(dev, struct cfg80211_registered_device, wiphy.dev);
25704232c2SJohannes Berg }
26704232c2SJohannes Berg 
274f6d4d1eSJohannes Berg #define SHOW_FMT(name, fmt, member)					\
284f6d4d1eSJohannes Berg static ssize_t name ## _show(struct device *dev,			\
294f6d4d1eSJohannes Berg 			      struct device_attribute *attr,		\
304f6d4d1eSJohannes Berg 			      char *buf)				\
314f6d4d1eSJohannes Berg {									\
324f6d4d1eSJohannes Berg 	return sprintf(buf, fmt "\n", dev_to_rdev(dev)->member);	\
33f0bc99c8SGreg Kroah-Hartman }									\
34f0bc99c8SGreg Kroah-Hartman static DEVICE_ATTR_RO(name)
35704232c2SJohannes Berg 
36b5850a7aSLuis R. Rodriguez SHOW_FMT(index, "%d", wiphy_idx);
374f6d4d1eSJohannes Berg SHOW_FMT(macaddress, "%pM", wiphy.perm_addr);
38ef15aac6SJohannes Berg SHOW_FMT(address_mask, "%pM", wiphy.addr_mask);
39ef15aac6SJohannes Berg 
name_show(struct device * dev,struct device_attribute * attr,char * buf)40cfd8e12fSBen Greear static ssize_t name_show(struct device *dev,
41cfd8e12fSBen Greear 			 struct device_attribute *attr,
425ec71dd7SJohannes Berg 			 char *buf)
435ec71dd7SJohannes Berg {
44cfd8e12fSBen Greear 	struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
455ec71dd7SJohannes Berg 
465ec71dd7SJohannes Berg 	return sprintf(buf, "%s\n", wiphy_name(wiphy));
47cfd8e12fSBen Greear }
48f0bc99c8SGreg Kroah-Hartman static DEVICE_ATTR_RO(name);
49cfd8e12fSBen Greear 
addresses_show(struct device * dev,struct device_attribute * attr,char * buf)50ef15aac6SJohannes Berg static ssize_t addresses_show(struct device *dev,
51ef15aac6SJohannes Berg 			      struct device_attribute *attr,
52ef15aac6SJohannes Berg 			      char *buf)
53ef15aac6SJohannes Berg {
54ef15aac6SJohannes Berg 	struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
55ef15aac6SJohannes Berg 	char *start = buf;
56ef15aac6SJohannes Berg 	int i;
57ef15aac6SJohannes Berg 
58ef15aac6SJohannes Berg 	if (!wiphy->addresses)
59ef15aac6SJohannes Berg 		return sprintf(buf, "%pM\n", wiphy->perm_addr);
60ef15aac6SJohannes Berg 
61ef15aac6SJohannes Berg 	for (i = 0; i < wiphy->n_addresses; i++)
62ec53c832SJohannes Berg 		buf += sprintf(buf, "%pM\n", wiphy->addresses[i].addr);
63ef15aac6SJohannes Berg 
64ef15aac6SJohannes Berg 	return buf - start;
65ef15aac6SJohannes Berg }
66f0bc99c8SGreg Kroah-Hartman static DEVICE_ATTR_RO(addresses);
67704232c2SJohannes Berg 
68f0bc99c8SGreg Kroah-Hartman static struct attribute *ieee80211_attrs[] = {
69f0bc99c8SGreg Kroah-Hartman 	&dev_attr_index.attr,
70f0bc99c8SGreg Kroah-Hartman 	&dev_attr_macaddress.attr,
71f0bc99c8SGreg Kroah-Hartman 	&dev_attr_address_mask.attr,
72f0bc99c8SGreg Kroah-Hartman 	&dev_attr_addresses.attr,
73f0bc99c8SGreg Kroah-Hartman 	&dev_attr_name.attr,
74f0bc99c8SGreg Kroah-Hartman 	NULL,
75704232c2SJohannes Berg };
76f0bc99c8SGreg Kroah-Hartman ATTRIBUTE_GROUPS(ieee80211);
77704232c2SJohannes Berg 
wiphy_dev_release(struct device * dev)78704232c2SJohannes Berg static void wiphy_dev_release(struct device *dev)
79704232c2SJohannes Berg {
80704232c2SJohannes Berg 	struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
81704232c2SJohannes Berg 
82704232c2SJohannes Berg 	cfg80211_dev_free(rdev);
83704232c2SJohannes Berg }
84704232c2SJohannes Berg 
85262918d8SLars-Peter Clausen #ifdef CONFIG_PM_SLEEP
cfg80211_leave_all(struct cfg80211_registered_device * rdev)8681256969SStanislaw Gruszka static void cfg80211_leave_all(struct cfg80211_registered_device *rdev)
8781256969SStanislaw Gruszka {
8881256969SStanislaw Gruszka 	struct wireless_dev *wdev;
8981256969SStanislaw Gruszka 
9053873f13SJohannes Berg 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
9181256969SStanislaw Gruszka 		cfg80211_leave(rdev, wdev);
9281256969SStanislaw Gruszka }
9381256969SStanislaw Gruszka 
wiphy_suspend(struct device * dev)94262918d8SLars-Peter Clausen static int wiphy_suspend(struct device *dev)
950378b3f1SJohannes Berg {
960378b3f1SJohannes Berg 	struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
970378b3f1SJohannes Berg 	int ret = 0;
980378b3f1SJohannes Berg 
99fe0984d3SArnd Bergmann 	rdev->suspend_at = ktime_get_boottime_seconds();
100cb3a8eecSDan Williams 
1010378b3f1SJohannes Berg 	rtnl_lock();
102a05829a7SJohannes Berg 	wiphy_lock(&rdev->wiphy);
10381256969SStanislaw Gruszka 	if (rdev->wiphy.registered) {
104e1957dbaSJohannes Berg 		if (!rdev->wiphy.wowlan_config) {
10581256969SStanislaw Gruszka 			cfg80211_leave_all(rdev);
106e1957dbaSJohannes Berg 			cfg80211_process_rdev_events(rdev);
107e1957dbaSJohannes Berg 		}
1085d9eefa2SJohannes Berg 		cfg80211_process_wiphy_works(rdev, NULL);
10981256969SStanislaw Gruszka 		if (rdev->ops->suspend)
1106abb9cb9SJohannes Berg 			ret = rdev_suspend(rdev, rdev->wiphy.wowlan_config);
11181256969SStanislaw Gruszka 		if (ret == 1) {
11281256969SStanislaw Gruszka 			/* Driver refuse to configure wowlan */
11381256969SStanislaw Gruszka 			cfg80211_leave_all(rdev);
114e1957dbaSJohannes Berg 			cfg80211_process_rdev_events(rdev);
1155d9eefa2SJohannes Berg 			cfg80211_process_wiphy_works(rdev, NULL);
11681256969SStanislaw Gruszka 			ret = rdev_suspend(rdev, NULL);
1170378b3f1SJohannes Berg 		}
118a3ee4dc8SJohannes Berg 		if (ret == 0)
119a3ee4dc8SJohannes Berg 			rdev->suspended = true;
12081256969SStanislaw Gruszka 	}
121a05829a7SJohannes Berg 	wiphy_unlock(&rdev->wiphy);
12281256969SStanislaw Gruszka 	rtnl_unlock();
1230378b3f1SJohannes Berg 
1240378b3f1SJohannes Berg 	return ret;
1250378b3f1SJohannes Berg }
1260378b3f1SJohannes Berg 
wiphy_resume(struct device * dev)1270378b3f1SJohannes Berg static int wiphy_resume(struct device *dev)
1280378b3f1SJohannes Berg {
1290378b3f1SJohannes Berg 	struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
1300378b3f1SJohannes Berg 	int ret = 0;
1310378b3f1SJohannes Berg 
132cb3a8eecSDan Williams 	/* Age scan results with time spent in suspend */
133fe0984d3SArnd Bergmann 	cfg80211_bss_age(rdev, ktime_get_boottime_seconds() - rdev->suspend_at);
134cb3a8eecSDan Williams 
1350378b3f1SJohannes Berg 	rtnl_lock();
136a05829a7SJohannes Berg 	wiphy_lock(&rdev->wiphy);
137b3ef5520SArend Van Spriel 	if (rdev->wiphy.registered && rdev->ops->resume)
138e35e4d28SHila Gonen 		ret = rdev_resume(rdev);
139a3ee4dc8SJohannes Berg 	rdev->suspended = false;
140*46b7eff5SJohannes Berg 	queue_work(system_unbound_wq, &rdev->wiphy_work);
141a05829a7SJohannes Berg 	wiphy_unlock(&rdev->wiphy);
14265bec836SJohannes Berg 
14365bec836SJohannes Berg 	if (ret)
14465bec836SJohannes Berg 		cfg80211_shutdown_all_interfaces(&rdev->wiphy);
14565bec836SJohannes Berg 
1460378b3f1SJohannes Berg 	rtnl_unlock();
1470378b3f1SJohannes Berg 
1480378b3f1SJohannes Berg 	return ret;
1490378b3f1SJohannes Berg }
150262918d8SLars-Peter Clausen 
151262918d8SLars-Peter Clausen static SIMPLE_DEV_PM_OPS(wiphy_pm_ops, wiphy_suspend, wiphy_resume);
152262918d8SLars-Peter Clausen #define WIPHY_PM_OPS (&wiphy_pm_ops)
153262918d8SLars-Peter Clausen #else
154262918d8SLars-Peter Clausen #define WIPHY_PM_OPS NULL
1556abb9cb9SJohannes Berg #endif
1560378b3f1SJohannes Berg 
wiphy_namespace(const struct device * d)157fa627348SGreg Kroah-Hartman static const void *wiphy_namespace(const struct device *d)
15804600794SJohannes Berg {
15904600794SJohannes Berg 	struct wiphy *wiphy = container_of(d, struct wiphy, dev);
16004600794SJohannes Berg 
16104600794SJohannes Berg 	return wiphy_net(wiphy);
16204600794SJohannes Berg }
16304600794SJohannes Berg 
164704232c2SJohannes Berg struct class ieee80211_class = {
165704232c2SJohannes Berg 	.name = "ieee80211",
166704232c2SJohannes Berg 	.dev_release = wiphy_dev_release,
167f0bc99c8SGreg Kroah-Hartman 	.dev_groups = ieee80211_groups,
168262918d8SLars-Peter Clausen 	.pm = WIPHY_PM_OPS,
16904600794SJohannes Berg 	.ns_type = &net_ns_type_operations,
17004600794SJohannes Berg 	.namespace = wiphy_namespace,
171704232c2SJohannes Berg };
172704232c2SJohannes Berg 
wiphy_sysfs_init(void)173704232c2SJohannes Berg int wiphy_sysfs_init(void)
174704232c2SJohannes Berg {
175704232c2SJohannes Berg 	return class_register(&ieee80211_class);
176704232c2SJohannes Berg }
177704232c2SJohannes Berg 
wiphy_sysfs_exit(void)178704232c2SJohannes Berg void wiphy_sysfs_exit(void)
179704232c2SJohannes Berg {
180704232c2SJohannes Berg 	class_unregister(&ieee80211_class);
181704232c2SJohannes Berg }
182