blob: ed5b014dbec72227d6d2a0531674a5bcd8761a09 [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"
Alexander Aringa5dd1d72014-11-02 04:18:35 +010024#include "core.h"
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040025
26static DEFINE_MUTEX(wpan_phy_mutex);
27static int wpan_phy_idx;
28
Michał Mirosław9f3b7952013-02-01 20:40:17 +010029static int wpan_phy_match(struct device *dev, const void *data)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040030{
31 return !strcmp(dev_name(dev), (const char *)data);
32}
33
34struct wpan_phy *wpan_phy_find(const char *str)
35{
36 struct device *dev;
37
38 if (WARN_ON(!str))
39 return NULL;
40
Michał Mirosław9f3b7952013-02-01 20:40:17 +010041 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040042 if (!dev)
43 return NULL;
44
45 return container_of(dev, struct wpan_phy, dev);
46}
47EXPORT_SYMBOL(wpan_phy_find);
48
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040049struct wpan_phy_iter_data {
50 int (*fn)(struct wpan_phy *phy, void *data);
51 void *data;
52};
53
54static int wpan_phy_iter(struct device *dev, void *_data)
55{
56 struct wpan_phy_iter_data *wpid = _data;
57 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
Varka Bhadram4710d802014-07-02 09:01:09 +053058
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040059 return wpid->fn(phy, wpid->data);
60}
61
62int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
Varka Bhadram4710d802014-07-02 09:01:09 +053063 void *data)
Dmitry Eremin-Solenikov1c889f42009-09-15 16:57:04 +040064{
65 struct wpan_phy_iter_data wpid = {
66 .fn = fn,
67 .data = data,
68 };
69
70 return class_for_each_device(&wpan_phy_class, NULL,
71 &wpid, wpan_phy_iter);
72}
73EXPORT_SYMBOL(wpan_phy_for_each);
74
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040075static int wpan_phy_idx_valid(int idx)
76{
77 return idx >= 0;
78}
79
Alexander Aringa5dd1d72014-11-02 04:18:35 +010080struct wpan_phy *
81wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040082{
Alexander Aringa5dd1d72014-11-02 04:18:35 +010083 struct cfg802154_registered_device *rdev;
84 size_t alloc_size;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040085
Alexander Aringa5dd1d72014-11-02 04:18:35 +010086 alloc_size = sizeof(*rdev) + priv_size;
87 rdev = kzalloc(alloc_size, GFP_KERNEL);
88 if (!rdev)
89 return NULL;
90
91 rdev->ops = ops;
92
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040093 mutex_lock(&wpan_phy_mutex);
Alexander Aringa5dd1d72014-11-02 04:18:35 +010094 rdev->wpan_phy.idx = wpan_phy_idx++;
95 if (unlikely(!wpan_phy_idx_valid(rdev->wpan_phy.idx))) {
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +040096 wpan_phy_idx--;
97 mutex_unlock(&wpan_phy_mutex);
Alexander Aringa5dd1d72014-11-02 04:18:35 +010098 kfree(rdev);
Denis Kirjanova6c0f822010-05-23 05:45:45 +000099 goto out;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400100 }
101 mutex_unlock(&wpan_phy_mutex);
102
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100103 mutex_init(&rdev->wpan_phy.pib_lock);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400104
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100105 device_initialize(&rdev->wpan_phy.dev);
106 dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy.idx);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400107
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100108 rdev->wpan_phy.dev.class = &wpan_phy_class;
109 rdev->wpan_phy.dev.platform_data = rdev;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400110
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100111 return &rdev->wpan_phy;
Denis Kirjanova6c0f822010-05-23 05:45:45 +0000112
113out:
114 return NULL;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400115}
116EXPORT_SYMBOL(wpan_phy_alloc);
117
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400118int wpan_phy_register(struct wpan_phy *phy)
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400119{
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400120 return device_add(&phy->dev);
121}
122EXPORT_SYMBOL(wpan_phy_register);
123
124void wpan_phy_unregister(struct wpan_phy *phy)
125{
126 device_del(&phy->dev);
127}
128EXPORT_SYMBOL(wpan_phy_unregister);
129
130void wpan_phy_free(struct wpan_phy *phy)
131{
132 put_device(&phy->dev);
133}
134EXPORT_SYMBOL(wpan_phy_free);
135
Alexander Aringa5dd1d72014-11-02 04:18:35 +0100136void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
137{
138 kfree(rdev);
139}
140
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400141static int __init wpan_phy_class_init(void)
142{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400143 int rc;
Varka Bhadram4710d802014-07-02 09:01:09 +0530144
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100145 rc = wpan_phy_sysfs_init();
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400146 if (rc)
147 goto err;
148
149 rc = ieee802154_nl_init();
150 if (rc)
151 goto err_nl;
152
153 return 0;
154err_nl:
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100155 wpan_phy_sysfs_exit();
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400156err:
157 return rc;
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400158}
Dmitry Eremin-Solenikov282a3952009-11-12 23:58:23 +0300159subsys_initcall(wpan_phy_class_init);
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400160
161static void __exit wpan_phy_class_exit(void)
162{
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400163 ieee802154_nl_exit();
Alexander Aringe23e9ec2014-10-28 18:21:32 +0100164 wpan_phy_sysfs_exit();
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400165}
166module_exit(wpan_phy_class_exit);
167
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400168MODULE_LICENSE("GPL v2");
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400169MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
170MODULE_AUTHOR("Dmitry Eremin-Solenikov");
Dmitry Eremin-Solenikov2bfb1072009-08-14 16:13:12 +0400171