Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 1 | /* |
| 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 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | * |
| 17 | */ |
| 18 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/device.h> |
| 23 | |
| 24 | #include <net/wpan-phy.h> |
| 25 | |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 26 | #include "ieee802154.h" |
| 27 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 28 | #define MASTER_SHOW_COMPLEX(name, format_string, args...) \ |
| 29 | static ssize_t name ## _show(struct device *dev, \ |
| 30 | struct device_attribute *attr, char *buf) \ |
| 31 | { \ |
| 32 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \ |
| 33 | int ret; \ |
| 34 | \ |
| 35 | mutex_lock(&phy->pib_lock); \ |
Dmitry Eremin-Solenikov | 375bb0e | 2009-09-28 18:58:32 +0400 | [diff] [blame] | 36 | ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \ |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 37 | mutex_unlock(&phy->pib_lock); \ |
| 38 | return ret; \ |
Greg Kroah-Hartman | 7f4708a | 2013-07-24 15:05:34 -0700 | [diff] [blame] | 39 | } \ |
| 40 | static DEVICE_ATTR_RO(name); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 41 | |
| 42 | #define MASTER_SHOW(field, format_string) \ |
| 43 | MASTER_SHOW_COMPLEX(field, format_string, phy->field) |
| 44 | |
| 45 | MASTER_SHOW(current_channel, "%d"); |
| 46 | MASTER_SHOW(current_page, "%d"); |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 47 | MASTER_SHOW(transmit_power, "%d +- 1 dB"); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 48 | MASTER_SHOW(cca_mode, "%d"); |
| 49 | |
Dmitry Eremin-Solenikov | a0b4a73 | 2009-09-22 15:26:48 +0400 | [diff] [blame] | 50 | static ssize_t channels_supported_show(struct device *dev, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 51 | struct device_attribute *attr, |
| 52 | char *buf) |
Dmitry Eremin-Solenikov | a0b4a73 | 2009-09-22 15:26:48 +0400 | [diff] [blame] | 53 | { |
| 54 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); |
| 55 | int ret; |
| 56 | int i, len = 0; |
| 57 | |
| 58 | mutex_lock(&phy->pib_lock); |
| 59 | for (i = 0; i < 32; i++) { |
| 60 | ret = snprintf(buf + len, PAGE_SIZE - len, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 61 | "%#09x\n", phy->channels_supported[i]); |
Dmitry Eremin-Solenikov | a0b4a73 | 2009-09-22 15:26:48 +0400 | [diff] [blame] | 62 | if (ret < 0) |
| 63 | break; |
| 64 | len += ret; |
| 65 | } |
| 66 | mutex_unlock(&phy->pib_lock); |
| 67 | return len; |
| 68 | } |
Greg Kroah-Hartman | 7f4708a | 2013-07-24 15:05:34 -0700 | [diff] [blame] | 69 | static DEVICE_ATTR_RO(channels_supported); |
Dmitry Eremin-Solenikov | a0b4a73 | 2009-09-22 15:26:48 +0400 | [diff] [blame] | 70 | |
Greg Kroah-Hartman | 7f4708a | 2013-07-24 15:05:34 -0700 | [diff] [blame] | 71 | static struct attribute *pmib_attrs[] = { |
| 72 | &dev_attr_current_channel.attr, |
| 73 | &dev_attr_current_page.attr, |
| 74 | &dev_attr_channels_supported.attr, |
| 75 | &dev_attr_transmit_power.attr, |
| 76 | &dev_attr_cca_mode.attr, |
| 77 | NULL, |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 78 | }; |
Greg Kroah-Hartman | 7f4708a | 2013-07-24 15:05:34 -0700 | [diff] [blame] | 79 | ATTRIBUTE_GROUPS(pmib); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 80 | |
| 81 | static void wpan_phy_release(struct device *d) |
| 82 | { |
| 83 | struct wpan_phy *phy = container_of(d, struct wpan_phy, dev); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 84 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 85 | kfree(phy); |
| 86 | } |
| 87 | |
| 88 | static struct class wpan_phy_class = { |
| 89 | .name = "ieee802154", |
| 90 | .dev_release = wpan_phy_release, |
Greg Kroah-Hartman | 7f4708a | 2013-07-24 15:05:34 -0700 | [diff] [blame] | 91 | .dev_groups = pmib_groups, |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static DEFINE_MUTEX(wpan_phy_mutex); |
| 95 | static int wpan_phy_idx; |
| 96 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 97 | static int wpan_phy_match(struct device *dev, const void *data) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 98 | { |
| 99 | return !strcmp(dev_name(dev), (const char *)data); |
| 100 | } |
| 101 | |
| 102 | struct wpan_phy *wpan_phy_find(const char *str) |
| 103 | { |
| 104 | struct device *dev; |
| 105 | |
| 106 | if (WARN_ON(!str)) |
| 107 | return NULL; |
| 108 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 109 | dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 110 | if (!dev) |
| 111 | return NULL; |
| 112 | |
| 113 | return container_of(dev, struct wpan_phy, dev); |
| 114 | } |
| 115 | EXPORT_SYMBOL(wpan_phy_find); |
| 116 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 117 | struct wpan_phy_iter_data { |
| 118 | int (*fn)(struct wpan_phy *phy, void *data); |
| 119 | void *data; |
| 120 | }; |
| 121 | |
| 122 | static int wpan_phy_iter(struct device *dev, void *_data) |
| 123 | { |
| 124 | struct wpan_phy_iter_data *wpid = _data; |
| 125 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 126 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 127 | return wpid->fn(phy, wpid->data); |
| 128 | } |
| 129 | |
| 130 | int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 131 | void *data) |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 132 | { |
| 133 | struct wpan_phy_iter_data wpid = { |
| 134 | .fn = fn, |
| 135 | .data = data, |
| 136 | }; |
| 137 | |
| 138 | return class_for_each_device(&wpan_phy_class, NULL, |
| 139 | &wpid, wpan_phy_iter); |
| 140 | } |
| 141 | EXPORT_SYMBOL(wpan_phy_for_each); |
| 142 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 143 | static int wpan_phy_idx_valid(int idx) |
| 144 | { |
| 145 | return idx >= 0; |
| 146 | } |
| 147 | |
| 148 | struct wpan_phy *wpan_phy_alloc(size_t priv_size) |
| 149 | { |
| 150 | struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size, |
| 151 | GFP_KERNEL); |
| 152 | |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 153 | if (!phy) |
| 154 | goto out; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 155 | mutex_lock(&wpan_phy_mutex); |
| 156 | phy->idx = wpan_phy_idx++; |
| 157 | if (unlikely(!wpan_phy_idx_valid(phy->idx))) { |
| 158 | wpan_phy_idx--; |
| 159 | mutex_unlock(&wpan_phy_mutex); |
| 160 | kfree(phy); |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 161 | goto out; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 162 | } |
| 163 | mutex_unlock(&wpan_phy_mutex); |
| 164 | |
| 165 | mutex_init(&phy->pib_lock); |
| 166 | |
| 167 | device_initialize(&phy->dev); |
| 168 | dev_set_name(&phy->dev, "wpan-phy%d", phy->idx); |
| 169 | |
| 170 | phy->dev.class = &wpan_phy_class; |
| 171 | |
Dmitry Eremin-Solenikov | 37eb0ed | 2009-09-18 16:35:06 +0400 | [diff] [blame] | 172 | phy->current_channel = -1; /* not initialised */ |
| 173 | phy->current_page = 0; /* for compatibility */ |
| 174 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 175 | return phy; |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 176 | |
| 177 | out: |
| 178 | return NULL; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 179 | } |
| 180 | EXPORT_SYMBOL(wpan_phy_alloc); |
| 181 | |
Dmitry Eremin-Solenikov | e9cf356 | 2009-09-28 19:01:20 +0400 | [diff] [blame] | 182 | int wpan_phy_register(struct wpan_phy *phy) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 183 | { |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 184 | return device_add(&phy->dev); |
| 185 | } |
| 186 | EXPORT_SYMBOL(wpan_phy_register); |
| 187 | |
| 188 | void wpan_phy_unregister(struct wpan_phy *phy) |
| 189 | { |
| 190 | device_del(&phy->dev); |
| 191 | } |
| 192 | EXPORT_SYMBOL(wpan_phy_unregister); |
| 193 | |
| 194 | void wpan_phy_free(struct wpan_phy *phy) |
| 195 | { |
| 196 | put_device(&phy->dev); |
| 197 | } |
| 198 | EXPORT_SYMBOL(wpan_phy_free); |
| 199 | |
| 200 | static int __init wpan_phy_class_init(void) |
| 201 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 202 | int rc; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 203 | |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 204 | rc = class_register(&wpan_phy_class); |
| 205 | if (rc) |
| 206 | goto err; |
| 207 | |
| 208 | rc = ieee802154_nl_init(); |
| 209 | if (rc) |
| 210 | goto err_nl; |
| 211 | |
| 212 | return 0; |
| 213 | err_nl: |
| 214 | class_unregister(&wpan_phy_class); |
| 215 | err: |
| 216 | return rc; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 217 | } |
Dmitry Eremin-Solenikov | 282a395 | 2009-11-12 23:58:23 +0300 | [diff] [blame] | 218 | subsys_initcall(wpan_phy_class_init); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 219 | |
| 220 | static void __exit wpan_phy_class_exit(void) |
| 221 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 222 | ieee802154_nl_exit(); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 223 | class_unregister(&wpan_phy_class); |
| 224 | } |
| 225 | module_exit(wpan_phy_class_exit); |
| 226 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 227 | MODULE_LICENSE("GPL v2"); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 228 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
| 229 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 230 | |