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 | * |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 13 | */ |
| 14 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/device.h> |
| 19 | |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 20 | #include <net/cfg802154.h> |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 21 | |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 22 | #include "ieee802154.h" |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 23 | #include "sysfs.h" |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 24 | #include "core.h" |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 25 | |
| 26 | static DEFINE_MUTEX(wpan_phy_mutex); |
| 27 | static int wpan_phy_idx; |
| 28 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 29 | static int wpan_phy_match(struct device *dev, const void *data) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 30 | { |
| 31 | return !strcmp(dev_name(dev), (const char *)data); |
| 32 | } |
| 33 | |
| 34 | struct 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ław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 41 | 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] | 42 | if (!dev) |
| 43 | return NULL; |
| 44 | |
| 45 | return container_of(dev, struct wpan_phy, dev); |
| 46 | } |
| 47 | EXPORT_SYMBOL(wpan_phy_find); |
| 48 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 49 | struct wpan_phy_iter_data { |
| 50 | int (*fn)(struct wpan_phy *phy, void *data); |
| 51 | void *data; |
| 52 | }; |
| 53 | |
| 54 | static 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 Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 58 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 59 | return wpid->fn(phy, wpid->data); |
| 60 | } |
| 61 | |
| 62 | 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] | 63 | void *data) |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 64 | { |
| 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 | } |
| 73 | EXPORT_SYMBOL(wpan_phy_for_each); |
| 74 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 75 | static int wpan_phy_idx_valid(int idx) |
| 76 | { |
| 77 | return idx >= 0; |
| 78 | } |
| 79 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 80 | struct wpan_phy * |
| 81 | wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 82 | { |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 83 | struct cfg802154_registered_device *rdev; |
| 84 | size_t alloc_size; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 85 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 86 | 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-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 93 | mutex_lock(&wpan_phy_mutex); |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 94 | rdev->wpan_phy.idx = wpan_phy_idx++; |
| 95 | if (unlikely(!wpan_phy_idx_valid(rdev->wpan_phy.idx))) { |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 96 | wpan_phy_idx--; |
| 97 | mutex_unlock(&wpan_phy_mutex); |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 98 | kfree(rdev); |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 99 | goto out; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 100 | } |
| 101 | mutex_unlock(&wpan_phy_mutex); |
| 102 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 103 | mutex_init(&rdev->wpan_phy.pib_lock); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 104 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 105 | device_initialize(&rdev->wpan_phy.dev); |
| 106 | dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy.idx); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 107 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 108 | rdev->wpan_phy.dev.class = &wpan_phy_class; |
| 109 | rdev->wpan_phy.dev.platform_data = rdev; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 110 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 111 | return &rdev->wpan_phy; |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 112 | |
| 113 | out: |
| 114 | return NULL; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 115 | } |
| 116 | EXPORT_SYMBOL(wpan_phy_alloc); |
| 117 | |
Dmitry Eremin-Solenikov | e9cf356 | 2009-09-28 19:01:20 +0400 | [diff] [blame] | 118 | int wpan_phy_register(struct wpan_phy *phy) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 119 | { |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 120 | return device_add(&phy->dev); |
| 121 | } |
| 122 | EXPORT_SYMBOL(wpan_phy_register); |
| 123 | |
| 124 | void wpan_phy_unregister(struct wpan_phy *phy) |
| 125 | { |
| 126 | device_del(&phy->dev); |
| 127 | } |
| 128 | EXPORT_SYMBOL(wpan_phy_unregister); |
| 129 | |
| 130 | void wpan_phy_free(struct wpan_phy *phy) |
| 131 | { |
| 132 | put_device(&phy->dev); |
| 133 | } |
| 134 | EXPORT_SYMBOL(wpan_phy_free); |
| 135 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 136 | void cfg802154_dev_free(struct cfg802154_registered_device *rdev) |
| 137 | { |
| 138 | kfree(rdev); |
| 139 | } |
| 140 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 141 | static int __init wpan_phy_class_init(void) |
| 142 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 143 | int rc; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 144 | |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 145 | rc = wpan_phy_sysfs_init(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 146 | if (rc) |
| 147 | goto err; |
| 148 | |
| 149 | rc = ieee802154_nl_init(); |
| 150 | if (rc) |
| 151 | goto err_nl; |
| 152 | |
| 153 | return 0; |
| 154 | err_nl: |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 155 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 156 | err: |
| 157 | return rc; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 158 | } |
Dmitry Eremin-Solenikov | 282a395 | 2009-11-12 23:58:23 +0300 | [diff] [blame] | 159 | subsys_initcall(wpan_phy_class_init); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 160 | |
| 161 | static void __exit wpan_phy_class_exit(void) |
| 162 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 163 | ieee802154_nl_exit(); |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 164 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 165 | } |
| 166 | module_exit(wpan_phy_class_exit); |
| 167 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 168 | MODULE_LICENSE("GPL v2"); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 169 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
| 170 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 171 | |