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 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 26 | static int wpan_phy_match(struct device *dev, const void *data) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 27 | { |
| 28 | return !strcmp(dev_name(dev), (const char *)data); |
| 29 | } |
| 30 | |
| 31 | struct wpan_phy *wpan_phy_find(const char *str) |
| 32 | { |
| 33 | struct device *dev; |
| 34 | |
| 35 | if (WARN_ON(!str)) |
| 36 | return NULL; |
| 37 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 38 | 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] | 39 | if (!dev) |
| 40 | return NULL; |
| 41 | |
| 42 | return container_of(dev, struct wpan_phy, dev); |
| 43 | } |
| 44 | EXPORT_SYMBOL(wpan_phy_find); |
| 45 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 46 | struct wpan_phy_iter_data { |
| 47 | int (*fn)(struct wpan_phy *phy, void *data); |
| 48 | void *data; |
| 49 | }; |
| 50 | |
| 51 | static int wpan_phy_iter(struct device *dev, void *_data) |
| 52 | { |
| 53 | struct wpan_phy_iter_data *wpid = _data; |
| 54 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 55 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 56 | return wpid->fn(phy, wpid->data); |
| 57 | } |
| 58 | |
| 59 | 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] | 60 | void *data) |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 61 | { |
| 62 | struct wpan_phy_iter_data wpid = { |
| 63 | .fn = fn, |
| 64 | .data = data, |
| 65 | }; |
| 66 | |
| 67 | return class_for_each_device(&wpan_phy_class, NULL, |
| 68 | &wpid, wpan_phy_iter); |
| 69 | } |
| 70 | EXPORT_SYMBOL(wpan_phy_for_each); |
| 71 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 72 | struct wpan_phy * |
| 73 | 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] | 74 | { |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 75 | static atomic_t wpan_phy_counter = ATOMIC_INIT(0); |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 76 | struct cfg802154_registered_device *rdev; |
| 77 | size_t alloc_size; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 78 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 79 | alloc_size = sizeof(*rdev) + priv_size; |
| 80 | rdev = kzalloc(alloc_size, GFP_KERNEL); |
| 81 | if (!rdev) |
| 82 | return NULL; |
| 83 | |
| 84 | rdev->ops = ops; |
| 85 | |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 86 | rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter); |
| 87 | |
| 88 | if (unlikely(rdev->wpan_phy_idx < 0)) { |
| 89 | /* ugh, wrapped! */ |
| 90 | atomic_dec(&wpan_phy_counter); |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 91 | kfree(rdev); |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 92 | return NULL; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 93 | } |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 94 | |
| 95 | /* atomic_inc_return makes it start at 1, make it start at 0 */ |
| 96 | rdev->wpan_phy_idx--; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 97 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 98 | mutex_init(&rdev->wpan_phy.pib_lock); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 99 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 100 | device_initialize(&rdev->wpan_phy.dev); |
Alexander Aring | 53f9ee61 | 2014-11-05 20:51:12 +0100 | [diff] [blame] | 101 | 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] | 102 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 103 | rdev->wpan_phy.dev.class = &wpan_phy_class; |
| 104 | rdev->wpan_phy.dev.platform_data = rdev; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 105 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 106 | return &rdev->wpan_phy; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 107 | } |
| 108 | EXPORT_SYMBOL(wpan_phy_alloc); |
| 109 | |
Dmitry Eremin-Solenikov | e9cf356 | 2009-09-28 19:01:20 +0400 | [diff] [blame] | 110 | int wpan_phy_register(struct wpan_phy *phy) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 111 | { |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 112 | return device_add(&phy->dev); |
| 113 | } |
| 114 | EXPORT_SYMBOL(wpan_phy_register); |
| 115 | |
| 116 | void wpan_phy_unregister(struct wpan_phy *phy) |
| 117 | { |
| 118 | device_del(&phy->dev); |
| 119 | } |
| 120 | EXPORT_SYMBOL(wpan_phy_unregister); |
| 121 | |
| 122 | void wpan_phy_free(struct wpan_phy *phy) |
| 123 | { |
| 124 | put_device(&phy->dev); |
| 125 | } |
| 126 | EXPORT_SYMBOL(wpan_phy_free); |
| 127 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame] | 128 | void cfg802154_dev_free(struct cfg802154_registered_device *rdev) |
| 129 | { |
| 130 | kfree(rdev); |
| 131 | } |
| 132 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 133 | static int __init wpan_phy_class_init(void) |
| 134 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 135 | int rc; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 136 | |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 137 | rc = wpan_phy_sysfs_init(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 138 | if (rc) |
| 139 | goto err; |
| 140 | |
| 141 | rc = ieee802154_nl_init(); |
| 142 | if (rc) |
| 143 | goto err_nl; |
| 144 | |
| 145 | return 0; |
| 146 | err_nl: |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 147 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 148 | err: |
| 149 | return rc; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 150 | } |
Dmitry Eremin-Solenikov | 282a395 | 2009-11-12 23:58:23 +0300 | [diff] [blame] | 151 | subsys_initcall(wpan_phy_class_init); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 152 | |
| 153 | static void __exit wpan_phy_class_exit(void) |
| 154 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 155 | ieee802154_nl_exit(); |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 156 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 157 | } |
| 158 | module_exit(wpan_phy_class_exit); |
| 159 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 160 | MODULE_LICENSE("GPL v2"); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 161 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
| 162 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 163 | |