blob: d2903933805c56347541030e905dc30c80bc2b2b [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
2/*
Alexander Aringe23e9ec2014-10-28 18:21:32 +01003 *
4 * Authors:
5 * Alexander Aring <aar@pengutronix.de>
6 *
7 * Based on: net/wireless/sysfs.c
8 */
9
10#include <linux/device.h>
Varka Bhadrama6cb8692015-06-24 11:36:35 +020011#include <linux/rtnetlink.h>
Alexander Aringe23e9ec2014-10-28 18:21:32 +010012
13#include <net/cfg802154.h>
14
Alexander Aringa5dd1d72014-11-02 04:18:35 +010015#include "core.h"
Alexander Aringc5fbbc42014-11-02 21:43:04 +010016#include "sysfs.h"
Varka Bhadrama6cb8692015-06-24 11:36:35 +020017#include "rdev-ops.h"
Alexander Aringa5dd1d72014-11-02 04:18:35 +010018
19static inline struct cfg802154_registered_device *
20dev_to_rdev(struct device *dev)
21{
22 return container_of(dev, struct cfg802154_registered_device,
23 wpan_phy.dev);
24}
25
Alexander Aringa6fd6932014-11-09 08:36:51 +010026#define SHOW_FMT(name, fmt, member) \
27static ssize_t name ## _show(struct device *dev, \
28 struct device_attribute *attr, \
29 char *buf) \
30{ \
31 return sprintf(buf, fmt "\n", dev_to_rdev(dev)->member); \
32} \
33static DEVICE_ATTR_RO(name)
34
35SHOW_FMT(index, "%d", wpan_phy_idx);
36
37static ssize_t name_show(struct device *dev,
38 struct device_attribute *attr,
39 char *buf)
40{
41 struct wpan_phy *wpan_phy = &dev_to_rdev(dev)->wpan_phy;
42
43 return sprintf(buf, "%s\n", dev_name(&wpan_phy->dev));
44}
45static DEVICE_ATTR_RO(name);
46
Alexander Aringa5dd1d72014-11-02 04:18:35 +010047static void wpan_phy_release(struct device *dev)
Alexander Aringe23e9ec2014-10-28 18:21:32 +010048{
Alexander Aringa5dd1d72014-11-02 04:18:35 +010049 struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
Alexander Aringe23e9ec2014-10-28 18:21:32 +010050
Alexander Aringa5dd1d72014-11-02 04:18:35 +010051 cfg802154_dev_free(rdev);
Alexander Aringe23e9ec2014-10-28 18:21:32 +010052}
53
54static struct attribute *pmib_attrs[] = {
Alexander Aringa6fd6932014-11-09 08:36:51 +010055 &dev_attr_index.attr,
56 &dev_attr_name.attr,
Alexander Aringe23e9ec2014-10-28 18:21:32 +010057 NULL,
58};
59ATTRIBUTE_GROUPS(pmib);
60
Varka Bhadrama6cb8692015-06-24 11:36:35 +020061#ifdef CONFIG_PM_SLEEP
62static int wpan_phy_suspend(struct device *dev)
63{
64 struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
65 int ret = 0;
66
67 if (rdev->ops->suspend) {
68 rtnl_lock();
69 ret = rdev_suspend(rdev);
70 rtnl_unlock();
71 }
72
73 return ret;
74}
75
76static int wpan_phy_resume(struct device *dev)
77{
78 struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
79 int ret = 0;
80
81 if (rdev->ops->resume) {
82 rtnl_lock();
83 ret = rdev_resume(rdev);
84 rtnl_unlock();
85 }
86
87 return ret;
88}
89
90static SIMPLE_DEV_PM_OPS(wpan_phy_pm_ops, wpan_phy_suspend, wpan_phy_resume);
91#define WPAN_PHY_PM_OPS (&wpan_phy_pm_ops)
92#else
93#define WPAN_PHY_PM_OPS NULL
94#endif
95
Alexander Aringe23e9ec2014-10-28 18:21:32 +010096struct class wpan_phy_class = {
97 .name = "ieee802154",
98 .dev_release = wpan_phy_release,
99 .dev_groups = pmib_groups,
Varka Bhadrama6cb8692015-06-24 11:36:35 +0200100 .pm = WPAN_PHY_PM_OPS,
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100101};
102
103int wpan_phy_sysfs_init(void)
104{
105 return class_register(&wpan_phy_class);
106}
107
108void wpan_phy_sysfs_exit(void)
109{
110 class_unregister(&wpan_phy_class);
111}