blob: 620abc2ba5fc89e62c794da0f673b85d957cf3c2 [file] [log] [blame]
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +04001/*
2 * Copyright (C) 2007, 2008, 2009 Siemens AG
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040013 */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/device.h>
19
Alexander Aring5ad60d32014-10-25 09:41:02 +020020#include <net/cfg802154.h>
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040021
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +040022#include "ieee802154.h"
Alexander Aringe23e9ec2014-10-28 18:21:32 +010023#include "sysfs.h"
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040024
25static DEFINE_MUTEX(wpan_phy_mutex);
26static int wpan_phy_idx;
27
Michał Mirosław9f3b7952013-02-01 20:40:17 +010028static int wpan_phy_match(struct device *dev, const void *data)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040029{
30 return !strcmp(dev_name(dev), (const char *)data);
31}
32
33struct wpan_phy *wpan_phy_find(const char *str)
34{
35 struct device *dev;
36
37 if (WARN_ON(!str))
38 return NULL;
39
Michał Mirosław9f3b7952013-02-01 20:40:17 +010040 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040041 if (!dev)
42 return NULL;
43
44 return container_of(dev, struct wpan_phy, dev);
45}
46EXPORT_SYMBOL(wpan_phy_find);
47
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040048struct wpan_phy_iter_data {
49 int (*fn)(struct wpan_phy *phy, void *data);
50 void *data;
51};
52
53static int wpan_phy_iter(struct device *dev, void *_data)
54{
55 struct wpan_phy_iter_data *wpid = _data;
56 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
Varka Bhadram4710d802014-07-02 09:01:09 +053057
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040058 return wpid->fn(phy, wpid->data);
59}
60
61int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
Varka Bhadram4710d802014-07-02 09:01:09 +053062 void *data)
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040063{
64 struct wpan_phy_iter_data wpid = {
65 .fn = fn,
66 .data = data,
67 };
68
69 return class_for_each_device(&wpan_phy_class, NULL,
70 &wpid, wpan_phy_iter);
71}
72EXPORT_SYMBOL(wpan_phy_for_each);
73
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040074static int wpan_phy_idx_valid(int idx)
75{
76 return idx >= 0;
77}
78
79struct wpan_phy *wpan_phy_alloc(size_t priv_size)
80{
81 struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
82 GFP_KERNEL);
83
Denis Kirjanova6c0f822010-05-23 05:45:45 +000084 if (!phy)
85 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040086 mutex_lock(&wpan_phy_mutex);
87 phy->idx = wpan_phy_idx++;
88 if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
89 wpan_phy_idx--;
90 mutex_unlock(&wpan_phy_mutex);
91 kfree(phy);
Denis Kirjanova6c0f822010-05-23 05:45:45 +000092 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040093 }
94 mutex_unlock(&wpan_phy_mutex);
95
96 mutex_init(&phy->pib_lock);
97
98 device_initialize(&phy->dev);
99 dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
100
101 phy->dev.class = &wpan_phy_class;
102
Dmitry Eremin-Solenikov37eb0ed2009-09-18 16:35:06 +0400103 phy->current_channel = -1; /* not initialised */
104 phy->current_page = 0; /* for compatibility */
105
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400106 return phy;
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000107
108out:
109 return NULL;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400110}
111EXPORT_SYMBOL(wpan_phy_alloc);
112
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400113int wpan_phy_register(struct wpan_phy *phy)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400114{
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400115 return device_add(&phy->dev);
116}
117EXPORT_SYMBOL(wpan_phy_register);
118
119void wpan_phy_unregister(struct wpan_phy *phy)
120{
121 device_del(&phy->dev);
122}
123EXPORT_SYMBOL(wpan_phy_unregister);
124
125void wpan_phy_free(struct wpan_phy *phy)
126{
127 put_device(&phy->dev);
128}
129EXPORT_SYMBOL(wpan_phy_free);
130
131static int __init wpan_phy_class_init(void)
132{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400133 int rc;
Varka Bhadram4710d802014-07-02 09:01:09 +0530134
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100135 rc = wpan_phy_sysfs_init();
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400136 if (rc)
137 goto err;
138
139 rc = ieee802154_nl_init();
140 if (rc)
141 goto err_nl;
142
143 return 0;
144err_nl:
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100145 wpan_phy_sysfs_exit();
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400146err:
147 return rc;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400148}
Dmitry Eremin-Solenikov282a3952009-11-12 23:58:23 +0300149subsys_initcall(wpan_phy_class_init);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400150
151static void __exit wpan_phy_class_exit(void)
152{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400153 ieee802154_nl_exit();
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100154 wpan_phy_sysfs_exit();
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400155}
156module_exit(wpan_phy_class_exit);
157
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400158MODULE_LICENSE("GPL v2");
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400159MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
160MODULE_AUTHOR("Dmitry Eremin-Solenikov");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400161