aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorAlexander Aring <alex.aring@gmail.com>2014-11-05 14:51:12 -0500
committerMarcel Holtmann <marcel@holtmann.org>2014-11-05 15:53:03 -0500
commit53f9ee61b46d81a43d8c6694d136896e8f49a7b8 (patch)
treed6d98d0ad30cb084516cbbc741425f95c0dee46c /net
parent0d8a52f933f817d0b62955a5a362fb7f2508f06c (diff)
ieee802154: rework wpan_phy index assignment
This patch reworks the wpan_phy index incrementation. It's now similar like wireless wiphy index incrementation. We move the wpan_phy index attribute inside of cfg802154_registered_device and use atomic operations instead locking mechanism via wpan_phy_mutex. Signed-off-by: Alexander Aring <alex.aring@gmail.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'net')
-rw-r--r--net/ieee802154/core.c30
-rw-r--r--net/ieee802154/core.h3
2 files changed, 14 insertions, 19 deletions
diff --git a/net/ieee802154/core.c b/net/ieee802154/core.c
index ed5b014dbec7..d1cd0edfb149 100644
--- a/net/ieee802154/core.c
+++ b/net/ieee802154/core.c
@@ -23,9 +23,6 @@
23#include "sysfs.h" 23#include "sysfs.h"
24#include "core.h" 24#include "core.h"
25 25
26static DEFINE_MUTEX(wpan_phy_mutex);
27static int wpan_phy_idx;
28
29static int wpan_phy_match(struct device *dev, const void *data) 26static int wpan_phy_match(struct device *dev, const void *data)
30{ 27{
31 return !strcmp(dev_name(dev), (const char *)data); 28 return !strcmp(dev_name(dev), (const char *)data);
@@ -72,14 +69,10 @@ int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
72} 69}
73EXPORT_SYMBOL(wpan_phy_for_each); 70EXPORT_SYMBOL(wpan_phy_for_each);
74 71
75static int wpan_phy_idx_valid(int idx)
76{
77 return idx >= 0;
78}
79
80struct wpan_phy * 72struct wpan_phy *
81wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size) 73wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size)
82{ 74{
75 static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
83 struct cfg802154_registered_device *rdev; 76 struct cfg802154_registered_device *rdev;
84 size_t alloc_size; 77 size_t alloc_size;
85 78
@@ -90,28 +83,27 @@ wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size)
90 83
91 rdev->ops = ops; 84 rdev->ops = ops;
92 85
93 mutex_lock(&wpan_phy_mutex); 86 rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
94 rdev->wpan_phy.idx = wpan_phy_idx++; 87
95 if (unlikely(!wpan_phy_idx_valid(rdev->wpan_phy.idx))) { 88 if (unlikely(rdev->wpan_phy_idx < 0)) {
96 wpan_phy_idx--; 89 /* ugh, wrapped! */
97 mutex_unlock(&wpan_phy_mutex); 90 atomic_dec(&wpan_phy_counter);
98 kfree(rdev); 91 kfree(rdev);
99 goto out; 92 return NULL;
100 } 93 }
101 mutex_unlock(&wpan_phy_mutex); 94
95 /* atomic_inc_return makes it start at 1, make it start at 0 */
96 rdev->wpan_phy_idx--;
102 97
103 mutex_init(&rdev->wpan_phy.pib_lock); 98 mutex_init(&rdev->wpan_phy.pib_lock);
104 99
105 device_initialize(&rdev->wpan_phy.dev); 100 device_initialize(&rdev->wpan_phy.dev);
106 dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy.idx); 101 dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy_idx);
107 102
108 rdev->wpan_phy.dev.class = &wpan_phy_class; 103 rdev->wpan_phy.dev.class = &wpan_phy_class;
109 rdev->wpan_phy.dev.platform_data = rdev; 104 rdev->wpan_phy.dev.platform_data = rdev;
110 105
111 return &rdev->wpan_phy; 106 return &rdev->wpan_phy;
112
113out:
114 return NULL;
115} 107}
116EXPORT_SYMBOL(wpan_phy_alloc); 108EXPORT_SYMBOL(wpan_phy_alloc);
117 109
diff --git a/net/ieee802154/core.h b/net/ieee802154/core.h
index 1bc172587157..fea60b3a8846 100644
--- a/net/ieee802154/core.h
+++ b/net/ieee802154/core.h
@@ -6,6 +6,9 @@
6struct cfg802154_registered_device { 6struct cfg802154_registered_device {
7 const struct cfg802154_ops *ops; 7 const struct cfg802154_ops *ops;
8 8
9 /* wpan_phy index, internal only */
10 int wpan_phy_idx;
11
9 /* must be last because of the way we do wpan_phy_priv(), 12 /* must be last because of the way we do wpan_phy_priv(),
10 * and it should at least be aligned to NETDEV_ALIGN 13 * and it should at least be aligned to NETDEV_ALIGN
11 */ 14 */