^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This file provides /sys/class/ieee80211/<wiphy name>/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * and some default attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2005-2006 Jiri Benc <jbenc@suse.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/nl80211.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/cfg80211.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "rdev-ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static inline struct cfg80211_registered_device *dev_to_rdev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return container_of(dev, struct cfg80211_registered_device, wiphy.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define SHOW_FMT(name, fmt, member) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static ssize_t name ## _show(struct device *dev, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct device_attribute *attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return sprintf(buf, fmt "\n", dev_to_rdev(dev)->member); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static DEVICE_ATTR_RO(name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) SHOW_FMT(index, "%d", wiphy_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) SHOW_FMT(macaddress, "%pM", wiphy.perm_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) SHOW_FMT(address_mask, "%pM", wiphy.addr_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static ssize_t name_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return sprintf(buf, "%s\n", wiphy_name(wiphy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static DEVICE_ATTR_RO(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static ssize_t addresses_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) char *start = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!wiphy->addresses)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return sprintf(buf, "%pM\n", wiphy->perm_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) for (i = 0; i < wiphy->n_addresses; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) buf += sprintf(buf, "%pM\n", wiphy->addresses[i].addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return buf - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static DEVICE_ATTR_RO(addresses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct attribute *ieee80211_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) &dev_attr_index.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) &dev_attr_macaddress.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) &dev_attr_address_mask.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) &dev_attr_addresses.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) &dev_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ATTRIBUTE_GROUPS(ieee80211);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void wiphy_dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) cfg80211_dev_free(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int wiphy_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* TODO, we probably need stuff here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static void cfg80211_leave_all(struct cfg80211_registered_device *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct wireless_dev *wdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) cfg80211_leave(rdev, wdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static int wiphy_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) rdev->suspend_at = ktime_get_boottime_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (rdev->wiphy.registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!rdev->wiphy.wowlan_config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) cfg80211_leave_all(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) cfg80211_process_rdev_events(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (rdev->ops->suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = rdev_suspend(rdev, rdev->wiphy.wowlan_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Driver refuse to configure wowlan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) cfg80211_leave_all(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) cfg80211_process_rdev_events(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ret = rdev_suspend(rdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int wiphy_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Age scan results with time spent in suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) cfg80211_bss_age(rdev, ktime_get_boottime_seconds() - rdev->suspend_at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (rdev->wiphy.registered && rdev->ops->resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = rdev_resume(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static SIMPLE_DEV_PM_OPS(wiphy_pm_ops, wiphy_suspend, wiphy_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define WIPHY_PM_OPS (&wiphy_pm_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #define WIPHY_PM_OPS NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static const void *wiphy_namespace(struct device *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct wiphy *wiphy = container_of(d, struct wiphy, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return wiphy_net(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct class ieee80211_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .name = "ieee80211",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .dev_release = wiphy_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .dev_groups = ieee80211_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .dev_uevent = wiphy_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .pm = WIPHY_PM_OPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .ns_type = &net_ns_type_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .namespace = wiphy_namespace,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int wiphy_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return class_register(&ieee80211_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void wiphy_sysfs_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) class_unregister(&ieee80211_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }