sysfs.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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>
 *
 * This file is GPLv2 as found in COPYING.
 */

#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
}

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

B
Ben Greear 已提交
39 40 41 42 43 44 45 46
static ssize_t name_show(struct device *dev,
			 struct device_attribute *attr,
			 char *buf) {
	struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
	return sprintf(buf, "%s\n", dev_name(&wiphy->dev));
}


47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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++)
		buf += sprintf(buf, "%pM\n", &wiphy->addresses[i].addr);

	return buf - start;
}
63 64

static struct device_attribute ieee80211_dev_attrs[] = {
65 66
	__ATTR_RO(index),
	__ATTR_RO(macaddress),
67 68
	__ATTR_RO(address_mask),
	__ATTR_RO(addresses),
B
Ben Greear 已提交
69
	__ATTR_RO(name),
70 71 72 73 74 75 76 77 78 79
	{}
};

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

	cfg80211_dev_free(rdev);
}

80
static int wiphy_uevent(struct device *dev, struct kobj_uevent_env *env)
81 82 83 84 85
{
	/* TODO, we probably need stuff here */
	return 0;
}

86
#ifdef CONFIG_PM
87 88 89 90 91 92 93 94
static void cfg80211_leave_all(struct cfg80211_registered_device *rdev)
{
	struct wireless_dev *wdev;

	list_for_each_entry(wdev, &rdev->wdev_list, list)
		cfg80211_leave(rdev, wdev);
}

J
Johannes Berg 已提交
95 96 97 98 99
static int wiphy_suspend(struct device *dev, pm_message_t state)
{
	struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
	int ret = 0;

100 101
	rdev->suspend_at = get_seconds();

102 103
	rtnl_lock();
	if (rdev->wiphy.registered) {
104
		if (!rdev->wiphy.wowlan_config)
105 106
			cfg80211_leave_all(rdev);
		if (rdev->ops->suspend)
107
			ret = rdev_suspend(rdev, rdev->wiphy.wowlan_config);
108 109 110 111 112
		if (ret == 1) {
			/* Driver refuse to configure wowlan */
			cfg80211_leave_all(rdev);
			ret = rdev_suspend(rdev, NULL);
		}
J
Johannes Berg 已提交
113
	}
114
	rtnl_unlock();
J
Johannes Berg 已提交
115 116 117 118 119 120 121 122 123

	return ret;
}

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

124 125 126
	/* Age scan results with time spent in suspend */
	cfg80211_bss_age(rdev, get_seconds() - rdev->suspend_at);

J
Johannes Berg 已提交
127 128
	if (rdev->ops->resume) {
		rtnl_lock();
129
		if (rdev->wiphy.registered)
130
			ret = rdev_resume(rdev);
J
Johannes Berg 已提交
131 132 133 134 135
		rtnl_unlock();
	}

	return ret;
}
136
#endif
J
Johannes Berg 已提交
137

138 139 140 141 142 143 144
static const void *wiphy_namespace(struct device *d)
{
	struct wiphy *wiphy = container_of(d, struct wiphy, dev);

	return wiphy_net(wiphy);
}

145 146 147 148 149 150
struct class ieee80211_class = {
	.name = "ieee80211",
	.owner = THIS_MODULE,
	.dev_release = wiphy_dev_release,
	.dev_attrs = ieee80211_dev_attrs,
	.dev_uevent = wiphy_uevent,
151
#ifdef CONFIG_PM
J
Johannes Berg 已提交
152 153
	.suspend = wiphy_suspend,
	.resume = wiphy_resume,
154
#endif
155 156
	.ns_type = &net_ns_type_operations,
	.namespace = wiphy_namespace,
157 158 159 160 161 162 163 164 165 166 167
};

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

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